@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.
Files changed (173) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +69 -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 +155 -0
  21. package/src/protocol/guards.d.ts +6 -0
  22. package/src/protocol/guards.js +208 -0
  23. package/src/protocol/layer-guards.d.ts +7 -0
  24. package/src/protocol/layer-guards.js +97 -0
  25. package/src/protocol/layers.d.ts +123 -0
  26. package/src/protocol/layers.js +1 -0
  27. package/src/protocol/types.d.ts +261 -0
  28. package/src/protocol/types.js +7 -0
  29. package/src/protocol.d.ts +2 -0
  30. package/src/protocol.js +2 -0
  31. package/src/raster.d.ts +4 -0
  32. package/src/raster.js +2 -0
  33. package/src/runtime/assets.d.ts +19 -0
  34. package/src/runtime/assets.js +63 -0
  35. package/src/runtime/captions.d.ts +26 -0
  36. package/src/runtime/captions.js +103 -0
  37. package/src/runtime/console.d.ts +13 -0
  38. package/src/runtime/console.js +99 -0
  39. package/src/runtime/cut.d.ts +7 -0
  40. package/src/runtime/cut.js +63 -0
  41. package/src/runtime/determinism.d.ts +12 -0
  42. package/src/runtime/determinism.js +47 -0
  43. package/src/runtime/film.d.ts +40 -0
  44. package/src/runtime/film.js +29 -0
  45. package/src/runtime/focus.d.ts +8 -0
  46. package/src/runtime/focus.js +71 -0
  47. package/src/runtime/fonts.d.ts +22 -0
  48. package/src/runtime/fonts.js +109 -0
  49. package/src/runtime/frame.d.ts +9 -0
  50. package/src/runtime/frame.js +123 -0
  51. package/src/runtime/kit/camera.d.ts +40 -0
  52. package/src/runtime/kit/camera.js +56 -0
  53. package/src/runtime/kit/chip.d.ts +25 -0
  54. package/src/runtime/kit/chip.js +75 -0
  55. package/src/runtime/kit/cursor.d.ts +46 -0
  56. package/src/runtime/kit/cursor.js +108 -0
  57. package/src/runtime/kit/dom.d.ts +14 -0
  58. package/src/runtime/kit/dom.js +46 -0
  59. package/src/runtime/kit/geometry.d.ts +22 -0
  60. package/src/runtime/kit/geometry.js +46 -0
  61. package/src/runtime/kit/index.d.ts +95 -0
  62. package/src/runtime/kit/index.js +149 -0
  63. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  64. package/src/runtime/kit/mark-strokes.js +226 -0
  65. package/src/runtime/kit/marks.d.ts +21 -0
  66. package/src/runtime/kit/marks.js +165 -0
  67. package/src/runtime/kit/math.d.ts +23 -0
  68. package/src/runtime/kit/math.js +35 -0
  69. package/src/runtime/kit/paper.d.ts +7 -0
  70. package/src/runtime/kit/paper.js +27 -0
  71. package/src/runtime/kit/random.d.ts +8 -0
  72. package/src/runtime/kit/random.js +32 -0
  73. package/src/runtime/kit/text-fx.d.ts +18 -0
  74. package/src/runtime/kit/text-fx.js +55 -0
  75. package/src/runtime/kit/typing.d.ts +21 -0
  76. package/src/runtime/kit/typing.js +42 -0
  77. package/src/runtime/layers/describe.d.ts +41 -0
  78. package/src/runtime/layers/describe.js +327 -0
  79. package/src/runtime/layers/item.d.ts +121 -0
  80. package/src/runtime/layers/item.js +149 -0
  81. package/src/runtime/layers/parts.d.ts +50 -0
  82. package/src/runtime/layers/parts.js +155 -0
  83. package/src/runtime/layers/rows.d.ts +4 -0
  84. package/src/runtime/layers/rows.js +233 -0
  85. package/src/runtime/layers/scan.d.ts +24 -0
  86. package/src/runtime/layers/scan.js +202 -0
  87. package/src/runtime/layers/walk.d.ts +16 -0
  88. package/src/runtime/layers/walk.js +333 -0
  89. package/src/runtime/load.d.ts +10 -0
  90. package/src/runtime/load.js +228 -0
  91. package/src/runtime/main.d.ts +1 -0
  92. package/src/runtime/main.js +69 -0
  93. package/src/runtime/modules.d.ts +23 -0
  94. package/src/runtime/modules.js +53 -0
  95. package/src/runtime/port.d.ts +8 -0
  96. package/src/runtime/port.js +46 -0
  97. package/src/runtime/questions.d.ts +24 -0
  98. package/src/runtime/questions.js +162 -0
  99. package/src/runtime/recut.d.ts +35 -0
  100. package/src/runtime/recut.js +158 -0
  101. package/src/runtime/render.d.ts +80 -0
  102. package/src/runtime/render.js +342 -0
  103. package/src/runtime/retouch/ask.d.ts +17 -0
  104. package/src/runtime/retouch/ask.js +156 -0
  105. package/src/runtime/retouch/entry.d.ts +77 -0
  106. package/src/runtime/retouch/entry.js +1 -0
  107. package/src/runtime/retouch/judge.d.ts +25 -0
  108. package/src/runtime/retouch/judge.js +209 -0
  109. package/src/runtime/retouch/look.d.ts +39 -0
  110. package/src/runtime/retouch/look.js +135 -0
  111. package/src/runtime/retouch/parts.d.ts +22 -0
  112. package/src/runtime/retouch/parts.js +166 -0
  113. package/src/runtime/retouch/retouch.d.ts +37 -0
  114. package/src/runtime/retouch/retouch.js +238 -0
  115. package/src/runtime/retouch/seat.d.ts +40 -0
  116. package/src/runtime/retouch/seat.js +228 -0
  117. package/src/runtime/retouch/state.d.ts +37 -0
  118. package/src/runtime/retouch/state.js +153 -0
  119. package/src/runtime/retouch/type-style.d.ts +19 -0
  120. package/src/runtime/retouch/type-style.js +111 -0
  121. package/src/runtime/scan.d.ts +2 -0
  122. package/src/runtime/scan.js +49 -0
  123. package/src/runtime/scenes/card.d.ts +12 -0
  124. package/src/runtime/scenes/card.js +56 -0
  125. package/src/runtime/scenes/install.d.ts +47 -0
  126. package/src/runtime/scenes/install.js +387 -0
  127. package/src/runtime/scenes/read.d.ts +15 -0
  128. package/src/runtime/scenes/read.js +57 -0
  129. package/src/runtime/scenes/scoped.d.ts +13 -0
  130. package/src/runtime/scenes/scoped.js +40 -0
  131. package/src/runtime/score.d.ts +26 -0
  132. package/src/runtime/score.js +221 -0
  133. package/src/runtime/sketch/pen.d.ts +3 -0
  134. package/src/runtime/sketch/pen.js +371 -0
  135. package/src/runtime/sketch/strokes.d.ts +41 -0
  136. package/src/runtime/sketch/strokes.js +187 -0
  137. package/src/runtime/snapshot/draw.d.ts +14 -0
  138. package/src/runtime/snapshot/draw.js +125 -0
  139. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  140. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  141. package/src/runtime/snapshot/plate.d.ts +20 -0
  142. package/src/runtime/snapshot/plate.js +79 -0
  143. package/src/runtime/snapshot/serialize.d.ts +49 -0
  144. package/src/runtime/snapshot/serialize.js +271 -0
  145. package/src/runtime/snapshot/styles.d.ts +8 -0
  146. package/src/runtime/snapshot/styles.js +109 -0
  147. package/src/runtime/snapshot/xml.d.ts +6 -0
  148. package/src/runtime/snapshot/xml.js +110 -0
  149. package/src/runtime/stage-size.d.ts +7 -0
  150. package/src/runtime/stage-size.js +3 -0
  151. package/src/runtime/stage.d.ts +27 -0
  152. package/src/runtime/stage.js +154 -0
  153. package/src/runtime/stretch.d.ts +13 -0
  154. package/src/runtime/stretch.js +16 -0
  155. package/src/runtime/texts.d.ts +24 -0
  156. package/src/runtime/texts.js +210 -0
  157. package/src/runtime/video-sync.d.ts +26 -0
  158. package/src/runtime/video-sync.js +126 -0
  159. package/src/stills.d.ts +41 -0
  160. package/src/stills.js +253 -0
  161. package/src/tools/film.d.ts +13 -0
  162. package/src/tools/film.js +116 -0
  163. package/src/tools/main.d.ts +1 -0
  164. package/src/tools/main.js +216 -0
  165. package/src/tools/score.d.ts +4 -0
  166. package/src/tools/score.js +29 -0
  167. package/src/tools/types.d.ts +69 -0
  168. package/src/tools/types.js +2 -0
  169. package/src/tools.d.ts +5 -0
  170. package/src/tools.js +3 -0
  171. package/src/virtual.d.ts +11 -0
  172. package/src/vite.d.ts +8 -0
  173. package/src/vite.js +43 -0
