@open-artifact/motion-stage 0.0.0-stage → 0.12.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 +69 -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,95 @@
|
|
|
1
|
+
import type { AssetStore } from '../assets.ts';
|
|
2
|
+
import type { StackName } from '../fonts.ts';
|
|
3
|
+
import type { PlateOptions } from '../snapshot/plate.ts';
|
|
4
|
+
import { Camera } from './camera.ts';
|
|
5
|
+
import type { ChipOptions, ShowOptions, SpotlightOptions } from './chip.ts';
|
|
6
|
+
import { CURSORS, Cursor } from './cursor.ts';
|
|
7
|
+
import { el, h } from './dom.ts';
|
|
8
|
+
import { camrect, center, phraseCamRect, phraseRect, srect } from './geometry.ts';
|
|
9
|
+
import type { StageRect } from './geometry.ts';
|
|
10
|
+
import { Marks } from './marks.ts';
|
|
11
|
+
import * as math from './math.ts';
|
|
12
|
+
import { cutPaper, polyLerp, polyStr } from './paper.ts';
|
|
13
|
+
import { rng, vnoise } from './random.ts';
|
|
14
|
+
import { TextFx } from './text-fx.ts';
|
|
15
|
+
import { esc, streamed } from './typing.ts';
|
|
16
|
+
import type { Typist } from './typing.ts';
|
|
17
|
+
export declare const KIT_VERSION = 2;
|
|
18
|
+
export interface VideoOptions {
|
|
19
|
+
loop?: boolean;
|
|
20
|
+
/** Film seconds at which the clip's first frame shows. */
|
|
21
|
+
start?: number;
|
|
22
|
+
class?: string;
|
|
23
|
+
style?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The cut's whole world. A film made of scenes adds `scene`, `scenes`, `score` and `narration` (scenes/install.ts);
|
|
27
|
+
* each scene sees a copy of it on its own clock and in its own box.
|
|
28
|
+
*/
|
|
29
|
+
export interface Kit {
|
|
30
|
+
version: number;
|
|
31
|
+
readonly W: number;
|
|
32
|
+
readonly H: number;
|
|
33
|
+
readonly stage: HTMLElement;
|
|
34
|
+
readonly fx: CanvasRenderingContext2D;
|
|
35
|
+
readonly fxCanvas: HTMLCanvasElement;
|
|
36
|
+
el: typeof el;
|
|
37
|
+
h: typeof h;
|
|
38
|
+
esc: typeof esc;
|
|
39
|
+
css(text: string): HTMLStyleElement;
|
|
40
|
+
module(name: string): string | null;
|
|
41
|
+
json(name: string): unknown;
|
|
42
|
+
exec(name: string): void;
|
|
43
|
+
modules(): string[];
|
|
44
|
+
assets: AssetStore;
|
|
45
|
+
cursors: typeof CURSORS;
|
|
46
|
+
fonts: Record<StackName, string>;
|
|
47
|
+
typing: {
|
|
48
|
+
times: number[];
|
|
49
|
+
};
|
|
50
|
+
clamp01: typeof math.clamp01;
|
|
51
|
+
clamp: typeof math.clamp;
|
|
52
|
+
seg: typeof math.seg;
|
|
53
|
+
lerp: typeof math.lerp;
|
|
54
|
+
eo: typeof math.eo;
|
|
55
|
+
ei: typeof math.ei;
|
|
56
|
+
eio: typeof math.eio;
|
|
57
|
+
eoq: typeof math.eoq;
|
|
58
|
+
eox: typeof math.eox;
|
|
59
|
+
eob: typeof math.eob;
|
|
60
|
+
spring: typeof math.spring;
|
|
61
|
+
step: typeof math.step;
|
|
62
|
+
pulse: typeof math.pulse;
|
|
63
|
+
smoothstep: typeof math.smoothstep;
|
|
64
|
+
breathe: typeof math.breathe;
|
|
65
|
+
rng: typeof rng;
|
|
66
|
+
vnoise: typeof vnoise;
|
|
67
|
+
typist(text: string, start: number, cps: number, seed?: number): Typist;
|
|
68
|
+
streamed: typeof streamed;
|
|
69
|
+
srect: typeof srect;
|
|
70
|
+
center: typeof center;
|
|
71
|
+
camrect: typeof camrect;
|
|
72
|
+
phraseRect: typeof phraseRect;
|
|
73
|
+
phraseCamRect: typeof phraseCamRect;
|
|
74
|
+
Camera: typeof Camera;
|
|
75
|
+
Cursor: typeof Cursor;
|
|
76
|
+
Marks: typeof Marks;
|
|
77
|
+
TextFx: typeof TextFx;
|
|
78
|
+
cutPaper: typeof cutPaper;
|
|
79
|
+
polyLerp: typeof polyLerp;
|
|
80
|
+
polyStr: typeof polyStr;
|
|
81
|
+
video(source: string, options?: VideoOptions): HTMLVideoElement;
|
|
82
|
+
seek(video: HTMLVideoElement, second: number): void;
|
|
83
|
+
cursor(glyph?: string): Cursor;
|
|
84
|
+
svgLayer(): SVGSVGElement;
|
|
85
|
+
chip(text: string, options?: ChipOptions): HTMLElement;
|
|
86
|
+
show(node: HTMLElement, t: number, tIn: number, tOut?: number | null, options?: ShowOptions): number;
|
|
87
|
+
spotlight(rect: StageRect | null, amount: number, options?: SpotlightOptions): void;
|
|
88
|
+
raster(html: string | Node, options?: PlateOptions): Promise<HTMLCanvasElement>;
|
|
89
|
+
plateStats(): {
|
|
90
|
+
count: number;
|
|
91
|
+
bytes: number;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export declare const typingClock: () => number[];
|
|
95
|
+
export declare function makeKit(): Kit;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { assetStore, assetUrl } from "../assets.js";
|
|
2
|
+
import { fontFamilies } from "../fonts.js";
|
|
3
|
+
import { findModule, injectScript, moduleNames } from "../modules.js";
|
|
4
|
+
import { plateStats, raster } from "../snapshot/plate.js";
|
|
5
|
+
import { builtStage } from "../stage.js";
|
|
6
|
+
import { stageSize } from "../stage-size.js";
|
|
7
|
+
import { videoSeeks, watchVideo } from "../video-sync.js";
|
|
8
|
+
import { Camera } from "./camera.js";
|
|
9
|
+
import { chip, show, spotlight } from "./chip.js";
|
|
10
|
+
import { CURSORS, Cursor, cursorElement } from "./cursor.js";
|
|
11
|
+
import { el, h } from "./dom.js";
|
|
12
|
+
import { camrect, center, phraseCamRect, phraseRect, srect } from "./geometry.js";
|
|
13
|
+
import { Marks } from "./marks.js";
|
|
14
|
+
import * as math from "./math.js";
|
|
15
|
+
import { cutPaper, polyLerp, polyStr } from "./paper.js";
|
|
16
|
+
import { rng, vnoise } from "./random.js";
|
|
17
|
+
import { TextFx } from "./text-fx.js";
|
|
18
|
+
import { esc, streamed, typist } from "./typing.js";
|
|
19
|
+
export const KIT_VERSION = 2;
|
|
20
|
+
/** Every key press a typist made, in film seconds: the score's ticks follow them. */
|
|
21
|
+
const typingTimes = [];
|
|
22
|
+
export const typingClock = () => typingTimes;
|
|
23
|
+
/** Inserted just under the fx canvas, which stays on top of everything the film adds. */
|
|
24
|
+
function beneathFx(node) {
|
|
25
|
+
const { stage, fxCanvas } = builtStage();
|
|
26
|
+
stage.insertBefore(node, fxCanvas.parentNode === stage ? fxCanvas : null);
|
|
27
|
+
}
|
|
28
|
+
export function makeKit() {
|
|
29
|
+
const kit = {
|
|
30
|
+
version: KIT_VERSION,
|
|
31
|
+
get W() {
|
|
32
|
+
return stageSize.width;
|
|
33
|
+
},
|
|
34
|
+
get H() {
|
|
35
|
+
return stageSize.height;
|
|
36
|
+
},
|
|
37
|
+
get stage() {
|
|
38
|
+
return builtStage().stage;
|
|
39
|
+
},
|
|
40
|
+
get fx() {
|
|
41
|
+
return builtStage().fx;
|
|
42
|
+
},
|
|
43
|
+
get fxCanvas() {
|
|
44
|
+
return builtStage().fxCanvas;
|
|
45
|
+
},
|
|
46
|
+
el,
|
|
47
|
+
h,
|
|
48
|
+
esc,
|
|
49
|
+
css(text) {
|
|
50
|
+
const style = el('style', { text: String(text) });
|
|
51
|
+
beneathFx(style);
|
|
52
|
+
return style;
|
|
53
|
+
},
|
|
54
|
+
module: (name) => findModule(String(name))?.text ?? null,
|
|
55
|
+
json(name) {
|
|
56
|
+
const text = kit.module(name);
|
|
57
|
+
if (text === null)
|
|
58
|
+
return null;
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(text);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
console.error('kit.json(' + name + '): ' + (error instanceof Error ? error.message : String(error)));
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
exec(name) {
|
|
68
|
+
const module = findModule(String(name));
|
|
69
|
+
if (!module) {
|
|
70
|
+
console.error('kit.exec: no module ' + name);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
injectScript(module.text, 'module ' + name);
|
|
74
|
+
},
|
|
75
|
+
modules: moduleNames,
|
|
76
|
+
assets: assetStore,
|
|
77
|
+
cursors: CURSORS,
|
|
78
|
+
fonts: fontFamilies,
|
|
79
|
+
typing: { times: typingTimes },
|
|
80
|
+
...math,
|
|
81
|
+
rng,
|
|
82
|
+
vnoise,
|
|
83
|
+
typist: (text, start, cps, seed) => typist(text, start, cps, seed, typingTimes),
|
|
84
|
+
streamed,
|
|
85
|
+
srect,
|
|
86
|
+
center,
|
|
87
|
+
camrect,
|
|
88
|
+
phraseRect,
|
|
89
|
+
phraseCamRect,
|
|
90
|
+
Camera,
|
|
91
|
+
Cursor,
|
|
92
|
+
Marks,
|
|
93
|
+
TextFx,
|
|
94
|
+
cutPaper,
|
|
95
|
+
polyLerp,
|
|
96
|
+
polyStr,
|
|
97
|
+
// A muted clip the stage keeps at film time t − start (looped if asked), playing with the transport; kit.seek shows an exact second instead.
|
|
98
|
+
video(source, options = {}) {
|
|
99
|
+
const video = el('video', {
|
|
100
|
+
muted: true,
|
|
101
|
+
playsInline: true,
|
|
102
|
+
preload: 'auto',
|
|
103
|
+
loop: !!options.loop,
|
|
104
|
+
attrs: { 'data-start': String(options.start || 0) },
|
|
105
|
+
class: options.class,
|
|
106
|
+
style: options.style,
|
|
107
|
+
});
|
|
108
|
+
if (!(video instanceof HTMLVideoElement))
|
|
109
|
+
throw new Error('kit.video: no video element');
|
|
110
|
+
let src = String(source || '');
|
|
111
|
+
const asset = assetUrl(src);
|
|
112
|
+
if (asset !== null)
|
|
113
|
+
src = asset;
|
|
114
|
+
else if (!/[:/]/.test(src))
|
|
115
|
+
console.error('kit.video: no asset ' + src);
|
|
116
|
+
watchVideo(video);
|
|
117
|
+
video.src = src;
|
|
118
|
+
return video;
|
|
119
|
+
},
|
|
120
|
+
seek(video, second) {
|
|
121
|
+
if (video instanceof HTMLVideoElement && isFinite(second))
|
|
122
|
+
videoSeeks.set(video, Number(second));
|
|
123
|
+
},
|
|
124
|
+
cursor(glyph) {
|
|
125
|
+
const node = cursorElement(glyph);
|
|
126
|
+
beneathFx(node);
|
|
127
|
+
return new Cursor(node);
|
|
128
|
+
},
|
|
129
|
+
svgLayer() {
|
|
130
|
+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
131
|
+
svg.setAttribute('width', String(stageSize.width));
|
|
132
|
+
svg.setAttribute('height', String(stageSize.height));
|
|
133
|
+
svg.setAttribute('viewBox', `0 0 ${stageSize.width} ${stageSize.height}`);
|
|
134
|
+
svg.style.cssText = 'position:absolute;left:0;top:0;pointer-events:none;overflow:visible';
|
|
135
|
+
beneathFx(svg);
|
|
136
|
+
return svg;
|
|
137
|
+
},
|
|
138
|
+
chip(text, options) {
|
|
139
|
+
const node = chip(text, options);
|
|
140
|
+
beneathFx(node);
|
|
141
|
+
return node;
|
|
142
|
+
},
|
|
143
|
+
show,
|
|
144
|
+
spotlight: (rect, amount, options) => spotlight(builtStage().fx, rect, amount, options),
|
|
145
|
+
raster,
|
|
146
|
+
plateStats,
|
|
147
|
+
};
|
|
148
|
+
return kit;
|
|
149
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Ease } from './math.ts';
|
|
2
|
+
import type { Point } from './paper.ts';
|
|
3
|
+
export interface MarkGeometry {
|
|
4
|
+
x?: number;
|
|
5
|
+
y?: number;
|
|
6
|
+
w?: number;
|
|
7
|
+
h?: number;
|
|
8
|
+
r?: number;
|
|
9
|
+
from?: Point;
|
|
10
|
+
to?: Point;
|
|
11
|
+
d?: Point[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A hand-drawn mark. Geometry is in the space of the SVG layer the marks draw in. Kinds:
|
|
15
|
+
* circle {x,y,w,h} (the box to circle), underline {x,y,w}, arrow {from,to,bend}, bracket {x,y,h,side},
|
|
16
|
+
* scribble {x,y,w,h}, burst {x,y,r,n}, check {x,y,size}, cross {x,y,size}, path {d}.
|
|
17
|
+
*/
|
|
18
|
+
export interface Mark extends MarkGeometry {
|
|
19
|
+
id?: string;
|
|
20
|
+
kind: string;
|
|
21
|
+
/** Draw-on start, film seconds. */
|
|
22
|
+
t0: number;
|
|
23
|
+
dur: number;
|
|
24
|
+
/** When it starts to erase. */
|
|
25
|
+
out?: number;
|
|
26
|
+
outDur: number;
|
|
27
|
+
until?: number;
|
|
28
|
+
color: string;
|
|
29
|
+
width: number;
|
|
30
|
+
seed: number;
|
|
31
|
+
opacity?: number;
|
|
32
|
+
rough?: boolean;
|
|
33
|
+
still?: boolean;
|
|
34
|
+
boil?: number;
|
|
35
|
+
ease?: Ease;
|
|
36
|
+
/** Geometry read every frame, for a thing that moves or does not exist yet. */
|
|
37
|
+
geom?: () => MarkGeometry | null | undefined;
|
|
38
|
+
n?: number;
|
|
39
|
+
spread?: number;
|
|
40
|
+
angle?: number;
|
|
41
|
+
size?: number;
|
|
42
|
+
bend?: number;
|
|
43
|
+
head?: number;
|
|
44
|
+
side?: string;
|
|
45
|
+
depth?: number;
|
|
46
|
+
pad?: number;
|
|
47
|
+
padX?: number;
|
|
48
|
+
padY?: number;
|
|
49
|
+
tilt?: number;
|
|
50
|
+
startDeg?: number;
|
|
51
|
+
sweepDeg?: number;
|
|
52
|
+
wave?: number;
|
|
53
|
+
rise?: number;
|
|
54
|
+
hook?: number;
|
|
55
|
+
}
|
|
56
|
+
/** The marks redraw this many times a second, each time a little different: the boil. */
|
|
57
|
+
export declare const BOIL_FPS = 12;
|
|
58
|
+
export declare const MARK_COLOR = "#e5484d";
|
|
59
|
+
/** The strokes of a mark at boil frame `boil`: each a polyline, drawn one after another. */
|
|
60
|
+
export declare function markStrokes(mark: Mark, boil: number): Point[][];
|
|
61
|
+
/** A smooth hand line through the points: Catmull-Rom as cubic Béziers. */
|
|
62
|
+
export declare function strokePath(points: Point[]): string;
|
|
63
|
+
/** [left, top, right, bottom] of a stroke's points and control points: a cubic never leaves the box of its four points. */
|
|
64
|
+
export declare function strokeBox(points: Point[]): [number, number, number, number];
|
|
65
|
+
export declare function strokeLength(points: Point[]): number;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { rng, vnoise } from "./random.js";
|
|
2
|
+
/** The marks redraw this many times a second, each time a little different: the boil. */
|
|
3
|
+
export const BOIL_FPS = 12;
|
|
4
|
+
export const MARK_COLOR = '#e5484d';
|
|
5
|
+
const point = (p) => p ?? [NaN, NaN];
|
|
6
|
+
/** The strokes of a mark at boil frame `boil`: each a polyline, drawn one after another. */
|
|
7
|
+
export function markStrokes(mark, boil) {
|
|
8
|
+
if (typeof mark.geom === 'function') {
|
|
9
|
+
const geometry = mark.geom();
|
|
10
|
+
if (!geometry)
|
|
11
|
+
return [];
|
|
12
|
+
Object.assign(mark, geometry);
|
|
13
|
+
}
|
|
14
|
+
const x = mark.x ?? NaN;
|
|
15
|
+
const y = mark.y ?? NaN;
|
|
16
|
+
const w = mark.w ?? NaN;
|
|
17
|
+
const h = mark.h ?? NaN;
|
|
18
|
+
if ((mark.kind === 'circle' || mark.kind === 'scribble') && !(w > 0 && h > 0 && isFinite(x + y)))
|
|
19
|
+
return [];
|
|
20
|
+
const next = rng(mark.seed * 7919 + 13);
|
|
21
|
+
const jitter = vnoise(mark.seed * 31 + boil * 977);
|
|
22
|
+
const amp = mark.boil !== undefined ? mark.boil : 1.1;
|
|
23
|
+
const shake = (i, scale = amp) => jitter(i * 0.37 + boil * 3.1) * scale;
|
|
24
|
+
const out = [];
|
|
25
|
+
switch (mark.kind) {
|
|
26
|
+
case 'circle': {
|
|
27
|
+
const pad = mark.pad !== undefined ? mark.pad : 0.18;
|
|
28
|
+
const cx = x + w / 2 + (next() - 0.5) * w * 0.04;
|
|
29
|
+
const cy = y + h / 2 + (next() - 0.5) * h * 0.06;
|
|
30
|
+
const rx = mark.padX !== undefined ? w / 2 + mark.padX : (w / 2) * (1 + pad) + h * 0.25;
|
|
31
|
+
const ry = mark.padY !== undefined ? h / 2 + mark.padY : (h / 2) * (1 + pad) + Math.min(h * 0.35, 26);
|
|
32
|
+
const tilt = ((mark.tilt !== undefined ? mark.tilt : -4 + next() * 5) * Math.PI) / 180;
|
|
33
|
+
const start = ((mark.startDeg !== undefined ? mark.startDeg : 195 + next() * 30) * Math.PI) / 180;
|
|
34
|
+
const sweep = ((mark.sweepDeg !== undefined ? mark.sweepDeg : 385 + next() * 30) * Math.PI) / 180;
|
|
35
|
+
const wobble = vnoise(mark.seed * 3 + 1);
|
|
36
|
+
const squash = vnoise(mark.seed * 5 + 2);
|
|
37
|
+
const points = [];
|
|
38
|
+
for (let i = 0; i <= 90; i++) {
|
|
39
|
+
const u = i / 90;
|
|
40
|
+
const a = start + sweep * u;
|
|
41
|
+
// The radius grows over the sweep, so the tail passes outside the start: an open spiral.
|
|
42
|
+
const drift = 1 + 0.07 * u + 0.045 * wobble(u * 3.2);
|
|
43
|
+
const px = Math.cos(a) * rx * drift * (1 + 0.03 * squash(u * 4 + 1));
|
|
44
|
+
const py = Math.sin(a) * ry * drift * (1 + 0.04 * wobble(u * 4 + 7));
|
|
45
|
+
points.push([
|
|
46
|
+
cx + px * Math.cos(tilt) - py * Math.sin(tilt) + shake(i),
|
|
47
|
+
cy + px * Math.sin(tilt) + py * Math.cos(tilt) + shake(i + 500),
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
out.push(points);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case 'underline': {
|
|
54
|
+
const wave = vnoise(mark.seed * 11 + 4);
|
|
55
|
+
const over = w * 0.04;
|
|
56
|
+
const points = [];
|
|
57
|
+
for (let i = 0; i <= 40; i++) {
|
|
58
|
+
const u = i / 40;
|
|
59
|
+
let py = y + wave(u * 2.3) * (mark.wave !== undefined ? mark.wave : 3.5) - u * (mark.rise || 2);
|
|
60
|
+
if (u > 0.93)
|
|
61
|
+
py -= ((u - 0.93) / 0.07) * (mark.hook !== undefined ? mark.hook : 7);
|
|
62
|
+
points.push([x - over + (w + 2 * over) * u + shake(i), py + shake(i + 300)]);
|
|
63
|
+
}
|
|
64
|
+
out.push(points);
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case 'arrow':
|
|
68
|
+
out.push(...arrowStrokes(mark, shake));
|
|
69
|
+
break;
|
|
70
|
+
case 'bracket': {
|
|
71
|
+
const side = mark.side === 'right' ? -1 : 1;
|
|
72
|
+
const depth = mark.depth || 18;
|
|
73
|
+
const wave = vnoise(mark.seed + 9);
|
|
74
|
+
const points = [];
|
|
75
|
+
for (let i = 0; i <= 30; i++) {
|
|
76
|
+
const u = i / 30;
|
|
77
|
+
const bulge = (u < 0.08 ? (0.08 - u) / 0.08 : u > 0.92 ? (u - 0.92) / 0.08 : 0) * depth;
|
|
78
|
+
points.push([x + side * bulge + wave(u * 3) * 2 + shake(i), y + h * u + shake(i + 90)]);
|
|
79
|
+
}
|
|
80
|
+
out.push(points);
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case 'scribble': {
|
|
84
|
+
const rows = Math.max(3, Math.round(w / (h * 0.55)));
|
|
85
|
+
const wave = vnoise(mark.seed + 21);
|
|
86
|
+
const points = [];
|
|
87
|
+
for (let i = 0; i <= rows; i++) {
|
|
88
|
+
const top = i % 2 === 0;
|
|
89
|
+
const px = x + w * (i / rows) + wave(i) * 4;
|
|
90
|
+
points.push([px + (top ? -h * 0.25 : h * 0.25) + shake(i), (top ? y + h * 0.12 : y + h * 0.88) + shake(i + 40, amp * 1.5)]);
|
|
91
|
+
}
|
|
92
|
+
out.push(points);
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case 'burst': {
|
|
96
|
+
const count = mark.n || 3;
|
|
97
|
+
const spread = mark.spread || 70;
|
|
98
|
+
const base = (mark.angle !== undefined ? mark.angle : -90) - spread / 2;
|
|
99
|
+
const r = mark.r ?? NaN;
|
|
100
|
+
for (let k = 0; k < count; k++) {
|
|
101
|
+
const a = ((base + spread * (count === 1 ? 0.5 : k / (count - 1))) * Math.PI) / 180;
|
|
102
|
+
const r0 = r * 0.55;
|
|
103
|
+
const r1 = r * (0.95 + 0.12 * ((k * 37) % 3));
|
|
104
|
+
out.push([
|
|
105
|
+
[x + Math.cos(a) * r0 + shake(k), y + Math.sin(a) * r0 + shake(k + 5)],
|
|
106
|
+
[x + Math.cos(a) * r1 + shake(k + 9), y + Math.sin(a) * r1 + shake(k + 14)],
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
case 'check': {
|
|
112
|
+
const s = mark.size || 40;
|
|
113
|
+
out.push([
|
|
114
|
+
[x - s * 0.45 + shake(1), y - s * 0.05 + shake(2)],
|
|
115
|
+
[x - s * 0.12 + shake(3), y + s * 0.32 + shake(4)],
|
|
116
|
+
[x + s * 0.55 + shake(5), y - s * 0.48 + shake(6)],
|
|
117
|
+
]);
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case 'cross': {
|
|
121
|
+
const s = mark.size || 40;
|
|
122
|
+
out.push([
|
|
123
|
+
[x - s / 2 + shake(1), y - s / 2 + shake(2)],
|
|
124
|
+
[x + s / 2 + shake(3), y + s / 2 + shake(4)],
|
|
125
|
+
]);
|
|
126
|
+
out.push([
|
|
127
|
+
[x + s / 2 + shake(5), y - s / 2 + shake(6)],
|
|
128
|
+
[x - s / 2 + shake(7), y + s / 2 + shake(8)],
|
|
129
|
+
]);
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case 'path':
|
|
133
|
+
out.push((mark.d ?? []).map(([px, py], i) => [px + shake(i), py + shake(i + 60)]));
|
|
134
|
+
break;
|
|
135
|
+
default:
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
function arrowStrokes(mark, shake) {
|
|
141
|
+
const [x0, y0] = point(mark.from);
|
|
142
|
+
const [x1, y1] = point(mark.to);
|
|
143
|
+
const dx = x1 - x0;
|
|
144
|
+
const dy = y1 - y0;
|
|
145
|
+
const length = Math.hypot(dx, dy) || 1;
|
|
146
|
+
const nx = -dy / length;
|
|
147
|
+
const ny = dx / length;
|
|
148
|
+
const bend = (mark.bend !== undefined ? mark.bend : 0.18) * length;
|
|
149
|
+
const wave = vnoise(mark.seed * 13 + 5);
|
|
150
|
+
const shaft = [];
|
|
151
|
+
for (let i = 0; i <= 44; i++) {
|
|
152
|
+
const u = i / 44;
|
|
153
|
+
const b = Math.sin(Math.PI * u) * bend + wave(u * 2) * 3;
|
|
154
|
+
shaft.push([x0 + dx * u + nx * b + shake(i), y0 + dy * u + ny * b + shake(i + 200)]);
|
|
155
|
+
}
|
|
156
|
+
// The head: two strokes back from the tip, off the shaft's last direction.
|
|
157
|
+
const [ax, ay] = shaft[41] ?? [x0, y0];
|
|
158
|
+
const tl = Math.hypot(x1 - ax, y1 - ay) || 1;
|
|
159
|
+
const ux = (x1 - ax) / tl;
|
|
160
|
+
const uy = (y1 - ay) / tl;
|
|
161
|
+
const head = mark.head !== undefined ? mark.head : Math.min(46, length * 0.28);
|
|
162
|
+
const spread = 0.5;
|
|
163
|
+
const left = [
|
|
164
|
+
[x1, y1],
|
|
165
|
+
[x1 - (ux * Math.cos(spread) - uy * Math.sin(spread)) * head + shake(7), y1 - (uy * Math.cos(spread) + ux * Math.sin(spread)) * head + shake(8)],
|
|
166
|
+
];
|
|
167
|
+
const right = [
|
|
168
|
+
[x1, y1],
|
|
169
|
+
[
|
|
170
|
+
x1 - (ux * Math.cos(-spread) - uy * Math.sin(-spread)) * head * 0.9 + shake(9),
|
|
171
|
+
y1 - (uy * Math.cos(-spread) + ux * Math.sin(-spread)) * head * 0.9 + shake(10),
|
|
172
|
+
],
|
|
173
|
+
];
|
|
174
|
+
return [shaft, left, right];
|
|
175
|
+
}
|
|
176
|
+
const fixed = (n) => n.toFixed(1);
|
|
177
|
+
/** A smooth hand line through the points: Catmull-Rom as cubic Béziers. */
|
|
178
|
+
export function strokePath(points) {
|
|
179
|
+
if (points.length < 3)
|
|
180
|
+
return 'M' + points.map((p) => fixed(p[0]) + ' ' + fixed(p[1])).join(' L');
|
|
181
|
+
const first = points[0] ?? [0, 0];
|
|
182
|
+
let d = `M${fixed(first[0])} ${fixed(first[1])}`;
|
|
183
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
184
|
+
const [c1, c2, end] = controls(points, i);
|
|
185
|
+
d += ` C${fixed(c1[0])} ${fixed(c1[1])},${fixed(c2[0])} ${fixed(c2[1])},${fixed(end[0])} ${fixed(end[1])}`;
|
|
186
|
+
}
|
|
187
|
+
return d;
|
|
188
|
+
}
|
|
189
|
+
function controls(points, i) {
|
|
190
|
+
const p1 = points[i] ?? [0, 0];
|
|
191
|
+
const p0 = points[i - 1] ?? p1;
|
|
192
|
+
const p2 = points[i + 1] ?? p1;
|
|
193
|
+
const p3 = points[i + 2] ?? p2;
|
|
194
|
+
return [
|
|
195
|
+
[p1[0] + (p2[0] - p0[0]) / 6, p1[1] + (p2[1] - p0[1]) / 6],
|
|
196
|
+
[p2[0] - (p3[0] - p1[0]) / 6, p2[1] - (p3[1] - p1[1]) / 6],
|
|
197
|
+
p2,
|
|
198
|
+
];
|
|
199
|
+
}
|
|
200
|
+
/** [left, top, right, bottom] of a stroke's points and control points: a cubic never leaves the box of its four points. */
|
|
201
|
+
export function strokeBox(points) {
|
|
202
|
+
const box = [Infinity, Infinity, -Infinity, -Infinity];
|
|
203
|
+
const take = (p) => {
|
|
204
|
+
box[0] = Math.min(box[0], p[0]);
|
|
205
|
+
box[1] = Math.min(box[1], p[1]);
|
|
206
|
+
box[2] = Math.max(box[2], p[0]);
|
|
207
|
+
box[3] = Math.max(box[3], p[1]);
|
|
208
|
+
};
|
|
209
|
+
for (const p of points)
|
|
210
|
+
take(p);
|
|
211
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
212
|
+
const [c1, c2] = controls(points, i);
|
|
213
|
+
take(c1);
|
|
214
|
+
take(c2);
|
|
215
|
+
}
|
|
216
|
+
return box;
|
|
217
|
+
}
|
|
218
|
+
export function strokeLength(points) {
|
|
219
|
+
let length = 0;
|
|
220
|
+
for (let i = 1; i < points.length; i++) {
|
|
221
|
+
const a = points[i - 1] ?? [0, 0];
|
|
222
|
+
const b = points[i] ?? a;
|
|
223
|
+
length += Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
224
|
+
}
|
|
225
|
+
return length;
|
|
226
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Mark } from './mark-strokes.ts';
|
|
2
|
+
import type { Point } from './paper.ts';
|
|
3
|
+
export type MarkInput = Partial<Mark> & {
|
|
4
|
+
kind: string;
|
|
5
|
+
t0: number;
|
|
6
|
+
};
|
|
7
|
+
/** Hand-drawn marks on an SVG layer: they draw on, boil, and erase. */
|
|
8
|
+
export declare class Marks {
|
|
9
|
+
svg: SVGSVGElement;
|
|
10
|
+
items: Mark[];
|
|
11
|
+
nodes: Map<Mark, SVGGElement>;
|
|
12
|
+
constructor(svg: SVGSVGElement);
|
|
13
|
+
/** Geometry as numbers, or `geom: () => ({x, y, w, h})` read every frame. Getters on `mark` stay getters. */
|
|
14
|
+
add(mark: MarkInput): this;
|
|
15
|
+
get(id: string): Mark | undefined;
|
|
16
|
+
clear(): void;
|
|
17
|
+
strokes(mark: Mark, boil: number): Point[][];
|
|
18
|
+
static toPath(points: Point[]): string;
|
|
19
|
+
render(t: number): void;
|
|
20
|
+
private drawMark;
|
|
21
|
+
}
|