@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,154 @@
1
+ import { RealMutationObserver, realNow } from "./determinism.js";
2
+ import { attachFontStyle, stackCss } from "./fonts.js";
3
+ import { el } from "./kit/dom.js";
4
+ import { stageSize } from "./stage-size.js";
5
+ /*
6
+ * Reading #fx back for a snapshot is slow, so its last PNG is used again until something touches the canvas: its
7
+ * width or height attribute, the loss or return of its context, or a call of any context method but these, which
8
+ * change neither the pixels nor the clip. A method a browser adds later is thus a touch.
9
+ */
10
+ const FX_HARMLESS = new Set(('constructor beginPath moveTo lineTo bezierCurveTo quadraticCurveTo arc arcTo ellipse rect roundRect closePath measureText getImageData createImageData createLinearGradient createRadialGradient ' +
11
+ 'createConicGradient createPattern getTransform getLineDash setLineDash isPointInPath isPointInStroke getContextAttributes isContextLost save translate scale rotate transform setTransform resetTransform').split(' '));
12
+ function watchFx(canvas, context) {
13
+ let touches = 0;
14
+ let clearedAt = -1;
15
+ let blankAt = -1;
16
+ let kept = null;
17
+ const proto = Object.getPrototypeOf(context);
18
+ const clearRect = context.clearRect;
19
+ const touch = () => {
20
+ touches++;
21
+ };
22
+ // On this one context, which every getContext('2d') on #fx returns: a film's own canvases keep the browser's methods.
23
+ for (const name of Object.getOwnPropertyNames(proto)) {
24
+ const method = Object.getOwnPropertyDescriptor(proto, name)?.value;
25
+ if (typeof method !== 'function' || FX_HARMLESS.has(name))
26
+ continue;
27
+ Object.defineProperty(context, name, {
28
+ configurable: true,
29
+ writable: true,
30
+ enumerable: true,
31
+ value(...args) {
32
+ touches++;
33
+ return Reflect.apply(method, this, args);
34
+ },
35
+ });
36
+ }
37
+ const sized = new RealMutationObserver(touch);
38
+ const sizedNow = () => {
39
+ if (sized.takeRecords().length)
40
+ touches++;
41
+ };
42
+ sized.observe(canvas, { attributes: true, attributeFilter: ['width', 'height'] });
43
+ canvas.addEventListener('contextlost', touch);
44
+ canvas.addEventListener('contextrestored', touch);
45
+ // The stage's own wipes are touches only where they may change something: a clip the film left on narrows a clear, and a reset drops it.
46
+ return {
47
+ canvas,
48
+ clear(width, height) {
49
+ sizedNow();
50
+ context.setTransform(1, 0, 0, 1, 0, 0);
51
+ clearRect.call(context, 0, 0, width, height);
52
+ if (touches !== clearedAt)
53
+ clearedAt = ++touches;
54
+ },
55
+ reset() {
56
+ sizedNow();
57
+ // Giving a canvas its own width again blanks it and its state.
58
+ canvas.width = canvas.width;
59
+ sized.takeRecords();
60
+ if (touches !== blankAt)
61
+ blankAt = clearedAt = ++touches;
62
+ },
63
+ png() {
64
+ sizedNow();
65
+ if (kept === null || kept.at !== touches)
66
+ kept = { at: touches, url: canvas.toDataURL('image/png') };
67
+ return kept.url;
68
+ },
69
+ end() {
70
+ sized.disconnect();
71
+ },
72
+ };
73
+ }
74
+ /** Inside .words a heading or paragraph has no margin unless the film gives it one; alone in the first layer, any rule of the film's wins. */
75
+ export const noMargin = (within) => '@layer film-reset{' + within + '.words :is(h1,h2,h3,h4,h5,h6,p){margin:0}}';
76
+ const RESET_CSS = '#stage{text-rendering:geometricPrecision;-webkit-font-smoothing:antialiased;font-kerning:normal}' +
77
+ '#stage,#stage *,#stage *::before,#stage *::after{animation:none!important;transition:none!important;caret-color:transparent!important;scroll-behavior:auto!important}' +
78
+ '#stage ::-webkit-scrollbar{display:none}';
79
+ let parts = null;
80
+ /** The frame's size at the last fit. */
81
+ let fittedTo = { width: -1, height: -1 };
82
+ export const stageParts = () => parts;
83
+ export function builtStage() {
84
+ if (!parts)
85
+ throw new Error('the stage is not built');
86
+ return parts;
87
+ }
88
+ export function buildStage(width, height, background) {
89
+ parts?.root.remove();
90
+ parts?.fxWatch.end();
91
+ stageSize.width = width;
92
+ stageSize.height = height;
93
+ const root = el('div', { attrs: { id: 'film-root' }, style: 'position:fixed;inset:0;overflow:hidden;background:' + background });
94
+ const stage = el('div', {
95
+ attrs: { id: 'stage' },
96
+ style: `position:absolute;left:0;top:0;width:${width}px;height:${height}px;overflow:hidden;transform-origin:0 0;background:${background}`,
97
+ });
98
+ const fontStyle = document.createElement('style');
99
+ fontStyle.id = 'film-fonts';
100
+ attachFontStyle(fontStyle);
101
+ const reset = el('style', { attrs: { id: 'film-reset' }, text: stackCss() + RESET_CSS + noMargin('#stage ') });
102
+ const fxCanvas = document.createElement('canvas');
103
+ fxCanvas.id = 'fx';
104
+ fxCanvas.width = width;
105
+ fxCanvas.height = height;
106
+ fxCanvas.style.cssText = `position:absolute;left:0;top:0;width:${width}px;height:${height}px;pointer-events:none;z-index:2147483647`;
107
+ const fx = fxCanvas.getContext('2d');
108
+ if (!fx)
109
+ throw new Error('this browser gives no 2d canvas');
110
+ stage.append(fontStyle, reset, fxCanvas);
111
+ root.appendChild(stage);
112
+ document.body.appendChild(root);
113
+ parts = { root, stage, fontStyle, fxCanvas, fx, fxWatch: watchFx(fxCanvas, fx) };
114
+ fit();
115
+ return parts;
116
+ }
117
+ /** Scales the stage to the frame, centred. */
118
+ export function fit() {
119
+ if (!parts)
120
+ return;
121
+ fittedTo = { width: window.innerWidth, height: window.innerHeight };
122
+ const frameWidth = fittedTo.width || stageSize.width;
123
+ const frameHeight = fittedTo.height || stageSize.height;
124
+ const scale = Math.min(frameWidth / stageSize.width, frameHeight / stageSize.height) || 1;
125
+ stageSize.fit = scale;
126
+ const style = parts.stage.style;
127
+ style.transform = 'scale(' + scale + ')';
128
+ style.left = (frameWidth - stageSize.width * scale) / 2 + 'px';
129
+ style.top = (frameHeight - stageSize.height * scale) / 2 + 'px';
130
+ }
131
+ /** A new frame has no layout until it has its size: there is nothing to measure on the stage before then. */
132
+ export function laidOut() {
133
+ const stage = parts?.stage;
134
+ return !!stage && stage.getBoundingClientRect().width >= 1;
135
+ }
136
+ const LAYOUT_POLL_MS = 50;
137
+ /** Settles once the stage has a layout, or after `ms` without one. Timers, not animation frames: a hidden frame gets none. */
138
+ export function whenLaidOut(ms) {
139
+ return new Promise((resolve) => {
140
+ const until = realNow() + ms;
141
+ const look = () => {
142
+ if (laidOut() || realNow() >= until)
143
+ resolve();
144
+ else
145
+ setTimeout(look, LAYOUT_POLL_MS);
146
+ };
147
+ look();
148
+ });
149
+ }
150
+ /** A film may have placed the stage itself: at an unchanged frame size it is left alone. */
151
+ export function fitIfResized() {
152
+ if (fittedTo.width !== window.innerWidth || fittedTo.height !== window.innerHeight)
153
+ fit();
154
+ }
@@ -0,0 +1,13 @@
1
+ import type { FilmSeconds } from '@openartifactsai/motion-core';
2
+ /**
3
+ * The stretch of the film a moment lies in, as the record of changes made by hand counts it. name: a scene's id;
4
+ * 'film' in a film not made of scenes, whatever the chapter; null: a scene that is not final, where nothing is laid.
5
+ * lo and hi are its ends in seconds from base: a scene's count from its own start, a chapter's from the film's.
6
+ */
7
+ export interface Stretch {
8
+ name: string | null;
9
+ base: number;
10
+ lo: number;
11
+ hi: number;
12
+ }
13
+ export declare function stretchOf(T: FilmSeconds): Stretch;
@@ -0,0 +1,16 @@
1
+ import { film } from "./film.js";
2
+ export function stretchOf(T) {
3
+ const parts = film.meta?.parts ?? [];
4
+ let i = 0;
5
+ while (i + 1 < parts.length && T >= (parts[i + 1]?.a0 ?? Infinity))
6
+ i++;
7
+ const part = parts[i];
8
+ if (!part || part.key === null) {
9
+ const a0 = part ? part.a0 : 0;
10
+ const nat = part ? part.nat : (film.cut?.duration ?? 0);
11
+ return { name: 'film', base: 0, lo: a0, hi: a0 + nat };
12
+ }
13
+ const key = part.key;
14
+ const scene = film.meta?.scenes?.find((each) => each.id === key);
15
+ return { name: scene && scene.state === 'final' ? key : null, base: part.a0, lo: 0, hi: part.nat };
16
+ }
@@ -0,0 +1,24 @@
1
+ import type { StageText } from '../protocol.js';
2
+ export declare function setTextsGuard(guard: () => boolean): void;
3
+ /** The words go with the next report: the one at 'watch' itself is the caller's to make. */
4
+ export declare function setWatch(on: boolean, places: boolean): void;
5
+ /** Sends the words; a frame without a layout yet has no places to tell, so they wait for it (see {@link retell}). */
6
+ export declare function tellTexts(): void;
7
+ /**
8
+ * One report when the resizing stops and the frame is laid out, not one for each resize: a report's hit tests are
9
+ * slow. Timers, not animation frames: a frame that is hidden gets none.
10
+ */
11
+ export declare function retell(): void;
12
+ /** It hides what lies under it: a picture, or an opaque background, under no faded holder. */
13
+ export declare function paintsOver(element: Element, stage: HTMLElement, memo: Map<Element, boolean>): boolean;
14
+ /**
15
+ * Nothing opaque lies over the line: tried at its middle, then near its ends, where long words may run under a
16
+ * neighbour. `drawn`: a part of an inline svg, whose thin ink the points can miss; a hit on its own svg counts.
17
+ */
18
+ export declare function seenAt(element: Element, line: DOMRect, stage: HTMLElement, memo: Map<Element, boolean>, drawn?: boolean): boolean;
19
+ /**
20
+ * Measured in the film's own pixels: on a small screen the whole picture may be a few pixels wide. places: each node
21
+ * is told the line of words it is part of, as a change by hand to its look would name it; not in a scene that is not
22
+ * final, where nothing of a person's is laid. The time this takes is not counted toward the time for hit tests.
23
+ */
24
+ export declare function textNodes(stage: HTMLElement, places: boolean): StageText[];
@@ -0,0 +1,211 @@
1
+ import { filmTime, realNow } from "./determinism.js";
2
+ import { captionBox } from "./caption-box.js";
3
+ import { srect } from "./kit/geometry.js";
4
+ import { say } from "./port.js";
5
+ import { inOwnColors } from "./snapshot/serialize.js";
6
+ import { lineFor } from "./retouch/retouch.js";
7
+ import { fitIfResized, laidOut, stageParts } from "./stage.js";
8
+ import { stretchOf } from "./stretch.js";
9
+ /*
10
+ * While the page watches, it is sent the stage's text nodes in document order: at 'watch', after each paused frame,
11
+ * after a turn that drew moments of its own, and after a resize. Each time, only while the stage shows the moment the
12
+ * page asked for, and once the frame is laid out. Hidden nodes are sent too: without them, nodes that are apart would
13
+ * sit side by side, and a line of the script could be found across the gap.
14
+ */
15
+ const MOST_NODES = 2000;
16
+ const MOST_CHARS = 60000;
17
+ const LONGEST_NODE = 301;
18
+ /** Hit testing slows as the stage fills: after this long in all, whatever is not hidden counts as seen. */
19
+ const HIT_TEST_MS = 40;
20
+ /** A wait for the resizing to stop, or for a layout, looks again after this long. */
21
+ const RETELL_MS = 100;
22
+ /** A frame that is still not laid out after this many looks has its words told as they are. */
23
+ const MOST_LOOKS = 30;
24
+ const watch = { on: false, places: false, resized: false, retelling: false };
25
+ /** Whether the stage shows the moment the page asked for; set at startup (render.ts knows). */
26
+ let showsAsked = () => true;
27
+ export function setTextsGuard(guard) {
28
+ showsAsked = guard;
29
+ }
30
+ /** The words go with the next report: the one at 'watch' itself is the caller's to make. */
31
+ export function setWatch(on, places) {
32
+ watch.on = on;
33
+ watch.places = on && places;
34
+ }
35
+ /** Sends the words; a frame without a layout yet has no places to tell, so they wait for it (see {@link retell}). */
36
+ export function tellTexts() {
37
+ const stage = stageParts()?.stage;
38
+ if (!watch.on || !stage)
39
+ return;
40
+ // Seen is a hit test, so the stage must fit the frame, which has its new size before it hears 'resize'.
41
+ fitIfResized();
42
+ if (!laidOut()) {
43
+ retell();
44
+ return;
45
+ }
46
+ sendTexts(stage);
47
+ }
48
+ function sendTexts(stage) {
49
+ const nodes = watch.places
50
+ ? inOwnColors(() => {
51
+ // The styles are worked out now, not on the time textNodes allows for hit tests.
52
+ void getComputedStyle(stage).display;
53
+ return textNodes(stage, true);
54
+ })
55
+ : textNodes(stage, false);
56
+ say({ type: 'texts', nodes, caption: captionBox(stage) });
57
+ }
58
+ /**
59
+ * One report when the resizing stops and the frame is laid out, not one for each resize: a report's hit tests are
60
+ * slow. Timers, not animation frames: a frame that is hidden gets none.
61
+ */
62
+ export function retell() {
63
+ watch.resized = true;
64
+ if (watch.retelling || !watch.on)
65
+ return;
66
+ watch.retelling = true;
67
+ let looks = 0;
68
+ const rest = () => {
69
+ looks += 1;
70
+ if (watch.resized || (!laidOut() && looks < MOST_LOOKS)) {
71
+ watch.resized = false;
72
+ setTimeout(rest, RETELL_MS);
73
+ return;
74
+ }
75
+ watch.retelling = false;
76
+ const stage = stageParts()?.stage;
77
+ if (watch.on && stage && showsAsked()) {
78
+ fitIfResized();
79
+ sendTexts(stage);
80
+ }
81
+ };
82
+ setTimeout(rest, RETELL_MS);
83
+ }
84
+ function alphaOf(colour) {
85
+ if (/^rgba\(/.test(colour))
86
+ return parseFloat(colour.split(',')[3] ?? '1');
87
+ if (colour.indexOf('/') > 0)
88
+ return parseFloat(colour.split('/')[1] ?? '1');
89
+ return 1;
90
+ }
91
+ /** It hides what lies under it: a picture, or an opaque background, under no faded holder. */
92
+ export function paintsOver(element, stage, memo) {
93
+ const known = memo.get(element);
94
+ if (known !== undefined)
95
+ return known;
96
+ const style = getComputedStyle(element);
97
+ let solid = /^(IMG|VIDEO|CANVAS)$/.test(element.tagName) || style.backgroundImage.indexOf('url(') >= 0 || alphaOf(style.backgroundColor) > 0.9;
98
+ for (let up = element; solid && up && up !== stage; up = up.parentElement)
99
+ if (parseFloat(getComputedStyle(up).opacity) < 0.9)
100
+ solid = false;
101
+ memo.set(element, solid);
102
+ return solid;
103
+ }
104
+ /** The point lies outside a box that clips the element. Any clip-path counts as clipping it, whatever its shape. */
105
+ function clippedAt(element, stage, x, y) {
106
+ for (let up = element; up && up !== stage.parentElement; up = up.parentElement) {
107
+ const style = getComputedStyle(up);
108
+ if (style.clipPath !== 'none')
109
+ return true;
110
+ if (up === element || (style.overflowX === 'visible' && style.overflowY === 'visible'))
111
+ continue;
112
+ const box = up.getBoundingClientRect();
113
+ if (x < box.left || x > box.right || y < box.top || y > box.bottom)
114
+ return true;
115
+ }
116
+ return false;
117
+ }
118
+ /**
119
+ * Nothing opaque lies over the line: tried at its middle, then near its ends, where long words may run under a
120
+ * neighbour. `drawn`: a part of an inline svg, whose thin ink the points can miss; a hit on its own svg counts.
121
+ */
122
+ export function seenAt(element, line, stage, memo, drawn = false) {
123
+ // Hit testing skips it, so it counts as seen.
124
+ if (getComputedStyle(element).pointerEvents === 'none')
125
+ return true;
126
+ const box = stage.getBoundingClientRect();
127
+ const left = Math.max(line.left, box.left);
128
+ const right = Math.min(line.right, box.right);
129
+ const y = (Math.max(line.top, box.top) + Math.min(line.bottom, box.bottom)) / 2;
130
+ if (!(right > left))
131
+ return false;
132
+ return [0.5, 0.1, 0.9].some((share) => {
133
+ const x = left + (right - left) * share;
134
+ for (const hit of document.elementsFromPoint(x, y)) {
135
+ if (hit === element || element.contains(hit))
136
+ return true;
137
+ if (hit.contains(element)) {
138
+ const svg = element instanceof SVGElement ? element.ownerSVGElement : null;
139
+ return (drawn && !!svg && svg.contains(hit)) || !clippedAt(element, stage, x, y);
140
+ }
141
+ if (paintsOver(hit, stage, memo))
142
+ return false;
143
+ }
144
+ return false;
145
+ });
146
+ }
147
+ const checksVisibility = (element) => typeof element.checkVisibility !== 'function' || element.checkVisibility({ opacityProperty: true, visibilityProperty: true, checkOpacity: true, checkVisibilityCSS: true });
148
+ /**
149
+ * Measured in the film's own pixels: on a small screen the whole picture may be a few pixels wide. places: each node
150
+ * is told the line of words it is part of, as a change by hand to its look would name it; not in a scene that is not
151
+ * final, where nothing of a person's is laid. The time this takes is not counted toward the time for hit tests.
152
+ */
153
+ export function textNodes(stage, places) {
154
+ const out = [];
155
+ let chars = 0;
156
+ let began = realNow();
157
+ let within = places ? stretchOf(filmTime()).name : null;
158
+ const lines = new Map();
159
+ const place = (node) => {
160
+ const p0 = realNow();
161
+ let at = null;
162
+ try {
163
+ at = lineFor(node, lines);
164
+ }
165
+ catch {
166
+ within = null;
167
+ }
168
+ began += realNow() - p0;
169
+ return at;
170
+ };
171
+ const memo = new Map();
172
+ const walker = document.createTreeWalker(stage, NodeFilter.SHOW_TEXT);
173
+ const range = document.createRange();
174
+ for (let node = walker.nextNode(); node && out.length < MOST_NODES && chars < MOST_CHARS; node = walker.nextNode()) {
175
+ if (!(node instanceof Text))
176
+ continue;
177
+ let holder = node.parentElement;
178
+ // A caption is skipped, the stage's or one the cut draws with that class: its words are spoken.
179
+ if (!holder || !/\S/.test(node.data) || holder.closest('style,script,.film-caption,[data-film-title],[data-film-placed]'))
180
+ continue;
181
+ // display: contents has no box of its own to hide or to hit.
182
+ while (holder && holder !== stage && getComputedStyle(holder).display === 'contents')
183
+ holder = holder.parentElement;
184
+ if (!holder)
185
+ continue;
186
+ const item = { text: node.data.slice(0, LONGEST_NODE) };
187
+ chars += item.text.length;
188
+ out.push(item);
189
+ range.selectNodeContents(node);
190
+ const bounds = range.getBoundingClientRect();
191
+ if (!(bounds.width > 0 && bounds.height > 0))
192
+ continue;
193
+ const rect = srect(range);
194
+ const line = range.getClientRects()[0];
195
+ if (rect.w < 1 || rect.h < 1 || !line)
196
+ continue;
197
+ item.x = rect.x;
198
+ item.y = rect.y;
199
+ item.w = rect.w;
200
+ item.h = rect.h;
201
+ item.line = (rect.h * line.height) / bounds.height;
202
+ if (within !== null) {
203
+ const at = place(node);
204
+ if (at !== null)
205
+ item.id = within + ':' + at;
206
+ }
207
+ const spent = realNow() - began;
208
+ item.seen = checksVisibility(holder) && (spent > HIT_TEST_MS || seenAt(holder, line, stage, memo));
209
+ }
210
+ return out;
211
+ }
@@ -0,0 +1,51 @@
1
+ import type { FilmSeconds } from '@openartifactsai/motion-core';
2
+ /** What `kit.seek(video, s)` asked a clip to show in this render, instead of film time less its start. */
3
+ export declare const videoSeeks: Map<HTMLVideoElement, number>;
4
+ /** How long after a seek its frame is waited for, at most. */
5
+ export declare const FRAME_SHOWN_WAIT_MS = 1500;
6
+ /** The event a clip gets once the frame of its last seek is shown. */
7
+ export declare const FRAME_SHOWN_EVENT = "filmframeshown";
8
+ /**
9
+ * Whether the clip's last seek, made less than `ms` ago, has not shown its frame yet. A hidden page shows no frames,
10
+ * so there it is never waited for.
11
+ */
12
+ export declare function frameUnshown(video: HTMLVideoElement, ms: number): boolean;
13
+ export declare function warnOnce(video: HTMLVideoElement, key: string, message: string): void;
14
+ /**
15
+ * The clip's own second at film time `t` (kit.video's `from`, `dur` and `speed` applied), looped or held at its end once
16
+ * its duration is known. Half a millisecond on, so a frame boundary reached through float arithmetic lands on that frame.
17
+ */
18
+ export declare function videoTime(video: HTMLVideoElement, t: FilmSeconds): {
19
+ at: number;
20
+ live: boolean;
21
+ explicit: boolean;
22
+ };
23
+ export declare function watchVideo(video: HTMLVideoElement): void;
24
+ /** How long a small frame (a still for the timeline, a check of the picture) waits for each clip's frame. */
25
+ export declare const SMALL_CLIP_WAIT_MS = 2000;
26
+ /** Whether a clip on the page has not reached the frame it was last sought to (a frame drawn now may show it stale). */
27
+ export declare function clipsBehind(): boolean;
28
+ /** A playing clip this far or more from its time is sought to it. */
29
+ export declare const RESEEK_SECONDS = 0.25;
30
+ /** Nearer than that but this far or more, it plays a little faster or slower until it catches up. */
31
+ export declare const NUDGE_FROM_SECONDS = 0.04;
32
+ /** By at most this share of its rate. */
33
+ export declare const MOST_NUDGE = 0.05;
34
+ /**
35
+ * The rate a playing clip plays at: the film's, or, while it is `drift` seconds behind (positive) or ahead of its time
36
+ * by NUDGE_FROM_SECONDS or more, faster or slower by half the drift as a share, MOST_NUDGE at most. So a clip whose
37
+ * sound narration plays stays within a frame or two of it, unheard, without a seek's stutter.
38
+ */
39
+ export declare function nudgedRate(drift: number, base: number): number;
40
+ export type VideoMode = 'play' | 'hold' | 'frame';
41
+ export interface VideoClock {
42
+ fps: number;
43
+ /** Film seconds per played second at this moment; clips play at it while the scenes are retimed. */
44
+ rate: number;
45
+ retimed: boolean;
46
+ }
47
+ /**
48
+ * play: clips play along, corrected when they drift; hold: paused on their frame; frame: an exported frame,
49
+ * with a promise that settles once every clip shows its own.
50
+ */
51
+ export declare function syncVideos(stage: HTMLElement | null, t: FilmSeconds, mode: VideoMode, clock: VideoClock, quick?: boolean): Promise<unknown> | null;