@openartifactsai/motion-stage 0.0.0-stage → 0.23.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.
Files changed (225) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +70 -4
  5. package/src/build.d.ts +18 -0
  6. package/src/build.js +73 -0
  7. package/src/host.d.ts +37 -0
  8. package/src/host.js +76 -0
  9. package/src/index.d.ts +11 -0
  10. package/src/index.js +6 -0
  11. package/src/layers.d.ts +30 -0
  12. package/src/layers.js +56 -0
  13. package/src/offline/audio.d.ts +19 -0
  14. package/src/offline/audio.js +20 -0
  15. package/src/offline/data.d.ts +26 -0
  16. package/src/offline/data.js +71 -0
  17. package/src/offline/main.d.ts +9 -0
  18. package/src/offline/main.js +52 -0
  19. package/src/offline/player.d.ts +23 -0
  20. package/src/offline/player.js +157 -0
  21. package/src/protocol/clip-guards.d.ts +5 -0
  22. package/src/protocol/clip-guards.js +22 -0
  23. package/src/protocol/guards.d.ts +6 -0
  24. package/src/protocol/guards.js +257 -0
  25. package/src/protocol/layer-guards.d.ts +7 -0
  26. package/src/protocol/layer-guards.js +100 -0
  27. package/src/protocol/layers.d.ts +135 -0
  28. package/src/protocol/layers.js +1 -0
  29. package/src/protocol/types.d.ts +340 -0
  30. package/src/protocol/types.js +7 -0
  31. package/src/protocol.d.ts +2 -0
  32. package/src/protocol.js +2 -0
  33. package/src/raster.d.ts +4 -0
  34. package/src/raster.js +2 -0
  35. package/src/runtime/assets.d.ts +19 -0
  36. package/src/runtime/assets.js +63 -0
  37. package/src/runtime/caption-box.d.ts +3 -0
  38. package/src/runtime/caption-box.js +16 -0
  39. package/src/runtime/captions.d.ts +31 -0
  40. package/src/runtime/captions.js +115 -0
  41. package/src/runtime/clip-captions.d.ts +9 -0
  42. package/src/runtime/clip-captions.js +27 -0
  43. package/src/runtime/clips.d.ts +83 -0
  44. package/src/runtime/clips.js +259 -0
  45. package/src/runtime/console.d.ts +13 -0
  46. package/src/runtime/console.js +99 -0
  47. package/src/runtime/cut.d.ts +7 -0
  48. package/src/runtime/cut.js +63 -0
  49. package/src/runtime/determinism.d.ts +12 -0
  50. package/src/runtime/determinism.js +47 -0
  51. package/src/runtime/film.d.ts +43 -0
  52. package/src/runtime/film.js +31 -0
  53. package/src/runtime/focus.d.ts +8 -0
  54. package/src/runtime/focus.js +71 -0
  55. package/src/runtime/fonts.d.ts +22 -0
  56. package/src/runtime/fonts.js +109 -0
  57. package/src/runtime/frame.d.ts +9 -0
  58. package/src/runtime/frame.js +140 -0
  59. package/src/runtime/fx/backdrop.d.ts +60 -0
  60. package/src/runtime/fx/backdrop.js +264 -0
  61. package/src/runtime/fx/color.d.ts +19 -0
  62. package/src/runtime/fx/color.js +84 -0
  63. package/src/runtime/fx/css-color.d.ts +6 -0
  64. package/src/runtime/fx/css-color.js +44 -0
  65. package/src/runtime/fx/curve.d.ts +12 -0
  66. package/src/runtime/fx/curve.js +34 -0
  67. package/src/runtime/fx/gl.d.ts +29 -0
  68. package/src/runtime/fx/gl.js +207 -0
  69. package/src/runtime/fx/glsl.d.ts +35 -0
  70. package/src/runtime/fx/glsl.js +113 -0
  71. package/src/runtime/fx/index.d.ts +23 -0
  72. package/src/runtime/fx/index.js +151 -0
  73. package/src/runtime/fx/key.d.ts +11 -0
  74. package/src/runtime/fx/key.js +21 -0
  75. package/src/runtime/fx/numbers.d.ts +14 -0
  76. package/src/runtime/fx/numbers.js +57 -0
  77. package/src/runtime/fx/pace.d.ts +17 -0
  78. package/src/runtime/fx/pace.js +26 -0
  79. package/src/runtime/fx/paint.d.ts +28 -0
  80. package/src/runtime/fx/paint.js +150 -0
  81. package/src/runtime/guard.d.ts +4 -0
  82. package/src/runtime/guard.js +141 -0
  83. package/src/runtime/hand-titles/index.d.ts +9 -0
  84. package/src/runtime/hand-titles/index.js +73 -0
  85. package/src/runtime/hand-titles/layer.d.ts +20 -0
  86. package/src/runtime/hand-titles/layer.js +118 -0
  87. package/src/runtime/hand-titles/place.d.ts +27 -0
  88. package/src/runtime/hand-titles/place.js +45 -0
  89. package/src/runtime/held-store.d.ts +35 -0
  90. package/src/runtime/held-store.js +63 -0
  91. package/src/runtime/held.d.ts +19 -0
  92. package/src/runtime/held.js +225 -0
  93. package/src/runtime/kit/camera.d.ts +59 -0
  94. package/src/runtime/kit/camera.js +81 -0
  95. package/src/runtime/kit/chip.d.ts +25 -0
  96. package/src/runtime/kit/chip.js +75 -0
  97. package/src/runtime/kit/count.d.ts +37 -0
  98. package/src/runtime/kit/count.js +100 -0
  99. package/src/runtime/kit/cursor.d.ts +46 -0
  100. package/src/runtime/kit/cursor.js +108 -0
  101. package/src/runtime/kit/dom.d.ts +14 -0
  102. package/src/runtime/kit/dom.js +46 -0
  103. package/src/runtime/kit/geometry.d.ts +24 -0
  104. package/src/runtime/kit/geometry.js +56 -0
  105. package/src/runtime/kit/index.d.ts +113 -0
  106. package/src/runtime/kit/index.js +157 -0
  107. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  108. package/src/runtime/kit/mark-strokes.js +226 -0
  109. package/src/runtime/kit/marks.d.ts +21 -0
  110. package/src/runtime/kit/marks.js +165 -0
  111. package/src/runtime/kit/math.d.ts +23 -0
  112. package/src/runtime/kit/math.js +35 -0
  113. package/src/runtime/kit/paper.d.ts +7 -0
  114. package/src/runtime/kit/paper.js +27 -0
  115. package/src/runtime/kit/random.d.ts +8 -0
  116. package/src/runtime/kit/random.js +32 -0
  117. package/src/runtime/kit/text-fx.d.ts +18 -0
  118. package/src/runtime/kit/text-fx.js +55 -0
  119. package/src/runtime/kit/typing.d.ts +21 -0
  120. package/src/runtime/kit/typing.js +42 -0
  121. package/src/runtime/laid/layer.d.ts +44 -0
  122. package/src/runtime/laid/layer.js +370 -0
  123. package/src/runtime/layers/describe.d.ts +41 -0
  124. package/src/runtime/layers/describe.js +327 -0
  125. package/src/runtime/layers/item.d.ts +123 -0
  126. package/src/runtime/layers/item.js +150 -0
  127. package/src/runtime/layers/parts.d.ts +53 -0
  128. package/src/runtime/layers/parts.js +158 -0
  129. package/src/runtime/layers/rows.d.ts +4 -0
  130. package/src/runtime/layers/rows.js +233 -0
  131. package/src/runtime/layers/scan.d.ts +24 -0
  132. package/src/runtime/layers/scan.js +204 -0
  133. package/src/runtime/layers/walk.d.ts +16 -0
  134. package/src/runtime/layers/walk.js +337 -0
  135. package/src/runtime/load.d.ts +10 -0
  136. package/src/runtime/load.js +267 -0
  137. package/src/runtime/main.d.ts +1 -0
  138. package/src/runtime/main.js +90 -0
  139. package/src/runtime/modules.d.ts +23 -0
  140. package/src/runtime/modules.js +53 -0
  141. package/src/runtime/port.d.ts +8 -0
  142. package/src/runtime/port.js +46 -0
  143. package/src/runtime/questions.d.ts +24 -0
  144. package/src/runtime/questions.js +162 -0
  145. package/src/runtime/recut.d.ts +81 -0
  146. package/src/runtime/recut.js +325 -0
  147. package/src/runtime/render.d.ts +80 -0
  148. package/src/runtime/render.js +358 -0
  149. package/src/runtime/retouch/ask.d.ts +17 -0
  150. package/src/runtime/retouch/ask.js +171 -0
  151. package/src/runtime/retouch/entry.d.ts +77 -0
  152. package/src/runtime/retouch/entry.js +1 -0
  153. package/src/runtime/retouch/judge.d.ts +25 -0
  154. package/src/runtime/retouch/judge.js +209 -0
  155. package/src/runtime/retouch/look.d.ts +39 -0
  156. package/src/runtime/retouch/look.js +135 -0
  157. package/src/runtime/retouch/own-styles.d.ts +19 -0
  158. package/src/runtime/retouch/own-styles.js +141 -0
  159. package/src/runtime/retouch/parts.d.ts +22 -0
  160. package/src/runtime/retouch/parts.js +166 -0
  161. package/src/runtime/retouch/retouch.d.ts +37 -0
  162. package/src/runtime/retouch/retouch.js +238 -0
  163. package/src/runtime/retouch/seat.d.ts +40 -0
  164. package/src/runtime/retouch/seat.js +228 -0
  165. package/src/runtime/retouch/state.d.ts +37 -0
  166. package/src/runtime/retouch/state.js +153 -0
  167. package/src/runtime/retouch/type-style.d.ts +19 -0
  168. package/src/runtime/retouch/type-style.js +111 -0
  169. package/src/runtime/scan.d.ts +2 -0
  170. package/src/runtime/scan.js +49 -0
  171. package/src/runtime/scenes/card.d.ts +12 -0
  172. package/src/runtime/scenes/card.js +56 -0
  173. package/src/runtime/scenes/install.d.ts +69 -0
  174. package/src/runtime/scenes/install.js +528 -0
  175. package/src/runtime/scenes/read.d.ts +27 -0
  176. package/src/runtime/scenes/read.js +73 -0
  177. package/src/runtime/scenes/scoped.d.ts +13 -0
  178. package/src/runtime/scenes/scoped.js +46 -0
  179. package/src/runtime/score.d.ts +32 -0
  180. package/src/runtime/score.js +262 -0
  181. package/src/runtime/sketch/pen.d.ts +3 -0
  182. package/src/runtime/sketch/pen.js +371 -0
  183. package/src/runtime/sketch/strokes.d.ts +41 -0
  184. package/src/runtime/sketch/strokes.js +187 -0
  185. package/src/runtime/snapshot/draw.d.ts +14 -0
  186. package/src/runtime/snapshot/draw.js +125 -0
  187. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  188. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  189. package/src/runtime/snapshot/lift.d.ts +41 -0
  190. package/src/runtime/snapshot/lift.js +161 -0
  191. package/src/runtime/snapshot/plate.d.ts +20 -0
  192. package/src/runtime/snapshot/plate.js +79 -0
  193. package/src/runtime/snapshot/serialize.d.ts +51 -0
  194. package/src/runtime/snapshot/serialize.js +286 -0
  195. package/src/runtime/snapshot/styles.d.ts +8 -0
  196. package/src/runtime/snapshot/styles.js +109 -0
  197. package/src/runtime/snapshot/xml.d.ts +6 -0
  198. package/src/runtime/snapshot/xml.js +110 -0
  199. package/src/runtime/stage-size.d.ts +7 -0
  200. package/src/runtime/stage-size.js +3 -0
  201. package/src/runtime/stage.d.ts +27 -0
  202. package/src/runtime/stage.js +154 -0
  203. package/src/runtime/stretch.d.ts +13 -0
  204. package/src/runtime/stretch.js +16 -0
  205. package/src/runtime/texts.d.ts +24 -0
  206. package/src/runtime/texts.js +211 -0
  207. package/src/runtime/video-sync.d.ts +51 -0
  208. package/src/runtime/video-sync.js +218 -0
  209. package/src/stills.d.ts +90 -0
  210. package/src/stills.js +329 -0
  211. package/src/tools/film.d.ts +13 -0
  212. package/src/tools/film.js +116 -0
  213. package/src/tools/levels.d.ts +8 -0
  214. package/src/tools/levels.js +28 -0
  215. package/src/tools/main.d.ts +1 -0
  216. package/src/tools/main.js +223 -0
  217. package/src/tools/score.d.ts +4 -0
  218. package/src/tools/score.js +35 -0
  219. package/src/tools/types.d.ts +84 -0
  220. package/src/tools/types.js +2 -0
  221. package/src/tools.d.ts +6 -0
  222. package/src/tools.js +4 -0
  223. package/src/virtual.d.ts +11 -0
  224. package/src/vite.d.ts +8 -0
  225. package/src/vite.js +43 -0
