@johnoscott/isometric-engine 0.14.1 → 0.16.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 (81) hide show
  1. package/README.md +1 -1
  2. package/iso-editor.js +249 -203
  3. package/iso-render.js +200 -154
  4. package/iso-systems.js +210 -162
  5. package/isometric-diagram.js +389 -0
  6. package/js/iso-core/index.js +5 -0
  7. package/js/iso-core/mono.js +242 -0
  8. package/js/iso-editor/ui/canvas.js +23 -1
  9. package/js/iso-objects/ants.js +36 -0
  10. package/js/iso-objects/index.js +55 -1
  11. package/js/iso-objects/looks.js +25 -0
  12. package/js/iso-objects/mono-paint.js +96 -0
  13. package/js/iso-objects/motion-builder.js +130 -0
  14. package/js/iso-objects/motion-doc.js +421 -0
  15. package/js/iso-objects/motion-entry.js +34 -0
  16. package/js/iso-objects/motion-schema.js +83 -0
  17. package/js/iso-objects/motion.js +640 -0
  18. package/js/iso-objects/resolve.js +238 -0
  19. package/js/iso-objects/scene.js +15 -13
  20. package/js/iso-objects/timeline.js +593 -0
  21. package/js/iso-systems/compile.js +83 -1
  22. package/js/iso-systems/index.js +4 -1
  23. package/js/iso-systems/render.js +20 -0
  24. package/js/iso-systems/schema.json +40 -0
  25. package/js/iso-viewer/index.js +6 -0
  26. package/js/iso-viewer/page.js +40 -16
  27. package/js/iso-viewer/play.js +340 -0
  28. package/js/iso-viewer/player.js +149 -175
  29. package/js/iso-viewer/transport.js +289 -0
  30. package/package.json +84 -2
  31. package/src/iso-core/index.ts +1 -0
  32. package/src/iso-core/mono.ts +306 -0
  33. package/src/iso-core/theme.ts +2 -1
  34. package/src/iso-editor/ui/canvas.ts +14 -1
  35. package/src/iso-objects/ants.ts +53 -0
  36. package/src/iso-objects/compile.ts +4 -1
  37. package/src/iso-objects/index.ts +16 -1
  38. package/src/iso-objects/looks.ts +21 -0
  39. package/src/iso-objects/mono-paint.ts +97 -0
  40. package/src/iso-objects/motion-builder.ts +79 -0
  41. package/src/iso-objects/motion-doc.ts +309 -0
  42. package/src/iso-objects/motion-entry.ts +13 -0
  43. package/src/iso-objects/motion-schema.ts +55 -0
  44. package/src/iso-objects/motion.ts +617 -0
  45. package/src/iso-objects/resolve.ts +196 -0
  46. package/src/iso-objects/scene.ts +32 -13
  47. package/src/iso-objects/timeline.ts +493 -0
  48. package/src/iso-objects/types.ts +22 -2
  49. package/src/iso-systems/compile.ts +86 -4
  50. package/src/iso-systems/index.ts +4 -1
  51. package/src/iso-systems/render.ts +45 -0
  52. package/src/iso-systems/schema.json +40 -0
  53. package/src/iso-viewer/index.ts +4 -0
  54. package/src/iso-viewer/page.ts +28 -11
  55. package/src/iso-viewer/play.ts +280 -0
  56. package/src/iso-viewer/player.ts +132 -150
  57. package/src/iso-viewer/transport.ts +272 -0
  58. package/types/iso-core/index.d.ts +1 -0
  59. package/types/iso-core/mono.d.ts +61 -0
  60. package/types/iso-core/theme.d.ts +2 -0
  61. package/types/iso-objects/ants.d.ts +34 -0
  62. package/types/iso-objects/compile.d.ts +3 -0
  63. package/types/iso-objects/index.d.ts +16 -1
  64. package/types/iso-objects/looks.d.ts +4 -0
  65. package/types/iso-objects/mono-paint.d.ts +20 -0
  66. package/types/iso-objects/motion-builder.d.ts +105 -0
  67. package/types/iso-objects/motion-doc.d.ts +193 -0
  68. package/types/iso-objects/motion-entry.d.ts +11 -0
  69. package/types/iso-objects/motion-schema.d.ts +594 -0
  70. package/types/iso-objects/motion.d.ts +202 -0
  71. package/types/iso-objects/resolve.d.ts +20 -0
  72. package/types/iso-objects/scene.d.ts +14 -0
  73. package/types/iso-objects/timeline.d.ts +66 -0
  74. package/types/iso-objects/types.d.ts +34 -0
  75. package/types/iso-systems/compile.d.ts +25 -3
  76. package/types/iso-systems/index.d.ts +4 -1
  77. package/types/iso-systems/render.d.ts +25 -0
  78. package/types/iso-viewer/index.d.ts +4 -0
  79. package/types/iso-viewer/play.d.ts +37 -0
  80. package/types/iso-viewer/transport.d.ts +73 -0
  81. package/types/player/isometric-diagram.d.ts +24 -0
