@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,56 @@
1
+ /*
2
+ * A scene that is not written yet, or failed, shows a card with its name on the film's ground. The ink is white or
3
+ * black, whichever stands out more (WCAG contrast) from the ground as the card shows it, greyed or not; at these
4
+ * strengths it is at least 3 to 1 on every ground the canvas can read.
5
+ */
6
+ export const LIGHT_INK = 'rgba(255,255,255,.72)';
7
+ export const DARK_INK = 'rgba(0,0,0,.64)';
8
+ /** How the canvas spells a colour: most CSS colours become #rrggbb or rgba(). What it cannot read leaves `failing`. */
9
+ function spelled(context, ground, failing) {
10
+ const before = context.fillStyle;
11
+ context.fillStyle = failing;
12
+ context.fillStyle = ground;
13
+ const spelling = String(context.fillStyle);
14
+ context.fillStyle = before;
15
+ return spelling;
16
+ }
17
+ /** The ground's red, green and blue, if it is an opaque colour the canvas reads (not a gradient, not var()). */
18
+ export function groundRgb(context, ground) {
19
+ const spelling = spelled(context, ground, '#ffffff');
20
+ if (spelling !== spelled(context, ground, '#000000'))
21
+ return null;
22
+ const found = /^#(..)(..)(..)$/.exec(spelling);
23
+ if (!found)
24
+ return null;
25
+ return [parseInt(found[1] ?? '0', 16), parseInt(found[2] ?? '0', 16), parseInt(found[3] ?? '0', 16)];
26
+ }
27
+ export const greyOf = (rgb) => 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2];
28
+ function luminance(rgb) {
29
+ const weights = [0.2126, 0.7152, 0.0722];
30
+ return rgb.reduce((sum, channel, i) => {
31
+ const x = channel / 255;
32
+ return sum + (weights[i] ?? 0) * (x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4));
33
+ }, 0.05);
34
+ }
35
+ /** The ink for a card on `rgb`, which is the card's ground as shown (greyed already where it is). */
36
+ export function cardInk(rgb) {
37
+ const apart = (shade, alpha) => {
38
+ const mixed = [rgb[0] * (1 - alpha) + shade * alpha, rgb[1] * (1 - alpha) + shade * alpha, rgb[2] * (1 - alpha) + shade * alpha];
39
+ const ratio = luminance(mixed) / luminance(rgb);
40
+ return ratio > 1 ? ratio : 1 / ratio;
41
+ };
42
+ return apart(255, 0.72) >= apart(0, 0.64) ? LIGHT_INK : DARK_INK;
43
+ }
44
+ /** The card ink on whatever the canvas makes of the ground: an unreadable ground counts as its fallback. */
45
+ export function inkOn(context, ground, greyed) {
46
+ let rgb = groundRgb(context, ground);
47
+ if (!rgb) {
48
+ const parts = (spelled(context, ground, '#ffffff').match(/[\d.]+/g) ?? ['255', '255', '255']).map(Number);
49
+ rgb = [parts[0] ?? 255, parts[1] ?? 255, parts[2] ?? 255];
50
+ }
51
+ if (greyed) {
52
+ const grey = greyOf(rgb);
53
+ rgb = [grey, grey, grey];
54
+ }
55
+ return cardInk(rgb);
56
+ }
@@ -0,0 +1,69 @@
1
+ import type { RenderMode, SceneEntrance, SceneReport } from '../../protocol.js';
2
+ import type { Kit } from '../kit/index.js';
3
+ /** Outlines a rough scene's box as pen strokes; gives back the pen that draws them again after each render. */
4
+ export type Sketcher = (box: HTMLElement, id: string) => () => void;
5
+ export interface SceneSystem {
6
+ /** Runs a js module with `kit` in reach (`own`: strict, in a scope of its own); gives back what it threw, or null. */
7
+ run(name: string, kit: Kit, own: boolean): unknown;
8
+ reseed(seed: number): void;
9
+ /** Hears what deferred code throws while the film loads. */
10
+ late(handler: ((error: unknown) => void) | null): void;
11
+ now(): number;
12
+ /** Gives the fx canvas its own width again; done by the film it would count as a touch of the canvas. */
13
+ resetFx(): void;
14
+ /** This viewer gets narration. */
15
+ voice: boolean;
16
+ /** Told each scene's box once the scene has come up. */
17
+ box(node: HTMLElement): void;
18
+ sketch: Sketcher | null;
19
+ mode(): RenderMode;
20
+ /** Told, as a scene's module is about to run, whose code it is: the scene's id, or null for a shared module or the score. */
21
+ scene?(id: string | null): void;
22
+ }
23
+ export interface SceneApi {
24
+ scene(id: unknown, definition: unknown): void;
25
+ score(write: unknown): void;
26
+ narration(speak: unknown): void;
27
+ scenes(): SceneCut;
28
+ }
29
+ /** A moment the stage holds a picture of: the last of scene `from`, whose next scene comes on over it. */
30
+ export interface HeldNeed {
31
+ from: string;
32
+ /** Film time, just before the next scene starts. */
33
+ t: number;
34
+ }
35
+ /** What the stage does with entrances: the pictures to make, where to put them, and which entrance is on show. */
36
+ export interface SceneEntrances {
37
+ needs(): HeldNeed[];
38
+ /** The picture to make for the entrance on show at film time `t`, or null. */
39
+ needAt(t: number): HeldNeed | null;
40
+ /** Where the stage finds the picture of scene `from` that the scene after it comes on over; null: it cuts in. */
41
+ use(pick: (from: string) => HTMLImageElement | null): void;
42
+ /** The entrance on show at film time `t`, or null. */
43
+ at(t: number): SceneEntrance | null;
44
+ all(): SceneEntrance[];
45
+ }
46
+ /** What `kit.scenes()` gives the film to set as window.CUT. */
47
+ export interface SceneCut {
48
+ width: number;
49
+ height: number;
50
+ fps: number;
51
+ duration: number;
52
+ background: string;
53
+ chapters: Array<{
54
+ t: number;
55
+ label: string;
56
+ }>;
57
+ init(): Promise<void>;
58
+ render(t: number): unknown;
59
+ score?: (builder: unknown) => void;
60
+ narration?: unknown;
61
+ }
62
+ /** What installing scenes gives the load: the kit's scene calls, each scene's state, and the entrances between them. */
63
+ export interface InstalledScenes {
64
+ api: SceneApi;
65
+ states: () => SceneReport[] | null;
66
+ entrances: SceneEntrances;
67
+ }
68
+ /** A film written one scene at a time. Adds kit.scene, kit.scenes, kit.score and kit.narration. */
69
+ export declare function installScenes(kit: Kit, sys: SceneSystem): InstalledScenes;
@@ -0,0 +1,528 @@
1
+ import { DEFAULT_ENTER_SECONDS, enterLook, enterProgress, enterSeconds, isSceneEnterKind } from '@openartifactsai/motion-core';
2
+ import { pickedEntrance, playedPieces } from "../recut.js";
3
+ import { sceneSeed } from "../kit/random.js";
4
+ import { inkOn } from "./card.js";
5
+ import { readScenes, sharedParts } from "./read.js";
6
+ import { scopedKit } from "./scoped.js";
7
+ // 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.
8
+ const LOAD_MS = 15000;
9
+ const MOST_MS = 5000;
10
+ const KEPT_MS = 250;
11
+ const LATE = 'a timer, a listener or an unawaited promise threw while the film loaded: ';
12
+ const sceneFile = (id) => 'scene-' + id + '.js';
13
+ /** The picture of a scene's end is taken this long before it, so it is the scene's own last frame. */
14
+ const HELD_BEFORE_S = 1 / 240;
15
+ function words(error) {
16
+ try {
17
+ const message = error instanceof Error ? error.message : error;
18
+ return String(message || error || 'error').slice(0, 300);
19
+ }
20
+ catch {
21
+ return 'error';
22
+ }
23
+ }
24
+ const isThenable = (value) => value !== null && (typeof value === 'object' || typeof value === 'function') && typeof Reflect.get(value, 'then') === 'function';
25
+ function definitionOf(given) {
26
+ if (given === null || typeof given !== 'object')
27
+ return null;
28
+ const render = Reflect.get(given, 'render');
29
+ const init = Reflect.get(given, 'init');
30
+ if (typeof render !== 'function')
31
+ return null;
32
+ return {
33
+ render: (t, kit) => Reflect.apply(render, given, [t, kit]),
34
+ init: typeof init === 'function' ? (kit) => Reflect.apply(init, given, [kit]) : null,
35
+ rough: Reflect.get(given, 'rough') === true,
36
+ };
37
+ }
38
+ /** A film written one scene at a time. Adds kit.scene, kit.scenes, kit.score and kit.narration. */
39
+ export function installScenes(kit, sys) {
40
+ const said = new Set();
41
+ const greys = new WeakSet();
42
+ const definitions = new Map();
43
+ const kits = new Map();
44
+ const boxes = new Map();
45
+ const pens = new Map();
46
+ const cards = new Map();
47
+ let list = null;
48
+ let open = null;
49
+ let mounting = null;
50
+ let scoreWrite = null;
51
+ let speak = null;
52
+ let shown = null;
53
+ let ground = '#ffffff';
54
+ let rough = false;
55
+ let frames = 0;
56
+ let toned = null;
57
+ /** The scenes as pieces (a scene split by hand is several), each piece's entrance (null: a cut), and where each starts. */
58
+ let runs = [];
59
+ let ins = [];
60
+ let begins = [];
61
+ let held = () => null;
62
+ /** The held picture on the stage, and what a push moved: [node, its own translate, its priority]. */
63
+ let laid = null;
64
+ let pushed = [];
65
+ // Logged once per scene and kind of fault: a message may hold the time, and render runs every frame.
66
+ const tell = (id, when, error) => {
67
+ if (said.has(id + when))
68
+ return;
69
+ said.add(id + when);
70
+ console.error('scene ' + id + ': ' + words(error));
71
+ };
72
+ // A scene's randomness follows from its id and its own clock, never from the scenes round it.
73
+ const seed = (id, t) => sys.reseed(sceneSeed(id, t * 1000));
74
+ const box = () => kit.el('div', { style: 'position:absolute;inset:0;overflow:hidden;isolation:isolate' });
75
+ const sketching = () => sys.mode() === 'rough';
76
+ /*
77
+ * While a greyed box is on show, one filter on the stage greys the ground, the box, the canvas and what shared.js
78
+ * put beside them alike, and a snapshot carries it. #film-root takes the ground's grey meanwhile, where that can
79
+ * be told, so no hairline of colour shows round the stage.
80
+ */
81
+ const tone = () => {
82
+ const on = !!shown && greys.has(shown) && sketching();
83
+ if (on === (toned !== null))
84
+ return;
85
+ const root = kit.stage.parentElement;
86
+ if (on)
87
+ toned = { filter: kit.stage.style.filter, round: root?.style.background ?? '' };
88
+ kit.stage.style.filter = on ? 'grayscale(1)' : (toned?.filter ?? '');
89
+ if (root) {
90
+ const grey = greyGround();
91
+ if (grey !== null)
92
+ root.style.background = on ? `rgb(${grey}, ${grey}, ${grey})` : (toned?.round ?? '');
93
+ }
94
+ if (!on)
95
+ toned = null;
96
+ };
97
+ const greyGround = () => {
98
+ const context = kit.fx;
99
+ const before = context.fillStyle;
100
+ context.fillStyle = '#ffffff';
101
+ context.fillStyle = ground;
102
+ const light = String(context.fillStyle);
103
+ context.fillStyle = '#000000';
104
+ context.fillStyle = ground;
105
+ const dark = String(context.fillStyle);
106
+ context.fillStyle = before;
107
+ const found = light === dark ? /^#(..)(..)(..)$/.exec(light) : null;
108
+ if (!found)
109
+ return null;
110
+ const [r, g, b] = [found[1], found[2], found[3]].map((hex) => parseInt(hex ?? '0', 16));
111
+ return Math.round(0.2126 * (r ?? 0) + 0.7152 * (g ?? 0) + 0.0722 * (b ?? 0));
112
+ };
113
+ const grey = (node) => {
114
+ greys.add(node);
115
+ tone();
116
+ return node;
117
+ };
118
+ const show = (node) => {
119
+ if (shown === node) {
120
+ tone();
121
+ return;
122
+ }
123
+ shown?.remove();
124
+ // A scene may have taken the canvas into its box.
125
+ if (kit.fxCanvas.parentNode !== kit.stage)
126
+ kit.stage.appendChild(kit.fxCanvas);
127
+ kit.stage.insertBefore(node, kit.fxCanvas);
128
+ shown = node;
129
+ tone();
130
+ };
131
+ const card = (scene) => {
132
+ // Every film's stage is "rough" until its last write, so only a scene that says rough: true marks a rough cut.
133
+ const greyed = rough && sketching() && (list ?? []).some((each) => definitions.get(each.id)?.rough === true);
134
+ const key = scene.id + (greyed ? ':grey' : '');
135
+ const made = cards.get(key);
136
+ if (made)
137
+ return made;
138
+ const node = greyed ? grey(box()) : box();
139
+ node.appendChild(kit.el('div', {
140
+ text: scene.name,
141
+ 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)}`,
142
+ }));
143
+ cards.set(key, node);
144
+ return node;
145
+ };
146
+ // 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.
147
+ const hurry = (me) => {
148
+ if (me.no && !me.soon) {
149
+ const no = me.no;
150
+ const late = me.late;
151
+ me.soon = setTimeout(() => no(late?.error), KEPT_MS);
152
+ }
153
+ };
154
+ const within = (returned, ms, me) => {
155
+ if (!isThenable(returned))
156
+ return returned;
157
+ const why = 'init was cut off after ' + (ms / 1000).toFixed(2) + ' s' + (ms < MOST_MS ? ', when the film’s time to load ran out' : '');
158
+ let timer;
159
+ const cut = new Promise((_, no) => {
160
+ me.no = no;
161
+ if (me.late)
162
+ hurry(me);
163
+ timer = setTimeout(() => no(new Error(why + (document.hidden === true ? ' (this tab was in the background)' : ''))), ms);
164
+ });
165
+ return Promise.race([returned, cut]).finally(() => {
166
+ clearTimeout(timer);
167
+ if (me.soon)
168
+ clearTimeout(me.soon);
169
+ me.no = null;
170
+ });
171
+ };
172
+ // The scene's box is on the stage while it loads, so init can measure. A scene that fails leaves nothing behind.
173
+ const mount = async (scene, start, ms) => {
174
+ const me = { late: null, no: null, soon: 0, gone: false };
175
+ mounting = me;
176
+ const stage = box();
177
+ const scoped = scopedKit(kit, stage, start, scene.dur, me, sys.run);
178
+ const typed = kit.typing.times.length;
179
+ const fail = (error) => {
180
+ mounting = null;
181
+ me.gone = true;
182
+ kit.typing.times.length = typed;
183
+ tell(scene.id, 'load', error);
184
+ if (me.late && me.late.error !== error)
185
+ console.error(LATE + words(me.late.error));
186
+ };
187
+ sys.resetFx();
188
+ show(stage);
189
+ seed(scene.id, 0);
190
+ open = scene.id;
191
+ sys.scene?.(scene.id);
192
+ const thrown = sys.run(sceneFile(scene.id), scoped, true);
193
+ open = null;
194
+ sys.scene?.(null);
195
+ if (thrown)
196
+ return fail(thrown);
197
+ const definition = definitions.get(scene.id);
198
+ if (!definition)
199
+ return fail("the module must call kit.scene('" + scene.id + "', { render(t, kit) { … } })");
200
+ try {
201
+ if (definition.rough)
202
+ grey(stage);
203
+ if (definition.init)
204
+ await within(definition.init(scoped), ms, me);
205
+ // Its first frame is tried, so a render that can never draw is a scene that failed. A promise is not waited for.
206
+ seed(scene.id, 0);
207
+ const first = definition.render(0, scoped);
208
+ if (isThenable(first))
209
+ first.then(undefined, () => undefined);
210
+ if (me.late)
211
+ throw me.late.error;
212
+ if (definition.rough && sys.sketch) {
213
+ const pen = sys.sketch(stage, scene.id);
214
+ pens.set(scene.id, pen);
215
+ if (sketching())
216
+ pen();
217
+ }
218
+ boxes.set(scene.id, stage);
219
+ kits.set(scene.id, scoped);
220
+ mounting = null;
221
+ sys.box(stage);
222
+ }
223
+ catch (error) {
224
+ fail(error);
225
+ }
226
+ };
227
+ /** Both pieces are drawn by a scene that loaded and is not a sketch. */
228
+ const drawn = (piece) => boxes.has(piece.of) && definitions.get(piece.of)?.rough !== true;
229
+ /** How far into its entrance piece i is at its own time `local`, or null when it is not coming on. */
230
+ const entering = (i, local) => {
231
+ const e = ins[i];
232
+ const piece = runs[i];
233
+ const before = runs[i - 1];
234
+ if (!e || !piece || !before || !(local < e.dur) || local < 0 || !drawn(piece) || !drawn(before))
235
+ return null;
236
+ return { from: before.id, kind: e.kind, k: enterProgress(local / e.dur) };
237
+ };
238
+ const unpush = () => {
239
+ for (const [node, translate, priority] of pushed)
240
+ node.style.setProperty('translate', translate, priority);
241
+ pushed = [];
242
+ };
243
+ // The picture goes on the stage itself, above the canvas and under the captions; what a person laid on the film stays above it.
244
+ const lay = (e, node) => {
245
+ const picture = e ? held(e.from) : null;
246
+ unpush();
247
+ if (laid && laid !== picture)
248
+ laid.remove();
249
+ laid = picture;
250
+ if (!picture || !e)
251
+ return;
252
+ let caption = null;
253
+ for (let c = kit.stage.lastElementChild; c && !caption; c = c.previousElementSibling)
254
+ if (c.classList.contains('film-caption'))
255
+ caption = c;
256
+ let next = picture.nextElementSibling;
257
+ while (next && next !== caption && next.hasAttribute('data-film-placed'))
258
+ next = next.nextElementSibling;
259
+ if (picture.parentNode !== kit.stage || next !== caption)
260
+ kit.stage.insertBefore(picture, caption);
261
+ const look = enterLook(e.kind, e.k, kit.W, kit.H);
262
+ picture.style.setProperty('opacity', String(look.opacity), 'important');
263
+ picture.style.setProperty('clip-path', look.clipPath, 'important');
264
+ picture.style.setProperty('translate', look.translate, 'important');
265
+ if (look.incoming === null || !node)
266
+ return;
267
+ for (const moved of kit.fxCanvas.parentNode === kit.stage ? [node, kit.fxCanvas] : [node]) {
268
+ pushed.push([moved, moved.style.getPropertyValue('translate'), moved.style.getPropertyPriority('translate')]);
269
+ moved.style.setProperty('translate', look.incoming, 'important');
270
+ }
271
+ };
272
+ const entrance = (i) => {
273
+ const e = ins[i];
274
+ const piece = runs[i];
275
+ const before = runs[i - 1];
276
+ if (!e || !piece || !before)
277
+ return null;
278
+ return { from: before.id, to: piece.id, at: begins[i] ?? 0, kind: e.kind, dur: e.dur, held: held(before.id) !== null };
279
+ };
280
+ // The picture is keyed by the piece it shows, so two pieces of one scene never share one.
281
+ const need = (i) => {
282
+ const before = runs[i - 1];
283
+ const piece = runs[i];
284
+ return ins[i] && before && piece && boxes.has(before.of) && boxes.has(piece.of) ? { from: before.id, t: Math.max(0, (begins[i] ?? 0) - HELD_BEFORE_S) } : null;
285
+ };
286
+ const showing = (t) => {
287
+ let i = 0;
288
+ while (i + 1 < begins.length && t >= (begins[i + 1] ?? Infinity))
289
+ i++;
290
+ return i;
291
+ };
292
+ const entrances = {
293
+ needs: () => ins.flatMap((_, i) => need(i) ?? []),
294
+ needAt(t) {
295
+ const i = showing(t);
296
+ return entering(i, t - (begins[i] ?? 0)) ? need(i) : null;
297
+ },
298
+ use(pick) {
299
+ held = pick;
300
+ },
301
+ at(t) {
302
+ const i = showing(t);
303
+ return entering(i, t - (begins[i] ?? 0)) ? entrance(i) : null;
304
+ },
305
+ all: () => ins.flatMap((_, i) => entrance(i) ?? []),
306
+ };
307
+ const api = {
308
+ scene(id, given) {
309
+ if (open === null || id !== open) {
310
+ console.warn('kit.scene: ' + String(id).slice(0, 40) + ' is not this module’s own scene; ignored');
311
+ return;
312
+ }
313
+ const definition = definitionOf(given);
314
+ if (definition)
315
+ definitions.set(open, definition);
316
+ },
317
+ score(write) {
318
+ if (typeof write === 'function')
319
+ scoreWrite = (builder, times) => Reflect.apply(write, undefined, [builder, times]);
320
+ },
321
+ narration(given) {
322
+ if (typeof given === 'function')
323
+ speak = (times) => Reflect.apply(given, undefined, [times]);
324
+ },
325
+ scenes() {
326
+ return scenesCut();
327
+ },
328
+ };
329
+ const blank = (init) => ({
330
+ width: 1920,
331
+ height: 1080,
332
+ fps: 60,
333
+ duration: 1,
334
+ background: '#ffffff',
335
+ chapters: [],
336
+ init,
337
+ render: () => undefined,
338
+ });
339
+ const scenesCut = () => {
340
+ const text = kit.module('scenes.json');
341
+ // The script may reach the page ahead of the list, whose arrival loads the film again.
342
+ if (text === null) {
343
+ console.warn('kit.scenes: no modules/scenes.json yet');
344
+ return blank(async () => undefined);
345
+ }
346
+ let record;
347
+ try {
348
+ record = readScenes(text);
349
+ }
350
+ catch (error) {
351
+ // 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."
352
+ return blank(async () => {
353
+ throw error;
354
+ });
355
+ }
356
+ const scenes = record.scenes;
357
+ list = scenes;
358
+ for (const label of record.odd)
359
+ console.warn(`scenes.json: ${label} cuts in. An "enter" is one of fade, push, wipe or iris, or {"kind", "dur"} with dur in seconds (above 0, up to 1.5).`);
360
+ if (scenes[0]?.enter)
361
+ console.warn('scenes.json: scene 1 has no scene before it, so its "enter" does nothing');
362
+ // A person's pick in recut.json comes before the film's own entrance. A piece of a scene split by hand cuts in
363
+ // from the piece before it unless a person picked otherwise. A piece cuts in when the end of the piece before it,
364
+ // or its own start, is not played (hidden, or trimmed off).
365
+ const recutText = kit.module('recut.json');
366
+ runs = playedPieces(scenes, recutText);
367
+ const entranceOf = (piece) => {
368
+ const own = piece.id === piece.of ? (scenes.find((scene) => scene.id === piece.id)?.enter ?? null) : null;
369
+ const picked = pickedEntrance(piece.id, own?.kind ?? 'cut', recutText);
370
+ if (picked === null)
371
+ return own;
372
+ return isSceneEnterKind(picked) ? { kind: picked, dur: own?.dur ?? DEFAULT_ENTER_SECONDS } : null;
373
+ };
374
+ ins = runs.map((piece, i) => {
375
+ const before = runs[i - 1];
376
+ const enter = entranceOf(piece);
377
+ const dur = before && enter && before.end && piece.start ? enterSeconds(enter, before.nat, piece.nat) : 0;
378
+ return dur > 0 && enter ? { kind: enter.kind, dur } : null;
379
+ });
380
+ ground = record.background;
381
+ rough = record.stage === 'rough';
382
+ const starts = [];
383
+ const times = Object.create(null);
384
+ let end = 0;
385
+ for (const scene of scenes) {
386
+ starts.push(end);
387
+ times[scene.id] = end;
388
+ end += scene.dur;
389
+ }
390
+ times['END'] = end;
391
+ begins = runs.map((piece) => piece.a0);
392
+ const cut = {
393
+ width: record.width,
394
+ height: record.height,
395
+ fps: record.fps,
396
+ duration: end,
397
+ background: ground,
398
+ chapters: scenes.map((scene, i) => ({ t: starts[i] ?? 0, label: scene.name })),
399
+ async init() {
400
+ const have = kit.modules();
401
+ const css = kit.module('base.css');
402
+ const began = sys.now();
403
+ let unwritten = scenes.filter((scene) => have.includes(sceneFile(scene.id))).length;
404
+ // 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.
405
+ sys.late((error) => {
406
+ if (!mounting || mounting.late) {
407
+ console.error(LATE + words(error));
408
+ return;
409
+ }
410
+ mounting.late = { error: error || new Error('error') };
411
+ hurry(mounting);
412
+ });
413
+ const aside = (name) => {
414
+ const thrown = have.includes(name) ? sys.run(name, kit, true) : null;
415
+ if (thrown)
416
+ console.error(name + ': ' + words(thrown));
417
+ };
418
+ if (css !== null)
419
+ kit.css(css);
420
+ sys.scene?.(null);
421
+ aside('shared.js');
422
+ // Then each shared-<n>-<thing>.js, in number order, each with a seed of its own.
423
+ const shared = sharedParts(have);
424
+ for (const name of shared.skipped)
425
+ console.warn(name + ': not run; only modules named shared-<n>-<thing>.js run after shared.js');
426
+ for (const name of shared.run) {
427
+ seed(name, 0);
428
+ aside(name);
429
+ }
430
+ for (const [i, scene] of scenes.entries()) {
431
+ if (!have.includes(sceneFile(scene.id)))
432
+ continue;
433
+ unwritten--;
434
+ await mount(scene, starts[i] ?? 0, Math.max(KEPT_MS, Math.min(MOST_MS, LOAD_MS - (sys.now() - began) - KEPT_MS * unwritten)));
435
+ }
436
+ seed('score.js', 0);
437
+ aside('score.js');
438
+ const write = scoreWrite;
439
+ if (write)
440
+ cut.score = (builder) => {
441
+ seed('score.js', 0);
442
+ write(builder, times);
443
+ };
444
+ if (sys.voice) {
445
+ // 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.
446
+ const kept = scoreWrite;
447
+ aside('narration.js');
448
+ let lines = null;
449
+ try {
450
+ lines = speak ? speak({ ...times }) : null;
451
+ }
452
+ catch (error) {
453
+ console.error('narration.js: ' + words(error));
454
+ }
455
+ scoreWrite = kept;
456
+ if (lines)
457
+ cut.narration = lines;
458
+ }
459
+ },
460
+ render(t) {
461
+ let i = 0;
462
+ while (i + 1 < scenes.length && t >= (starts[i + 1] ?? Infinity))
463
+ i++;
464
+ const scene = scenes[i];
465
+ if (!scene)
466
+ return undefined;
467
+ const start = starts[i] ?? 0;
468
+ const frame = ++frames;
469
+ // The one canvas starts every frame blank and in its default state, so nothing one scene sets on kit.fx reaches another.
470
+ sys.resetFx();
471
+ const sceneBox = boxes.get(scene.id);
472
+ const definition = definitions.get(scene.id);
473
+ const sceneKit = kits.get(scene.id);
474
+ // What a push moved stands where the scene put it while the scene draws, so what it measures is its own.
475
+ unpush();
476
+ if (!sceneBox || !definition || !sceneKit) {
477
+ lay(null, null);
478
+ return show(card(scene));
479
+ }
480
+ show(sceneBox);
481
+ seed(scene.id, t - start);
482
+ // A fault shows the card for this frame alone: a frame follows from t, never from the frames before it.
483
+ const fail = (error) => {
484
+ tell(scene.id, 'render', error);
485
+ if (frame !== frames)
486
+ return;
487
+ sys.resetFx();
488
+ lay(null, null);
489
+ show(card(scene));
490
+ };
491
+ // The piece before, held as a picture, laid over this one as it comes on.
492
+ const at = showing(t);
493
+ const enter = () => lay(entering(at, t - (begins[at] ?? 0)), sceneBox);
494
+ try {
495
+ const drawn = definition.render(t - start, sceneKit);
496
+ const pen = sketching() ? pens.get(scene.id) : undefined;
497
+ // At once, and again when a promise settles: the browser may paint in between.
498
+ pen?.();
499
+ enter();
500
+ if (isThenable(drawn))
501
+ return Promise.resolve(drawn)
502
+ .then(pen ? () => pen() : undefined)
503
+ .then(() => {
504
+ if (frame === frames)
505
+ enter();
506
+ })
507
+ .then(undefined, fail);
508
+ return drawn;
509
+ }
510
+ catch (error) {
511
+ fail(error);
512
+ return undefined;
513
+ }
514
+ },
515
+ };
516
+ return cut;
517
+ };
518
+ const states = () => {
519
+ if (!list)
520
+ return null;
521
+ const have = kit.modules();
522
+ return list.map((scene) => ({
523
+ id: scene.id,
524
+ state: !have.includes(sceneFile(scene.id)) ? 'todo' : !boxes.has(scene.id) ? 'failed' : definitions.get(scene.id)?.rough ? 'rough' : 'final',
525
+ }));
526
+ };
527
+ return { api, states, entrances };
528
+ }
@@ -0,0 +1,27 @@
1
+ import { type SceneEnter } from '@openartifactsai/motion-core';
2
+ export interface SceneEntry {
3
+ id: string;
4
+ name: string;
5
+ dur: number;
6
+ /** How it comes on after the scene before; none: a cut. */
7
+ enter?: SceneEnter;
8
+ }
9
+ export interface ScenesRecord {
10
+ stage: 'rough' | 'final';
11
+ width: number;
12
+ height: number;
13
+ fps: 24 | 30 | 60;
14
+ background: string;
15
+ scenes: SceneEntry[];
16
+ /** The film's language tag, as written. */
17
+ lang?: string;
18
+ /** Scenes ("scene 3") whose "enter" the player cannot read: they cut in. */
19
+ odd: string[];
20
+ }
21
+ /** `modules/scenes.json` with its list, or a throw. */
22
+ export declare function readScenes(text: string): ScenesRecord;
23
+ /** shared-<n>-<thing>.js in number order (shared-2 before shared-10), and the other shared-*.js names, which never run. */
24
+ export declare function sharedParts(names: readonly string[]): {
25
+ run: string[];
26
+ skipped: string[];
27
+ };