@open-artifact/motion-stage 0.0.0-stage → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +69 -4
  5. package/src/build.d.ts +18 -0
  6. package/src/build.js +73 -0
  7. package/src/host.d.ts +37 -0
  8. package/src/host.js +76 -0
  9. package/src/index.d.ts +11 -0
  10. package/src/index.js +6 -0
  11. package/src/layers.d.ts +30 -0
  12. package/src/layers.js +56 -0
  13. package/src/offline/audio.d.ts +19 -0
  14. package/src/offline/audio.js +20 -0
  15. package/src/offline/data.d.ts +26 -0
  16. package/src/offline/data.js +71 -0
  17. package/src/offline/main.d.ts +9 -0
  18. package/src/offline/main.js +52 -0
  19. package/src/offline/player.d.ts +23 -0
  20. package/src/offline/player.js +155 -0
  21. package/src/protocol/guards.d.ts +6 -0
  22. package/src/protocol/guards.js +208 -0
  23. package/src/protocol/layer-guards.d.ts +7 -0
  24. package/src/protocol/layer-guards.js +97 -0
  25. package/src/protocol/layers.d.ts +123 -0
  26. package/src/protocol/layers.js +1 -0
  27. package/src/protocol/types.d.ts +261 -0
  28. package/src/protocol/types.js +7 -0
  29. package/src/protocol.d.ts +2 -0
  30. package/src/protocol.js +2 -0
  31. package/src/raster.d.ts +4 -0
  32. package/src/raster.js +2 -0
  33. package/src/runtime/assets.d.ts +19 -0
  34. package/src/runtime/assets.js +63 -0
  35. package/src/runtime/captions.d.ts +26 -0
  36. package/src/runtime/captions.js +103 -0
  37. package/src/runtime/console.d.ts +13 -0
  38. package/src/runtime/console.js +99 -0
  39. package/src/runtime/cut.d.ts +7 -0
  40. package/src/runtime/cut.js +63 -0
  41. package/src/runtime/determinism.d.ts +12 -0
  42. package/src/runtime/determinism.js +47 -0
  43. package/src/runtime/film.d.ts +40 -0
  44. package/src/runtime/film.js +29 -0
  45. package/src/runtime/focus.d.ts +8 -0
  46. package/src/runtime/focus.js +71 -0
  47. package/src/runtime/fonts.d.ts +22 -0
  48. package/src/runtime/fonts.js +109 -0
  49. package/src/runtime/frame.d.ts +9 -0
  50. package/src/runtime/frame.js +123 -0
  51. package/src/runtime/kit/camera.d.ts +40 -0
  52. package/src/runtime/kit/camera.js +56 -0
  53. package/src/runtime/kit/chip.d.ts +25 -0
  54. package/src/runtime/kit/chip.js +75 -0
  55. package/src/runtime/kit/cursor.d.ts +46 -0
  56. package/src/runtime/kit/cursor.js +108 -0
  57. package/src/runtime/kit/dom.d.ts +14 -0
  58. package/src/runtime/kit/dom.js +46 -0
  59. package/src/runtime/kit/geometry.d.ts +22 -0
  60. package/src/runtime/kit/geometry.js +46 -0
  61. package/src/runtime/kit/index.d.ts +95 -0
  62. package/src/runtime/kit/index.js +149 -0
  63. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  64. package/src/runtime/kit/mark-strokes.js +226 -0
  65. package/src/runtime/kit/marks.d.ts +21 -0
  66. package/src/runtime/kit/marks.js +165 -0
  67. package/src/runtime/kit/math.d.ts +23 -0
  68. package/src/runtime/kit/math.js +35 -0
  69. package/src/runtime/kit/paper.d.ts +7 -0
  70. package/src/runtime/kit/paper.js +27 -0
  71. package/src/runtime/kit/random.d.ts +8 -0
  72. package/src/runtime/kit/random.js +32 -0
  73. package/src/runtime/kit/text-fx.d.ts +18 -0
  74. package/src/runtime/kit/text-fx.js +55 -0
  75. package/src/runtime/kit/typing.d.ts +21 -0
  76. package/src/runtime/kit/typing.js +42 -0
  77. package/src/runtime/layers/describe.d.ts +41 -0
  78. package/src/runtime/layers/describe.js +327 -0
  79. package/src/runtime/layers/item.d.ts +121 -0
  80. package/src/runtime/layers/item.js +149 -0
  81. package/src/runtime/layers/parts.d.ts +50 -0
  82. package/src/runtime/layers/parts.js +155 -0
  83. package/src/runtime/layers/rows.d.ts +4 -0
  84. package/src/runtime/layers/rows.js +233 -0
  85. package/src/runtime/layers/scan.d.ts +24 -0
  86. package/src/runtime/layers/scan.js +202 -0
  87. package/src/runtime/layers/walk.d.ts +16 -0
  88. package/src/runtime/layers/walk.js +333 -0
  89. package/src/runtime/load.d.ts +10 -0
  90. package/src/runtime/load.js +228 -0
  91. package/src/runtime/main.d.ts +1 -0
  92. package/src/runtime/main.js +69 -0
  93. package/src/runtime/modules.d.ts +23 -0
  94. package/src/runtime/modules.js +53 -0
  95. package/src/runtime/port.d.ts +8 -0
  96. package/src/runtime/port.js +46 -0
  97. package/src/runtime/questions.d.ts +24 -0
  98. package/src/runtime/questions.js +162 -0
  99. package/src/runtime/recut.d.ts +35 -0
  100. package/src/runtime/recut.js +158 -0
  101. package/src/runtime/render.d.ts +80 -0
  102. package/src/runtime/render.js +342 -0
  103. package/src/runtime/retouch/ask.d.ts +17 -0
  104. package/src/runtime/retouch/ask.js +156 -0
  105. package/src/runtime/retouch/entry.d.ts +77 -0
  106. package/src/runtime/retouch/entry.js +1 -0
  107. package/src/runtime/retouch/judge.d.ts +25 -0
  108. package/src/runtime/retouch/judge.js +209 -0
  109. package/src/runtime/retouch/look.d.ts +39 -0
  110. package/src/runtime/retouch/look.js +135 -0
  111. package/src/runtime/retouch/parts.d.ts +22 -0
  112. package/src/runtime/retouch/parts.js +166 -0
  113. package/src/runtime/retouch/retouch.d.ts +37 -0
  114. package/src/runtime/retouch/retouch.js +238 -0
  115. package/src/runtime/retouch/seat.d.ts +40 -0
  116. package/src/runtime/retouch/seat.js +228 -0
  117. package/src/runtime/retouch/state.d.ts +37 -0
  118. package/src/runtime/retouch/state.js +153 -0
  119. package/src/runtime/retouch/type-style.d.ts +19 -0
  120. package/src/runtime/retouch/type-style.js +111 -0
  121. package/src/runtime/scan.d.ts +2 -0
  122. package/src/runtime/scan.js +49 -0
  123. package/src/runtime/scenes/card.d.ts +12 -0
  124. package/src/runtime/scenes/card.js +56 -0
  125. package/src/runtime/scenes/install.d.ts +47 -0
  126. package/src/runtime/scenes/install.js +387 -0
  127. package/src/runtime/scenes/read.d.ts +15 -0
  128. package/src/runtime/scenes/read.js +57 -0
  129. package/src/runtime/scenes/scoped.d.ts +13 -0
  130. package/src/runtime/scenes/scoped.js +40 -0
  131. package/src/runtime/score.d.ts +26 -0
  132. package/src/runtime/score.js +221 -0
  133. package/src/runtime/sketch/pen.d.ts +3 -0
  134. package/src/runtime/sketch/pen.js +371 -0
  135. package/src/runtime/sketch/strokes.d.ts +41 -0
  136. package/src/runtime/sketch/strokes.js +187 -0
  137. package/src/runtime/snapshot/draw.d.ts +14 -0
  138. package/src/runtime/snapshot/draw.js +125 -0
  139. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  140. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  141. package/src/runtime/snapshot/plate.d.ts +20 -0
  142. package/src/runtime/snapshot/plate.js +79 -0
  143. package/src/runtime/snapshot/serialize.d.ts +49 -0
  144. package/src/runtime/snapshot/serialize.js +271 -0
  145. package/src/runtime/snapshot/styles.d.ts +8 -0
  146. package/src/runtime/snapshot/styles.js +109 -0
  147. package/src/runtime/snapshot/xml.d.ts +6 -0
  148. package/src/runtime/snapshot/xml.js +110 -0
  149. package/src/runtime/stage-size.d.ts +7 -0
  150. package/src/runtime/stage-size.js +3 -0
  151. package/src/runtime/stage.d.ts +27 -0
  152. package/src/runtime/stage.js +154 -0
  153. package/src/runtime/stretch.d.ts +13 -0
  154. package/src/runtime/stretch.js +16 -0
  155. package/src/runtime/texts.d.ts +24 -0
  156. package/src/runtime/texts.js +210 -0
  157. package/src/runtime/video-sync.d.ts +26 -0
  158. package/src/runtime/video-sync.js +126 -0
  159. package/src/stills.d.ts +41 -0
  160. package/src/stills.js +253 -0
  161. package/src/tools/film.d.ts +13 -0
  162. package/src/tools/film.js +116 -0
  163. package/src/tools/main.d.ts +1 -0
  164. package/src/tools/main.js +216 -0
  165. package/src/tools/score.d.ts +4 -0
  166. package/src/tools/score.js +29 -0
  167. package/src/tools/types.d.ts +69 -0
  168. package/src/tools/types.js +2 -0
  169. package/src/tools.d.ts +5 -0
  170. package/src/tools.js +3 -0
  171. package/src/virtual.d.ts +11 -0
  172. package/src/vite.d.ts +8 -0
  173. package/src/vite.js +43 -0