@@ -0,0 +1,593 @@
1
+ import { didYouMean } from "../iso-core/errors.js";
2
+ import { ANT_SPEED, antDash, antsOf } from "./ants.js";
3
+ import { silhouette } from "./labels.js";
4
+ import { motionModel, planOf } from "./motion.js";
5
+ import { focusView } from "./focus.js";
6
+ import { checkMotion, curveAt, easeOf, msOf, readMs, springAt } from "./motion-doc.js";
7
+ const r2 = (v) => Math.round(v * 100) / 100 || 0;
8
+ const r4 = (v) => Math.round(v * 1e4) / 1e4 || 0;
9
+ const clamp = (f) => Math.max(0, Math.min(1, f));
10
+ const mix = (a, b, f) => a + (b - a) * f;
11
+ const FADE = 250;
12
+ class Sum {
13
+ items = /* @__PURE__ */ new Map();
14
+ labels = /* @__PURE__ */ new Map();
15
+ routes = /* @__PURE__ */ new Map();
16
+ tokens = [];
17
+ pulses = [];
18
+ outlines = [];
19
+ variants = {};
20
+ marks = 1;
21
+ at(m, id) {
22
+ let v = m.get(id);
23
+ if (!v) m.set(id, v = {});
24
+ return v;
25
+ }
26
+ item(id, s) {
27
+ const v = this.at(this.items, id);
28
+ if (s.opacity !== void 0) v.opacity = (v.opacity ?? 1) * s.opacity;
29
+ if (s.dx) v.dx = (v.dx ?? 0) + s.dx;
30
+ if (s.dy) v.dy = (v.dy ?? 0) + s.dy;
31
+ if (s.scale !== void 0) {
32
+ v.scale = (v.scale ?? 1) * s.scale;
33
+ v.about = s.about ?? v.about;
34
+ }
35
+ if (s.clip) v.clip = s.clip;
36
+ }
37
+ label(id, s) {
38
+ const v = this.at(this.labels, id);
39
+ if (s.opacity !== void 0) v.opacity = (v.opacity ?? 1) * s.opacity;
40
+ if (s.dx) v.dx = (v.dx ?? 0) + s.dx;
41
+ if (s.dy) v.dy = (v.dy ?? 0) + s.dy;
42
+ if (s.typed !== void 0) v.typed = Math.min(v.typed ?? 1, s.typed);
43
+ }
44
+ route(id, s) {
45
+ const v = this.at(this.routes, id);
46
+ if (s.opacity !== void 0) v.opacity = (v.opacity ?? 1) * s.opacity;
47
+ if (s.drawn !== void 0 && s.drawn < (v.drawn ?? 1)) {
48
+ v.drawn = s.drawn;
49
+ v.by = s.by;
50
+ }
51
+ if (s.ants) v.ants = s.ants;
52
+ }
53
+ add(s) {
54
+ for (const [id, x] of Object.entries(s.items ?? {})) this.item(id, x);
55
+ for (const [id, x] of Object.entries(s.labels ?? {})) this.label(id, x);
56
+ for (const [id, x] of Object.entries(s.routes ?? {})) this.route(id, x);
57
+ if (s.marks) this.marks *= s.marks.opacity;
58
+ this.tokens.push(...s.tokens ?? []);
59
+ this.pulses.push(...s.pulses ?? []);
60
+ }
61
+ state(camera) {
62
+ const out = {};
63
+ const rec = (m, clean) => {
64
+ const o = {};
65
+ for (const [id, v] of m) {
66
+ const c = clean(v);
67
+ if (Object.keys(c).length) o[id] = c;
68
+ }
69
+ return o;
70
+ };
71
+ const some = (o) => Object.keys(o).length > 0;
72
+ const op = (v) => v === void 0 || r4(v) >= 1 ? {} : { opacity: r4(Math.max(0, v)) };
73
+ const move = (v) => ({ ...r2(v.dx ?? 0) ? { dx: r2(v.dx) } : {}, ...r2(v.dy ?? 0) ? { dy: r2(v.dy) } : {} });
74
+ const items = rec(this.items, (v) => ({ ...op(v.opacity), ...move(v), ...v.scale !== void 0 && r4(v.scale) !== 1 ? { scale: r4(v.scale), ...v.about ? { about: v.about } : {} } : {}, ...v.clip ? { clip: v.clip } : {} }));
75
+ const labels = rec(this.labels, (v) => ({ ...op(v.opacity), ...v.typed !== void 0 && v.typed < 1 ? { typed: r4(v.typed) } : {}, ...move(v) }));
76
+ const routes = rec(this.routes, (v) => {
77
+ const hidden = v.opacity !== void 0 && r4(v.opacity) <= 0, part = v.drawn !== void 0 && v.drawn < 1;
78
+ return { ...op(v.opacity), ...part ? { drawn: r4(v.drawn), ...v.by ? { by: v.by } : {} } : {}, ...v.ants && !hidden && !part ? { ants: v.ants } : {} };
79
+ });
80
+ if (some(items)) out.items = items;
81
+ if (some(labels)) out.labels = labels;
82
+ if (some(routes)) out.routes = routes;
83
+ if (r4(this.marks) < 1) out.marks = { opacity: r4(this.marks) };
84
+ if (this.tokens.length) out.tokens = this.tokens;
85
+ if (this.pulses.length) out.pulses = this.pulses;
86
+ if (this.outlines.length) out.outlines = this.outlines;
87
+ if (some(this.variants)) out.variants = this.variants;
88
+ if (camera) out.camera = camera;
89
+ return out;
90
+ }
91
+ }
92
+ function motionSource(scene) {
93
+ const ants = antsOf(scene.raw), looks = scene.rendered.looks, model = motionModel(scene), free = Object.keys(model.notes).filter((n) => model.notes[n] === void 0);
94
+ return {
95
+ model,
96
+ kinds: Object.fromEntries(scene.routes.map((r) => [r.id, r.kind])),
97
+ outlines: Object.fromEntries(scene.layout.items.map((p) => [p.id, silhouette(p.box).map((q) => [r2(q[0]), r2(q[1])])])),
98
+ ...looks && Object.keys(looks).length ? { looks } : {},
99
+ ...ants ? { ants } : {},
100
+ ...free.length ? { free } : {}
101
+ };
102
+ }
103
+ function bindMotion(drawing, doc, opts = {}) {
104
+ const src = "model" in drawing ? drawing : motionSource(drawing);
105
+ const m = src.free ? { ...src.model, notes: { ...src.model.notes, ...Object.fromEntries(src.free.map((n) => [n, void 0])) } } : src.model;
106
+ const diags = checkMotion(doc), fps = doc.fps, fileAnts = src.ants;
107
+ const none = () => ({ ok: false, diags, duration: 0, beats: [], markers: {}, ambient: "on", loops: false, at: () => ({}), cues: () => void 0, beatAt: () => void 0, startOf: () => void 0 });
108
+ if (diags.length) return none();
109
+ const err = (path, message, hint) => {
110
+ diags.push({ severity: "error", path, message, ...hint ? { hint } : {} });
111
+ };
112
+ const ms = (t, otherwise) => t === void 0 ? otherwise : msOf(t, fps);
113
+ const pieces = new Map(m.pieces.map((p) => [p.id, p])), routes = new Set(m.routes.map((r) => r.id)), notes = new Set(Object.keys(m.notes));
114
+ const kindOf = new Map(Object.entries(src.kinds));
115
+ const all = [...pieces.keys(), ...routes, ...notes];
116
+ const known = new Set(all);
117
+ const notesOn = /* @__PURE__ */ new Map();
118
+ for (const [n, on] of Object.entries(m.notes)) if (on !== void 0) notesOn.set(on, [...notesOn.get(on) ?? [], n]);
119
+ const what = { any: known, thing: new Set(pieces.keys()), route: routes };
120
+ const words = { any: "thing, connector or note", thing: "thing", route: "connector" };
121
+ const need = (id, path, kind = "any") => {
122
+ if (!what[kind].has(id)) {
123
+ const d = didYouMean(id, what[kind]);
124
+ err(path, `no ${words[kind]} "${id}" in this drawing`, d ? `did you mean "${d}"?` : void 0);
125
+ }
126
+ };
127
+ const needAll = (v, path, kind = "any") => v?.forEach((id, i) => need(id, `${path}/${i}`, kind));
128
+ const outline = new Map(Object.entries(src.outlines));
129
+ const plans = /* @__PURE__ */ new Map();
130
+ function nodeEnd(n, path) {
131
+ const from = "from" in n ? ms(n.from, 0) : 0, dur = "duration" in n ? ms(n.duration, NaN) : NaN, given = Number.isFinite(dur);
132
+ if ("sequence" in n) {
133
+ const s = n.sequence;
134
+ return ms(s.from, 0) + seqLength(s, `${path}/sequence`);
135
+ }
136
+ if ("series" in n) return layoutSeries(n.series, `${path}/series`).end;
137
+ if ("layers" in n) return Math.max(0, ...n.layers.map((x, i) => nodeEnd(x, `${path}/layers/${i}`)));
138
+ if ("assemble" in n || "disassemble" in n) {
139
+ const kind = "assemble" in n ? "assemble" : "disassemble", o = "assemble" in n ? n.assemble : n.disassemble;
140
+ let p = plans.get(n);
141
+ if (!p) plans.set(n, p = planOf(m, kind, { seed: doc.seed ?? 1, ...o, ...given ? { duration: dur } : {} }));
142
+ return from + p.duration;
143
+ }
144
+ if ("draw" in n) return from + (given ? dur : 400) + Math.max(0, n.draw.length - 1) * ms(n.stagger, 0);
145
+ if ("lift" in n) return from + Math.max(given ? dur : 750, ...n.opacity?.times.map((t) => msOf(t, fps)) ?? []) + Math.max(0, n.lift.length - 1) * ms(n.stagger, 0);
146
+ if (given) return from + dur;
147
+ if ("travel" in n) return n.loop ? from : from + n.travel.length * ms(n.each, 1150);
148
+ if ("pulse" in n) return n.loop ? from : from + 1500;
149
+ if ("type" in n) return from + 400;
150
+ if ("camera" in n) return from + 900;
151
+ if ("move" in n) return from + Math.max(0, ...[n.x, n.y, n.scale, n.opacity].flatMap((c) => c?.times.map((t) => msOf(t, fps)) ?? []));
152
+ return from;
153
+ }
154
+ const contents = (s, path) => Math.max(0, ...(s.children ?? []).map((n, i) => nodeEnd(n, `${path}/children/${i}`)), ...(s.layers ?? []).map((n, i) => nodeEnd(n, `${path}/layers/${i}`)), s.series ? layoutSeries(s.series, `${path}/series`).end : 0);
155
+ const runOf = /* @__PURE__ */ new Map();
156
+ function inner(s, path) {
157
+ const have = runOf.get(s);
158
+ if (have !== void 0) return have;
159
+ let d;
160
+ if (s.duration !== void 0 && typeof s.duration === "object") d = readMs(s.say, s.duration.read);
161
+ else d = s.duration !== void 0 ? ms(s.duration, 0) : contents(s, path);
162
+ if (s.duration === void 0 && d <= 0 && s.say) d = readMs(s.say, true);
163
+ if (d <= 0) err(`${path}/duration`, "how long is it? (nothing in it says)");
164
+ runOf.set(s, d);
165
+ return d;
166
+ }
167
+ const seqLength = (s, path) => {
168
+ const d = inner(s, path), rate = s.playbackRate ?? 1;
169
+ if (s.loop === true) return d / rate;
170
+ return (typeof s.loop === "number" ? d * s.loop : d) / rate;
171
+ };
172
+ function layoutSeries(items, path) {
173
+ const list = [];
174
+ let at2 = 0, into;
175
+ items.forEach((it, i) => {
176
+ if ("transition" in it) {
177
+ const t = it.transition, g = t.timing;
178
+ const length = t.presentation === "none" ? 0 : "linear" in g ? ms(g.linear, 0) : ms(g.spring.duration, settleMs(g.spring));
179
+ into = { t, length };
180
+ return;
181
+ }
182
+ const s = it.sequence, p = `${path}/${i}/sequence`, len = seqLength(s, p);
183
+ const start = Math.max(0, at2 + ms(s.offset, 0) - (into?.length ?? 0));
184
+ if (into && list.length && into.length > Math.min(list[list.length - 1].end - list[list.length - 1].start, len)) err(`${path}/${i - 1}/transition/timing`, "a transition is no longer than either side of it");
185
+ list.push({ s, path: p, start, end: start + len, ...into ? { into } : {} });
186
+ at2 = start + len;
187
+ into = void 0;
188
+ });
189
+ return { list, end: Math.max(0, ...list.map((x) => x.end)) };
190
+ }
191
+ const top = layoutSeries(doc.series ?? [], "/series");
192
+ const extra = Math.max(0, ...(doc.layers ?? []).map((n, i) => nodeEnd(n, `/layers/${i}`)));
193
+ const duration = Math.max(top.end, extra);
194
+ if (duration <= 0 && !diags.length) err("", "a motion document has a series, or layers, that last");
195
+ const starts = /* @__PURE__ */ new Map();
196
+ const cams = [];
197
+ const fit = m.viewBox;
198
+ const framed = (b, margin, zoom) => {
199
+ const k = zoom !== void 0 ? 1 / zoom : Math.max((b.w + margin * 2) / fit[2], (b.h + margin * 2) / fit[3]);
200
+ return [b.x + b.w / 2 - fit[2] * k / 2, b.y + b.h / 2 - fit[3] * k / 2, fit[2] * k, fit[3] * k];
201
+ };
202
+ function visit(n, path, abs) {
203
+ const at2 = (t) => abs ? { at: abs.at + t / abs.rate, rate: abs.rate } : void 0;
204
+ if ("sequence" in n) {
205
+ seq(n.sequence, `${path}/sequence`, at2(ms(n.sequence.from, 0)), false);
206
+ return;
207
+ }
208
+ if ("series" in n) {
209
+ for (const p of layoutSeries(n.series, `${path}/series`).list) seq(p.s, p.path, at2(p.start), false);
210
+ return;
211
+ }
212
+ if ("layers" in n) {
213
+ n.layers.forEach((x, i) => visit(x, `${path}/layers/${i}`, abs));
214
+ return;
215
+ }
216
+ if ("travel" in n) n.travel.forEach((x, i) => (Array.isArray(x) ? x : [x]).forEach((id, j) => need(id.split(":")[0], `${path}/travel/${i}${Array.isArray(x) ? `/${j}` : ""}`, "route")));
217
+ else if ("pulse" in n) needAll(n.pulse, `${path}/pulse`, "thing");
218
+ else if ("ants" in n) {
219
+ if (n.ants !== true) needAll(n.ants, `${path}/ants`, "route");
220
+ } else if ("draw" in n) needAll(n.draw, `${path}/draw`, "route");
221
+ else if ("type" in n) needAll(n.type, `${path}/type`);
222
+ else if ("move" in n) need(n.move, `${path}/move`, "thing");
223
+ else if ("lift" in n) needAll(n.lift, `${path}/lift`, "thing");
224
+ else if ("focus" in n) focusIds(n.focus, `${path}/focus`);
225
+ else if ("highlight" in n) needAll(n.highlight, `${path}/highlight`, "thing");
226
+ else if ("camera" in n) {
227
+ const c = n.camera, over = ms(n.duration, 900);
228
+ if (!abs) {
229
+ err(`${path}/camera`, "a camera can't be in a loop");
230
+ return;
231
+ }
232
+ let to = fit;
233
+ if (c !== "fit") {
234
+ const f = c.focus;
235
+ if (Array.isArray(f) && typeof f[0] === "number") to = framed({ x: f[0], y: f[1], w: f[2], h: f[3] }, c.margin ?? 0, c.zoom);
236
+ else {
237
+ const ids = typeof f === "string" ? [f] : f;
238
+ ids.forEach((id, i) => need(id, `${path}/camera/focus${typeof f === "string" ? "" : `/${i}`}`, "thing"));
239
+ const bs = ids.map((id) => pieces.get(id)?.bbox).filter((b) => !!b);
240
+ if (bs.length) {
241
+ const x = Math.min(...bs.map((b) => b.x)), y = Math.min(...bs.map((b) => b.y));
242
+ to = framed({ x, y, w: Math.max(...bs.map((b) => b.x + b.w)) - x, h: Math.max(...bs.map((b) => b.y + b.h)) - y }, c.margin ?? 40, c.zoom);
243
+ }
244
+ }
245
+ }
246
+ cams.push({ at: abs.at + ms(n.from, 0) / abs.rate, over: over / abs.rate, ease: easeOf(n.easing, over, "settle"), to });
247
+ }
248
+ }
249
+ const focusIds = (f, path) => {
250
+ if (f) {
251
+ needAll(f.lit, `${path}/lit`);
252
+ needAll(f.always, `${path}/always`);
253
+ }
254
+ };
255
+ function seq(s, path, abs, isTop) {
256
+ if (s.name !== void 0 && abs) starts.set(s.name, abs.at);
257
+ if (!isTop) {
258
+ for (const k of ["show", "hide", "say", "enter", "focus", "highlight", "levels", "variants", "ants"]) if (s[k] !== void 0) err(`${path}/${k}`, `${k} is for a sequence of the document's own series`, k === "focus" || k === "highlight" || k === "ants" ? `here, a child: { ${k}: \u2026 }` : void 0);
259
+ }
260
+ needAll(s.show, `${path}/show`);
261
+ needAll(s.hide, `${path}/hide`);
262
+ needAll(s.highlight, `${path}/highlight`, "thing");
263
+ focusIds(s.focus, `${path}/focus`);
264
+ for (const id of Object.keys(s.levels ?? {})) if (id !== "*") need(id, `${path}/levels/${id}`);
265
+ for (const [id, name] of Object.entries(s.variants ?? {})) {
266
+ need(id, `${path}/variants/${id}`, "thing");
267
+ const have = src.looks?.[id] ?? [];
268
+ if (pieces.has(id) && name !== "" && !have.includes(name)) {
269
+ const d = didYouMean(name, have);
270
+ err(`${path}/variants/${id}`, have.length ? `${id} has no look "${name}" (it has ${have.join(", ")})` : `${id} declares no looks (variants: in the diagram)`, d ? `did you mean "${d}"?` : void 0);
271
+ }
272
+ }
273
+ if (Array.isArray(s.ants)) needAll(s.ants, `${path}/ants`, "route");
274
+ const rate = s.playbackRate ?? 1;
275
+ const local = abs && !s.loop ? { at: abs.at - ms(s.trimBefore, 0) / (abs.rate * rate), rate: abs.rate * rate } : void 0;
276
+ (s.children ?? []).forEach((n, i) => visit(n, `${path}/children/${i}`, local));
277
+ (s.layers ?? []).forEach((n, i) => visit(n, `${path}/layers/${i}`, local));
278
+ if (s.series) for (const p of layoutSeries(s.series, `${path}/series`).list) seq(p.s, p.path, local && { at: local.at + p.start / local.rate, rate: local.rate }, false);
279
+ }
280
+ for (const p of top.list) seq(p.s, p.path, { at: p.start, rate: 1 }, true);
281
+ (doc.layers ?? []).forEach((n, i) => visit(n, `/layers/${i}`, { at: 0, rate: 1 }));
282
+ cams.sort((a, b) => a.at - b.at);
283
+ const markers = {};
284
+ for (const [k, v] of Object.entries(doc.markers ?? {})) {
285
+ const t = msOf(v, fps);
286
+ if (t !== void 0) {
287
+ markers[k] = t;
288
+ continue;
289
+ }
290
+ const s = String(v), hit = [...starts.keys()].filter((x) => s === x || s.startsWith(x + "+") || s.startsWith(x + "-")).sort((a, b) => b.length - a.length)[0];
291
+ if (hit === void 0) {
292
+ err(`/markers/${k}`, `"${s}" isn't a moment of this document`);
293
+ continue;
294
+ }
295
+ const rest = s.slice(hit.length), off = rest ? msOf(rest.slice(1), fps) : 0;
296
+ if (off === void 0) {
297
+ err(`/markers/${k}`, `"${rest}" isn't a time`);
298
+ continue;
299
+ }
300
+ markers[k] = starts.get(hit) + (rest[0] === "-" ? -off : off);
301
+ }
302
+ if (diags.length) return none();
303
+ const beats = top.list.map((p, i) => ({ name: p.s.name ?? `beat-${i + 1}`, index: i, start: p.start, end: p.end, ...p.s.say ? { say: p.s.say } : {} }));
304
+ const managed = /* @__PURE__ */ new Map();
305
+ for (const p of top.list) {
306
+ for (const id of p.s.show ?? []) if (!managed.has(id)) managed.set(id, false);
307
+ for (const id of p.s.hide ?? []) if (!managed.has(id)) managed.set(id, true);
308
+ }
309
+ const folds = [new Map(managed)];
310
+ for (const p of top.list) {
311
+ const v = new Map(folds[folds.length - 1]);
312
+ for (const id of p.s.show ?? []) v.set(id, true);
313
+ for (const id of p.s.hide ?? []) v.set(id, false);
314
+ folds.push(v);
315
+ }
316
+ const looked = [/* @__PURE__ */ new Map()];
317
+ for (const p of top.list) {
318
+ const v = new Map(looked[looked.length - 1]);
319
+ for (const [id, n] of Object.entries(p.s.variants ?? {})) v.set(id, n);
320
+ looked.push(v);
321
+ }
322
+ const left = (f, id) => !f || f.lit.includes(id) || f.always?.includes(id) || f.scope === "links" && pieces.has(id) ? 1 : f.level ?? 0.12;
323
+ const level = (k, id) => {
324
+ const v = k < 0 ? void 0 : top.list[k].s.levels;
325
+ return v ? v[id] ?? v["*"] ?? 1 : 1;
326
+ };
327
+ const share = (k, id) => (folds[k + 1].get(id) === false ? 0 : 1) * (k < 0 ? 1 : left(top.list[k].s.focus, id)) * level(k, id);
328
+ const noteShare = (k, n) => {
329
+ const on = m.notes[n];
330
+ if (on === void 0) return share(k, n);
331
+ const v = k < 0 ? void 0 : top.list[k].s.levels?.[n];
332
+ return share(k, on) * (folds[k + 1].get(n) === false ? 0 : 1) * (v ?? 1);
333
+ };
334
+ const beatIndex = (T) => {
335
+ let k = -1;
336
+ for (let i = 0; i < top.list.length; i++) if (top.list[i].start <= T) k = i;
337
+ return k;
338
+ };
339
+ const entered = (k, T) => {
340
+ const p = top.list[k], lt = T - p.start;
341
+ if (p.into && p.into.length > 0) {
342
+ const g = p.into.t.timing;
343
+ return "spring" in g ? lt >= p.into.length ? 1 : clamp(springAt(lt, { ...g.spring, overshootClamping: true }) / springAt(p.into.length, { ...g.spring, overshootClamping: true })) : clamp(lt / p.into.length);
344
+ }
345
+ const e = p.s.enter;
346
+ if (!e) return 1;
347
+ const over = ms(e.fade, 0);
348
+ return over <= 0 ? 1 : easeOf(e.easing, over, "ease")(clamp(lt / over));
349
+ };
350
+ function each(n, t, a, end, c) {
351
+ if ("sequence" in n) {
352
+ const s = n.sequence, from2 = ms(s.from, 0);
353
+ run(s, t - from2, a - from2, runLength(s, end - from2), c);
354
+ return;
355
+ }
356
+ if ("series" in n) {
357
+ for (const p of layoutSeries(n.series, "").list) run(p.s, t - p.start, a - p.start, p.end - p.start, c);
358
+ return;
359
+ }
360
+ if ("layers" in n) {
361
+ for (const x of n.layers) each(x, t, a, end, c);
362
+ return;
363
+ }
364
+ const from = ms("from" in n ? n.from : void 0, 0), lt = t - from;
365
+ if ("assemble" in n || "disassemble" in n) {
366
+ c.sum.add(plans.get(n).at(lt));
367
+ return;
368
+ }
369
+ if ("draw" in n) {
370
+ const one = Math.max(1, ms(n.duration, 400)), gap = ms(n.stagger, 0), e = easeOf(n.easing, one, "ease-out");
371
+ n.draw.forEach((id, i) => {
372
+ const f = e(clamp((lt - i * gap) / one));
373
+ if (f <= 0) {
374
+ c.sum.route(id, { opacity: 0, drawn: 0 });
375
+ c.sum.label(id, { opacity: 0 });
376
+ } else if (f < 1) {
377
+ c.sum.route(id, { drawn: f, ...n.by ? { by: n.by } : {} });
378
+ c.sum.label(id, { opacity: f });
379
+ }
380
+ });
381
+ return;
382
+ }
383
+ if ("type" in n) {
384
+ const d = ms(n.duration, 400), f = easeOf(n.easing, d, "linear")(clamp(lt / Math.max(1, d)));
385
+ if (f < 1) for (const id of n.type) c.sum.label(id, f <= 0 ? { opacity: 0 } : { typed: f });
386
+ return;
387
+ }
388
+ if ("move" in n) {
389
+ const g = (q) => q ? curveAt(q, lt, fps) : void 0, dx = g(n.x) ?? 0, dy = g(n.y) ?? 0, sc = g(n.scale), op = g(n.opacity);
390
+ c.sum.item(n.move, { dx, dy, ...sc === void 0 ? {} : { scale: sc, about: pieces.get(n.move).foot }, ...op === void 0 ? {} : { opacity: clamp(op) } });
391
+ for (const id of [n.move, ...notesOn.get(n.move) ?? []]) c.sum.label(id, { dx, dy, ...op === void 0 ? {} : { opacity: clamp(op) } });
392
+ return;
393
+ }
394
+ if ("lift" in n) {
395
+ const gap = ms(n.stagger, 0), one = Math.max(1, ms(n.duration, 750)), e = easeOf(n.easing, one, "ease-out"), down = n.to === "down", N = n.lift.length;
396
+ n.lift.forEach((id, i) => {
397
+ const own = lt - (down ? N - 1 - i : i) * gap, f = e(clamp(own / one)), dy = (i + 1) * n.by * (down ? f : -f), o = n.opacity ? clamp(curveAt(n.opacity, own, fps)) : 1;
398
+ if (!dy && o >= 1) return;
399
+ c.sum.item(id, { dy, opacity: o });
400
+ for (const x of [id, ...notesOn.get(id) ?? []]) c.sum.label(x, { dy, opacity: o });
401
+ });
402
+ return;
403
+ }
404
+ if ("camera" in n) return;
405
+ const lasts = n.duration === void 0 ? Math.max(0, end - from) : ms(n.duration, 0);
406
+ if (lt < 0 || lt > lasts) return;
407
+ if ("focus" in n || "highlight" in n) {
408
+ const fade = Math.max(1, ms(n.fade, FADE)), amount = clamp(Math.min(lt / fade, n.duration === void 0 ? 1 : (lasts - lt) / fade));
409
+ const e = easeOf(n.easing, fade, "linear")(amount);
410
+ if ("focus" in n) dim(n.focus, e, c.sum);
411
+ else for (const id of n.highlight) if (c.shown(id)) ring(id, e, c.sum);
412
+ return;
413
+ }
414
+ if ("ants" in n) {
415
+ march(n.ants === true ? [...routes] : n.ants, c, n.speed, n.dash);
416
+ return;
417
+ }
418
+ if ("pulse" in n) {
419
+ const d = 1500, p = n.loop ? turned(a - from, d, c) / d : lt / Math.max(1, n.duration === void 0 ? d : lasts);
420
+ if (p < 0 || p >= 1) return;
421
+ for (const id of n.pulse) if (c.shown(id)) {
422
+ const b = pieces.get(id), rx = b.bbox.w * 0.5 * (0.7 + p * 0.6);
423
+ c.sum.pulses.push({ x: r2(b.foot[0]), y: r2(b.foot[1]), rx: r2(rx), ry: r2(rx * 0.58), opacity: r4(1 - p) });
424
+ }
425
+ return;
426
+ }
427
+ if ("travel" in n) {
428
+ const one = ms(n.each, 1150), hold = ms(n.hold, n.loop ? 700 : 0), period = n.travel.length * one + hold;
429
+ const q = n.loop ? turned(a - from, period, c) : lt, i = Math.floor(q / one);
430
+ if (i >= n.travel.length) return;
431
+ const part = (q - i * one) / one, f = easeOf(n.easing, one, "ease-in-out")(part), group = n.travel[i];
432
+ for (const x of Array.isArray(group) ? group : [group]) {
433
+ const [id, kind] = x.split(":");
434
+ if (c.shown(id)) c.sum.tokens.push({ route: id, at: r4((kind ?? kindOf.get(id)) === "blocked" ? stalled(part) : f), ...kind ? { kind } : {} });
435
+ }
436
+ }
437
+ }
438
+ const stalled = (f) => f < 0.7 ? f / 0.7 : 1 - (f - 0.7) / 0.3 * 0.25;
439
+ const turned = (x, period, c) => c.off ? 0 : (x % period + period) % period;
440
+ const ring = (id, amount, sum) => {
441
+ if (r4(amount) > 0 && outline.has(id)) sum.outlines.push({ id, points: outline.get(id), ...r4(amount) < 1 ? { opacity: r4(amount) } : {} });
442
+ };
443
+ const dim = (f, amount, sum) => {
444
+ for (const id of all) {
445
+ const on = notes.has(id) && !f.lit.includes(id) && !f.always?.includes(id) ? m.notes[id] : void 0, o = mix(1, left(f, on ?? id), amount);
446
+ if (o >= 1) continue;
447
+ if (pieces.has(id)) {
448
+ sum.item(id, { opacity: o });
449
+ sum.label(id, { opacity: o });
450
+ } else if (routes.has(id)) {
451
+ sum.route(id, { opacity: o });
452
+ sum.label(id, { opacity: o });
453
+ } else sum.label(id, { opacity: o });
454
+ }
455
+ };
456
+ const march = (ids, c, speed = ANT_SPEED, dash) => {
457
+ for (const id of ids) {
458
+ const d = antDash(kindOf.get(id) ?? "", { dash, kinds: opts.kinds, still: opts.still });
459
+ if (!d) continue;
460
+ const period = d[0] + d[1], o = turned(speed * c.A / 1e3, period, c);
461
+ c.sum.route(id, { ants: { dash: d, offset: r2(o) } });
462
+ }
463
+ };
464
+ const runLength = (s, rest) => s.loop === true ? Math.max(0, rest) : seqLength(s, "");
465
+ function run(s, t, a, length, c) {
466
+ if (t < 0 || t > length || t === length && length < duration && length > 0 && s.loop) return;
467
+ const rate = s.playbackRate ?? 1, one = inner(s, ""), trim = ms(s.trimBefore, 0);
468
+ const turn = s.loop && one > 0 ? t * rate % one : t * rate, lt = turn + trim, la = a * rate + trim;
469
+ for (const n of s.children ?? []) each(n, lt, la, one + trim, c);
470
+ for (const n of s.layers ?? []) each(n, lt, la, one + trim, c);
471
+ if (s.series) for (const p of layoutSeries(s.series, "").list) run(p.s, lt - p.start, la - p.start, p.end - p.start, c);
472
+ }
473
+ function at(ms0, o = {}) {
474
+ const T = Math.max(0, Math.min(duration, Number.isFinite(ms0) ? ms0 : 0));
475
+ const off = o.ambient === "off" || doc.ambient === "off" && o.ambient === void 0, A = typeof o.ambient === "number" ? o.ambient : T;
476
+ const sum = new Sum(), k = beatIndex(T), f = k < 0 ? 1 : entered(k, T);
477
+ const of = (id, g) => k < 0 ? g(-1, id) : f >= 1 ? g(k, id) : mix(g(k - 1, id), g(k, id), f);
478
+ const shown = (id) => (k < 0 ? folds[0] : folds[k + 1]).get(id) !== false;
479
+ if (managed.size || top.list.some((p) => p.s.focus || p.s.levels)) for (const id of all) {
480
+ const v = of(id, notes.has(id) ? noteShare : share);
481
+ if (v >= 1) continue;
482
+ if (pieces.has(id)) sum.item(id, { opacity: v });
483
+ if (routes.has(id)) sum.route(id, { opacity: v });
484
+ sum.label(id, { opacity: v });
485
+ }
486
+ if (k >= 0) for (const id of looked[looked.length - 1].keys()) {
487
+ const was = looked[k].get(id) ?? "", now = looked[k + 1].get(id) ?? "", v = {};
488
+ if (was && f < 1) v[was] = r4(1 - f);
489
+ if (now) v[now] = r4((v[now] ?? 0) + f);
490
+ for (const n of Object.keys(v)) if (v[n] <= 0) delete v[n];
491
+ if (Object.keys(v).length) sum.variants[id] = v;
492
+ }
493
+ if (k >= 0) {
494
+ const now = new Set(top.list[k].s.highlight ?? []), was = new Set(k > 0 && f < 1 ? top.list[k - 1].s.highlight ?? [] : []);
495
+ for (const id of /* @__PURE__ */ new Set([...was, ...now])) ring(id, mix(was.has(id) ? 1 : 0, now.has(id) ? 1 : 0, f), sum);
496
+ }
497
+ const c = { sum, shown, off, A };
498
+ const ants = k >= 0 ? top.list[k].s.ants : void 0, file = ants === void 0 ? fileAnts : void 0;
499
+ if (ants) march(ants === true ? [...routes] : ants, c);
500
+ else if (file) march(file.ids ?? [...routes], c, file.speed, file.dash);
501
+ top.list.forEach((p, i) => {
502
+ if (T >= p.start && (T < p.end || i === k && T <= p.end)) run(p.s, T - p.start, A - p.start, p.end - p.start, c);
503
+ });
504
+ for (const n of doc.layers ?? []) each(n, T, A, duration, c);
505
+ let view;
506
+ for (const q of cams) {
507
+ if (q.at > T) break;
508
+ const u = q.over <= 0 ? 1 : q.ease(clamp((T - q.at) / q.over)), was = view ?? fit;
509
+ if (u >= 1) {
510
+ view = q.to;
511
+ continue;
512
+ }
513
+ const z0 = was[2], z1 = q.to[2], z = Math.exp(mix(Math.log(z0), Math.log(z1), u)), g = Math.abs(z1 - z0) < 1e-6 ? u : (z - z0) / (z1 - z0);
514
+ const cx = mix(was[0] + was[2] / 2, q.to[0] + q.to[2] / 2, g), cy = mix(was[1] + was[3] / 2, q.to[1] + q.to[3] / 2, g), h = z * fit[3] / fit[2];
515
+ view = [cx - z / 2, cy - h / 2, z, h];
516
+ }
517
+ const cam = view?.map(r2);
518
+ return sum.state(cam && cam.some((v, i) => Math.abs(v - fit[i]) > 5e-3) ? cam : void 0);
519
+ }
520
+ const moving = (n) => {
521
+ if (!n || typeof n !== "object") return false;
522
+ if (Array.isArray(n)) return n.some(moving);
523
+ const o = n;
524
+ if (o.ants !== void 0 && o.ants !== false) return true;
525
+ if (o.loop === true && ("travel" in o || "pulse" in o)) return true;
526
+ return Object.values(o).some(moving);
527
+ };
528
+ const loops = fileAnts !== void 0 || moving(doc.series) || moving(doc.layers);
529
+ const beatAt = (t) => {
530
+ const k = beatIndex(Math.max(0, Math.min(duration, t)));
531
+ return k < 0 ? void 0 : beats[k];
532
+ };
533
+ return {
534
+ ok: true,
535
+ diags,
536
+ duration,
537
+ beats,
538
+ markers,
539
+ ambient: doc.ambient ?? "on",
540
+ loops,
541
+ at,
542
+ beatAt,
543
+ startOf: (n) => starts.get(n) ?? beats.find((b) => b.name === n)?.start,
544
+ cues(t) {
545
+ const b = beatAt(t);
546
+ if (!b) return void 0;
547
+ const T = Math.max(0, Math.min(duration, t));
548
+ return { beat: b.name, index: b.index, of: beats.length, progress: r4(clamp((T - b.start) / Math.max(1, b.end - b.start))), ...b.say ? { say: b.say, ...b.say.chapter !== void 0 ? { chapter: b.say.chapter } : {}, ...b.say.color !== void 0 ? { color: b.say.color } : {} } : {} };
549
+ }
550
+ };
551
+ }
552
+ function settleMs(c) {
553
+ const m = c.mass ?? 1, k = c.stiffness ?? 100, d = c.damping ?? 10, w0 = Math.sqrt(k / m), z = Math.min(1, d / (2 * Math.sqrt(k * m)));
554
+ return Math.ceil(Math.log(200) / (z * w0) * 1e3 * (z >= 1 ? 1.6 : 1));
555
+ }
556
+ function motionOfSteps(scene) {
557
+ const { steps, vis } = scene.steps, full = scene.rendered.viewBox;
558
+ let zoomed = false;
559
+ const m = motionModel(scene), ids = /* @__PURE__ */ new Set([...m.pieces.map((p) => p.id), ...m.routes.map((r) => r.id), ...Object.keys(m.notes)]), has = (id) => ids.has(id);
560
+ const free = Object.keys(m.notes).filter((n) => m.notes[n] === void 0);
561
+ const series = steps.map((st, i) => {
562
+ const show = [], hide = [];
563
+ for (const [id, v] of Object.entries(vis)) {
564
+ if (i === 0 ? !v.includes(true) : v[i - 1] && !v[i]) hide.push(id);
565
+ else if (i > 0 ? v[i] && !v[i - 1] : false) show.push(id);
566
+ }
567
+ const children = [];
568
+ const token = (t) => `${t.route}:${t.kind}`;
569
+ if (st.phases.length) children.push({ travel: st.phases.map((g) => g.length === 1 ? token(g[0]) : g.map(token)), each: 1150, hold: 700, loop: true });
570
+ const pulse = (scene.raw.steps ?? [])[i]?.pulse?.filter((id) => scene.layout.byId.has(id)) ?? [];
571
+ if (pulse.length) children.push({ pulse, loop: true });
572
+ if (st.focus) {
573
+ const [x, y, w, h] = focusView(full, st.focus).map(r2);
574
+ children.push({ camera: { focus: [x, y, w, h] }, duration: 900, easing: "ease-in-out" });
575
+ zoomed = true;
576
+ } else if (zoomed) {
577
+ children.push({ camera: "fit", duration: 900, easing: "ease-in-out" });
578
+ zoomed = false;
579
+ }
580
+ const s = { name: `step-${st.n}`, duration: st.duration, say: { title: st.heading, ...st.caption ? { text: st.caption } : {}, ...st.command ? { code: st.command } : {} } };
581
+ if (show.length) s.show = show;
582
+ if (hide.length) s.hide = hide;
583
+ s.focus = scene.raw.dim === "all" ? { lit: st.lit.filter(has), scope: "all", level: 0.14 } : { lit: st.lit.filter(has), scope: "links", level: 0.12, ...free.length ? { always: free } : {} };
584
+ if (children.length) s.children = children;
585
+ return { sequence: s };
586
+ });
587
+ return { isomotion: 1, ...series.length ? { series } : {} };
588
+ }
589
+ export {
590
+ bindMotion,
591
+ motionOfSteps,
592
+ motionSource
593
+ };