@open-artifact/motion-stage 0.0.0-stage → 0.12.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +71 -4
- package/src/build.d.ts +18 -0
- package/src/build.js +73 -0
- package/src/host.d.ts +37 -0
- package/src/host.js +76 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +6 -0
- package/src/layers.d.ts +30 -0
- package/src/layers.js +56 -0
- package/src/offline/audio.d.ts +19 -0
- package/src/offline/audio.js +20 -0
- package/src/offline/data.d.ts +26 -0
- package/src/offline/data.js +71 -0
- package/src/offline/main.d.ts +9 -0
- package/src/offline/main.js +52 -0
- package/src/offline/player.d.ts +23 -0
- package/src/offline/player.js +155 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +208 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +97 -0
- package/src/protocol/layers.d.ts +123 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +261 -0
- package/src/protocol/types.js +7 -0
- package/src/protocol.d.ts +2 -0
- package/src/protocol.js +2 -0
- package/src/raster.d.ts +4 -0
- package/src/raster.js +2 -0
- package/src/runtime/assets.d.ts +19 -0
- package/src/runtime/assets.js +63 -0
- package/src/runtime/captions.d.ts +26 -0
- package/src/runtime/captions.js +103 -0
- package/src/runtime/console.d.ts +13 -0
- package/src/runtime/console.js +99 -0
- package/src/runtime/cut.d.ts +7 -0
- package/src/runtime/cut.js +63 -0
- package/src/runtime/determinism.d.ts +12 -0
- package/src/runtime/determinism.js +47 -0
- package/src/runtime/film.d.ts +40 -0
- package/src/runtime/film.js +29 -0
- package/src/runtime/focus.d.ts +8 -0
- package/src/runtime/focus.js +71 -0
- package/src/runtime/fonts.d.ts +22 -0
- package/src/runtime/fonts.js +109 -0
- package/src/runtime/frame.d.ts +9 -0
- package/src/runtime/frame.js +123 -0
- package/src/runtime/kit/camera.d.ts +40 -0
- package/src/runtime/kit/camera.js +56 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -0
- package/src/runtime/kit/cursor.d.ts +46 -0
- package/src/runtime/kit/cursor.js +108 -0
- package/src/runtime/kit/dom.d.ts +14 -0
- package/src/runtime/kit/dom.js +46 -0
- package/src/runtime/kit/geometry.d.ts +22 -0
- package/src/runtime/kit/geometry.js +46 -0
- package/src/runtime/kit/index.d.ts +95 -0
- package/src/runtime/kit/index.js +149 -0
- package/src/runtime/kit/mark-strokes.d.ts +65 -0
- package/src/runtime/kit/mark-strokes.js +226 -0
- package/src/runtime/kit/marks.d.ts +21 -0
- package/src/runtime/kit/marks.js +165 -0
- package/src/runtime/kit/math.d.ts +23 -0
- package/src/runtime/kit/math.js +35 -0
- package/src/runtime/kit/paper.d.ts +7 -0
- package/src/runtime/kit/paper.js +27 -0
- package/src/runtime/kit/random.d.ts +8 -0
- package/src/runtime/kit/random.js +32 -0
- package/src/runtime/kit/text-fx.d.ts +18 -0
- package/src/runtime/kit/text-fx.js +55 -0
- package/src/runtime/kit/typing.d.ts +21 -0
- package/src/runtime/kit/typing.js +42 -0
- package/src/runtime/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +121 -0
- package/src/runtime/layers/item.js +149 -0
- package/src/runtime/layers/parts.d.ts +50 -0
- package/src/runtime/layers/parts.js +155 -0
- package/src/runtime/layers/rows.d.ts +4 -0
- package/src/runtime/layers/rows.js +233 -0
- package/src/runtime/layers/scan.d.ts +24 -0
- package/src/runtime/layers/scan.js +202 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +333 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +228 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +69 -0
- package/src/runtime/modules.d.ts +23 -0
- package/src/runtime/modules.js +53 -0
- package/src/runtime/port.d.ts +8 -0
- package/src/runtime/port.js +46 -0
- package/src/runtime/questions.d.ts +24 -0
- package/src/runtime/questions.js +162 -0
- package/src/runtime/recut.d.ts +35 -0
- package/src/runtime/recut.js +158 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +342 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +156 -0
- package/src/runtime/retouch/entry.d.ts +77 -0
- package/src/runtime/retouch/entry.js +1 -0
- package/src/runtime/retouch/judge.d.ts +25 -0
- package/src/runtime/retouch/judge.js +209 -0
- package/src/runtime/retouch/look.d.ts +39 -0
- package/src/runtime/retouch/look.js +135 -0
- package/src/runtime/retouch/parts.d.ts +22 -0
- package/src/runtime/retouch/parts.js +166 -0
- package/src/runtime/retouch/retouch.d.ts +37 -0
- package/src/runtime/retouch/retouch.js +238 -0
- package/src/runtime/retouch/seat.d.ts +40 -0
- package/src/runtime/retouch/seat.js +228 -0
- package/src/runtime/retouch/state.d.ts +37 -0
- package/src/runtime/retouch/state.js +153 -0
- package/src/runtime/retouch/type-style.d.ts +19 -0
- package/src/runtime/retouch/type-style.js +111 -0
- package/src/runtime/scan.d.ts +2 -0
- package/src/runtime/scan.js +49 -0
- package/src/runtime/scenes/card.d.ts +12 -0
- package/src/runtime/scenes/card.js +56 -0
- package/src/runtime/scenes/install.d.ts +47 -0
- package/src/runtime/scenes/install.js +387 -0
- package/src/runtime/scenes/read.d.ts +15 -0
- package/src/runtime/scenes/read.js +57 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +40 -0
- package/src/runtime/score.d.ts +26 -0
- package/src/runtime/score.js +221 -0
- package/src/runtime/sketch/pen.d.ts +3 -0
- package/src/runtime/sketch/pen.js +371 -0
- package/src/runtime/sketch/strokes.d.ts +41 -0
- package/src/runtime/sketch/strokes.js +187 -0
- package/src/runtime/snapshot/draw.d.ts +14 -0
- package/src/runtime/snapshot/draw.js +125 -0
- package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
- package/src/runtime/snapshot/hold-first-draw.js +128 -0
- package/src/runtime/snapshot/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +49 -0
- package/src/runtime/snapshot/serialize.js +271 -0
- package/src/runtime/snapshot/styles.d.ts +8 -0
- package/src/runtime/snapshot/styles.js +109 -0
- package/src/runtime/snapshot/xml.d.ts +6 -0
- package/src/runtime/snapshot/xml.js +110 -0
- package/src/runtime/stage-size.d.ts +7 -0
- package/src/runtime/stage-size.js +3 -0
- package/src/runtime/stage.d.ts +27 -0
- package/src/runtime/stage.js +154 -0
- package/src/runtime/stretch.d.ts +13 -0
- package/src/runtime/stretch.js +16 -0
- package/src/runtime/texts.d.ts +24 -0
- package/src/runtime/texts.js +210 -0
- package/src/runtime/video-sync.d.ts +26 -0
- package/src/runtime/video-sync.js +126 -0
- package/src/stills.d.ts +41 -0
- package/src/stills.js +253 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +216 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +29 -0
- package/src/tools/types.d.ts +69 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +5 -0
- package/src/tools.js +3 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { filmTime, realNow } from "./determinism.js";
|
|
2
|
+
import { film } from "./film.js";
|
|
3
|
+
import { say } from "./port.js";
|
|
4
|
+
import { RenderStuck, afterSample, holdStage, nextTask, releaseStage, renderAt, renderState, rendersSettle, syncClips, takeTurn, unsettledNow } from "./render.js";
|
|
5
|
+
import { scanStage } from "./scan.js";
|
|
6
|
+
import { PLAIN_INK, forcedColorsActive, inOwnColors, serializeForRaster, svgForRaster } from "./snapshot/serialize.js";
|
|
7
|
+
import { builtStage } from "./stage.js";
|
|
8
|
+
import { stageSize } from "./stage-size.js";
|
|
9
|
+
import { warnOnce } from "./video-sync.js";
|
|
10
|
+
/** A pull that only inspects the picture: past this, the page could not use it anyway. */
|
|
11
|
+
export const SMALL_SNAPSHOT_MAX_CHARS = 1000000;
|
|
12
|
+
/** With the pull's own render (up to 10 s) and its clips, a pull fits the host's 15 s for a frame. */
|
|
13
|
+
const REDRAW_BUDGET_MS = 12000;
|
|
14
|
+
const MOST_PASSES = 3;
|
|
15
|
+
function snapshot(ask, scale, t) {
|
|
16
|
+
scanStage();
|
|
17
|
+
const parts = builtStage();
|
|
18
|
+
const { width, height } = stageSize;
|
|
19
|
+
// The snapshot lacks the stage root, so under forced colors it is given the root's text color.
|
|
20
|
+
const { ink, xml } = inOwnColors(() => ({
|
|
21
|
+
ink: forcedColorsActive() ? getComputedStyle(document.documentElement).color || PLAIN_INK : undefined,
|
|
22
|
+
xml: serializeForRaster(parts.stage, {
|
|
23
|
+
width,
|
|
24
|
+
height,
|
|
25
|
+
mediaScale: ask.smallMedia ? scale : 1,
|
|
26
|
+
fx: ask.readFx ? null : parts.fxWatch,
|
|
27
|
+
smoothAt: ask.smooth ? scale : 0,
|
|
28
|
+
warn: warnOnce,
|
|
29
|
+
}),
|
|
30
|
+
}));
|
|
31
|
+
const made = svgForRaster(xml, width, height, scale, ink);
|
|
32
|
+
if (ask.smallMedia && made.svg.length > SMALL_SNAPSHOT_MAX_CHARS)
|
|
33
|
+
throw new Error('the stage is too large to inspect: its snapshot is ' + made.svg.length + ' characters');
|
|
34
|
+
return { type: 'frame', seq: ask.seq, t, svg: made.svg, pw: made.pw, ph: made.ph };
|
|
35
|
+
}
|
|
36
|
+
const failure = (error) => (error instanceof Error ? error : new Error(String(error)));
|
|
37
|
+
/** Draws the asked moment and answers with its snapshot, after any live render still painting has settled. */
|
|
38
|
+
export function pullFrame(ask) {
|
|
39
|
+
const scale = ask.scale > 0 && ask.scale <= 4 ? ask.scale : 1;
|
|
40
|
+
takeTurn(async () => {
|
|
41
|
+
if (!film.loaded) {
|
|
42
|
+
say({ type: 'frame-error', seq: ask.seq, hold: true, env: true, message: 'the film is not loaded' });
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const began = realNow();
|
|
46
|
+
holdStage();
|
|
47
|
+
let rendered = false;
|
|
48
|
+
try {
|
|
49
|
+
const wait = rendersSettle();
|
|
50
|
+
if (wait)
|
|
51
|
+
await wait;
|
|
52
|
+
/*
|
|
53
|
+
* A render a wait gave up on may settle while this pull draws and paint its own moment over this one: then
|
|
54
|
+
* this moment is drawn again, twice at most. Only the renders pending as the pull begins count.
|
|
55
|
+
*/
|
|
56
|
+
const before = unsettledNow();
|
|
57
|
+
let kept = null;
|
|
58
|
+
let late = true;
|
|
59
|
+
let drawnAt = filmTime();
|
|
60
|
+
for (let passes = 0; late && passes < MOST_PASSES; passes++) {
|
|
61
|
+
const still = before.filter((entry) => !entry.done);
|
|
62
|
+
const passBegan = realNow();
|
|
63
|
+
let awaited = false;
|
|
64
|
+
try {
|
|
65
|
+
rendered = false;
|
|
66
|
+
const sampled = renderState.gavePromise;
|
|
67
|
+
let drawn = renderAt(ask.t);
|
|
68
|
+
if (!renderState.ok)
|
|
69
|
+
throw failure(renderState.error);
|
|
70
|
+
if (drawn) {
|
|
71
|
+
awaited = true;
|
|
72
|
+
await drawn;
|
|
73
|
+
}
|
|
74
|
+
// A sample this draw began goes first, and this moment is drawn again: nothing else draws meanwhile, since this pull holds the stage.
|
|
75
|
+
const again = afterSample(sampled, ask.t);
|
|
76
|
+
if (again) {
|
|
77
|
+
awaited = true;
|
|
78
|
+
drawn = (await again).q;
|
|
79
|
+
if (!renderState.ok)
|
|
80
|
+
throw failure(renderState.error);
|
|
81
|
+
if (drawn)
|
|
82
|
+
await drawn;
|
|
83
|
+
}
|
|
84
|
+
rendered = true;
|
|
85
|
+
drawnAt = filmTime();
|
|
86
|
+
// A pull that only inspects the picture waits at most 250 ms for a clip.
|
|
87
|
+
const clips = syncClips('frame', ask.smallMedia);
|
|
88
|
+
if (clips) {
|
|
89
|
+
awaited = true;
|
|
90
|
+
await clips;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
// A redraw that fails answers with the pass before; one that never settled may still paint, so it fails the pull.
|
|
95
|
+
if (kept === null || error instanceof RenderStuck)
|
|
96
|
+
throw error;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
// A render is marked done a few promise steps after it paints.
|
|
100
|
+
if (still.length && awaited)
|
|
101
|
+
await nextTask();
|
|
102
|
+
late = awaited && still.some((entry) => entry.done) && 2 * realNow() - began - passBegan < REDRAW_BUDGET_MS;
|
|
103
|
+
kept = late ? snapshot(ask, scale, drawnAt) : null;
|
|
104
|
+
}
|
|
105
|
+
const out = kept ?? snapshot(ask, scale, drawnAt);
|
|
106
|
+
say({ ...out, ms: Math.round(realNow() - began) });
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
const fatal = error instanceof RenderStuck;
|
|
110
|
+
const message = String(error instanceof Error ? error.message : error).slice(0, 300);
|
|
111
|
+
say({
|
|
112
|
+
type: 'frame-error',
|
|
113
|
+
seq: ask.seq,
|
|
114
|
+
hold: !fatal,
|
|
115
|
+
env: fatal && error.inBackground,
|
|
116
|
+
message: rendered || fatal ? message : 'render(' + filmTime().toFixed(3) + ') threw: ' + message,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
releaseStage();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Ease } from './math.ts';
|
|
2
|
+
export interface CameraKey {
|
|
3
|
+
t: number;
|
|
4
|
+
cx: number;
|
|
5
|
+
cy: number;
|
|
6
|
+
z: number;
|
|
7
|
+
ease?: Ease;
|
|
8
|
+
/** A hard cut at `t`: the key before holds right up to it. */
|
|
9
|
+
cut?: boolean;
|
|
10
|
+
/** When the ease toward this key begins; by default after the key before's `hold`. */
|
|
11
|
+
from?: number;
|
|
12
|
+
hold?: number;
|
|
13
|
+
/** [dx/s, dy/s, dlogz/s]: a slow creep while this key holds, carried into the next ease. */
|
|
14
|
+
drift?: [number, number, number?];
|
|
15
|
+
}
|
|
16
|
+
export interface CameraView {
|
|
17
|
+
cx: number;
|
|
18
|
+
cy: number;
|
|
19
|
+
z: number;
|
|
20
|
+
}
|
|
21
|
+
export interface CameraState extends CameraView {
|
|
22
|
+
tx: number;
|
|
23
|
+
ty: number;
|
|
24
|
+
}
|
|
25
|
+
export interface CameraExtra {
|
|
26
|
+
x?: number;
|
|
27
|
+
y?: number;
|
|
28
|
+
z?: number;
|
|
29
|
+
rot?: number;
|
|
30
|
+
}
|
|
31
|
+
/** Wraps a layer of a scene: (cx, cy) is the point shown at the stage's centre, z the zoom. */
|
|
32
|
+
export declare class Camera {
|
|
33
|
+
el: HTMLElement;
|
|
34
|
+
keys: CameraKey[];
|
|
35
|
+
state: CameraState;
|
|
36
|
+
constructor(el: HTMLElement);
|
|
37
|
+
set(keys: CameraKey[]): this;
|
|
38
|
+
at(t: number): CameraView;
|
|
39
|
+
apply(t: number, extra?: CameraExtra): CameraState;
|
|
40
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { eio, lerp, seg } from "./math.js";
|
|
2
|
+
import { stageSize } from "../stage-size.js";
|
|
3
|
+
/** Wraps a layer of a scene: (cx, cy) is the point shown at the stage's centre, z the zoom. */
|
|
4
|
+
export class Camera {
|
|
5
|
+
el;
|
|
6
|
+
keys;
|
|
7
|
+
state;
|
|
8
|
+
constructor(el) {
|
|
9
|
+
this.el = el;
|
|
10
|
+
this.keys = [{ t: 0, cx: stageSize.width / 2, cy: stageSize.height / 2, z: 1 }];
|
|
11
|
+
this.state = { tx: 0, ty: 0, z: 1, cx: 960, cy: 540 };
|
|
12
|
+
el.style.transformOrigin = '0 0';
|
|
13
|
+
}
|
|
14
|
+
set(keys) {
|
|
15
|
+
this.keys = keys.slice().sort((a, b) => a.t - b.t);
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
at(t) {
|
|
19
|
+
const keys = this.keys;
|
|
20
|
+
let index = 0;
|
|
21
|
+
for (let k = 0; k < keys.length; k++)
|
|
22
|
+
if ((keys[k]?.t ?? Infinity) <= t)
|
|
23
|
+
index = k;
|
|
24
|
+
const a = keys[index];
|
|
25
|
+
if (!a)
|
|
26
|
+
return { cx: stageSize.width / 2, cy: stageSize.height / 2, z: 1 };
|
|
27
|
+
const b = keys[index + 1] ?? null;
|
|
28
|
+
const easeFrom = b ? (b.cut ? b.t : b.from !== undefined ? b.from : a.t + (a.hold ?? 0)) : Infinity;
|
|
29
|
+
const held = Math.max(0, Math.min(t, easeFrom) - a.t);
|
|
30
|
+
let cx = a.cx;
|
|
31
|
+
let cy = a.cy;
|
|
32
|
+
let z = a.z;
|
|
33
|
+
if (a.drift) {
|
|
34
|
+
cx += a.drift[0] * held;
|
|
35
|
+
cy += a.drift[1] * held;
|
|
36
|
+
z *= Math.exp((a.drift[2] ?? 0) * held);
|
|
37
|
+
}
|
|
38
|
+
if (b && !b.cut && t > easeFrom) {
|
|
39
|
+
const f = (b.ease ?? eio)(seg(t, easeFrom, b.t));
|
|
40
|
+
z = Math.exp(lerp(Math.log(z), Math.log(b.z), f));
|
|
41
|
+
cx = lerp(cx, b.cx, f);
|
|
42
|
+
cy = lerp(cy, b.cy, f);
|
|
43
|
+
}
|
|
44
|
+
return { cx, cy, z };
|
|
45
|
+
}
|
|
46
|
+
apply(t, extra) {
|
|
47
|
+
const view = this.at(t);
|
|
48
|
+
const z = view.z * (extra?.z || 1);
|
|
49
|
+
const tx = stageSize.width / 2 - view.cx * z + (extra?.x || 0);
|
|
50
|
+
const ty = stageSize.height / 2 - view.cy * z + (extra?.y || 0);
|
|
51
|
+
this.state = { tx, ty, z, cx: view.cx, cy: view.cy };
|
|
52
|
+
this.el.style.transform =
|
|
53
|
+
`translate(${tx.toFixed(2)}px, ${ty.toFixed(2)}px) scale(${z.toFixed(5)})` + (extra?.rot ? ` rotate(${extra.rot}deg)` : '');
|
|
54
|
+
return this.state;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { StageRect } from './geometry.ts';
|
|
2
|
+
export interface ChipOptions {
|
|
3
|
+
x?: number;
|
|
4
|
+
y?: number;
|
|
5
|
+
/** A point the chip's leader line runs to, with a dot on it. */
|
|
6
|
+
anchor?: [number, number];
|
|
7
|
+
lineColor?: string;
|
|
8
|
+
dotColor?: string;
|
|
9
|
+
style?: string;
|
|
10
|
+
}
|
|
11
|
+
/** A caption chip: a white pill in 26px sans, with an optional 1.5px leader to an anchor dot. */
|
|
12
|
+
export declare function chip(text: string, options?: ChipOptions): HTMLElement;
|
|
13
|
+
export interface ShowOptions {
|
|
14
|
+
dur?: number;
|
|
15
|
+
dy?: number;
|
|
16
|
+
}
|
|
17
|
+
/** Fades and raises a chip, or any absolutely placed node, in at `tIn` and out at `tOut`. Gives back how far in it is. */
|
|
18
|
+
export declare function show(node: HTMLElement, t: number, tIn: number, tOut: number | null | undefined, options?: ShowOptions): number;
|
|
19
|
+
export declare function roundRectPath(context: CanvasRenderingContext2D, x: number, y: number, width: number, height: number, radius: number): void;
|
|
20
|
+
export interface SpotlightOptions {
|
|
21
|
+
pad?: number;
|
|
22
|
+
radius?: number;
|
|
23
|
+
}
|
|
24
|
+
/** Dims the whole stage on the fx canvas but a rounded hole round `rect`. */
|
|
25
|
+
export declare function spotlight(context: CanvasRenderingContext2D, rect: StageRect | null, amount: number, options?: SpotlightOptions): void;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { fontFamilies } from "../fonts.js";
|
|
2
|
+
import { stageSize } from "../stage-size.js";
|
|
3
|
+
import { el } from "./dom.js";
|
|
4
|
+
import { ei, eo, seg } from "./math.js";
|
|
5
|
+
/** Where a chip was placed, which `show` keeps while it raises it. */
|
|
6
|
+
const placed = new WeakMap();
|
|
7
|
+
/** A caption chip: a white pill in 26px sans, with an optional 1.5px leader to an anchor dot. */
|
|
8
|
+
export function chip(text, options = {}) {
|
|
9
|
+
const x = options.x ?? stageSize.width / 2;
|
|
10
|
+
const y = options.y ?? stageSize.height - 120;
|
|
11
|
+
const box = el('div', {
|
|
12
|
+
class: 'film-chip',
|
|
13
|
+
style: 'position:absolute;left:0;top:0;width:0;height:0;z-index:2147483645;opacity:0;pointer-events:none',
|
|
14
|
+
});
|
|
15
|
+
if (options.anchor) {
|
|
16
|
+
const ax = options.anchor[0] - x;
|
|
17
|
+
const ay = options.anchor[1] - y;
|
|
18
|
+
const length = Math.hypot(ax, ay);
|
|
19
|
+
const angle = (Math.atan2(ay, ax) * 180) / Math.PI;
|
|
20
|
+
box.appendChild(el('div', {
|
|
21
|
+
class: 'film-chip-line',
|
|
22
|
+
style: `position:absolute;left:0;top:-0.75px;width:${length.toFixed(1)}px;height:1.5px;background:${options.lineColor ?? '#111'};transform-origin:0 50%;transform:rotate(${angle.toFixed(2)}deg)`,
|
|
23
|
+
}));
|
|
24
|
+
box.appendChild(el('div', {
|
|
25
|
+
class: 'film-chip-dot',
|
|
26
|
+
style: `position:absolute;left:${ax - 5}px;top:${ay - 5}px;width:10px;height:10px;border-radius:50%;background:${options.dotColor ?? '#111'};box-shadow:0 0 0 3px #fff`,
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
box.appendChild(el('div', {
|
|
30
|
+
class: 'film-chip-pill',
|
|
31
|
+
text: String(text),
|
|
32
|
+
style: 'position:absolute;left:0;top:0;transform:translate(-50%,-50%);padding:12px 22px;border-radius:999px;background:#fff;color:#111;font:500 26px/1.25 ' +
|
|
33
|
+
fontFamilies.sans +
|
|
34
|
+
';letter-spacing:.005em;white-space:nowrap;box-shadow:0 6px 24px rgba(20,20,19,.14),0 0 0 1px rgba(20,20,19,.06)' +
|
|
35
|
+
(options.style ? ';' + options.style : ''),
|
|
36
|
+
}));
|
|
37
|
+
box.style.transform = `translate(${x}px,${y}px)`;
|
|
38
|
+
placed.set(box, [x, y]);
|
|
39
|
+
return box;
|
|
40
|
+
}
|
|
41
|
+
/** Fades and raises a chip, or any absolutely placed node, in at `tIn` and out at `tOut`. Gives back how far in it is. */
|
|
42
|
+
export function show(node, t, tIn, tOut, options = {}) {
|
|
43
|
+
const dur = options.dur || 0.35;
|
|
44
|
+
const dy = options.dy ?? 12;
|
|
45
|
+
let progress = eo(seg(t, tIn, tIn + dur));
|
|
46
|
+
if (tOut !== null && tOut !== undefined)
|
|
47
|
+
progress *= 1 - ei(seg(t, tOut, tOut + dur));
|
|
48
|
+
node.style.opacity = String(progress);
|
|
49
|
+
const xy = placed.get(node);
|
|
50
|
+
node.style.transform = (xy ? `translate(${xy[0]}px,${xy[1]}px) ` : '') + 'translateY(' + ((1 - progress) * dy).toFixed(1) + 'px)';
|
|
51
|
+
return progress;
|
|
52
|
+
}
|
|
53
|
+
export function roundRectPath(context, x, y, width, height, radius) {
|
|
54
|
+
const r = Math.min(radius, width / 2, height / 2);
|
|
55
|
+
context.moveTo(x + r, y);
|
|
56
|
+
context.arcTo(x + width, y, x + width, y + height, r);
|
|
57
|
+
context.arcTo(x + width, y + height, x, y + height, r);
|
|
58
|
+
context.arcTo(x, y + height, x, y, r);
|
|
59
|
+
context.arcTo(x, y, x + width, y, r);
|
|
60
|
+
context.closePath();
|
|
61
|
+
}
|
|
62
|
+
/** Dims the whole stage on the fx canvas but a rounded hole round `rect`. */
|
|
63
|
+
export function spotlight(context, rect, amount, options = {}) {
|
|
64
|
+
if (!(amount > 0.001) || !rect)
|
|
65
|
+
return;
|
|
66
|
+
const pad = options.pad ?? 24;
|
|
67
|
+
const radius = options.radius ?? 18;
|
|
68
|
+
context.save();
|
|
69
|
+
context.fillStyle = 'rgba(20,20,19,' + (0.55 * amount).toFixed(3) + ')';
|
|
70
|
+
context.beginPath();
|
|
71
|
+
context.rect(0, 0, stageSize.width, stageSize.height);
|
|
72
|
+
roundRectPath(context, rect.x - pad, rect.y - pad, rect.w + pad * 2, rect.h + pad * 2, radius);
|
|
73
|
+
context.fill('evenodd');
|
|
74
|
+
context.restore();
|
|
75
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Point } from './paper.ts';
|
|
2
|
+
export declare const CURSOR_ARROW = "<svg width=\"30\" height=\"30\" viewBox=\"0 0 24 24\"><path d=\"M5.2 2.9v17.4l4.3-4.1 2.7 6.2 2.9-1.25-2.65-6.05h5.85z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.35\" stroke-linejoin=\"round\"/></svg>";
|
|
3
|
+
export declare const CURSOR_EW = "<svg width=\"46\" height=\"46\" viewBox=\"0 0 24 24\" style=\"margin:-23px 0 0 -23px\"><path d=\"M2.5 12l4.2-4.2v2.7h10.6V7.8L21.5 12l-4.2 4.2v-2.7H6.7v2.7z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/></svg>";
|
|
4
|
+
export declare const CURSOR_NESW = "<svg width=\"46\" height=\"46\" viewBox=\"0 0 24 24\" style=\"margin:-23px 0 0 -23px\"><g transform=\"rotate(-45 12 12)\"><path d=\"M2.5 12l4.2-4.2v2.7h10.6V7.8L21.5 12l-4.2 4.2v-2.7H6.7v2.7z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/></g></svg>";
|
|
5
|
+
export declare const CURSOR_NWSE = "<svg width=\"46\" height=\"46\" viewBox=\"0 0 24 24\" style=\"margin:-23px 0 0 -23px\"><g transform=\"rotate(45 12 12)\"><path d=\"M2.5 12l4.2-4.2v2.7h10.6V7.8L21.5 12l-4.2 4.2v-2.7H6.7v2.7z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/></g></svg>";
|
|
6
|
+
export declare const CURSOR_COPY = "<svg width=\"40\" height=\"40\" viewBox=\"0 0 32 32\"><path d=\"M5.2 2.9v17.4l4.3-4.1 2.7 6.2 2.9-1.25-2.65-6.05h5.85z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.35\" stroke-linejoin=\"round\"/><circle cx=\"22.5\" cy=\"22\" r=\"7\" fill=\"#34c84a\" stroke=\"#fff\" stroke-width=\"1.6\"/><path d=\"M22.5 18.2v7.6M18.7 22h7.6\" stroke=\"#fff\" stroke-width=\"2\" stroke-linecap=\"round\"/></svg>";
|
|
7
|
+
export declare const CURSOR_TEXT = "<svg width=\"30\" height=\"34\" viewBox=\"0 0 24 28\" style=\"margin:-17px 0 0 -12px\"><path d=\"M8 3h3.2c.5 0 .8.3.8.8V24.2c0 .5-.3.8-.8.8H8M16 3h-3.2c-.5 0-.8.3-.8.8M16 25h-3.2c-.5 0-.8-.3-.8-.8M9.5 14h5\" fill=\"none\" stroke=\"#fff\" stroke-width=\"3.2\" stroke-linecap=\"round\"/><path d=\"M8 3h3.2c.5 0 .8.3.8.8V24.2c0 .5-.3.8-.8.8H8M16 3h-3.2c-.5 0-.8.3-.8.8M16 25h-3.2c-.5 0-.8-.3-.8-.8M9.5 14h5\" fill=\"none\" stroke=\"#111\" stroke-width=\"1.3\" stroke-linecap=\"round\"/></svg>";
|
|
8
|
+
/** `kit.cursors`: glyphs a film passes to `kit.cursor(...)` or a cursor's `shapes`. */
|
|
9
|
+
export declare const CURSORS: {
|
|
10
|
+
readonly arrow: "<svg width=\"30\" height=\"30\" viewBox=\"0 0 24 24\"><path d=\"M5.2 2.9v17.4l4.3-4.1 2.7 6.2 2.9-1.25-2.65-6.05h5.85z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.35\" stroke-linejoin=\"round\"/></svg>";
|
|
11
|
+
readonly ew: "<svg width=\"46\" height=\"46\" viewBox=\"0 0 24 24\" style=\"margin:-23px 0 0 -23px\"><path d=\"M2.5 12l4.2-4.2v2.7h10.6V7.8L21.5 12l-4.2 4.2v-2.7H6.7v2.7z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/></svg>";
|
|
12
|
+
readonly nesw: "<svg width=\"46\" height=\"46\" viewBox=\"0 0 24 24\" style=\"margin:-23px 0 0 -23px\"><g transform=\"rotate(-45 12 12)\"><path d=\"M2.5 12l4.2-4.2v2.7h10.6V7.8L21.5 12l-4.2 4.2v-2.7H6.7v2.7z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/></g></svg>";
|
|
13
|
+
readonly nwse: "<svg width=\"46\" height=\"46\" viewBox=\"0 0 24 24\" style=\"margin:-23px 0 0 -23px\"><g transform=\"rotate(45 12 12)\"><path d=\"M2.5 12l4.2-4.2v2.7h10.6V7.8L21.5 12l-4.2 4.2v-2.7H6.7v2.7z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/></g></svg>";
|
|
14
|
+
readonly copy: "<svg width=\"40\" height=\"40\" viewBox=\"0 0 32 32\"><path d=\"M5.2 2.9v17.4l4.3-4.1 2.7 6.2 2.9-1.25-2.65-6.05h5.85z\" fill=\"#111\" stroke=\"#fff\" stroke-width=\"1.35\" stroke-linejoin=\"round\"/><circle cx=\"22.5\" cy=\"22\" r=\"7\" fill=\"#34c84a\" stroke=\"#fff\" stroke-width=\"1.6\"/><path d=\"M22.5 18.2v7.6M18.7 22h7.6\" stroke=\"#fff\" stroke-width=\"2\" stroke-linecap=\"round\"/></svg>";
|
|
15
|
+
readonly text: "<svg width=\"30\" height=\"34\" viewBox=\"0 0 24 28\" style=\"margin:-17px 0 0 -12px\"><path d=\"M8 3h3.2c.5 0 .8.3.8.8V24.2c0 .5-.3.8-.8.8H8M16 3h-3.2c-.5 0-.8.3-.8.8M16 25h-3.2c-.5 0-.8-.3-.8-.8M9.5 14h5\" fill=\"none\" stroke=\"#fff\" stroke-width=\"3.2\" stroke-linecap=\"round\"/><path d=\"M8 3h3.2c.5 0 .8.3.8.8V24.2c0 .5-.3.8-.8.8H8M16 3h-3.2c-.5 0-.8.3-.8.8M16 25h-3.2c-.5 0-.8-.3-.8-.8M9.5 14h5\" fill=\"none\" stroke=\"#111\" stroke-width=\"1.3\" stroke-linecap=\"round\"/></svg>";
|
|
16
|
+
};
|
|
17
|
+
/** A cursor for the stage: a press ring and a glyph, the arrow or the SVG markup the film passes. */
|
|
18
|
+
export declare function cursorElement(glyph: string | undefined): HTMLElement;
|
|
19
|
+
export interface CursorKey {
|
|
20
|
+
t: number;
|
|
21
|
+
at: Point | (() => Point);
|
|
22
|
+
/** How far the path bows sideways, as a share of its length. */
|
|
23
|
+
bow?: number;
|
|
24
|
+
/** Move at an even pace instead of easing. */
|
|
25
|
+
lin?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface CursorShape {
|
|
28
|
+
spans: Array<[number, number]>;
|
|
29
|
+
gfx: string;
|
|
30
|
+
}
|
|
31
|
+
/** keys [{t, at, bow?, lin?}], clicks [t…], holds [[a, b]…], ewSpans and shapes pick the glyph. */
|
|
32
|
+
export declare class Cursor {
|
|
33
|
+
el: HTMLElement;
|
|
34
|
+
gfx: HTMLElement | null;
|
|
35
|
+
ring: HTMLElement | null;
|
|
36
|
+
keys: CursorKey[];
|
|
37
|
+
clicks: number[];
|
|
38
|
+
holds: Array<[number, number]>;
|
|
39
|
+
ewSpans: Array<[number, number]>;
|
|
40
|
+
shapes: CursorShape[];
|
|
41
|
+
cur: string;
|
|
42
|
+
visible: (t: number) => number;
|
|
43
|
+
constructor(el: HTMLElement);
|
|
44
|
+
pos(t: number): Point;
|
|
45
|
+
render(t: number): void;
|
|
46
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { eio, eo, lerp, seg } from "./math.js";
|
|
2
|
+
export const CURSOR_ARROW = `<svg width="30" height="30" viewBox="0 0 24 24"><path d="M5.2 2.9v17.4l4.3-4.1 2.7 6.2 2.9-1.25-2.65-6.05h5.85z" fill="#111" stroke="#fff" stroke-width="1.35" stroke-linejoin="round"/></svg>`;
|
|
3
|
+
const DOUBLE_ARROW = `<path d="M2.5 12l4.2-4.2v2.7h10.6V7.8L21.5 12l-4.2 4.2v-2.7H6.7v2.7z" fill="#111" stroke="#fff" stroke-width="1.2" stroke-linejoin="round"/>`;
|
|
4
|
+
export const CURSOR_EW = `<svg width="46" height="46" viewBox="0 0 24 24" style="margin:-23px 0 0 -23px">${DOUBLE_ARROW}</svg>`;
|
|
5
|
+
export const CURSOR_NESW = `<svg width="46" height="46" viewBox="0 0 24 24" style="margin:-23px 0 0 -23px"><g transform="rotate(-45 12 12)">${DOUBLE_ARROW}</g></svg>`;
|
|
6
|
+
export const CURSOR_NWSE = `<svg width="46" height="46" viewBox="0 0 24 24" style="margin:-23px 0 0 -23px"><g transform="rotate(45 12 12)">${DOUBLE_ARROW}</g></svg>`;
|
|
7
|
+
export const CURSOR_COPY = `<svg width="40" height="40" viewBox="0 0 32 32"><path d="M5.2 2.9v17.4l4.3-4.1 2.7 6.2 2.9-1.25-2.65-6.05h5.85z" fill="#111" stroke="#fff" stroke-width="1.35" stroke-linejoin="round"/><circle cx="22.5" cy="22" r="7" fill="#34c84a" stroke="#fff" stroke-width="1.6"/><path d="M22.5 18.2v7.6M18.7 22h7.6" stroke="#fff" stroke-width="2" stroke-linecap="round"/></svg>`;
|
|
8
|
+
const I_BEAM = 'M8 3h3.2c.5 0 .8.3.8.8V24.2c0 .5-.3.8-.8.8H8M16 3h-3.2c-.5 0-.8.3-.8.8M16 25h-3.2c-.5 0-.8-.3-.8-.8M9.5 14h5';
|
|
9
|
+
export const CURSOR_TEXT = `<svg width="30" height="34" viewBox="0 0 24 28" style="margin:-17px 0 0 -12px"><path d="${I_BEAM}" fill="none" stroke="#fff" stroke-width="3.2" stroke-linecap="round"/><path d="${I_BEAM}" fill="none" stroke="#111" stroke-width="1.3" stroke-linecap="round"/></svg>`;
|
|
10
|
+
/** `kit.cursors`: glyphs a film passes to `kit.cursor(...)` or a cursor's `shapes`. */
|
|
11
|
+
export const CURSORS = {
|
|
12
|
+
arrow: CURSOR_ARROW,
|
|
13
|
+
ew: CURSOR_EW,
|
|
14
|
+
nesw: CURSOR_NESW,
|
|
15
|
+
nwse: CURSOR_NWSE,
|
|
16
|
+
copy: CURSOR_COPY,
|
|
17
|
+
text: CURSOR_TEXT,
|
|
18
|
+
};
|
|
19
|
+
/** A cursor for the stage: a press ring and a glyph, the arrow or the SVG markup the film passes. */
|
|
20
|
+
export function cursorElement(glyph) {
|
|
21
|
+
const box = document.createElement('div');
|
|
22
|
+
box.className = 'film-cursor';
|
|
23
|
+
box.style.cssText = 'position:absolute;left:0;top:0;width:0;height:0;z-index:2147483646;pointer-events:none';
|
|
24
|
+
box.innerHTML =
|
|
25
|
+
'<div class="cursor-ring" style="position:absolute;left:-22px;top:-22px;width:44px;height:44px;border-radius:50%;border:3px solid rgba(20,20,19,.55);opacity:0"></div>' +
|
|
26
|
+
'<div class="cursor-gfx" style="position:absolute;left:-4px;top:-3px;transform-origin:4px 3px">' +
|
|
27
|
+
(glyph || CURSOR_ARROW) +
|
|
28
|
+
'</div>';
|
|
29
|
+
return box;
|
|
30
|
+
}
|
|
31
|
+
/** keys [{t, at, bow?, lin?}], clicks [t…], holds [[a, b]…], ewSpans and shapes pick the glyph. */
|
|
32
|
+
export class Cursor {
|
|
33
|
+
el;
|
|
34
|
+
gfx;
|
|
35
|
+
ring;
|
|
36
|
+
keys = [];
|
|
37
|
+
clicks = [];
|
|
38
|
+
holds = [];
|
|
39
|
+
ewSpans = [];
|
|
40
|
+
shapes = [];
|
|
41
|
+
cur = '';
|
|
42
|
+
visible = () => 1;
|
|
43
|
+
constructor(el) {
|
|
44
|
+
this.el = el;
|
|
45
|
+
this.gfx = el.querySelector('.cursor-gfx');
|
|
46
|
+
this.ring = el.querySelector('.cursor-ring');
|
|
47
|
+
}
|
|
48
|
+
pos(t) {
|
|
49
|
+
const keys = this.keys;
|
|
50
|
+
const place = (key) => (typeof key.at === 'function' ? key.at() : key.at);
|
|
51
|
+
const first = keys[0];
|
|
52
|
+
const last = keys[keys.length - 1];
|
|
53
|
+
if (!first || !last)
|
|
54
|
+
return [-100, -100];
|
|
55
|
+
if (t <= first.t)
|
|
56
|
+
return place(first);
|
|
57
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
58
|
+
const a = keys[i];
|
|
59
|
+
const b = keys[i + 1];
|
|
60
|
+
if (!a || !b || !(t >= a.t && t < b.t))
|
|
61
|
+
continue;
|
|
62
|
+
const p = b.lin ? seg(t, a.t, b.t) : eio(seg(t, a.t, b.t));
|
|
63
|
+
const from = place(a);
|
|
64
|
+
const to = place(b);
|
|
65
|
+
let x = lerp(from[0], to[0], p);
|
|
66
|
+
let y = lerp(from[1], to[1], p);
|
|
67
|
+
if (b.bow) {
|
|
68
|
+
const bend = Math.sin(Math.PI * p) * b.bow;
|
|
69
|
+
x += -(to[1] - from[1]) * bend;
|
|
70
|
+
y += (to[0] - from[0]) * bend;
|
|
71
|
+
}
|
|
72
|
+
return [x, y];
|
|
73
|
+
}
|
|
74
|
+
return place(last);
|
|
75
|
+
}
|
|
76
|
+
render(t) {
|
|
77
|
+
const opacity = this.visible(t);
|
|
78
|
+
this.el.style.display = opacity > 0.001 ? '' : 'none';
|
|
79
|
+
if (opacity <= 0.001)
|
|
80
|
+
return;
|
|
81
|
+
const [x, y] = this.pos(t);
|
|
82
|
+
this.el.style.opacity = String(opacity);
|
|
83
|
+
this.el.style.transform = `translate(${x}px, ${y}px)`;
|
|
84
|
+
const within = ([a, b]) => t >= a && t < b;
|
|
85
|
+
const shape = this.shapes.find((s) => s.spans.some(within));
|
|
86
|
+
const glyph = shape ? shape.gfx : this.ewSpans.some(within) ? CURSOR_EW : CURSOR_ARROW;
|
|
87
|
+
if (glyph !== this.cur && this.gfx) {
|
|
88
|
+
this.gfx.innerHTML = glyph;
|
|
89
|
+
this.cur = glyph;
|
|
90
|
+
}
|
|
91
|
+
let press = 0;
|
|
92
|
+
let ringAge = 9;
|
|
93
|
+
for (const click of this.clicks) {
|
|
94
|
+
const age = t - click;
|
|
95
|
+
if (age >= 0 && age < ringAge)
|
|
96
|
+
ringAge = age;
|
|
97
|
+
if (age >= -0.04 && age < 0.14)
|
|
98
|
+
press = 1;
|
|
99
|
+
}
|
|
100
|
+
const held = this.holds.some(within);
|
|
101
|
+
if (this.gfx)
|
|
102
|
+
this.gfx.style.transform = `scale(${press || held ? 0.86 : 1})`;
|
|
103
|
+
if (this.ring) {
|
|
104
|
+
this.ring.style.opacity = String(ringAge < 0.4 ? 0.55 * (1 - ringAge / 0.4) : 0);
|
|
105
|
+
this.ring.style.transform = `scale(${lerp(0.35, 1, eo(ringAge / 0.4))})`;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface ElementProps {
|
|
2
|
+
class?: string;
|
|
3
|
+
style?: string | Record<string, string>;
|
|
4
|
+
text?: string;
|
|
5
|
+
html?: string;
|
|
6
|
+
attrs?: Record<string, string>;
|
|
7
|
+
[property: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The element `html` makes, or for several roots a box-less parent holding them all: display:contents leaves
|
|
11
|
+
* a flex or grid parent's layout alone, and a fragment would empty once appended. A trailing `<style>` is dropped.
|
|
12
|
+
*/
|
|
13
|
+
export declare function h(html: string): Element | null;
|
|
14
|
+
export declare function el(tag: string, props?: ElementProps | null, children?: Array<Node | string | null | undefined>): HTMLElement;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The element `html` makes, or for several roots a box-less parent holding them all: display:contents leaves
|
|
3
|
+
* a flex or grid parent's layout alone, and a fragment would empty once appended. A trailing `<style>` is dropped.
|
|
4
|
+
*/
|
|
5
|
+
export function h(html) {
|
|
6
|
+
const template = document.createElement('template');
|
|
7
|
+
template.innerHTML = String(html).trim();
|
|
8
|
+
const content = template.content;
|
|
9
|
+
const drawn = [...content.children].filter((child) => !/^(style|script)$/i.test(child.tagName));
|
|
10
|
+
if (content.childElementCount < 2 || (drawn.length < 2 && drawn[0] === content.firstElementChild))
|
|
11
|
+
return content.firstElementChild;
|
|
12
|
+
const holder = document.createElement('div');
|
|
13
|
+
holder.style.display = 'contents';
|
|
14
|
+
holder.setAttribute('data-kit-holder', '');
|
|
15
|
+
holder.appendChild(content);
|
|
16
|
+
return holder;
|
|
17
|
+
}
|
|
18
|
+
export function el(tag, props = null, children = []) {
|
|
19
|
+
const node = document.createElement(tag);
|
|
20
|
+
for (const [key, value] of Object.entries(props ?? {})) {
|
|
21
|
+
if (value === null || value === undefined)
|
|
22
|
+
continue;
|
|
23
|
+
if (key === 'class')
|
|
24
|
+
node.className = String(value);
|
|
25
|
+
else if (key === 'style' && typeof value === 'object') {
|
|
26
|
+
for (const [name, rule] of Object.entries(value))
|
|
27
|
+
Reflect.set(node.style, name, rule);
|
|
28
|
+
}
|
|
29
|
+
else if (key === 'style')
|
|
30
|
+
node.style.cssText = String(value);
|
|
31
|
+
else if (key === 'text')
|
|
32
|
+
node.textContent = String(value);
|
|
33
|
+
else if (key === 'html')
|
|
34
|
+
node.innerHTML = String(value);
|
|
35
|
+
else if (key === 'attrs' && typeof value === 'object') {
|
|
36
|
+
for (const [name, attribute] of Object.entries(value))
|
|
37
|
+
node.setAttribute(name, String(attribute));
|
|
38
|
+
}
|
|
39
|
+
else
|
|
40
|
+
Reflect.set(node, key, value);
|
|
41
|
+
}
|
|
42
|
+
for (const child of children ?? [])
|
|
43
|
+
if (child !== null && child !== undefined)
|
|
44
|
+
node.appendChild(typeof child === 'string' ? document.createTextNode(child) : child);
|
|
45
|
+
return node;
|
|
46
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CameraState } from './camera.ts';
|
|
2
|
+
import type { Point } from './paper.ts';
|
|
3
|
+
export interface StageRect {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
w: number;
|
|
7
|
+
h: number;
|
|
8
|
+
}
|
|
9
|
+
/** A node's box in stage px, as seen: the frame's fit undone, a camera's zoom kept. */
|
|
10
|
+
export declare function srect(node: Element | Range): StageRect;
|
|
11
|
+
export declare const center: (node: Element | Range, dx?: number, dy?: number) => (() => Point);
|
|
12
|
+
/** A node's box in a camera's own scene space, where marks and a cursor inside the camera are placed. */
|
|
13
|
+
export declare const camrect: (node: Element | Range, camera: {
|
|
14
|
+
state: CameraState;
|
|
15
|
+
}) => StageRect;
|
|
16
|
+
/** The stage box of the last line of `phrase` inside `el`'s text, or null where it is not shown yet. */
|
|
17
|
+
export declare function phraseRect(el: Element | null, phrase: string): (StageRect & {
|
|
18
|
+
lines: number;
|
|
19
|
+
}) | null;
|
|
20
|
+
export declare function phraseCamRect(el: Element | null, phrase: string, camera: {
|
|
21
|
+
state: CameraState;
|
|
22
|
+
}): StageRect | null;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { stageElement, stageSize } from "../stage-size.js";
|
|
2
|
+
const stageBox = () => stageElement()?.getBoundingClientRect() ?? new DOMRect();
|
|
3
|
+
/** A node's box in stage px, as seen: the frame's fit undone, a camera's zoom kept. */
|
|
4
|
+
export function srect(node) {
|
|
5
|
+
const box = node.getBoundingClientRect();
|
|
6
|
+
const stage = stageBox();
|
|
7
|
+
const k = stageSize.fit;
|
|
8
|
+
return { x: (box.left - stage.left) / k, y: (box.top - stage.top) / k, w: box.width / k, h: box.height / k };
|
|
9
|
+
}
|
|
10
|
+
export const center = (node, dx = 0, dy = 0) => () => {
|
|
11
|
+
const box = srect(node);
|
|
12
|
+
return [box.x + box.w / 2 + dx, box.y + box.h / 2 + dy];
|
|
13
|
+
};
|
|
14
|
+
const unzoomed = (box, camera) => {
|
|
15
|
+
const c = camera.state;
|
|
16
|
+
return { x: (box.x - c.tx) / c.z, y: (box.y - c.ty) / c.z, w: box.w / c.z, h: box.h / c.z };
|
|
17
|
+
};
|
|
18
|
+
/** A node's box in a camera's own scene space, where marks and a cursor inside the camera are placed. */
|
|
19
|
+
export const camrect = (node, camera) => unzoomed(srect(node), camera);
|
|
20
|
+
/** The stage box of the last line of `phrase` inside `el`'s text, or null where it is not shown yet. */
|
|
21
|
+
export function phraseRect(el, phrase) {
|
|
22
|
+
if (!el)
|
|
23
|
+
return null;
|
|
24
|
+
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
|
|
25
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
26
|
+
const text = node instanceof Text ? node.data : '';
|
|
27
|
+
const index = text.indexOf(phrase);
|
|
28
|
+
if (index < 0)
|
|
29
|
+
continue;
|
|
30
|
+
const range = document.createRange();
|
|
31
|
+
range.setStart(node, index);
|
|
32
|
+
range.setEnd(node, index + phrase.length);
|
|
33
|
+
const rects = [...range.getClientRects()];
|
|
34
|
+
const last = rects[rects.length - 1];
|
|
35
|
+
if (!last)
|
|
36
|
+
return null;
|
|
37
|
+
const stage = stageBox();
|
|
38
|
+
const k = stageSize.fit;
|
|
39
|
+
return { x: (last.left - stage.left) / k, y: (last.top - stage.top) / k, w: last.width / k, h: last.height / k, lines: rects.length };
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
export function phraseCamRect(el, phrase, camera) {
|
|
44
|
+
const box = phraseRect(el, phrase);
|
|
45
|
+
return box ? unzoomed(box, camera) : null;
|
|
46
|
+
}
|