@@ -0,0 +1,333 @@
1
+ import { srect } from "../kit/geometry.js";
2
+ import { inOwnColors } from "../snapshot/serialize.js";
3
+ import { paintsOver, seenAt } from "../texts.js";
4
+ import { FAINT, FULL, newItem, opacityOf, wordsIn } from "./item.js";
5
+ import { PAINT, PAINT_KEYS, SKIP, UNIT, drawnAs, fills, hiddenByHand, inks, isPart, isPen, isWords, kidsOf, ownText, paints, saidOf, showsPaint, styleOf } from "./parts.js";
6
+ /*
7
+ * One look at the stage as drawn at one moment: every thing on it, where it is, how solid, and what of it the film
8
+ * changes from look to look.
9
+ */
10
+ const MOST_ELS = 1500;
11
+ /** Parts of svg drawings read part by part at one look, over all of them. */
12
+ const MOST_ART = 400;
13
+ /** A thing this share of the stage or more, solid, may hide what lies under it. */
14
+ const COVER = 0.25;
15
+ /** A part's box is this many px at the least each way: a straight stroke's has no height. */
16
+ const THIN = 8;
17
+ const SVG_TAGS = /^(svg|g)$/i;
18
+ function named(r, el) {
19
+ if (el.hasAttribute('data-label') || el.id)
20
+ return true;
21
+ return !!r.prefix && Array.from(el.classList).some((name) => name.indexOf(r.prefix ?? '') === 0);
22
+ }
23
+ /** It holds something that may draw but has no row, so no row can stand for all of it. */
24
+ export function markOdd(it) {
25
+ for (; it && (it.art || it.easel); it = it.up)
26
+ it.odd = true;
27
+ }
28
+ function thick(r) {
29
+ const w = Math.max(r.w, THIN);
30
+ const h = Math.max(r.h, THIN);
31
+ return r.w + r.h < 1 ? r : { x: r.x - (w - r.w) / 2, y: r.y - (h - r.h) / 2, w, h };
32
+ }
33
+ /** An svg as large as the stage, or a holder with no size of its own, is as large as what it holds. */
34
+ function unitRect(r, el) {
35
+ const box = srect(el);
36
+ if (box.w >= 1 && box.h >= 1 && !(/^svg$/i.test(el.tagName) && box.w * box.h >= FULL * r.W * r.H))
37
+ return box;
38
+ let u = null;
39
+ for (let c = el.firstElementChild; c; c = c.nextElementSibling) {
40
+ if (/^(defs|style|title|desc)$/i.test(c.tagName))
41
+ continue;
42
+ const k = srect(c);
43
+ if (k.w >= 0.5 && k.h >= 0.5)
44
+ u = u ? { x: Math.min(u.x, k.x), y: Math.min(u.y, k.y), w: Math.max(u.x + u.w, k.x + k.w) - Math.min(u.x, k.x), h: Math.max(u.y + u.h, k.y + k.h) - Math.min(u.y, k.y) } : k;
45
+ }
46
+ return u ?? { x: 0, y: 0, w: 0, h: 0 };
47
+ }
48
+ const setVary = (it, key) => {
49
+ it.vary[key] = true;
50
+ };
51
+ function walk(r, el, op, i, memo, state, path, up, hid, dark) {
52
+ const art = !!up && !!up.easel;
53
+ if ((!art && state.count++ > MOST_ELS) || SKIP.test(el.tagName) || (el.id === 'fx' && el.parentNode === r.stage))
54
+ return;
55
+ const cs = getComputedStyle(el);
56
+ if (cs.display === 'none')
57
+ return;
58
+ const o = op * opacityOf(cs);
59
+ let key = path.join('.') + '/' + el.tagName.toLowerCase();
60
+ let it = r.nodes.get(el);
61
+ // A place and tag already taken by a node still there: the next of that name, which may be a thing whose node is gone.
62
+ if (!it)
63
+ for (it = r.els.get(key); it && it.el !== el && it.el.isConnected; it = r.els.get(key))
64
+ key += '+';
65
+ if (!it) {
66
+ if (state.closed)
67
+ return;
68
+ it = newItem(el, key, path, up, named(r, el), UNIT.test(el.tagName) || el.classList.contains('film-cursor'));
69
+ r.els.set(key, it);
70
+ }
71
+ if (art && !it.art) {
72
+ it.art = true;
73
+ it.unit = !/^(g|text)$/i.test(el.tagName);
74
+ it.words = /^text$/i.test(el.tagName);
75
+ it.round = /^(circle|ellipse)$/i.test(el.tagName);
76
+ }
77
+ if (it.el !== el)
78
+ it.anew = true;
79
+ if (el.hasAttribute('data-film-over'))
80
+ it.laid = true;
81
+ // An svg or a g not read part by part at some look is one thing for the rest of the reading. away: not on the picture at this look.
82
+ const away = state.away.has(el);
83
+ if (!away && it.easel !== false && SVG_TAGS.test(el.tagName) && art === /^g$/i.test(el.tagName) && !(it.easel = state.open.has(el)))
84
+ it.unit = true;
85
+ it.el = el;
86
+ r.nodes.set(el, it);
87
+ it.held = hid;
88
+ it.own = hiddenByHand(el);
89
+ if (!hid && it.own)
90
+ hid = it;
91
+ it.hid = hid;
92
+ if (it.own || it.laid)
93
+ for (let a = up; a && !a.marked; a = a.up)
94
+ a.marked = true;
95
+ const style = styleOf(el);
96
+ const said = style ? style.visibility : '';
97
+ dark = said === 'visible' ? false : said === 'hidden' || said === 'collapse' || dark;
98
+ // A style attribute that says visible, or any visibility with !important, ends a hide, so none is offered.
99
+ if (!state.closed && style && (said === 'visible' || (said && style.getPropertyPriority('visibility'))))
100
+ it.vary.hidden = true;
101
+ if (!it.unit && !it.words && !it.art)
102
+ it.words = isWords(el);
103
+ let sig = (el.getAttribute('style') ?? '') + '|' + (el.getAttribute('class') ?? '');
104
+ if (it.words)
105
+ for (let w = el.firstChild; w; w = w.nextSibling)
106
+ sig += w instanceof Element ? (isPen(w) ? '' : w.outerHTML) : w instanceof Text ? w.data : '';
107
+ if (it.sig !== null && it.sig !== sig)
108
+ it.moved = true;
109
+ it.sig = sig;
110
+ // A class that comes and goes with the moment (a state, such as "on") is no part of what the thing is known by.
111
+ if (!state.closed)
112
+ it.always = it.always ? it.always.filter((name) => el.classList.contains(name)) : Array.from(el.classList);
113
+ if (!state.closed && (it.art || it.easel)) {
114
+ // A g read as one thing is watched with all under it, and a text with its words, since its print is of them.
115
+ const whole = it.art && it.unit && /^g$/i.test(el.tagName);
116
+ const spent = whole && (state.watch -= el.getElementsByTagName('*').length) < 0;
117
+ const as = (whole && !spent ? drawnAs(el, false, new Map()) : saidOf(el)) + (whole || it.words ? '|' + (el.textContent ?? '') : '');
118
+ if (spent || (it.as !== null && it.as !== as)) {
119
+ it.vary.drawn = true;
120
+ for (let v = it; v && v.art; v = v.up)
121
+ v.vary.within = true;
122
+ }
123
+ it.as = as;
124
+ }
125
+ // A part's paint as computed: a class, a rule or a script may change it where no attribute of the part's changes.
126
+ if (!state.closed && it.art && it.unit) {
127
+ const was = it.hues;
128
+ const now = {};
129
+ for (const p of PAINT_KEYS) {
130
+ now[p] = cs.getPropertyValue(PAINT[p]);
131
+ if (was && was[p] !== now[p])
132
+ setVary(it, p);
133
+ }
134
+ it.hues = now;
135
+ if (/^(path|line|polyline|polygon)$/i.test(el.tagName) && [cs.markerStart, cs.markerMid, cs.markerEnd].some((m) => !!m && m !== 'none'))
136
+ markOdd(it);
137
+ }
138
+ if (!state.closed && !it.art) {
139
+ const now = {
140
+ fill: cs.backgroundColor + cs.backgroundImage,
141
+ ink: cs.color,
142
+ radius: cs.borderTopLeftRadius + cs.borderTopRightRadius + cs.borderBottomRightRadius + cs.borderBottomLeftRadius,
143
+ size: cs.scale,
144
+ };
145
+ const was = it.hues;
146
+ if (was)
147
+ for (const h of ['fill', 'ink', 'radius', 'size'])
148
+ if (now[h] !== was[h])
149
+ setVary(it, h);
150
+ it.hues = now;
151
+ }
152
+ const under = (it.unit && !it.easel) || it.words || away ? [] : kidsOf(el);
153
+ // How many parts of each tag it holds: where that changes, so does the print of each of that tag.
154
+ if (!state.closed && it.easel && !away) {
155
+ const tally = {};
156
+ for (const c of under)
157
+ if (isPart(c))
158
+ tally[c.tagName] = (tally[c.tagName] ?? 0) + 1;
159
+ const before = it.tally;
160
+ if (before)
161
+ for (const tag of Object.keys(tally).concat(Object.keys(before)))
162
+ if (before[tag] !== tally[tag]) {
163
+ it.vary.tags[tag] = true;
164
+ for (let h = it; h && h.art; h = h.up)
165
+ h.vary.within = true;
166
+ }
167
+ it.tally = tally;
168
+ }
169
+ // For a camera: whatever holds others, is at some moment as large as the stage, and whose own box differs between two looks.
170
+ if (!(it.big && it.pans) && under.length) {
171
+ const wr = srect(el);
172
+ if (wr.w >= FULL * r.W && wr.h >= FULL * r.H)
173
+ it.big = true;
174
+ it.most = Math.max(it.most, wr.w * wr.h);
175
+ // The box of a g is only the box round what it holds, which shifts as a part moves: it pans if it is itself moved.
176
+ const tf = it.art ? [el.getAttribute('transform'), cs.transform, cs.translate, cs.scale, cs.rotate].join() : it.easel ? [el.getAttribute('viewBox'), el.getAttribute('preserveAspectRatio')].join() : null;
177
+ const lay = it.lay;
178
+ if (lay && (tf !== it.tf || (!it.art && Math.abs(wr.x - lay.x) + Math.abs(wr.y - lay.y) + Math.abs(wr.w - lay.w) + Math.abs(wr.h - lay.h) >= 1)))
179
+ it.pans = true;
180
+ it.lay = wr;
181
+ it.tf = tf;
182
+ }
183
+ const paint = !it.unit && paints(cs, el);
184
+ if (paint)
185
+ it.paint = true;
186
+ if ((it.unit || it.words || paint) && (!it.art || inks(cs, el))) {
187
+ it.cand = true;
188
+ if (it.art && fills(cs, el))
189
+ it.filled = true;
190
+ const box = it.art ? thick(srect(el)) : it.unit ? unitRect(r, el) : srect(el);
191
+ const lit = o >= FAINT && (hid ? !dark : cs.visibility !== 'hidden');
192
+ let on = lit && box.w >= 1 && box.h >= 1 && box.x < r.W && box.y < r.H && box.x + box.w > 0 && box.y + box.h > 0;
193
+ // Of no length a stroke draws its caps, but for a path with no d; a rect, circle or ellipse its stroke, if it has any extent.
194
+ if (!state.closed &&
195
+ it.art &&
196
+ lit &&
197
+ box.w + box.h < 1 &&
198
+ (/^(rect|circle|ellipse)$/i.test(el.tagName) ? box.w + box.h > 0 : /^(round|square)$/.test(cs.strokeLinecap) && !(/^path$/i.test(el.tagName) && !el.getAttribute('d'))) &&
199
+ parseFloat(cs.strokeWidth) > 0 &&
200
+ showsPaint(cs.stroke, cs.strokeOpacity))
201
+ it.speck = true;
202
+ if (on && it.words && !ownText(el)) {
203
+ on = false;
204
+ for (let c = el.firstElementChild; c && !on; c = c.nextElementSibling)
205
+ on = !isPen(c) && opacityOf(getComputedStyle(c)) >= FAINT;
206
+ }
207
+ it.on[i] = on;
208
+ it.op[i] = on ? o : 0;
209
+ it.box[i] = on ? box : null;
210
+ // Round or not is told once, at the first look at which it is on, and so has a size to go by.
211
+ if (on && paint && it.round === null && el instanceof HTMLElement) {
212
+ const ew = el.offsetWidth;
213
+ const eh = el.offsetHeight;
214
+ const rad = cs.borderTopLeftRadius;
215
+ it.round = ew < eh * 1.25 && eh < ew * 1.25 && parseFloat(rad) >= (/%$/.test(rad) ? 45 : Math.min(ew, eh) * 0.45);
216
+ }
217
+ if (on) {
218
+ state.on.push(it);
219
+ if (!hid && !it.words && o > 0.9 && box.w * box.h >= COVER * r.W * r.H && paintsOver(el, r.stage, memo))
220
+ state.covers.push(it);
221
+ }
222
+ if (it.words) {
223
+ const s = wordsIn(el);
224
+ if (s.length > it.text.length)
225
+ it.text = s.slice(0, 300);
226
+ }
227
+ }
228
+ under.forEach((child, k) => {
229
+ const part = isPart(child);
230
+ if (!it.easel || part)
231
+ walk(r, child, o, i, memo, state, path.concat(k), it, hid, dark);
232
+ else if (!state.closed && (/^(svg|a|switch)$/i.test(child.tagName) || child.hasAttribute('data-film-mark')))
233
+ markOdd(it);
234
+ });
235
+ }
236
+ /**
237
+ * Which svgs, and which g in them, are read part by part at this look: MOST_ART parts in all, level by level over all
238
+ * the drawings on the picture, so a heavy one loses its depth and not its breadth, and in a level what lies in front
239
+ * first. What holds a part a person's change is laid on is opened whatever is left.
240
+ */
241
+ function opened(r) {
242
+ const open = new Set();
243
+ const away = new Set();
244
+ const must = new Set();
245
+ let left = MOST_ART;
246
+ const dim = new Map();
247
+ const through = (n) => {
248
+ if (!n || n === r.stage || !(n instanceof Element))
249
+ return 1;
250
+ const known = dim.get(n);
251
+ if (known !== undefined)
252
+ return known;
253
+ const out = through(n.parentNode) * opacityOf(getComputedStyle(n));
254
+ dim.set(n, out);
255
+ return out;
256
+ };
257
+ // Of an svg a person's change is laid on, or that stands in such a thing, opacity and visibility are not asked: that change may be what hides it.
258
+ const shows = (s) => {
259
+ const box = srect(s);
260
+ if (!(box.w > 0) || box.x >= r.W || box.y >= r.H || box.x + box.w <= 0 || box.y + box.h <= 0)
261
+ return false;
262
+ if (s.closest('[data-film-over]'))
263
+ return true;
264
+ return through(s) >= FAINT && getComputedStyle(s).visibility !== 'hidden';
265
+ };
266
+ let level = Array.from(r.stage.getElementsByTagName('svg')).filter((s) => {
267
+ if (s.ownerSVGElement || isPen(s))
268
+ return false;
269
+ if (shows(s))
270
+ return true;
271
+ away.add(s);
272
+ return false;
273
+ });
274
+ for (const m of Array.from(r.stage.querySelectorAll('svg [data-film-over]')))
275
+ for (let p = m.parentNode; p && p !== r.stage && !must.has(p); p = p.parentNode)
276
+ must.add(p);
277
+ while (level.length) {
278
+ let next = [];
279
+ for (const h of level.reverse()) {
280
+ const parts = kidsOf(h).filter(isPart);
281
+ if (parts.length > left && !must.has(h))
282
+ continue;
283
+ left -= parts.length;
284
+ open.add(h);
285
+ next = parts.filter((p) => /^g$/i.test(p.tagName)).concat(next);
286
+ }
287
+ level = next;
288
+ }
289
+ return { open, away };
290
+ }
291
+ /** Draws moment `t` (played) and reads look `i` of the stage. */
292
+ export function lookAt(r, render, t, i, closed) {
293
+ render(t);
294
+ for (const use of Array.from(r.stage.getElementsByTagName('use'))) {
295
+ const id = (use.getAttribute('href') ?? use.getAttribute('xlink:href') ?? '').slice(1);
296
+ r.used.add(id);
297
+ try {
298
+ r.used.add(decodeURIComponent(id));
299
+ }
300
+ catch {
301
+ // As written.
302
+ }
303
+ }
304
+ const memo = new Map();
305
+ const plan = opened(r);
306
+ const state = { count: 0, open: plan.open, away: plan.away, watch: MOST_ART, closed, on: [], covers: [] };
307
+ // One inOwnColors for the whole walk: under a contrast theme each makes the browser work out every style twice more.
308
+ inOwnColors(() => kidsOf(r.stage).forEach((c, k) => walk(r, c, 1, i, memo, state, [k], null, false, false)));
309
+ // Hit testing is dear, so only what has its middle under something large and solid is tested.
310
+ for (const it of state.on) {
311
+ const box = it.box[i];
312
+ if (!box)
313
+ continue;
314
+ const x = box.x + box.w / 2;
315
+ const y = box.y + box.h / 2;
316
+ for (const cover of state.covers) {
317
+ const b = cover.box[i];
318
+ if (!b || cover === it || cover.el.contains(it.el) || it.el.contains(cover.el) || x < b.x || y < b.y || x > b.x + b.w || y > b.y + b.h)
319
+ continue;
320
+ if (!it.hid && !seenAt(it.el, it.el.getBoundingClientRect(), r.stage, memo, true)) {
321
+ it.on[i] = false;
322
+ it.op[i] = 0;
323
+ it.box[i] = null;
324
+ }
325
+ break;
326
+ }
327
+ if (!closed && it.on[i]) {
328
+ it.shown++;
329
+ if (Math.min(box.x + box.w, r.W) - Math.max(box.x, 0) >= FULL * r.W && Math.min(box.y + box.h, r.H) - Math.max(box.y, 0) >= FULL * r.H)
330
+ it.full++;
331
+ }
332
+ }
333
+ }
@@ -0,0 +1,10 @@
1
+ import type { StageFilm } from '../protocol.ts';
2
+ import type { HoldFirstDraw } from './snapshot/hold-first-draw.ts';
3
+ /**
4
+ * Told of each scene's box as it comes up: the classes in it, so a class counts as an element's own alike in every
5
+ * runner. Noted whether or not the film came with the record of what a person did by hand to how things look: the
6
+ * editor's stage is given that after, and may be given one for the first time while it is open.
7
+ */
8
+ export declare function sceneBox(node: HTMLElement): void;
9
+ /** Runs the film's script, builds its stage, records its score and draws its first frame; then says 'ready'. */
10
+ export declare function loadFilm(given: StageFilm, hold: HoldFirstDraw): Promise<void>;
@@ -0,0 +1,228 @@
1
+ import { parseRetouchedRecord } from '@open-artifact/motion-core';
2
+ import { addAssets } from "./assets.js";
3
+ import { captionLayer, readNarration } from "./captions.js";
4
+ import { errorPhase, failedToLoad, reportError, setErrorPhase, setLateHandler } from "./console.js";
5
+ import { chaptersOf, readCut } from "./cut.js";
6
+ import { realNow, reseed, setFilmTime } from "./determinism.js";
7
+ import { film } from "./film.js";
8
+ import { fillStacks, fontFamilies, fontsIn, resetFonts } from "./fonts.js";
9
+ import { makeKit } from "./kit/index.js";
10
+ import { addModules, findModule, injectScript, setScriptKit, tryScript } from "./modules.js";
11
+ import { say } from "./port.js";
12
+ import { filmParts, playedScore, plainClock, sceneClock } from "./recut.js";
13
+ import { forgetReading } from "./layers/scan.js";
14
+ import { takeRecord } from "./questions.js";
15
+ import { afterSample, busy, hasPending, isPlaying, onShow, renderAt, renderState, showsAsked, syncClips, unretouched } from "./render.js";
16
+ import { noteBox } from "./retouch/retouch.js";
17
+ import { recordScore } from "./score.js";
18
+ import { scanStage } from "./scan.js";
19
+ import { installScenes } from "./scenes/install.js";
20
+ import { setPlateHold } from "./snapshot/plate.js";
21
+ import { sketchScene } from "./sketch/pen.js";
22
+ import { buildStage, builtStage } from "./stage.js";
23
+ import { tellTexts } from "./texts.js";
24
+ const messageOf = (error) => String(error instanceof Error ? error.message : error);
25
+ let begun = false;
26
+ /**
27
+ * Told of each scene's box as it comes up: the classes in it, so a class counts as an element's own alike in every
28
+ * runner. Noted whether or not the film came with the record of what a person did by hand to how things look: the
29
+ * editor's stage is given that after, and may be given one for the first time while it is open.
30
+ */
31
+ export function sceneBox(node) {
32
+ try {
33
+ noteBox(node);
34
+ }
35
+ catch {
36
+ unretouched();
37
+ }
38
+ }
39
+ /**
40
+ * What a person did to the scenes by hand moves what the page is told (length, chapters, score), never what the
41
+ * cut sees. A fault costs their changes, not the film.
42
+ */
43
+ function recut(cut, chapters, ids) {
44
+ const record = findModule('recut.json');
45
+ try {
46
+ const clock = sceneClock(filmParts(cut.duration, chapters, ids), record ? record.text : null);
47
+ if (clock.aside)
48
+ console.info('a person’s scene lengths (modules/recut.json) would take the film past 300 s, so every scene plays at its own length.');
49
+ if (clock.moved)
50
+ console.info('a person changed scene lengths (modules/recut.json): the film plays ' +
51
+ clock.duration +
52
+ ' s, its own time is ' +
53
+ Math.round(cut.duration * 1e6) / 1e6 +
54
+ ' s. Times given to and by this player are as played; render(t) and scenes.json are the film’s own. meta.parts lists both.');
55
+ return clock;
56
+ }
57
+ catch {
58
+ if (record)
59
+ console.warn('what a person did to the scenes (modules/recut.json) could not be applied, so the film plays as written.');
60
+ return plainClock(cut.duration);
61
+ }
62
+ }
63
+ function recordedScore(cut, kit) {
64
+ const write = cut.score;
65
+ if (!write)
66
+ return null;
67
+ try {
68
+ return recordScore((builder) => {
69
+ write(builder, kit);
70
+ });
71
+ }
72
+ catch (error) {
73
+ console.error('score(S) threw: ' + messageOf(error));
74
+ return null;
75
+ }
76
+ }
77
+ function narrationOf(cut, given, clock) {
78
+ const limits = given.narration;
79
+ if (!limits || cut.narration === undefined || cut.narration === null)
80
+ return null;
81
+ try {
82
+ const read = readNarration(cut.narration, cut.duration, limits);
83
+ if (!read)
84
+ return null;
85
+ // A line starts on the action it speaks of, so its start moves with the picture; its length is the take's own.
86
+ const lines = read.lines.map((line) => ({ at: clock.back(line.at), dur: line.dur, text: line.text, audio: line.audio }));
87
+ if (read.captions !== false)
88
+ film.captions = captionLayer(lines, read.captions.style, Math.min(cut.width, cut.height), fontFamilies.sans);
89
+ return { lines, duck: read.duck };
90
+ }
91
+ catch (error) {
92
+ film.captions = null;
93
+ console.error('the narration could not be read, so the film plays without it: ' + messageOf(error));
94
+ return null;
95
+ }
96
+ }
97
+ /**
98
+ * A sample the first frame's draw began: once it has painted, the moment on show is drawn again, unless a frame is on
99
+ * its way or a pull holds the stage, and the clips and the words follow it. After 'ready': the page takes an error
100
+ * before it for a failed load.
101
+ */
102
+ function afterReady(sampled) {
103
+ const again = afterSample(sampled, onShow, () => !hasPending() && !busy.framing);
104
+ if (!again)
105
+ return;
106
+ const after = () => {
107
+ try {
108
+ if (!hasPending() && !busy.framing && !isPlaying())
109
+ void syncClips('hold');
110
+ // The words wait for the page's first render: until then the stage shows moment 0, which it never asked for.
111
+ if (showsAsked())
112
+ tellTexts();
113
+ }
114
+ catch {
115
+ // The next frame does it.
116
+ }
117
+ };
118
+ void again.then((a) => {
119
+ if (a.q)
120
+ a.q.then(after, (error) => {
121
+ reportError(errorPhase(), error);
122
+ after();
123
+ });
124
+ else
125
+ after();
126
+ });
127
+ }
128
+ /** Runs the film's script, builds its stage, records its score and draws its first frame; then says 'ready'. */
129
+ export async function loadFilm(given, hold) {
130
+ if (begun) {
131
+ reportError('load', new Error('already loaded; mount a fresh runner'));
132
+ return;
133
+ }
134
+ begun = true;
135
+ const began = realNow();
136
+ try {
137
+ resetFonts();
138
+ setPlateHold(hold, given.clippedTextWaitMs);
139
+ film.mode = given.mode;
140
+ addAssets(given.assets);
141
+ addModules(given.modules);
142
+ fillStacks(given.faces);
143
+ const kit = makeKit();
144
+ const scenes = installScenes(kit, {
145
+ run: (name, scopedKit, own) => {
146
+ const module = findModule(name);
147
+ return module ? tryScript(module.text, 'module ' + name, scopedKit, own) : new Error('no module ' + name);
148
+ },
149
+ reseed,
150
+ late: setLateHandler,
151
+ now: realNow,
152
+ resetFx: () => builtStage().fxWatch.reset(),
153
+ voice: given.narration !== null,
154
+ box: sceneBox,
155
+ sketch: sketchScene,
156
+ mode: () => film.mode,
157
+ });
158
+ Object.assign(kit, scenes.api);
159
+ setScriptKit(kit);
160
+ film.kit = kit;
161
+ buildStage(1920, 1080, '#ffffff');
162
+ setErrorPhase('load');
163
+ setFilmTime(0);
164
+ Reflect.set(window, 'CUT', undefined);
165
+ injectScript(given.script, 'cut.js');
166
+ // The SyntaxError or throw was reported with its line.
167
+ if (failedToLoad())
168
+ return;
169
+ if (builtStage().stage.childNodes.length > 3)
170
+ console.warn('cut.js built DOM at top level; it is discarded when the stage is sized, so build the scene in init(kit)');
171
+ const cut = readCut(Reflect.get(window, 'CUT'));
172
+ if (typeof cut === 'string') {
173
+ reportError('load', new Error(cut));
174
+ return;
175
+ }
176
+ film.cut = cut;
177
+ const { stage } = buildStage(cut.width, cut.height, cut.background);
178
+ // Fonts are in before init() measures anything.
179
+ await fontsIn();
180
+ if (cut.init)
181
+ await cut.init(kit);
182
+ try {
183
+ await document.fonts.ready;
184
+ }
185
+ catch {
186
+ // No font loading API: carry on.
187
+ }
188
+ const painted = getComputedStyle(stage);
189
+ const states = scenes.states();
190
+ const chapters = chaptersOf(cut);
191
+ const score = recordedScore(cut, kit);
192
+ const clock = recut(cut, chapters, states ? states.map((state) => state.id) : null);
193
+ if (clock.moved)
194
+ film.clock = clock;
195
+ const meta = {
196
+ width: cut.width,
197
+ height: cut.height,
198
+ fps: cut.fps,
199
+ duration: clock.moved ? clock.duration : cut.duration,
200
+ filmDuration: cut.duration,
201
+ chapters: chapters.map((chapter) => {
202
+ const part = clock.parts.find((each) => Math.abs(each.a0 - chapter.t) <= 0.05 && each.own === chapter.label);
203
+ return { t: clock.back(chapter.t), label: part ? part.label : chapter.label };
204
+ }),
205
+ scenes: states,
206
+ background: [painted.backgroundColor, painted.backgroundImage],
207
+ parts: clock.parts,
208
+ };
209
+ film.meta = meta;
210
+ // No record clears one: a look may have been tried ahead of the load.
211
+ const looks = findModule('retouched.json');
212
+ takeRecord(looks ? parseRetouchedRecord(looks.text) : null);
213
+ forgetReading();
214
+ const narration = narrationOf(cut, given, clock);
215
+ setErrorPhase('render');
216
+ const sampled = renderState.gavePromise;
217
+ // track() catches this promise's failure, so the window never hears of it: it is reported here.
218
+ renderAt(0)?.then(undefined, (error) => reportError(errorPhase(), error));
219
+ void syncClips('hold');
220
+ scanStage();
221
+ film.loaded = true;
222
+ say({ type: 'ready', meta, score: score ? playedScore(score, clock) : null, narration, loadMs: Math.round(realNow() - began) });
223
+ afterReady(sampled);
224
+ }
225
+ catch (error) {
226
+ reportError('load', error);
227
+ }
228
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,69 @@
1
+ import { captureConsole, catchErrors } from "./console.js";
2
+ import { installFilmClock } from "./determinism.js";
3
+ import { film } from "./film.js";
4
+ import { containFocus, holdLinks, watchNavigation } from "./focus.js";
5
+ import { pullFrame } from "./frame.js";
6
+ import { loadFilm } from "./load.js";
7
+ import { connect } from "./port.js";
8
+ import { lookAt, readLayers, tryLook } from "./questions.js";
9
+ import { askRender, redraw, showsAsked } from "./render.js";
10
+ import { scanStage } from "./scan.js";
11
+ import { makeHoldFirstDraw } from "./snapshot/hold-first-draw.js";
12
+ import { watchForcedColors } from "./snapshot/serialize.js";
13
+ import { fit } from "./stage.js";
14
+ import { retell, setTextsGuard, setWatch, tellTexts } from "./texts.js";
15
+ // Both are taken before the film clock and the film's own code can replace what they read.
16
+ const hold = makeHoldFirstDraw();
17
+ watchForcedColors();
18
+ installFilmClock();
19
+ watchNavigation();
20
+ captureConsole();
21
+ catchErrors();
22
+ containFocus();
23
+ holdLinks();
24
+ setTextsGuard(showsAsked);
25
+ window.addEventListener('resize', () => {
26
+ fit();
27
+ retell();
28
+ });
29
+ function dispatch(message) {
30
+ switch (message.type) {
31
+ case 'load':
32
+ void loadFilm(message.film, hold);
33
+ return;
34
+ case 'render':
35
+ askRender({ t: message.t, seq: message.seq, playing: message.playing });
36
+ return;
37
+ case 'mode':
38
+ film.mode = message.mode;
39
+ if (film.loaded)
40
+ redraw();
41
+ return;
42
+ case 'frame':
43
+ pullFrame(message);
44
+ return;
45
+ case 'watch':
46
+ setWatch(message.on, message.places);
47
+ // Before the page's first render of a load the stage shows moment 0, which it never asked for: that render tells.
48
+ if (showsAsked())
49
+ tellTexts();
50
+ return;
51
+ case 'scan':
52
+ scanStage();
53
+ return;
54
+ case 'layers':
55
+ readLayers(message);
56
+ return;
57
+ case 'retouch':
58
+ tryLook(message.record);
59
+ return;
60
+ case 'look':
61
+ lookAt(message);
62
+ return;
63
+ default: {
64
+ const never = message;
65
+ void never;
66
+ }
67
+ }
68
+ }
69
+ connect(dispatch);
@@ -0,0 +1,23 @@
1
+ import type { StageModule, StageModuleKind } from '../protocol.ts';
2
+ interface HeldModule {
3
+ kind: StageModuleKind;
4
+ text: string;
5
+ }
6
+ /** The global through which injected code finds the kit it was given: read once, at the top of each script. */
7
+ export declare const KIT_GLOBAL = "__stageKit";
8
+ export declare function addModules(list: readonly StageModule[]): void;
9
+ export declare const moduleNames: () => string[];
10
+ /** By its whole name, or by a bare stem that only one module has: "scenes" finds "scenes.json". */
11
+ export declare function findModule(name: string): HeldModule | null;
12
+ /**
13
+ * Runs code in the page with `kit` in reach. What it throws surfaces through the window's error event. `own`: strict
14
+ * code in a function's scope, so that no top-level name reaches window, where another module's could meet it.
15
+ */
16
+ export declare function injectScript(code: string, label: string, own?: boolean): void;
17
+ export declare function setScriptKit(kit: unknown): void;
18
+ /**
19
+ * Runs code with `kit` as its kit and gives back what it threw, or null: a script put into the page throws to the
20
+ * window's error event during the call, never to the caller, so a try alone would catch nothing.
21
+ */
22
+ export declare function tryScript(code: string, label: string, kit: unknown, own: boolean): unknown;
23
+ export {};