@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,204 @@
1
+ import { realNow } from "../determinism.js";
2
+ import { srect } from "../kit/geometry.js";
3
+ import { inOwnColors } from "../snapshot/serialize.js";
4
+ import { stageSize } from "../stage-size.js";
5
+ import { lookOf } from "./describe.js";
6
+ import { MOST, breath, inTurn, pinned, union } from "./item.js";
7
+ import { SHAPE, fills, showsPaint } from "./parts.js";
8
+ import { buildRows } from "./rows.js";
9
+ import { lookAt } from "./walk.js";
10
+ const MOST_LOOKS = 96;
11
+ const FEWEST_LOOKS = 5;
12
+ const STEP_S = 1 / 15;
13
+ const SLICE_MS = 12;
14
+ const WHOLE_MS = 1500;
15
+ const LAST_MS = 4000;
16
+ const MOST_TOLD = 400;
17
+ const DEEP = 4;
18
+ /** The last reading, kept while the stretch asked of is the same: a new moment on show needs only a new look. */
19
+ let kept = null;
20
+ export const forgetReading = () => {
21
+ kept = null;
22
+ };
23
+ const boxOf = (r) => [r.x, r.y, r.w, r.h];
24
+ /** The box round an element's text alone: a Range over the element would also take in each child element's whole box. */
25
+ function wordsBox(el) {
26
+ const doc = el.ownerDocument;
27
+ const walker = doc.createTreeWalker(el, NodeFilter.SHOW_TEXT);
28
+ const span = doc.createRange();
29
+ let out = null;
30
+ for (let n = walker.nextNode(); n; n = walker.nextNode()) {
31
+ if (!(n instanceof Text) || !/\S/.test(n.data))
32
+ continue;
33
+ span.selectNodeContents(n);
34
+ const r = srect(span);
35
+ if (r.w > 0 && r.h > 0)
36
+ out = union(out, r);
37
+ }
38
+ return out;
39
+ }
40
+ /**
41
+ * Reads the layers of a stretch: what is on the picture, when, how it comes and goes, and how the film draws it at the
42
+ * moment on show. `gone`: a newer question has come, or none is wanted any more; the reading then ends with null.
43
+ */
44
+ export async function scanLayers(d, sys, gone) {
45
+ const stage = sys.stage;
46
+ const W = stageSize.width;
47
+ const H = stageSize.height;
48
+ const from = Math.max(0, d.from || 0);
49
+ const to = Math.max(from + 0.01, d.to || 0);
50
+ const stretch = d.scene || 'film';
51
+ if (srect(stage).w < 1)
52
+ throw new Error('the page has no layout');
53
+ const key = from + '|' + to;
54
+ let read = kept && kept.key === key ? kept : null;
55
+ if (!read) {
56
+ const reading = { stage, W, H, prefix: d.scene ? d.scene + '-' : null, els: new Map(), nodes: new WeakMap(), used: new Set() };
57
+ const all = Math.max(8, Math.min(MOST_LOOKS, Math.ceil((to - from) / STEP_S)));
58
+ const turn = inTurn(all);
59
+ const taken = [];
60
+ let spent = 0;
61
+ const moment = (i) => from + ((to - from - 1 / 240) * i) / (all - 1);
62
+ // The film's clock stands in for performance.now while a moment is drawn, so the time spent is read off the real one.
63
+ let s0 = realNow();
64
+ for (let q = 0; q < turn.length; q++) {
65
+ // A film that is slow to draw is looked at less often.
66
+ if ((spent > WHOLE_MS && q >= FEWEST_LOOKS) || spent > LAST_MS)
67
+ break;
68
+ if (realNow() - s0 > SLICE_MS) {
69
+ await breath();
70
+ if (gone())
71
+ return null;
72
+ s0 = realNow();
73
+ }
74
+ const i = turn[q] ?? 0;
75
+ const l0 = realNow();
76
+ lookAt(reading, sys.render, moment(i), i, false);
77
+ spent += realNow() - l0;
78
+ taken.push(i);
79
+ }
80
+ taken.sort((a, b) => a - b);
81
+ const n = taken.length;
82
+ const times = taken.map(moment);
83
+ for (const it of reading.els.values()) {
84
+ const on = it.on;
85
+ const op = it.op;
86
+ const box = it.box;
87
+ it.on = taken.map((i) => on[i] ?? false);
88
+ it.op = taken.map((i) => op[i] ?? 0);
89
+ it.box = taken.map((i) => box[i] ?? null);
90
+ }
91
+ const s = { W, H, from, to, n, times, stretch, prefix: reading.prefix };
92
+ read = { key, list: buildRows(s, reading.els), reading, n };
93
+ // A reading that found nothing is not kept: the page may have been parked.
94
+ kept = read.list.length ? read : null;
95
+ }
96
+ const reading = read.reading;
97
+ const at = Math.max(from, Math.min(to - 1 / 240, d.at || 0));
98
+ const NOW = read.n;
99
+ for (const it of reading.els.values()) {
100
+ it.on[NOW] = false;
101
+ it.box[NOW] = null;
102
+ }
103
+ lookAt(reading, sys.render, at, NOW, true);
104
+ /*
105
+ * What each row shows of what it holds: DEEP levels, MOST to a row and MOST_TOLD rows in all, shared out level by
106
+ * level. A cut takes all that is pinned, then what is on the picture now, which a press can pick, and keeps the order.
107
+ */
108
+ const memo = {};
109
+ const shows = new Map();
110
+ const met = new Set();
111
+ let total = read.list.length;
112
+ const rank = (k) => (pinned(k) ? 0 : k.parts.some((p) => p.on[NOW]) ? 1 : 2);
113
+ for (let level = read.list, deep = 1; level.length && deep < DEEP; deep++) {
114
+ level = level.reduce((next, l) => {
115
+ const most = Math.min(MOST, Math.max(0, MOST_TOLD - total));
116
+ const pick = [0, 1, 2].reduce((some, r) => some.concat(l.kids.filter((k) => rank(k) === r)), []).filter((k, i) => i < most || pinned(k) || met.has(k.id));
117
+ const kids = l.kids.filter((k) => pick.includes(k));
118
+ shows.set(l, kids);
119
+ for (const k of kids)
120
+ if (!met.has(k.id)) {
121
+ met.add(k.id);
122
+ total++;
123
+ }
124
+ return next.concat(kids);
125
+ }, []);
126
+ }
127
+ const used = reading.used;
128
+ const rows = inOwnColors(() => {
129
+ const depth = (x) => x.kids.reduce((most, k) => Math.max(most, 1 + depth(k)), 0);
130
+ /*
131
+ * entire: all that is drawn in the svg is listed under the row, each a shape of which every paint that shows is
132
+ * offered, so a colour changed on the row leaves none of it behind. ever: a part off the picture now counts as on.
133
+ */
134
+ const entire = (l, ever) => {
135
+ const tips = [];
136
+ const tip = (x) => {
137
+ if (x.sole)
138
+ tips.push(x.sole);
139
+ else if (x.kids.length)
140
+ (shows.get(x) ?? []).forEach(tip);
141
+ else if (x !== l)
142
+ tips.push(x);
143
+ };
144
+ tip(l);
145
+ if (!tips.length || !l.svg || l.svg.odd || l.below + depth(l) > DEEP)
146
+ return false;
147
+ if (!l.inked.every((p) => tips.some((x) => x.it === p)))
148
+ return false;
149
+ return tips.every((x) => {
150
+ const el = x.it.el;
151
+ const cs = getComputedStyle(el);
152
+ const k = x.now ?? (ever ? lookOf(x, sys, memo, used) : null);
153
+ if (ever && !el.isConnected)
154
+ return true;
155
+ const stroked = parseFloat(cs.strokeWidth) > 0 && showsPaint(cs.stroke, cs.strokeOpacity);
156
+ return !!k && SHAPE.test(el.tagName) && (!fills(cs, el) || k.svgfill !== null) && (!stroked || k.svgstroke !== null);
157
+ });
158
+ };
159
+ const told = (l) => {
160
+ let b = null;
161
+ for (const p of l.parts)
162
+ if (p.on[NOW])
163
+ b = union(b, p.box[NOW] ?? null);
164
+ l.now = b ? lookOf(l, sys, memo, used) : null;
165
+ const only = l.parts[0];
166
+ const words = b && l.kind === 'text' && l.parts.length === 1 && only ? wordsBox(only.el) : null;
167
+ let kids = (shows.get(l) ?? []).map(told);
168
+ let sole = l.sole ? told(l.sole) : null;
169
+ const whole = !!l.svg && entire(l, false);
170
+ // A lone shape the row can never stand for has a row while a person's change is laid on it, the way to take that back.
171
+ if (sole && l.svg && !whole) {
172
+ if (l.sole && pinned(l.sole) && !entire(l, true))
173
+ kids = [sole];
174
+ sole = null;
175
+ }
176
+ const held = l.it.held;
177
+ return {
178
+ id: l.id,
179
+ kind: l.kind,
180
+ round: l.round,
181
+ name: l.name,
182
+ hidden: held ? 'held' : l.it.own && !l.pack ? 'own' : false,
183
+ under: held ? stretch + ':' + held.key : null,
184
+ own: l.it.own && !l.pack,
185
+ many: l.many,
186
+ texts: l.texts,
187
+ spans: l.spans.slice(0, 6).map(([a, z]) => [a, z]),
188
+ at: l.at,
189
+ enter: l.enter,
190
+ leave: l.leave,
191
+ box: b ? boxOf(b) : null,
192
+ wordsBox: words ? boxOf(words) : null,
193
+ look: l.now,
194
+ kids,
195
+ sole,
196
+ whole,
197
+ drawn: l.drawn === 'group' && (shows.get(l) ?? []).length < l.kids.length ? '' : l.drawn,
198
+ full: l.full,
199
+ };
200
+ };
201
+ return read.list.map(told);
202
+ });
203
+ return { seq: d.seq, from: from, to: to, at: at, lost: sys.lost(), underHide: sys.underHide(), rows };
204
+ }
@@ -0,0 +1,16 @@
1
+ import type { Item } from './item.js';
2
+ export interface Reading {
3
+ stage: HTMLElement;
4
+ W: number;
5
+ H: number;
6
+ /** The class prefix by which the scene names its things. */
7
+ prefix: string | null;
8
+ els: Map<string, Item>;
9
+ nodes: WeakMap<Element, Item>;
10
+ /** The ids that a use on the stage points to at any look. */
11
+ used: Set<string>;
12
+ }
13
+ /** It holds something that may draw but has no row, so no row can stand for all of it. */
14
+ export declare function markOdd(it: Item | null): void;
15
+ /** Draws moment `t` (played) and reads look `i` of the stage. */
16
+ export declare function lookAt(r: Reading, render: (t: number) => void, t: number, i: number, closed: boolean): void;
@@ -0,0 +1,337 @@
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, isHeld, 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) || isHeld(el) || (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 * (el.hasAttribute('data-film-thing') ? 1 : 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).slice(0, 300);
224
+ // Looks are not taken in time order. The look at the moment on show changes no finding, so it never breaks a tie.
225
+ if (s.length > it.text.length || (s.length === it.text.length && !state.closed && i > it.textAt)) {
226
+ it.text = s;
227
+ if (!state.closed)
228
+ it.textAt = i;
229
+ }
230
+ }
231
+ }
232
+ under.forEach((child, k) => {
233
+ const part = isPart(child);
234
+ if (!it.easel || part)
235
+ walk(r, child, o, i, memo, state, path.concat(k), it, hid, dark);
236
+ else if (!state.closed && (/^(svg|a|switch)$/i.test(child.tagName) || child.hasAttribute('data-film-mark')))
237
+ markOdd(it);
238
+ });
239
+ }
240
+ /**
241
+ * 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
242
+ * the drawings on the picture, so a heavy one loses its depth and not its breadth, and in a level what lies in front
243
+ * first. What holds a part a person's change is laid on is opened whatever is left.
244
+ */
245
+ function opened(r) {
246
+ const open = new Set();
247
+ const away = new Set();
248
+ const must = new Set();
249
+ let left = MOST_ART;
250
+ const dim = new Map();
251
+ const through = (n) => {
252
+ if (!n || n === r.stage || !(n instanceof Element))
253
+ return 1;
254
+ const known = dim.get(n);
255
+ if (known !== undefined)
256
+ return known;
257
+ const out = through(n.parentNode) * opacityOf(getComputedStyle(n));
258
+ dim.set(n, out);
259
+ return out;
260
+ };
261
+ // 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.
262
+ const shows = (s) => {
263
+ const box = srect(s);
264
+ if (!(box.w > 0) || box.x >= r.W || box.y >= r.H || box.x + box.w <= 0 || box.y + box.h <= 0)
265
+ return false;
266
+ if (s.closest('[data-film-over]'))
267
+ return true;
268
+ return through(s) >= FAINT && getComputedStyle(s).visibility !== 'hidden';
269
+ };
270
+ let level = Array.from(r.stage.getElementsByTagName('svg')).filter((s) => {
271
+ if (s.ownerSVGElement || isPen(s))
272
+ return false;
273
+ if (shows(s))
274
+ return true;
275
+ away.add(s);
276
+ return false;
277
+ });
278
+ for (const m of Array.from(r.stage.querySelectorAll('svg [data-film-over]')))
279
+ for (let p = m.parentNode; p && p !== r.stage && !must.has(p); p = p.parentNode)
280
+ must.add(p);
281
+ while (level.length) {
282
+ let next = [];
283
+ for (const h of level.reverse()) {
284
+ const parts = kidsOf(h).filter(isPart);
285
+ if (parts.length > left && !must.has(h))
286
+ continue;
287
+ left -= parts.length;
288
+ open.add(h);
289
+ next = parts.filter((p) => /^g$/i.test(p.tagName)).concat(next);
290
+ }
291
+ level = next;
292
+ }
293
+ return { open, away };
294
+ }
295
+ /** Draws moment `t` (played) and reads look `i` of the stage. */
296
+ export function lookAt(r, render, t, i, closed) {
297
+ render(t);
298
+ for (const use of Array.from(r.stage.getElementsByTagName('use'))) {
299
+ const id = (use.getAttribute('href') ?? use.getAttribute('xlink:href') ?? '').slice(1);
300
+ r.used.add(id);
301
+ try {
302
+ r.used.add(decodeURIComponent(id));
303
+ }
304
+ catch {
305
+ // As written.
306
+ }
307
+ }
308
+ const memo = new Map();
309
+ const plan = opened(r);
310
+ const state = { count: 0, open: plan.open, away: plan.away, watch: MOST_ART, closed, on: [], covers: [] };
311
+ // One inOwnColors for the whole walk: under a contrast theme each makes the browser work out every style twice more.
312
+ inOwnColors(() => kidsOf(r.stage).forEach((c, k) => walk(r, c, 1, i, memo, state, [k], null, false, false)));
313
+ // Hit testing is dear, so only what has its middle under something large and solid is tested.
314
+ for (const it of state.on) {
315
+ const box = it.box[i];
316
+ if (!box)
317
+ continue;
318
+ const x = box.x + box.w / 2;
319
+ const y = box.y + box.h / 2;
320
+ for (const cover of state.covers) {
321
+ const b = cover.box[i];
322
+ 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)
323
+ continue;
324
+ if (!it.hid && !seenAt(it.el, it.el.getBoundingClientRect(), r.stage, memo, true)) {
325
+ it.on[i] = false;
326
+ it.op[i] = 0;
327
+ it.box[i] = null;
328
+ }
329
+ break;
330
+ }
331
+ if (!closed && it.on[i]) {
332
+ it.shown++;
333
+ 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)
334
+ it.full++;
335
+ }
336
+ }
337
+ }
@@ -0,0 +1,10 @@
1
+ import type { StageFilm } from '../protocol.js';
2
+ import type { HoldFirstDraw } from './snapshot/hold-first-draw.js';
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>;