@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,17 @@
1
+ /** How finely the backdrops are painted while the film plays: a share of the shown size, smaller when painting is slow. */
2
+ export declare const PACE_STEPS: readonly [1, 0.75, 0.5, 0.35];
3
+ /** A live paint slower than this, on average, makes the next ones smaller. */
4
+ export declare const SLOW_MS = 45;
5
+ /** How many paints in a row are weighed before a change. */
6
+ export declare const WEIGHED = 12;
7
+ export interface PaceState {
8
+ step: number;
9
+ /** Shaders show the picture they last drew while the film plays: painting them is too slow here. */
10
+ hold: boolean;
11
+ spent: number[];
12
+ }
13
+ export declare const newPace: (soft: boolean) => PaceState;
14
+ /** The scale of a live paint: the shown size (in device pixels, in eighths, never above the film's own), times the pace's share. */
15
+ export declare function liveScale(fit: number, ratio: number, pace: PaceState): number;
16
+ /** Notes how long a paint took while playing; once enough are weighed and they are slow, paints get smaller, and at the smallest shaders hold. */
17
+ export declare function notePaint(pace: PaceState, ms: number): void;
@@ -0,0 +1,26 @@
1
+ /** How finely the backdrops are painted while the film plays: a share of the shown size, smaller when painting is slow. */
2
+ export const PACE_STEPS = [1, 0.75, 0.5, 0.35];
3
+ /** A live paint slower than this, on average, makes the next ones smaller. */
4
+ export const SLOW_MS = 45;
5
+ /** How many paints in a row are weighed before a change. */
6
+ export const WEIGHED = 12;
7
+ export const newPace = (soft) => ({ step: 0, hold: soft, spent: [] });
8
+ /** The scale of a live paint: the shown size (in device pixels, in eighths, never above the film's own), times the pace's share. */
9
+ export function liveScale(fit, ratio, pace) {
10
+ const shown = Math.min(1, Math.ceil(Math.max(0.01, fit * Math.max(1, ratio)) * 8) / 8);
11
+ return shown * (PACE_STEPS[pace.step] ?? 1);
12
+ }
13
+ /** Notes how long a paint took while playing; once enough are weighed and they are slow, paints get smaller, and at the smallest shaders hold. */
14
+ export function notePaint(pace, ms) {
15
+ pace.spent.push(ms);
16
+ if (pace.spent.length < WEIGHED)
17
+ return;
18
+ const average = pace.spent.reduce((sum, each) => sum + each, 0) / pace.spent.length;
19
+ pace.spent = [];
20
+ if (average <= SLOW_MS)
21
+ return;
22
+ if (pace.step < PACE_STEPS.length - 1)
23
+ pace.step++;
24
+ else
25
+ pace.hold = true;
26
+ }
@@ -0,0 +1,28 @@
1
+ import type { Rgb } from './color.js';
2
+ /** A 2D context a step paints on: the backdrop's own, or an offscreen one from `FX.mk`. */
3
+ export type Paintable = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
4
+ /** What a paint is told of the picture: film size, real size, real pixels per film pixel, and the film's clock. */
5
+ export interface Frame {
6
+ W: number;
7
+ H: number;
8
+ pw: number;
9
+ ph: number;
10
+ px: number;
11
+ T: number;
12
+ }
13
+ /** An offscreen 2D context of `w` by `h` real pixels. */
14
+ export declare function makeLayer(w: number, h: number): CanvasRenderingContext2D;
15
+ /** A gradient stop: a color spread evenly with the others, or `[at, color]`. */
16
+ export type Stop = string | readonly [number, string];
17
+ export declare const linear: (c: Paintable, x0: number, y0: number, x1: number, y1: number, stops: readonly unknown[]) => CanvasGradient;
18
+ export declare const radial: (c: Paintable, x: number, y: number, r0: number, r1: number, stops: readonly unknown[]) => CanvasGradient;
19
+ /** A soft round light at x, y of radius r, laid on by screen unless `blend` says otherwise. */
20
+ export declare function glowAt(c: Paintable, x: number, y: number, r: number, color: Rgb, a: number, blend: GlobalCompositeOperation): void;
21
+ /** Film grain over the whole picture at strength `a`, moving 24 times a second of the film's clock. */
22
+ export declare function grainOver(c: Paintable, f: Frame, a: number, blend: GlobalCompositeOperation): void;
23
+ /** Edges darkened toward `color`, clear in the middle. */
24
+ export declare function vignetteOver(c: Paintable, f: Frame, a: number, color: Rgb): void;
25
+ /** Light that spills from the brightest parts of what is painted so far, laid over it by screen. */
26
+ export declare function bloomOver(c: Paintable, f: Frame, a: number): void;
27
+ /** The picture filling a `w` by `h` box centered on the origin, cropped to keep its shape. */
28
+ export declare function coverBox(c: Paintable, picture: CanvasImageSource, w: number, h: number): void;
@@ -0,0 +1,150 @@
1
+ import { rgbaText } from "./color.js";
2
+ import { hash, rng } from "./numbers.js";
3
+ /** An offscreen 2D context of `w` by `h` real pixels. */
4
+ export function makeLayer(w, h) {
5
+ const canvas = document.createElement('canvas');
6
+ canvas.width = Math.max(1, Math.round(Number(w) || 1));
7
+ canvas.height = Math.max(1, Math.round(Number(h) || 1));
8
+ const context = canvas.getContext('2d');
9
+ if (!context)
10
+ throw new Error('this browser gives no 2d canvas');
11
+ return context;
12
+ }
13
+ function addStops(gradient, stops) {
14
+ const last = Math.max(1, stops.length - 1);
15
+ stops.forEach((stop, i) => {
16
+ if (Array.isArray(stop))
17
+ gradient.addColorStop(Number(stop[0]), String(stop[1]));
18
+ else
19
+ gradient.addColorStop(i / last, String(stop));
20
+ });
21
+ return gradient;
22
+ }
23
+ export const linear = (c, x0, y0, x1, y1, stops) => addStops(c.createLinearGradient(x0, y0, x1, y1), stops);
24
+ export const radial = (c, x, y, r0, r1, stops) => addStops(c.createRadialGradient(x, y, r0, x, y, r1), stops);
25
+ /** A soft round light at x, y of radius r, laid on by screen unless `blend` says otherwise. */
26
+ export function glowAt(c, x, y, r, color, a, blend) {
27
+ if (!(r > 0))
28
+ return;
29
+ c.save();
30
+ c.globalCompositeOperation = blend;
31
+ c.fillStyle = radial(c, x, y, 0, r, [
32
+ [0, rgbaText(color, a)],
33
+ [0.3, rgbaText(color, a * 0.45)],
34
+ [0.65, rgbaText(color, a * 0.1)],
35
+ [1, rgbaText(color, 0)],
36
+ ]);
37
+ c.fillRect(x - r, y - r, 2 * r, 2 * r);
38
+ c.restore();
39
+ }
40
+ const GRAIN_SIDE = 256;
41
+ let grainTile = null;
42
+ /** One tile of grey specks, the same on every computer. */
43
+ function grainTexture() {
44
+ if (grainTile)
45
+ return grainTile;
46
+ const layer = makeLayer(GRAIN_SIDE, GRAIN_SIDE);
47
+ const image = layer.createImageData(GRAIN_SIDE, GRAIN_SIDE);
48
+ const next = rng('grain');
49
+ for (let at = 0; at < image.data.length; at += 4) {
50
+ const v = Math.round(128 + (next() - next()) * 127);
51
+ image.data[at] = v;
52
+ image.data[at + 1] = v;
53
+ image.data[at + 2] = v;
54
+ image.data[at + 3] = 255;
55
+ }
56
+ layer.putImageData(image, 0, 0);
57
+ grainTile = layer.canvas;
58
+ return grainTile;
59
+ }
60
+ /** Film grain over the whole picture at strength `a`, moving 24 times a second of the film's clock. */
61
+ export function grainOver(c, f, a, blend) {
62
+ const pattern = c.createPattern(grainTexture(), 'repeat');
63
+ if (!pattern)
64
+ return;
65
+ const tick = Math.floor(f.T * 24);
66
+ const size = Math.max(1, f.px * 1.5);
67
+ c.save();
68
+ c.setTransform(size, 0, 0, size, -hash(tick) * GRAIN_SIDE * size, -hash(tick + 0.37) * GRAIN_SIDE * size);
69
+ c.globalAlpha = Math.max(0, Math.min(1, a));
70
+ c.globalCompositeOperation = blend;
71
+ c.fillStyle = pattern;
72
+ c.fillRect(0, 0, f.pw / size + GRAIN_SIDE, f.ph / size + GRAIN_SIDE);
73
+ c.restore();
74
+ }
75
+ /** Edges darkened toward `color`, clear in the middle. */
76
+ export function vignetteOver(c, f, a, color) {
77
+ const diagonal = Math.hypot(f.W, f.H);
78
+ c.save();
79
+ c.setTransform(f.px, 0, 0, f.px, 0, 0);
80
+ c.fillStyle = radial(c, f.W / 2, f.H / 2, diagonal * 0.2, diagonal * 0.55, [rgbaText(color, 0), rgbaText(color, a)]);
81
+ c.fillRect(0, 0, f.W, f.H);
82
+ c.restore();
83
+ }
84
+ const BLOOM_STEPS = [4, 8, 16, 32];
85
+ let bloomLayers = null;
86
+ /** Light that spills from the brightest parts of what is painted so far, laid over it by screen. */
87
+ export function bloomOver(c, f, a) {
88
+ if (!bloomLayers || bloomLayers.pw !== f.pw || bloomLayers.ph !== f.ph)
89
+ bloomLayers = { pw: f.pw, ph: f.ph, layers: BLOOM_STEPS.map((d) => makeLayer(Math.max(2, Math.floor(f.pw / d)), Math.max(2, Math.floor(f.ph / d)))) };
90
+ const layers = bloomLayers.layers;
91
+ const first = layers[0];
92
+ if (!first)
93
+ return;
94
+ first.globalCompositeOperation = 'copy';
95
+ first.drawImage(c.canvas, 0, 0, first.canvas.width, first.canvas.height);
96
+ first.globalCompositeOperation = 'multiply';
97
+ first.drawImage(first.canvas, 0, 0);
98
+ first.drawImage(first.canvas, 0, 0);
99
+ for (let i = 1; i < layers.length; i++) {
100
+ const to = layers[i];
101
+ const from = layers[i - 1];
102
+ if (!to || !from)
103
+ continue;
104
+ to.globalCompositeOperation = 'copy';
105
+ to.drawImage(from.canvas, 0, 0, to.canvas.width, to.canvas.height);
106
+ }
107
+ for (let i = layers.length - 2; i >= 1; i--) {
108
+ const to = layers[i];
109
+ const from = layers[i + 1];
110
+ if (!to || !from)
111
+ continue;
112
+ to.globalCompositeOperation = 'lighter';
113
+ to.globalAlpha = 0.65;
114
+ to.drawImage(from.canvas, 0, 0, to.canvas.width, to.canvas.height);
115
+ to.globalAlpha = 1;
116
+ }
117
+ const spread = layers[1];
118
+ if (!spread)
119
+ return;
120
+ c.save();
121
+ c.setTransform(1, 0, 0, 1, 0, 0);
122
+ c.globalCompositeOperation = 'screen';
123
+ c.globalAlpha = Math.max(0, Math.min(1, a));
124
+ c.imageSmoothingEnabled = true;
125
+ c.imageSmoothingQuality = 'high';
126
+ c.drawImage(spread.canvas, 0, 0, f.pw, f.ph);
127
+ c.restore();
128
+ }
129
+ /** A picture's size, whatever kind it is. */
130
+ function sizeOf(picture) {
131
+ if (picture instanceof HTMLImageElement)
132
+ return { w: picture.naturalWidth, h: picture.naturalHeight };
133
+ if (picture instanceof HTMLVideoElement)
134
+ return { w: picture.videoWidth, h: picture.videoHeight };
135
+ if (picture instanceof SVGImageElement)
136
+ return { w: picture.width.baseVal.value, h: picture.height.baseVal.value };
137
+ if ('displayWidth' in picture)
138
+ return { w: picture.displayWidth, h: picture.displayHeight };
139
+ return { w: Number(picture.width) || 0, h: Number(picture.height) || 0 };
140
+ }
141
+ /** The picture filling a `w` by `h` box centered on the origin, cropped to keep its shape. */
142
+ export function coverBox(c, picture, w, h) {
143
+ const size = sizeOf(picture);
144
+ if (!(size.w > 0 && size.h > 0 && w > 0 && h > 0))
145
+ return;
146
+ const k = Math.min(size.w / w, size.h / h);
147
+ const sw = w * k;
148
+ const sh = h * k;
149
+ c.drawImage(picture, (size.w - sw) / 2, (size.h - sh) / 2, sw, sh, -w / 2, -h / 2, w, h);
150
+ }
@@ -0,0 +1,4 @@
1
+ /** The error a film's console shows (and the agent reads) for a refused call. */
2
+ export declare const refusedMessage: (what: string) => string;
3
+ /** Puts every guard in place. Call once, before the film's code runs. */
4
+ export declare function guardFilm(): void;
@@ -0,0 +1,141 @@
1
+ /*
2
+ * A film is a picture: its code never hears what a person types, pastes or drops, never holds a selection, and
3
+ * opens no frame or window of its own. The frame's sandbox already refuses popups, forms and dialogs; what it lets
4
+ * through is refused here, before any film code runs, so the film cannot register first.
5
+ */
6
+ /** Events that carry what a person types, pastes, copies or drops, or start a selection. */
7
+ const HELD_EVENTS = [
8
+ 'keydown',
9
+ 'keyup',
10
+ 'keypress',
11
+ 'beforeinput',
12
+ 'input',
13
+ 'compositionstart',
14
+ 'compositionupdate',
15
+ 'compositionend',
16
+ 'paste',
17
+ 'copy',
18
+ 'cut',
19
+ 'drop',
20
+ 'dragstart',
21
+ 'selectstart',
22
+ ];
23
+ /** Elements that would load a document of their own inside the stage. */
24
+ const NESTED_FRAMES = 'iframe, frame, embed, object, portal, fencedframe';
25
+ /** The error a film's console shows (and the agent reads) for a refused call. */
26
+ export const refusedMessage = (what) => `${what} is not available to a film: the stage is a picture, not a page.`;
27
+ const said = new Set();
28
+ function refuse(what) {
29
+ if (said.has(what))
30
+ return;
31
+ said.add(what);
32
+ console.error(refusedMessage(what));
33
+ }
34
+ function handBack() {
35
+ try {
36
+ const active = document.activeElement;
37
+ if (active instanceof HTMLElement || active instanceof SVGElement)
38
+ active.blur();
39
+ window.parent.focus();
40
+ }
41
+ catch {
42
+ // A cross-origin focus refused: nothing else to do.
43
+ }
44
+ }
45
+ /** Holds every trusted typing, clipboard, drop and selection event before the film's listeners hear it. */
46
+ function holdInput() {
47
+ const hold = (event) => {
48
+ if (!event.isTrusted)
49
+ return;
50
+ event.stopImmediatePropagation();
51
+ if (event.cancelable)
52
+ event.preventDefault();
53
+ if (event.type === 'keydown')
54
+ handBack();
55
+ };
56
+ for (const kind of HELD_EVENTS)
57
+ window.addEventListener(kind, hold, true);
58
+ // A drag over the stage shows that nothing can be dropped; the drop itself is held above.
59
+ window.addEventListener('dragover', (event) => {
60
+ if (!event.isTrusted)
61
+ return;
62
+ event.stopImmediatePropagation();
63
+ if (event.dataTransfer)
64
+ event.dataTransfer.dropEffect = 'none';
65
+ }, true);
66
+ }
67
+ function dropFrame(element) {
68
+ refuse(`<${element.localName}>`);
69
+ element.remove();
70
+ }
71
+ /** Takes out any frame, embed or object the film adds, as soon as it is added. */
72
+ function dropNestedFrames() {
73
+ for (const element of document.querySelectorAll(NESTED_FRAMES))
74
+ dropFrame(element);
75
+ new MutationObserver((records) => {
76
+ for (const record of records) {
77
+ for (const node of record.addedNodes) {
78
+ if (!(node instanceof Element))
79
+ continue;
80
+ if (node.matches(NESTED_FRAMES))
81
+ dropFrame(node);
82
+ for (const inner of node.querySelectorAll(NESTED_FRAMES))
83
+ dropFrame(inner);
84
+ }
85
+ }
86
+ }).observe(document, { childList: true, subtree: true });
87
+ // A frame added and read in the same task is gone a moment later; until then its document is not handed out.
88
+ const hide = (prototype, key) => {
89
+ try {
90
+ Object.defineProperty(prototype, key, { configurable: false, get: () => null });
91
+ }
92
+ catch {
93
+ // Left as it is.
94
+ }
95
+ };
96
+ hide(HTMLIFrameElement.prototype, 'contentWindow');
97
+ hide(HTMLIFrameElement.prototype, 'contentDocument');
98
+ hide(HTMLObjectElement.prototype, 'contentWindow');
99
+ hide(HTMLObjectElement.prototype, 'contentDocument');
100
+ }
101
+ function replace(target, key, value) {
102
+ try {
103
+ Object.defineProperty(target, key, { configurable: false, writable: false, value });
104
+ }
105
+ catch {
106
+ // Left as it is.
107
+ }
108
+ }
109
+ /** Window and document calls that would open a window, rewrite the stage or edit it are refused, with a console error. */
110
+ function refuseCalls() {
111
+ replace(window, 'open', () => {
112
+ refuse('window.open()');
113
+ return null;
114
+ });
115
+ replace(Document.prototype, 'open', () => {
116
+ refuse('document.open()');
117
+ return null;
118
+ });
119
+ replace(Document.prototype, 'write', () => refuse('document.write()'));
120
+ replace(Document.prototype, 'writeln', () => refuse('document.writeln()'));
121
+ replace(Document.prototype, 'execCommand', () => {
122
+ refuse('document.execCommand()');
123
+ return false;
124
+ });
125
+ try {
126
+ Object.defineProperty(Document.prototype, 'designMode', {
127
+ configurable: false,
128
+ get: () => 'off',
129
+ set: () => refuse('document.designMode'),
130
+ });
131
+ }
132
+ catch {
133
+ // Left as it is.
134
+ }
135
+ }
136
+ /** Puts every guard in place. Call once, before the film's code runs. */
137
+ export function guardFilm() {
138
+ holdInput();
139
+ dropNestedFrames();
140
+ refuseCalls();
141
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The stage's hand titles: the record as last given (read from the film's modules as it loads, then as the page
3
+ * sends it), drawn after each frame, and their sizes told to the page so it can put handles on them.
4
+ */
5
+ import type { FilmSeconds } from '@openartifactsai/motion-core';
6
+ /** Draws the titles up at the film's own second `T`. */
7
+ export declare function drawTitles(T: FilmSeconds): void;
8
+ /** Takes the record's text (`null`: no titles) and draws it on the moment on show. */
9
+ export declare function setTitles(text: string | null): void;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * The stage's hand titles: the record as last given (read from the film's modules as it loads, then as the page
3
+ * sends it), drawn after each frame, and their sizes told to the page so it can put handles on them.
4
+ */
5
+ import { film } from "../film.js";
6
+ import { fontFamilies } from "../fonts.js";
7
+ import { say } from "../port.js";
8
+ import { stageParts } from "../stage.js";
9
+ import { titleLayer } from "./layer.js";
10
+ import { sceneAt } from "./place.js";
11
+ let layer = null;
12
+ /** The film's own second drawn last. */
13
+ let drawnAt = null;
14
+ /** What the page was told last, so it hears only of a change. */
15
+ let told = '';
16
+ /** A fault in drawing titles costs the titles, not the film. */
17
+ function untitled(error) {
18
+ console.warn('The titles a person wrote (modules/hand-titles.json) could not be drawn: the film is shown without them.', error);
19
+ try {
20
+ layer?.remove();
21
+ }
22
+ catch {
23
+ // Nothing is drawn.
24
+ }
25
+ layer = null;
26
+ }
27
+ const showsCard = (key) => film.meta?.scenes?.some((scene) => scene.id === key && (scene.state === 'todo' || scene.state === 'failed')) === true;
28
+ function heldUp(stage) {
29
+ return stage.querySelector(':scope > [data-film-held]') !== null;
30
+ }
31
+ /** Tells the page the size of each title on show (`null` while a scene comes on over a held picture), on a change. */
32
+ function tell(stage) {
33
+ const boxes = layer === null ? [] : heldUp(stage) ? null : layer.boxes();
34
+ const said = JSON.stringify(boxes);
35
+ if (said === told)
36
+ return;
37
+ told = said;
38
+ say({ type: 'titles', boxes });
39
+ }
40
+ /** Draws the titles up at the film's own second `T`. */
41
+ export function drawTitles(T) {
42
+ drawnAt = T;
43
+ const parts = stageParts();
44
+ const cut = film.cut;
45
+ if (layer === null || parts === null || cut === null)
46
+ return;
47
+ try {
48
+ const on = sceneAt(film.meta?.parts ?? [], T, showsCard);
49
+ layer.draw(on?.scene ?? null, on?.local ?? 0, parts.stage, cut.width, cut.height, fontFamilies.display || fontFamilies.sans);
50
+ tell(parts.stage);
51
+ }
52
+ catch (error) {
53
+ untitled(error);
54
+ }
55
+ }
56
+ /** Takes the record's text (`null`: no titles) and draws it on the moment on show. */
57
+ export function setTitles(text) {
58
+ told = '';
59
+ try {
60
+ layer?.remove();
61
+ layer = text === null ? null : titleLayer(text);
62
+ }
63
+ catch (error) {
64
+ untitled(error);
65
+ }
66
+ if (drawnAt !== null)
67
+ drawTitles(drawnAt);
68
+ else {
69
+ const stage = stageParts()?.stage;
70
+ if (stage)
71
+ tell(stage);
72
+ }
73
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Draws a person's titles (modules/hand-titles.json) over the film: a layer as large as the stage, above what the
3
+ * film draws, under the picture a scene comes on over and under the captions. What it shows depends only on the scene
4
+ * on show and the second of it, so a frame pulled for an export shows the same as the stage.
5
+ */
6
+ export declare const TITLE_LAYER_ATTRIBUTE = "data-film-title";
7
+ export interface TitleBox {
8
+ id: string;
9
+ w: number;
10
+ h: number;
11
+ }
12
+ export interface TitleLayer {
13
+ /** Shows the titles of `scene` that are up at its second `local`; none when `scene` is null. */
14
+ draw(scene: string | null, local: number, stage: HTMLElement, width: number, height: number, display: string): void;
15
+ /** The size of each title on show, in the film's pixels. */
16
+ boxes(): TitleBox[];
17
+ remove(): void;
18
+ }
19
+ /** The layer for the titles of `text` (the module's text; nothing shows for none). */
20
+ export declare function titleLayer(text: string | null): TitleLayer;
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Draws a person's titles (modules/hand-titles.json) over the film: a layer as large as the stage, above what the
3
+ * film draws, under the picture a scene comes on over and under the captions. What it shows depends only on the scene
4
+ * on show and the second of it, so a frame pulled for an export shows the same as the stage.
5
+ */
6
+ import { handTitleShows, parseHandTitles } from '@openartifactsai/motion-core';
7
+ import { fittedSize, rgbText, titleBox, titleEdge, titlePlate } from "./place.js";
8
+ export const TITLE_LAYER_ATTRIBUTE = 'data-film-title';
9
+ /** What a film's own styles could do to the layer or a title, undone. */
10
+ const KEEP_PLAIN = 'margin:0;padding:0;border:0;background:none;outline:0;box-shadow:none;transform:none;translate:none;rotate:none;scale:none;filter:none;opacity:1;' +
11
+ 'visibility:visible;clip-path:none;mask:none;mix-blend-mode:normal;text-shadow:none;text-transform:none;letter-spacing:normal;' +
12
+ 'word-spacing:normal;font-style:normal;font-variant:normal;text-decoration:none;white-space:normal;writing-mode:horizontal-tb;' +
13
+ 'animation:none;transition:none;min-width:0;min-height:0;max-height:none;float:none;display:block';
14
+ const important = (css) => css
15
+ .split(';')
16
+ .filter((rule) => rule !== '')
17
+ .map((rule) => `${rule} !important`)
18
+ .join(';');
19
+ /** The layer for the titles of `text` (the module's text; nothing shows for none). */
20
+ export function titleLayer(text) {
21
+ const titles = text === null ? [] : parseHandTitles(text);
22
+ const layer = document.createElement('div');
23
+ layer.setAttribute(TITLE_LAYER_ATTRIBUTE, '');
24
+ layer.setAttribute('aria-hidden', 'true');
25
+ const nodes = new Map();
26
+ /** The size each title's words fit at, by what decides it. */
27
+ const fitted = new Map();
28
+ let shown = [];
29
+ const nodeOf = (title) => {
30
+ let node = nodes.get(title.id);
31
+ if (node === undefined) {
32
+ node = document.createElement('div');
33
+ node.dir = 'auto';
34
+ node.textContent = title.text;
35
+ nodes.set(title.id, node);
36
+ }
37
+ return node;
38
+ };
39
+ const inkOf = (title) => (title.ink.startsWith('#') ? title.ink : `var(--${title.ink}, #ffffff)`);
40
+ /** The color a CSS value comes to on the stage, as `#rrggbb`. */
41
+ const colorOn = (probe, css) => {
42
+ probe.style.setProperty('color', css, 'important');
43
+ return rgbText(getComputedStyle(probe).color);
44
+ };
45
+ const lay = (title, node, width, height, display) => {
46
+ const edge = titleEdge(width, height);
47
+ const px = title.size * Math.min(width, height);
48
+ node.style.cssText = important(`${KEEP_PLAIN};position:absolute;left:0;top:0;width:max-content;max-width:${width - 2 * edge}px;font-family:${display};font-weight:700;` +
49
+ `line-height:1.15;text-wrap:balance;text-align:center;overflow-wrap:anywhere;font-size:${px}px`);
50
+ const ink = colorOn(node, inkOf(title)) ?? '#ffffff';
51
+ if (title.label === true) {
52
+ const ground = colorOn(node, 'var(--ground, transparent)');
53
+ node.style.setProperty('color', ink, 'important');
54
+ node.style.setProperty('background', titlePlate(ink, ground), 'important');
55
+ node.style.setProperty('padding', '0.2em 0.45em', 'important');
56
+ node.style.setProperty('border-radius', '0.2em', 'important');
57
+ }
58
+ const key = `${title.text}\n${px}\n${title.label === true}`;
59
+ let size = fitted.get(key);
60
+ if (size === undefined) {
61
+ size = fittedSize(px, (each) => {
62
+ node.style.setProperty('font-size', `${each}px`, 'important');
63
+ return node.offsetHeight > height - 2 * edge;
64
+ });
65
+ fitted.set(key, size);
66
+ }
67
+ node.style.setProperty('font-size', `${size}px`, 'important');
68
+ const place = titleBox(title.x, title.y, node.offsetWidth, node.offsetHeight, width, height);
69
+ node.style.setProperty('left', `${place.left}px`, 'important');
70
+ node.style.setProperty('top', `${place.top}px`, 'important');
71
+ };
72
+ /** Above the film, under the held picture, the captions and what a person laid in front. */
73
+ const seat = (stage) => {
74
+ let above = null;
75
+ for (let child = stage.firstElementChild; child !== null && above === null; child = child.nextElementSibling) {
76
+ if (child === layer)
77
+ continue;
78
+ if (child.hasAttribute('data-film-held') || child.classList.contains('film-caption') || child.getAttribute('data-film-placed') === 'front')
79
+ above = child;
80
+ }
81
+ if (layer.parentNode !== stage || layer.nextElementSibling !== above)
82
+ stage.insertBefore(layer, above);
83
+ };
84
+ return {
85
+ draw(scene, local, stage, width, height, display) {
86
+ const next = scene === null ? [] : titles.filter((title) => title.scene === scene && handTitleShows(title, local));
87
+ if (next.length === 0 && shown.length === 0 && layer.parentNode === null)
88
+ return;
89
+ seat(stage);
90
+ layer.style.cssText = important(`${KEEP_PLAIN};position:absolute;left:0;top:0;width:${width}px;height:${height}px;z-index:2147483647;pointer-events:none;overflow:visible`);
91
+ // A scene pushed in over the one before brings its titles with it.
92
+ const held = stage.querySelector(':scope > [data-film-held]');
93
+ const pushed = held instanceof HTMLElement ? Number.parseFloat(held.style.getPropertyValue('translate')) : Number.NaN;
94
+ if (Number.isFinite(pushed))
95
+ layer.style.setProperty('translate', `${width + pushed}px 0px`, 'important');
96
+ for (const title of shown)
97
+ if (!next.includes(title))
98
+ nodes.get(title.id)?.remove();
99
+ for (const title of next) {
100
+ const node = nodeOf(title);
101
+ if (node.parentNode !== layer)
102
+ layer.appendChild(node);
103
+ lay(title, node, width, height, display);
104
+ }
105
+ shown = next;
106
+ },
107
+ boxes() {
108
+ return shown.flatMap((title) => {
109
+ const node = nodes.get(title.id);
110
+ return node === undefined ? [] : [{ id: title.id, w: node.offsetWidth, h: node.offsetHeight }];
111
+ });
112
+ },
113
+ remove() {
114
+ layer.remove();
115
+ shown = [];
116
+ },
117
+ };
118
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The arithmetic of drawing hand titles, apart from the page: where a title sits in the frame, how small its words
3
+ * get to fit, which plate it is drawn on, and which scene and second of it the film shows. Pure.
4
+ */
5
+ export { titleBox, titleEdge } from '@openartifactsai/motion-core';
6
+ /** Each step down a title takes while its words are too tall for the frame. */
7
+ export declare const FIT_STEP = 0.92;
8
+ export declare const MOST_FIT_STEPS = 24;
9
+ /** The font size a title fits at: from `px`, a step down at a time while `tall(size)` says it is too tall. */
10
+ export declare function fittedSize(px: number, tall: (size: number) => boolean): number;
11
+ /** The plate under a labelled title: the film's ground where the ink reads on it, else black or white, whichever reads. */
12
+ export declare function titlePlate(ink: string, ground: string | null): string;
13
+ /** `rgb(…)` or `rgba(…)` as `#rrggbb`, or `null`. */
14
+ export declare function rgbText(value: string): string | null;
15
+ /** A part of the film as played: its id and where it begins on the film's own clock. */
16
+ export interface ShownPart {
17
+ key: string | null;
18
+ a0: number;
19
+ }
20
+ /**
21
+ * The scene the film shows at its own second `T` (an id, or `_<n>`) and the second of that scene, or `null` when no
22
+ * scene is drawn there (the card of a scene still to come shows in its place).
23
+ */
24
+ export declare function sceneAt(parts: readonly ShownPart[], T: number, card: (key: string) => boolean): {
25
+ scene: string;
26
+ local: number;
27
+ } | null;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * The arithmetic of drawing hand titles, apart from the page: where a title sits in the frame, how small its words
3
+ * get to fit, which plate it is drawn on, and which scene and second of it the film shows. Pure.
4
+ */
5
+ import { contrastRatio, readableInk, sceneKeyOrIndex } from '@openartifactsai/motion-core';
6
+ export { titleBox, titleEdge } from '@openartifactsai/motion-core';
7
+ /** Each step down a title takes while its words are too tall for the frame. */
8
+ export const FIT_STEP = 0.92;
9
+ export const MOST_FIT_STEPS = 24;
10
+ /** The font size a title fits at: from `px`, a step down at a time while `tall(size)` says it is too tall. */
11
+ export function fittedSize(px, tall) {
12
+ let size = px;
13
+ for (let step = 0; step < MOST_FIT_STEPS && tall(size); step++)
14
+ size *= FIT_STEP;
15
+ return size;
16
+ }
17
+ /** The plate under a labelled title: the film's ground where the ink reads on it, else black or white, whichever reads. */
18
+ export function titlePlate(ink, ground) {
19
+ if (ground !== null && contrastRatio(ink, ground) >= 4.5)
20
+ return ground;
21
+ return readableInk(ink);
22
+ }
23
+ /** `rgb(…)` or `rgba(…)` as `#rrggbb`, or `null`. */
24
+ export function rgbText(value) {
25
+ const match = /^rgba?\(\s*(\d+(?:\.\d+)?)[\s,]+(\d+(?:\.\d+)?)[\s,]+(\d+(?:\.\d+)?)/.exec(value.trim());
26
+ if (match === null)
27
+ return null;
28
+ return '#' + [match[1], match[2], match[3]].map((channel) => Math.max(0, Math.min(255, Math.round(Number(channel)))).toString(16).padStart(2, '0')).join('');
29
+ }
30
+ /**
31
+ * The scene the film shows at its own second `T` (an id, or `_<n>`) and the second of that scene, or `null` when no
32
+ * scene is drawn there (the card of a scene still to come shows in its place).
33
+ */
34
+ export function sceneAt(parts, T, card) {
35
+ let index = -1;
36
+ for (let i = 0; i < parts.length; i++)
37
+ if (parts[i].a0 <= T + 1e-9)
38
+ index = i;
39
+ if (index < 0)
40
+ return null;
41
+ const part = parts[index];
42
+ if (part.key !== null && card(part.key))
43
+ return null;
44
+ return { scene: sceneKeyOrIndex(part.key, index), local: Math.max(0, T - part.a0) };
45
+ }