@@ -0,0 +1,157 @@
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 { clipAttributes, noteClip, noteSeek, prepareClip } from "../clips.js";
9
+ import { Camera } from "./camera.js";
10
+ import { chip, show, spotlight } from "./chip.js";
11
+ import { count } from "./count.js";
12
+ import { CURSORS, Cursor, cursorElement } from "./cursor.js";
13
+ import { el, h } from "./dom.js";
14
+ import { camrect, center, phraseCamRect, phraseRect, srect } from "./geometry.js";
15
+ import { Marks } from "./marks.js";
16
+ import * as math from "./math.js";
17
+ import { cutPaper, polyLerp, polyStr } from "./paper.js";
18
+ import { rng, vnoise } from "./random.js";
19
+ import { TextFx } from "./text-fx.js";
20
+ import { esc, streamed, typist } from "./typing.js";
21
+ export const KIT_VERSION = 2;
22
+ /** Every key press a typist made, in film seconds: the score's ticks follow them. */
23
+ const typingTimes = [];
24
+ export const typingClock = () => typingTimes;
25
+ /** Inserted just under the fx canvas, which stays on top of everything the film adds. */
26
+ function beneathFx(node) {
27
+ const { stage, fxCanvas } = builtStage();
28
+ stage.insertBefore(node, fxCanvas.parentNode === stage ? fxCanvas : null);
29
+ }
30
+ export function makeKit() {
31
+ const kit = {
32
+ version: KIT_VERSION,
33
+ get W() {
34
+ return stageSize.width;
35
+ },
36
+ get H() {
37
+ return stageSize.height;
38
+ },
39
+ get stage() {
40
+ return builtStage().stage;
41
+ },
42
+ get fx() {
43
+ return builtStage().fx;
44
+ },
45
+ get fxCanvas() {
46
+ return builtStage().fxCanvas;
47
+ },
48
+ el,
49
+ h,
50
+ esc,
51
+ css(text) {
52
+ const style = el('style', { text: String(text) });
53
+ beneathFx(style);
54
+ return style;
55
+ },
56
+ module: (name) => findModule(String(name))?.text ?? null,
57
+ json(name) {
58
+ const text = kit.module(name);
59
+ if (text === null)
60
+ return null;
61
+ try {
62
+ return JSON.parse(text);
63
+ }
64
+ catch (error) {
65
+ console.error('kit.json(' + name + '): ' + (error instanceof Error ? error.message : String(error)));
66
+ return null;
67
+ }
68
+ },
69
+ exec(name) {
70
+ const module = findModule(String(name));
71
+ if (!module) {
72
+ console.error('kit.exec: no module ' + name);
73
+ return;
74
+ }
75
+ injectScript(module.text, 'module ' + name);
76
+ },
77
+ modules: moduleNames,
78
+ assets: assetStore,
79
+ cursors: CURSORS,
80
+ fonts: fontFamilies,
81
+ typing: { times: typingTimes },
82
+ ...math,
83
+ rng,
84
+ vnoise,
85
+ typist: (text, start, cps, seed) => typist(text, start, cps, seed, typingTimes),
86
+ streamed,
87
+ srect,
88
+ center,
89
+ camrect,
90
+ phraseRect,
91
+ phraseCamRect,
92
+ Camera,
93
+ Cursor,
94
+ Marks,
95
+ TextFx,
96
+ cutPaper,
97
+ polyLerp,
98
+ polyStr,
99
+ // A muted clip the stage keeps at its second from + (t − start) × speed for dur film seconds (looped if asked), playing with
100
+ // the transport; kit.seek shows an exact second instead. Its sound, when asked for, is played by the host (clips.ts).
101
+ video(source, options = {}) {
102
+ const video = el('video', {
103
+ muted: true,
104
+ playsInline: true,
105
+ preload: 'auto',
106
+ loop: !!options.loop,
107
+ attrs: clipAttributes(options),
108
+ class: options.class,
109
+ style: options.style,
110
+ });
111
+ if (!(video instanceof HTMLVideoElement))
112
+ throw new Error('kit.video: no video element');
113
+ prepareClip(video);
114
+ let src = String(source || '');
115
+ const asset = assetUrl(src);
116
+ noteClip(video, asset !== null ? src : null, options.sound, options.captions, options.lang);
117
+ if (asset !== null)
118
+ src = asset;
119
+ else if (!/[:/]/.test(src))
120
+ console.error('kit.video: no asset ' + src);
121
+ watchVideo(video);
122
+ video.src = src;
123
+ return video;
124
+ },
125
+ seek(video, second) {
126
+ if (!(video instanceof HTMLVideoElement) || !isFinite(second))
127
+ return;
128
+ videoSeeks.set(video, Number(second));
129
+ noteSeek(video);
130
+ },
131
+ count,
132
+ cursor(glyph) {
133
+ const node = cursorElement(glyph);
134
+ beneathFx(node);
135
+ return new Cursor(node);
136
+ },
137
+ svgLayer() {
138
+ const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
139
+ svg.setAttribute('width', String(stageSize.width));
140
+ svg.setAttribute('height', String(stageSize.height));
141
+ svg.setAttribute('viewBox', `0 0 ${stageSize.width} ${stageSize.height}`);
142
+ svg.style.cssText = 'position:absolute;left:0;top:0;pointer-events:none;overflow:visible';
143
+ beneathFx(svg);
144
+ return svg;
145
+ },
146
+ chip(text, options) {
147
+ const node = chip(text, options);
148
+ beneathFx(node);
149
+ return node;
150
+ },
151
+ show,
152
+ spotlight: (rect, amount, options) => spotlight(builtStage().fx, rect, amount, options),
153
+ raster,
154
+ plateStats,
155
+ };
156
+ return kit;
157
+ }
@@ -0,0 +1,65 @@
1
+ import type { Ease } from './math.js';
2
+ import type { Point } from './paper.js';
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.js';
2
+ import type { Point } from './paper.js';
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
+ }
@@ -0,0 +1,165 @@
1
+ import { clamp, eio, seg } from "./math.js";
2
+ import { BOIL_FPS, MARK_COLOR, markStrokes, strokeBox, strokeLength, strokePath } from "./mark-strokes.js";
3
+ import { vnoise } from "./random.js";
4
+ import { stageSize } from "../stage-size.js";
5
+ const SVG = 'http://www.w3.org/2000/svg';
6
+ // Kept apart from the Marks object, whose properties a film may overwrite.
7
+ const FILTERS = new WeakMap();
8
+ let filterCount = 0;
9
+ /**
10
+ * A browser filters a filter's whole region, so each stroke gets its own copy of the stage-wide `#mk-rough`,
11
+ * only as large as the stroke (half the pen and 12 px for the displacement). The noise at a point depends on
12
+ * where it sits in the layer, so a stroke looks as it would under the one big filter.
13
+ */
14
+ function strokeFilter(marks, path, points, width) {
15
+ const box = strokeBox(points);
16
+ const more = width / 2 + 12;
17
+ const own = FILTERS.get(marks);
18
+ if (!own || !(width >= 0) || !isFinite(more + box[0] + box[1] + box[2] + box[3]))
19
+ return 'url(#mk-rough)';
20
+ let filter = own.copies.get(path);
21
+ if (!filter) {
22
+ // A film may take a mark's nodes away itself, without clear().
23
+ for (const [stroke, copy] of own.copies)
24
+ if (!stroke.isConnected && !marks.svg.contains(stroke)) {
25
+ copy.remove();
26
+ own.copies.delete(stroke);
27
+ }
28
+ const copy = own.pattern.cloneNode(true);
29
+ if (!(copy instanceof Element))
30
+ return 'url(#mk-rough)';
31
+ copy.id = 'mk-rough-' + ++filterCount;
32
+ copy.setAttribute('filterUnits', 'userSpaceOnUse');
33
+ marks.svg.appendChild(copy);
34
+ own.copies.set(path, copy);
35
+ filter = copy;
36
+ }
37
+ const x = Math.floor(box[0] - more);
38
+ const y = Math.floor(box[1] - more);
39
+ const region = [x, y, Math.ceil(box[2] + more) - x, Math.ceil(box[3] + more) - y];
40
+ ['x', 'y', 'width', 'height'].forEach((name, i) => {
41
+ const value = String(region[i]);
42
+ if (filter.getAttribute(name) !== value)
43
+ filter.setAttribute(name, value);
44
+ });
45
+ return 'url(#' + filter.id + ')';
46
+ }
47
+ /** Hand-drawn marks on an SVG layer: they draw on, boil, and erase. */
48
+ export class Marks {
49
+ svg;
50
+ items = [];
51
+ nodes = new Map();
52
+ constructor(svg) {
53
+ this.svg = svg;
54
+ svg.setAttribute('fill', 'none');
55
+ svg.setAttribute('stroke-linecap', 'round');
56
+ svg.setAttribute('stroke-linejoin', 'round');
57
+ // Strokes copy this element rather than look it up by id later: svg may not be in the document yet.
58
+ let pattern = document.getElementById('mk-rough');
59
+ if (!pattern) {
60
+ const defs = document.createElementNS(SVG, 'defs');
61
+ defs.innerHTML =
62
+ '<filter id="mk-rough" filterUnits="userSpaceOnUse" x="-400" y="-400" width="2720" height="1880"><feTurbulence type="fractalNoise" baseFrequency="0.9" numOctaves="1" seed="3" result="n"/><feDisplacementMap in="SourceGraphic" in2="n" scale="2.2" xChannelSelector="R" yChannelSelector="G"/></filter>';
63
+ const filter = defs.firstElementChild;
64
+ // Marks in a camera's space draw past the edges of a small stage, so the filter never covers less than at 1920×1080.
65
+ filter?.setAttribute('width', String(Math.max(1920, stageSize.width) + 800));
66
+ filter?.setAttribute('height', String(Math.max(1080, stageSize.height) + 800));
67
+ svg.appendChild(defs);
68
+ pattern = filter;
69
+ }
70
+ if (pattern)
71
+ FILTERS.set(this, { pattern, copies: new Map() });
72
+ }
73
+ /** Geometry as numbers, or `geom: () => ({x, y, w, h})` read every frame. Getters on `mark` stay getters. */
74
+ add(mark) {
75
+ const item = { dur: 0.45, color: MARK_COLOR, width: 10, seed: 1, outDur: 0.3 };
76
+ Object.defineProperties(item, Object.getOwnPropertyDescriptors(mark));
77
+ this.items.push(item);
78
+ return this;
79
+ }
80
+ get(id) {
81
+ return this.items.find((mark) => mark.id === id);
82
+ }
83
+ clear() {
84
+ const copies = FILTERS.get(this)?.copies;
85
+ this.items = [];
86
+ for (const node of this.nodes.values())
87
+ node.remove();
88
+ if (copies)
89
+ for (const copy of copies.values())
90
+ copy.remove();
91
+ this.nodes.clear();
92
+ copies?.clear();
93
+ }
94
+ strokes(mark, boil) {
95
+ return markStrokes(mark, boil);
96
+ }
97
+ static toPath(points) {
98
+ return strokePath(points);
99
+ }
100
+ render(t) {
101
+ const boil = Math.floor(t * BOIL_FPS);
102
+ for (const mark of this.items) {
103
+ const gone = mark.out !== undefined && t >= mark.out + mark.outDur;
104
+ const on = t >= mark.t0 && !gone && !(mark.until !== undefined && t >= mark.until);
105
+ let group = this.nodes.get(mark);
106
+ if (!on) {
107
+ if (group)
108
+ group.style.display = 'none';
109
+ continue;
110
+ }
111
+ if (!group) {
112
+ group = document.createElementNS(SVG, 'g');
113
+ // Layers passes over the kit's marks: they are not parts of the film's drawing.
114
+ group.setAttribute('data-film-mark', '');
115
+ this.svg.appendChild(group);
116
+ this.nodes.set(mark, group);
117
+ }
118
+ group.style.display = '';
119
+ this.drawMark(mark, group, t, boil);
120
+ }
121
+ }
122
+ drawMark(mark, group, t, boil) {
123
+ const strokes = this.strokes(mark, mark.still ? 0 : boil);
124
+ while (group.childNodes.length < strokes.length)
125
+ group.appendChild(document.createElementNS(SVG, 'path'));
126
+ const lengths = strokes.map(strokeLength);
127
+ const total = lengths.reduce((a, b) => a + b, 0) || 1;
128
+ const drawOn = mark.ease ?? ((x) => 1 - Math.pow(1 - x, 2.4));
129
+ const drawn = drawOn(seg(t, mark.t0, mark.t0 + mark.dur)) * total;
130
+ const erased = mark.out !== undefined ? eio(seg(t, mark.out, mark.out + mark.outDur)) * total : 0;
131
+ const weight = mark.still ? 1 : 1 + 0.04 * vnoise(mark.seed + 77)(boil * 1.7);
132
+ let before = 0;
133
+ for (let i = 0; i < group.childNodes.length; i++) {
134
+ const path = group.childNodes[i];
135
+ if (!(path instanceof SVGPathElement))
136
+ continue;
137
+ const stroke = strokes[i];
138
+ if (!stroke) {
139
+ path.style.display = 'none';
140
+ continue;
141
+ }
142
+ path.style.display = '';
143
+ const length = lengths[i] ?? 0;
144
+ const from = clamp(erased - before, 0, length);
145
+ const to = clamp(drawn - before, 0, length);
146
+ before += length;
147
+ path.setAttribute('d', strokePath(stroke));
148
+ path.setAttribute('stroke', mark.color);
149
+ path.setAttribute('stroke-width', (mark.width * weight).toFixed(2));
150
+ if (mark.opacity !== undefined)
151
+ path.setAttribute('opacity', String(mark.opacity));
152
+ if (mark.rough !== false)
153
+ path.setAttribute('filter', strokeFilter(this, path, stroke, mark.width * weight));
154
+ else {
155
+ const copy = FILTERS.get(this)?.copies.get(path);
156
+ if (copy && path.getAttribute('filter') === 'url(#' + copy.id + ')')
157
+ path.removeAttribute('filter');
158
+ }
159
+ const shown = Math.max(0, to - from);
160
+ path.setAttribute('stroke-dasharray', `${shown.toFixed(1)} ${(length + 50).toFixed(1)}`);
161
+ path.setAttribute('stroke-dashoffset', (-from).toFixed(1));
162
+ path.style.opacity = String(shown < 0.5 ? 0 : mark.opacity !== undefined ? mark.opacity : 1);
163
+ }
164
+ }
165
+ }
@@ -0,0 +1,23 @@
1
+ export declare const clamp01: (x: number) => number;
2
+ export declare const clamp: (x: number, low: number, high: number) => number;
3
+ /** 0 to 1 across `a` to `b`. */
4
+ export declare const seg: (t: number, a: number, b: number) => number;
5
+ export declare const lerp: (a: number, b: number, p: number) => number;
6
+ /** Ease-out cubic. */
7
+ export declare const eo: (x: number) => number;
8
+ /** Ease-in cubic. */
9
+ export declare const ei: (x: number) => number;
10
+ export declare const eio: (x: number) => number;
11
+ /** Ease-out quart. */
12
+ export declare const eoq: (x: number) => number;
13
+ /** Ease-out expo: a whip. */
14
+ export declare const eox: (x: number) => number;
15
+ /** Back-out: overshoots, then settles. */
16
+ export declare const eob: (x: number, overshoot?: number) => number;
17
+ export declare const spring: (x: number, damp?: number, freq?: number) => number;
18
+ export declare const step: (t: number, a: number) => 0 | 1;
19
+ export declare const pulse: (t: number, a: number, b: number) => 0 | 1;
20
+ export declare const smoothstep: (a: number, b: number, x: number) => number;
21
+ /** A scale that swells and shrinks gently around 1. */
22
+ export declare const breathe: (t: number, amount?: number, period?: number) => number;
23
+ export type Ease = (x: number) => number;