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