@@ -0,0 +1,387 @@
1
+ import { sceneSeed } from "../kit/random.js";
2
+ import { inkOn } from "./card.js";
3
+ import { readScenes } from "./read.js";
4
+ import { scopedKit } from "./scoped.js";
5
+ // Of the 20 s the page gives a film to load, 15 are for the scenes' inits; one may take 5 at most, and never what is kept for those after it.
6
+ const LOAD_MS = 15000;
7
+ const MOST_MS = 5000;
8
+ const KEPT_MS = 250;
9
+ const LATE = 'a timer, a listener or an unawaited promise threw while the film loaded: ';
10
+ const sceneFile = (id) => 'scene-' + id + '.js';
11
+ function words(error) {
12
+ try {
13
+ const message = error instanceof Error ? error.message : error;
14
+ return String(message || error || 'error').slice(0, 300);
15
+ }
16
+ catch {
17
+ return 'error';
18
+ }
19
+ }
20
+ const isThenable = (value) => value !== null && (typeof value === 'object' || typeof value === 'function') && typeof Reflect.get(value, 'then') === 'function';
21
+ function definitionOf(given) {
22
+ if (given === null || typeof given !== 'object')
23
+ return null;
24
+ const render = Reflect.get(given, 'render');
25
+ const init = Reflect.get(given, 'init');
26
+ if (typeof render !== 'function')
27
+ return null;
28
+ return {
29
+ render: (t, kit) => Reflect.apply(render, given, [t, kit]),
30
+ init: typeof init === 'function' ? (kit) => Reflect.apply(init, given, [kit]) : null,
31
+ rough: Reflect.get(given, 'rough') === true,
32
+ };
33
+ }
34
+ /** A film written one scene at a time. Adds kit.scene, kit.scenes, kit.score and kit.narration. */
35
+ export function installScenes(kit, sys) {
36
+ const definitions = new Map();
37
+ const boxes = new Map();
38
+ const kits = new Map();
39
+ const cards = new Map();
40
+ const pens = new Map();
41
+ const said = new Set();
42
+ const greys = new WeakSet();
43
+ let list = null;
44
+ let open = null;
45
+ let mounting = null;
46
+ let scoreWrite = null;
47
+ let speak = null;
48
+ let shown = null;
49
+ let ground = '#ffffff';
50
+ let rough = false;
51
+ let frames = 0;
52
+ let toned = null;
53
+ // Logged once per scene and kind of fault: a message may hold the time, and render runs every frame.
54
+ const tell = (id, when, error) => {
55
+ if (said.has(id + when))
56
+ return;
57
+ said.add(id + when);
58
+ console.error('scene ' + id + ': ' + words(error));
59
+ };
60
+ // A scene's randomness follows from its id and its own clock, never from the scenes round it.
61
+ const seed = (id, t) => sys.reseed(sceneSeed(id, t * 1000));
62
+ const box = () => kit.el('div', { style: 'position:absolute;inset:0;overflow:hidden;isolation:isolate' });
63
+ const sketching = () => sys.mode() === 'rough';
64
+ /*
65
+ * While a greyed box is on show, one filter on the stage greys the ground, the box, the canvas and what shared.js
66
+ * put beside them alike, and a snapshot carries it. #film-root takes the ground's grey meanwhile, where that can
67
+ * be told, so no hairline of colour shows round the stage.
68
+ */
69
+ const tone = () => {
70
+ const on = !!shown && greys.has(shown) && sketching();
71
+ if (on === (toned !== null))
72
+ return;
73
+ const root = kit.stage.parentElement;
74
+ if (on)
75
+ toned = { filter: kit.stage.style.filter, round: root?.style.background ?? '' };
76
+ kit.stage.style.filter = on ? 'grayscale(1)' : (toned?.filter ?? '');
77
+ if (root) {
78
+ const grey = greyGround();
79
+ if (grey !== null)
80
+ root.style.background = on ? `rgb(${grey}, ${grey}, ${grey})` : (toned?.round ?? '');
81
+ }
82
+ if (!on)
83
+ toned = null;
84
+ };
85
+ const greyGround = () => {
86
+ const context = kit.fx;
87
+ const before = context.fillStyle;
88
+ context.fillStyle = '#ffffff';
89
+ context.fillStyle = ground;
90
+ const light = String(context.fillStyle);
91
+ context.fillStyle = '#000000';
92
+ context.fillStyle = ground;
93
+ const dark = String(context.fillStyle);
94
+ context.fillStyle = before;
95
+ const found = light === dark ? /^#(..)(..)(..)$/.exec(light) : null;
96
+ if (!found)
97
+ return null;
98
+ const [r, g, b] = [found[1], found[2], found[3]].map((hex) => parseInt(hex ?? '0', 16));
99
+ return Math.round(0.2126 * (r ?? 0) + 0.7152 * (g ?? 0) + 0.0722 * (b ?? 0));
100
+ };
101
+ const grey = (node) => {
102
+ greys.add(node);
103
+ tone();
104
+ return node;
105
+ };
106
+ const show = (node) => {
107
+ if (shown === node) {
108
+ tone();
109
+ return;
110
+ }
111
+ shown?.remove();
112
+ // A scene may have taken the canvas into its box.
113
+ if (kit.fxCanvas.parentNode !== kit.stage)
114
+ kit.stage.appendChild(kit.fxCanvas);
115
+ kit.stage.insertBefore(node, kit.fxCanvas);
116
+ shown = node;
117
+ tone();
118
+ };
119
+ const card = (scene) => {
120
+ // Every film's stage is "rough" until its last write, so only a scene that says rough: true marks a rough cut.
121
+ const greyed = rough && sketching() && (list ?? []).some((each) => definitions.get(each.id)?.rough === true);
122
+ const key = scene.id + (greyed ? ':grey' : '');
123
+ const made = cards.get(key);
124
+ if (made)
125
+ return made;
126
+ const node = greyed ? grey(box()) : box();
127
+ node.appendChild(kit.el('div', {
128
+ text: scene.name,
129
+ style: `position:absolute;inset:0;display:grid;place-content:center;text-align:center;padding:0 8%;font:500 ${Math.round(kit.H / 19)}px ${kit.fonts.sans};color:${inkOn(kit.fx, ground, greyed)}`,
130
+ }));
131
+ cards.set(key, node);
132
+ return node;
133
+ };
134
+ // After the first throw of deferred code, init is waited for only a little longer: long enough for what else the same fault throws to fall to this scene.
135
+ const hurry = (me) => {
136
+ if (me.no && !me.soon) {
137
+ const no = me.no;
138
+ const late = me.late;
139
+ me.soon = setTimeout(() => no(late?.error), KEPT_MS);
140
+ }
141
+ };
142
+ const within = (returned, ms, me) => {
143
+ if (!isThenable(returned))
144
+ return returned;
145
+ const why = 'init was cut off after ' + (ms / 1000).toFixed(2) + ' s' + (ms < MOST_MS ? ', when the film’s time to load ran out' : '');
146
+ let timer;
147
+ const cut = new Promise((_, no) => {
148
+ me.no = no;
149
+ if (me.late)
150
+ hurry(me);
151
+ timer = setTimeout(() => no(new Error(why + (document.hidden === true ? ' (this tab was in the background)' : ''))), ms);
152
+ });
153
+ return Promise.race([returned, cut]).finally(() => {
154
+ clearTimeout(timer);
155
+ if (me.soon)
156
+ clearTimeout(me.soon);
157
+ me.no = null;
158
+ });
159
+ };
160
+ // The scene's box is on the stage while it loads, so init can measure. A scene that fails leaves nothing behind.
161
+ const mount = async (scene, start, ms) => {
162
+ const me = { late: null, no: null, soon: 0, gone: false };
163
+ mounting = me;
164
+ const stage = box();
165
+ const scoped = scopedKit(kit, stage, start, scene.dur, me, sys.run);
166
+ const typed = kit.typing.times.length;
167
+ const fail = (error) => {
168
+ mounting = null;
169
+ me.gone = true;
170
+ kit.typing.times.length = typed;
171
+ tell(scene.id, 'load', error);
172
+ if (me.late && me.late.error !== error)
173
+ console.error(LATE + words(me.late.error));
174
+ };
175
+ sys.resetFx();
176
+ show(stage);
177
+ seed(scene.id, 0);
178
+ open = scene.id;
179
+ const thrown = sys.run(sceneFile(scene.id), scoped, true);
180
+ open = null;
181
+ if (thrown)
182
+ return fail(thrown);
183
+ const definition = definitions.get(scene.id);
184
+ if (!definition)
185
+ return fail("the module must call kit.scene('" + scene.id + "', { render(t, kit) { … } })");
186
+ try {
187
+ if (definition.rough)
188
+ grey(stage);
189
+ if (definition.init)
190
+ await within(definition.init(scoped), ms, me);
191
+ // Its first frame is tried, so a render that can never draw is a scene that failed. A promise is not waited for.
192
+ seed(scene.id, 0);
193
+ const first = definition.render(0, scoped);
194
+ if (isThenable(first))
195
+ first.then(undefined, () => undefined);
196
+ if (me.late)
197
+ throw me.late.error;
198
+ if (definition.rough && sys.sketch) {
199
+ const pen = sys.sketch(stage, scene.id);
200
+ pens.set(scene.id, pen);
201
+ if (sketching())
202
+ pen();
203
+ }
204
+ boxes.set(scene.id, stage);
205
+ kits.set(scene.id, scoped);
206
+ mounting = null;
207
+ sys.box(stage);
208
+ }
209
+ catch (error) {
210
+ fail(error);
211
+ }
212
+ };
213
+ const api = {
214
+ scene(id, given) {
215
+ if (open === null || id !== open) {
216
+ console.warn('kit.scene: ' + String(id).slice(0, 40) + ' is not this module’s own scene; ignored');
217
+ return;
218
+ }
219
+ const definition = definitionOf(given);
220
+ if (definition)
221
+ definitions.set(open, definition);
222
+ },
223
+ score(write) {
224
+ if (typeof write === 'function')
225
+ scoreWrite = (builder, times) => Reflect.apply(write, undefined, [builder, times]);
226
+ },
227
+ narration(given) {
228
+ if (typeof given === 'function')
229
+ speak = (times) => Reflect.apply(given, undefined, [times]);
230
+ },
231
+ scenes() {
232
+ return scenesCut();
233
+ },
234
+ };
235
+ const blank = (init) => ({
236
+ width: 1920,
237
+ height: 1080,
238
+ fps: 60,
239
+ duration: 1,
240
+ background: '#ffffff',
241
+ chapters: [],
242
+ init,
243
+ render: () => undefined,
244
+ });
245
+ const scenesCut = () => {
246
+ const text = kit.module('scenes.json');
247
+ // The script may reach the page ahead of the list, whose arrival loads the film again.
248
+ if (text === null) {
249
+ console.warn('kit.scenes: no modules/scenes.json yet');
250
+ return blank(async () => undefined);
251
+ }
252
+ let record;
253
+ try {
254
+ record = readScenes(text);
255
+ }
256
+ catch (error) {
257
+ // Thrown from init, which the stage calls itself: of what a script throws as it is read, a browser may pass on no more than "Script error."
258
+ return blank(async () => {
259
+ throw error;
260
+ });
261
+ }
262
+ const scenes = record.scenes;
263
+ list = scenes;
264
+ ground = record.background;
265
+ rough = record.stage === 'rough';
266
+ const starts = [];
267
+ const times = Object.create(null);
268
+ let end = 0;
269
+ for (const scene of scenes) {
270
+ starts.push(end);
271
+ times[scene.id] = end;
272
+ end += scene.dur;
273
+ }
274
+ times['END'] = end;
275
+ const cut = {
276
+ width: record.width,
277
+ height: record.height,
278
+ fps: record.fps,
279
+ duration: end,
280
+ background: ground,
281
+ chapters: scenes.map((scene, i) => ({ t: starts[i] ?? 0, label: scene.name })),
282
+ async init() {
283
+ const have = kit.modules();
284
+ const css = kit.module('base.css');
285
+ const began = sys.now();
286
+ let unwritten = scenes.filter((scene) => have.includes(sceneFile(scene.id))).length;
287
+ // The first thing deferred code throws is put down to the scene being brought up; the rest is logged. Either way the film comes up.
288
+ sys.late((error) => {
289
+ if (!mounting || mounting.late) {
290
+ console.error(LATE + words(error));
291
+ return;
292
+ }
293
+ mounting.late = { error: error || new Error('error') };
294
+ hurry(mounting);
295
+ });
296
+ const aside = (name) => {
297
+ const thrown = have.includes(name) ? sys.run(name, kit, true) : null;
298
+ if (thrown)
299
+ console.error(name + ': ' + words(thrown));
300
+ };
301
+ if (css !== null)
302
+ kit.css(css);
303
+ aside('shared.js');
304
+ for (const [i, scene] of scenes.entries()) {
305
+ if (!have.includes(sceneFile(scene.id)))
306
+ continue;
307
+ unwritten--;
308
+ await mount(scene, starts[i] ?? 0, Math.max(KEPT_MS, Math.min(MOST_MS, LOAD_MS - (sys.now() - began) - KEPT_MS * unwritten)));
309
+ }
310
+ seed('score.js', 0);
311
+ aside('score.js');
312
+ const write = scoreWrite;
313
+ if (write)
314
+ cut.score = (builder) => {
315
+ seed('score.js', 0);
316
+ write(builder, times);
317
+ };
318
+ if (sys.voice) {
319
+ // narration.js gets a copy of the times, and a kit.score call it makes is undone: the score is the same with the voice or without it.
320
+ const kept = scoreWrite;
321
+ aside('narration.js');
322
+ let lines = null;
323
+ try {
324
+ lines = speak ? speak({ ...times }) : null;
325
+ }
326
+ catch (error) {
327
+ console.error('narration.js: ' + words(error));
328
+ }
329
+ scoreWrite = kept;
330
+ if (lines)
331
+ cut.narration = lines;
332
+ }
333
+ },
334
+ render(t) {
335
+ let i = 0;
336
+ while (i + 1 < scenes.length && t >= (starts[i + 1] ?? Infinity))
337
+ i++;
338
+ const scene = scenes[i];
339
+ if (!scene)
340
+ return undefined;
341
+ const start = starts[i] ?? 0;
342
+ const frame = ++frames;
343
+ // The one canvas starts every frame blank and in its default state, so nothing one scene sets on kit.fx reaches another.
344
+ sys.resetFx();
345
+ const sceneBox = boxes.get(scene.id);
346
+ const definition = definitions.get(scene.id);
347
+ const sceneKit = kits.get(scene.id);
348
+ if (!sceneBox || !definition || !sceneKit)
349
+ return show(card(scene));
350
+ show(sceneBox);
351
+ seed(scene.id, t - start);
352
+ // A fault shows the card for this frame alone: a frame follows from t, never from the frames before it.
353
+ const fail = (error) => {
354
+ tell(scene.id, 'render', error);
355
+ if (frame !== frames)
356
+ return;
357
+ sys.resetFx();
358
+ show(card(scene));
359
+ };
360
+ try {
361
+ const drawn = definition.render(t - start, sceneKit);
362
+ const pen = sketching() ? pens.get(scene.id) : undefined;
363
+ // At once, and again when a promise settles: the browser may paint in between.
364
+ pen?.();
365
+ if (isThenable(drawn))
366
+ return Promise.resolve(drawn).then(pen ? () => pen() : undefined).then(undefined, fail);
367
+ return drawn;
368
+ }
369
+ catch (error) {
370
+ fail(error);
371
+ return undefined;
372
+ }
373
+ },
374
+ };
375
+ return cut;
376
+ };
377
+ const states = () => {
378
+ if (!list)
379
+ return null;
380
+ const have = kit.modules();
381
+ return list.map((scene) => ({
382
+ id: scene.id,
383
+ state: !have.includes(sceneFile(scene.id)) ? 'todo' : !boxes.has(scene.id) ? 'failed' : definitions.get(scene.id)?.rough ? 'rough' : 'final',
384
+ }));
385
+ };
386
+ return { api, states };
387
+ }
@@ -0,0 +1,15 @@
1
+ export interface SceneEntry {
2
+ id: string;
3
+ name: string;
4
+ dur: number;
5
+ }
6
+ export interface ScenesRecord {
7
+ stage: 'rough' | 'final';
8
+ width: number;
9
+ height: number;
10
+ fps: 24 | 30 | 60;
11
+ background: string;
12
+ scenes: SceneEntry[];
13
+ }
14
+ /** `modules/scenes.json` with its list, or a throw. */
15
+ export declare function readScenes(text: string): ScenesRecord;
@@ -0,0 +1,57 @@
1
+ const MOST_SCENES = 24;
2
+ const LONGEST_FILM_S = 300;
3
+ const SCENE_ID = /^[a-z][a-z0-9-]{0,31}$/;
4
+ /** One sentence and no stack, for the page to show as it is. */
5
+ function fault(why) {
6
+ const error = new Error('scenes.json: ' + why);
7
+ error.stack = '';
8
+ return error;
9
+ }
10
+ const fields = (value) => value !== null && typeof value === 'object' && !Array.isArray(value) ? value : null;
11
+ const whole = (value, fallback) => (Number.isInteger(value) && Number(value) >= 16 && Number(value) <= 4096 ? Number(value) : fallback);
12
+ /** `modules/scenes.json` with its list, or a throw. */
13
+ export function readScenes(text) {
14
+ let parsed = null;
15
+ try {
16
+ parsed = JSON.parse(text);
17
+ }
18
+ catch {
19
+ // Said below.
20
+ }
21
+ const record = fields(parsed);
22
+ if (!record || record['v'] !== 1 || (record['stage'] !== 'rough' && record['stage'] !== 'final'))
23
+ throw fault('must be JSON like {"v": 1, "stage": "rough", "scenes": […]}');
24
+ const list = record['scenes'];
25
+ if (!Array.isArray(list) || list.length < 1 || list.length > MOST_SCENES)
26
+ throw fault('"scenes" must list 1 to ' + MOST_SCENES + ' scenes');
27
+ const seen = new Set();
28
+ let total = 0;
29
+ const scenes = list.map((entry, i) => {
30
+ const label = 'scene ' + (i + 1);
31
+ const scene = fields(entry);
32
+ const id = scene?.['id'];
33
+ if (!scene || typeof id !== 'string' || !SCENE_ID.test(id))
34
+ throw fault(label + ' needs an "id" of lower-case letters, digits and "-", starting with a letter, 32 characters at most');
35
+ if (seen.has(id))
36
+ throw fault('two scenes are called ' + id);
37
+ seen.add(id);
38
+ const dur = scene['dur'];
39
+ if (typeof dur !== 'number' || !isFinite(dur) || !(dur > 0))
40
+ throw fault(label + ' needs a "dur" in seconds, above 0');
41
+ total += dur;
42
+ const name = scene['name'];
43
+ return { id, name: (typeof name === 'string' ? name.trim().slice(0, 48).trim() : '') || id, dur };
44
+ });
45
+ if (total > LONGEST_FILM_S)
46
+ throw fault('the scenes add up to ' + Math.round(total * 100) / 100 + ' s; an animation is ' + LONGEST_FILM_S + ' s at most');
47
+ const fps = record['fps'];
48
+ const background = record['background'];
49
+ return {
50
+ stage: record['stage'],
51
+ width: whole(record['width'], 1920),
52
+ height: whole(record['height'], 1080),
53
+ fps: fps === 24 || fps === 30 ? fps : 60,
54
+ background: typeof background === 'string' && background !== '' && background.length <= 64 ? background : '#ffffff',
55
+ scenes,
56
+ };
57
+ }
@@ -0,0 +1,13 @@
1
+ import type { Kit } from '../kit/index.ts';
2
+ /** A scene being brought up: what deferred code threw meanwhile, and how to cut its init short. */
3
+ export interface Mounting {
4
+ late: {
5
+ error: unknown;
6
+ } | null;
7
+ no: ((error: unknown) => void) | null;
8
+ soon: ReturnType<typeof setTimeout> | 0;
9
+ /** It failed: what its init still does is not the film's. */
10
+ gone: boolean;
11
+ }
12
+ /** The film's kit on the scene's clock and in the scene's box. */
13
+ export declare function scopedKit(kit: Kit, box: HTMLElement, start: number, dur: number, me: Mounting, run: (name: string, kit: Kit, own: boolean) => unknown): Kit;
@@ -0,0 +1,40 @@
1
+ /** The film's kit on the scene's clock and in the scene's box. */
2
+ export function scopedKit(kit, box, start, dur, me, run) {
3
+ const scoped = Object.create(kit);
4
+ const into = (node) => {
5
+ box.appendChild(node);
6
+ return node;
7
+ };
8
+ Object.defineProperties(scoped, {
9
+ stage: { value: box },
10
+ start: { value: start },
11
+ dur: { value: dur },
12
+ });
13
+ scoped.css = (text) => into(kit.el('style', { text: String(text) }));
14
+ scoped.cursor = (glyph) => {
15
+ const cursor = kit.cursor(glyph);
16
+ into(cursor.el);
17
+ return cursor;
18
+ };
19
+ scoped.svgLayer = () => into(kit.svgLayer());
20
+ scoped.chip = (text, options) => into(kit.chip(text, options));
21
+ scoped.video = (source, options = {}) => kit.video(source, { ...options, start: start + (Number(options.start) || 0) });
22
+ scoped.typist = (text, at, cps, seed) => {
23
+ const times = kit.typing.times;
24
+ const before = times.length;
25
+ const made = kit.typist(text, at, cps, seed);
26
+ // A failed scene's init may run on: what it types then is not the film's.
27
+ if (me.gone)
28
+ times.length = before;
29
+ for (let i = before; i < times.length; i++)
30
+ times[i] = (times[i] ?? 0) + start;
31
+ return made;
32
+ };
33
+ // Rethrown, so the scene's code that called exec can catch it. It runs as in any film, with no scope of its own: its functions land on window.
34
+ scoped.exec = (name) => {
35
+ const thrown = run(name, scoped, false);
36
+ if (thrown)
37
+ throw thrown;
38
+ };
39
+ return scoped;
40
+ }
@@ -0,0 +1,26 @@
1
+ import type { ScoreData, VoiceParameters } from '@open-artifact/motion-core';
2
+ /** What a score call is given: film code's own object, read field by field. */
3
+ export type ScoreOptions = Record<string, unknown>;
4
+ export interface ScoreBuilder {
5
+ tempo(bpm?: number, options?: ScoreOptions): ScoreBuilder;
6
+ beat(beat: number): number;
7
+ toBeat(time: number): number;
8
+ seed(seed: number): ScoreBuilder;
9
+ rand(): number;
10
+ program(track: string, patch?: string, options?: ScoreOptions): ScoreBuilder;
11
+ note(track: string, pitch: unknown, options?: ScoreOptions): ScoreBuilder;
12
+ chord(track: string, pitches: unknown, options?: ScoreOptions): ScoreBuilder;
13
+ pattern(track: string, pattern: string, options?: ScoreOptions): ScoreBuilder;
14
+ run(track: string, pitches: unknown, options?: ScoreOptions): ScoreBuilder;
15
+ drum(name: string, options?: ScoreOptions): ScoreBuilder;
16
+ drums(patterns: Record<string, string>, options?: ScoreOptions): ScoreBuilder;
17
+ fx(name: string, time: number, options?: ScoreOptions): ScoreBuilder;
18
+ ticks(times: ArrayLike<number>, options?: ScoreOptions): ScoreBuilder;
19
+ master(options?: ScoreOptions): ScoreBuilder;
20
+ }
21
+ export type ScoreParameters = VoiceParameters;
22
+ /**
23
+ * Runs `write(S)` and gives back plain score data on the film's own clock. A pitch that cannot be read costs its
24
+ * own note, not the score; past MOST_EVENTS refusals it throws all the same, so an endless list ends.
25
+ */
26
+ export declare function recordScore(write: (builder: ScoreBuilder) => void): ScoreData;