@johnoscott/isometric-engine 0.12.6 → 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 (71) hide show
  1. package/EDITOR-GUIDE.md +9 -2
  2. package/README.md +1 -1
  3. package/assets/app.css +6 -0
  4. package/assets/marks.css +2 -0
  5. package/iso-editor.js +194 -184
  6. package/iso-render.js +157 -148
  7. package/iso-systems.js +170 -159
  8. package/js/iso-editor/make-way.js +62 -0
  9. package/js/iso-editor/ui/canvas.js +477 -216
  10. package/js/iso-editor/ui/palette-look.js +53 -0
  11. package/js/iso-editor/ui/setup.js +3 -0
  12. package/js/iso-objects/ants.js +36 -0
  13. package/js/iso-objects/index.js +47 -1
  14. package/js/iso-objects/looks.js +25 -0
  15. package/js/iso-objects/motion-builder.js +130 -0
  16. package/js/iso-objects/motion-doc.js +500 -0
  17. package/js/iso-objects/motion-entry.js +32 -0
  18. package/js/iso-objects/motion.js +635 -0
  19. package/js/iso-objects/resolve.js +238 -0
  20. package/js/iso-objects/scene.js +15 -13
  21. package/js/iso-objects/timeline.js +553 -0
  22. package/js/iso-systems/compile.js +59 -1
  23. package/js/iso-systems/index.js +4 -1
  24. package/js/iso-systems/render.js +20 -0
  25. package/js/iso-systems/schema.json +39 -0
  26. package/js/iso-viewer/page.js +2 -1
  27. package/js/iso-viewer/player.js +23 -34
  28. package/package.json +46 -1
  29. package/src/iso-editor/make-way.ts +89 -0
  30. package/src/iso-editor/ui/app.css +4 -0
  31. package/src/iso-editor/ui/canvas.ts +390 -170
  32. package/src/iso-editor/ui/marks.css +2 -0
  33. package/src/iso-editor/ui/palette-look.ts +51 -0
  34. package/src/iso-editor/ui/setup.ts +8 -0
  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 +14 -1
  38. package/src/iso-objects/looks.ts +21 -0
  39. package/src/iso-objects/motion-builder.ts +79 -0
  40. package/src/iso-objects/motion-doc.ts +361 -0
  41. package/src/iso-objects/motion-entry.ts +12 -0
  42. package/src/iso-objects/motion.ts +616 -0
  43. package/src/iso-objects/resolve.ts +196 -0
  44. package/src/iso-objects/scene.ts +29 -13
  45. package/src/iso-objects/timeline.ts +443 -0
  46. package/src/iso-objects/types.ts +20 -2
  47. package/src/iso-systems/compile.ts +58 -3
  48. package/src/iso-systems/index.ts +4 -1
  49. package/src/iso-systems/render.ts +41 -0
  50. package/src/iso-systems/schema.json +39 -0
  51. package/src/iso-viewer/page.ts +2 -1
  52. package/src/iso-viewer/player.ts +24 -41
  53. package/types/iso-editor/make-way.d.ts +40 -0
  54. package/types/iso-editor/ui/canvas.d.ts +33 -12
  55. package/types/iso-editor/ui/palette-look.d.ts +5 -0
  56. package/types/iso-editor/ui/setup.d.ts +5 -0
  57. package/types/iso-objects/ants.d.ts +33 -0
  58. package/types/iso-objects/compile.d.ts +3 -0
  59. package/types/iso-objects/index.d.ts +14 -1
  60. package/types/iso-objects/looks.d.ts +4 -0
  61. package/types/iso-objects/motion-builder.d.ts +105 -0
  62. package/types/iso-objects/motion-doc.d.ts +787 -0
  63. package/types/iso-objects/motion-entry.d.ts +10 -0
  64. package/types/iso-objects/motion.d.ts +200 -0
  65. package/types/iso-objects/resolve.d.ts +20 -0
  66. package/types/iso-objects/scene.d.ts +11 -0
  67. package/types/iso-objects/timeline.d.ts +46 -0
  68. package/types/iso-objects/types.d.ts +32 -0
  69. package/types/iso-systems/compile.d.ts +20 -3
  70. package/types/iso-systems/index.d.ts +4 -1
  71. package/types/iso-systems/render.d.ts +21 -0
@@ -0,0 +1,635 @@
1
+ import { iso } from "../iso-core/kernel.js";
2
+ import { offscreen } from "./assemble.js";
3
+ import { polyBounds, silhouette } from "./labels.js";
4
+ const ASSEMBLE_STYLES = ["edge", "drop", "rise", "fly", "pop", "scatter", "build", "fade"];
5
+ const DISASSEMBLE_STYLES = ["explode", "lift-off", "sink", "scatter", "fly-out", "collapse", "fade", "reverse"];
6
+ const MOTION_ORDERS = ["by-depth", "by-layer", "by-region", "by-flow", "as-listed", "random"];
7
+ const MOTION_EASINGS = ["linear", "ease", "ease-in", "ease-out", "settle", "spring", "bounce"];
8
+ const r2 = (v) => Math.round(v * 100) / 100 || 0;
9
+ const r4 = (v) => Math.round(v * 1e4) / 1e4 || 0;
10
+ function motionModel(s) {
11
+ const depthOf = (p) => {
12
+ let d = 0;
13
+ for (let a = p.parent; a; a = a.parent) d++;
14
+ return d;
15
+ };
16
+ const inherited = (p) => {
17
+ const chain = [];
18
+ for (let a = p; a; a = a.parent) {
19
+ const m = (a.region ?? a.node)?.motion;
20
+ if (m) chain.unshift(m);
21
+ }
22
+ return chain.length ? Object.assign({}, ...chain) : void 0;
23
+ };
24
+ const AIR = new Set(s.layout.items.filter((p) => p.region && s.layout.catalog && s.rendered.svg.includes(`class="tunnel" data-id="${p.id}"`)).map((p) => p.id));
25
+ return {
26
+ viewBox: s.rendered.viewBox,
27
+ pieces: s.layout.items.map((p) => {
28
+ const b = p.box, c = iso((b.x0 + b.x1) / 2, (b.y0 + b.y1) / 2, b.z0), m = inherited(p);
29
+ return {
30
+ id: p.id,
31
+ kind: p.type,
32
+ ...p.parent ? { parent: p.parent.id } : {},
33
+ depth: depthOf(p),
34
+ layer: AIR.has(p.id) ? 2 : p.type === "region" ? 0 : 1,
35
+ key: r2(b.x0 + b.y0 + b.z0 * 0.01),
36
+ bbox: polyBounds(silhouette(b)),
37
+ foot: [r2(c[0]), r2(c[1])],
38
+ base: [iso(b.x0, b.y1, b.z0), iso(b.x1, b.y1, b.z0), iso(b.x1, b.y0, b.z0)].map((q) => [r2(q[0]), r2(q[1])]),
39
+ ...m ? { motion: m } : {}
40
+ };
41
+ }),
42
+ routes: s.routes.filter((r) => s.rendered.paths[r.id]).map((r) => ({ id: r.id, path: s.rendered.paths[r.id], ...ends(r.raw) })),
43
+ notes: Object.fromEntries((s.raw.notes ?? []).map((n, i) => [`${s.id}-note-${i}`, n.on])),
44
+ intro: s.rendered.assembly
45
+ };
46
+ }
47
+ const endId = (v) => typeof v === "string" ? v.split(".")[0] : void 0;
48
+ const ends = (r) => {
49
+ const from = endId(r.from), to = endId(r.to);
50
+ return { ...from ? { from } : {}, ...to ? { to } : {} };
51
+ };
52
+ const bezier = (x1, y1, x2, y2) => (x) => {
53
+ if (x <= 0) return 0;
54
+ if (x >= 1) return 1;
55
+ const at = (a, b, t) => 3 * a * t * (1 - t) ** 2 + 3 * b * t * t * (1 - t) + t ** 3;
56
+ let lo = 0, hi = 1;
57
+ for (let i = 0; i < 40; i++) {
58
+ const m = (lo + hi) / 2;
59
+ if (at(x1, x2, m) < x) lo = m;
60
+ else hi = m;
61
+ }
62
+ return at(y1, y2, (lo + hi) / 2);
63
+ };
64
+ const CURVES = {
65
+ linear: (f) => f,
66
+ ease: bezier(0.42, 0, 0.58, 1),
67
+ "ease-in": bezier(0.42, 0, 1, 1),
68
+ "ease-out": bezier(0, 0, 0.58, 1),
69
+ // the page player's own: quick in, a long settle
70
+ settle: bezier(0.16, 0.84, 0.3, 1),
71
+ spring: (f) => f <= 0 ? 0 : f >= 1 ? 1 : 1 - Math.exp(-6 * f) * Math.cos(9.5 * f) * (1 - f),
72
+ bounce: (f) => {
73
+ if (f <= 0) return 0;
74
+ if (f >= 1) return 1;
75
+ const n = 7.5625, d = 2.75;
76
+ if (f < 1 / d) return n * f * f;
77
+ if (f < 2 / d) return n * (f - 1.5 / d) ** 2 + 0.75;
78
+ if (f < 2.5 / d) return n * (f - 2.25 / d) ** 2 + 0.9375;
79
+ return n * (f - 2.625 / d) ** 2 + 0.984375;
80
+ }
81
+ };
82
+ function stream(seed) {
83
+ let a = seed >>> 0;
84
+ return () => {
85
+ a = a + 1831565813 >>> 0;
86
+ let t = a;
87
+ t = Math.imul(t ^ t >>> 15, t | 1);
88
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
89
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
90
+ };
91
+ }
92
+ const hashOf = (s) => {
93
+ let h = 2166136261;
94
+ for (let i = 0; i < s.length; i++) {
95
+ h ^= s.charCodeAt(i);
96
+ h = Math.imul(h, 16777619);
97
+ }
98
+ return h >>> 0;
99
+ };
100
+ const clamp = (f) => Math.max(0, Math.min(1, f));
101
+ const MARGIN = 24;
102
+ function side(b, vb, to) {
103
+ const [x, y, w, h] = vb;
104
+ if (to === "left") return [-(b.x + b.w - x) - MARGIN, 0];
105
+ if (to === "right") return [x + w - b.x + MARGIN, 0];
106
+ if (to === "top") return [0, -(b.y + b.h - y) - MARGIN];
107
+ return [0, y + h - b.y + MARGIN];
108
+ }
109
+ function motionOrder(m, order, o = {}) {
110
+ const P = m.pieces, byId = new Map(P.map((p) => [p.id, p]));
111
+ const back = (a, b) => a.depth - b.depth || a.key - b.key;
112
+ if (order === "as-listed") return [...P];
113
+ if (order === "random") {
114
+ const next2 = stream(o.seed ?? 1);
115
+ return P.map((p) => [next2(), p]).sort((a, b) => a[0] - b[0]).map((x) => x[1]);
116
+ }
117
+ if (order === "by-layer") return [...P].sort((a, b) => a.layer - b.layer || back(a, b));
118
+ if (order === "by-depth") return [...P].sort((a, b) => a.key - b.key || a.depth - b.depth);
119
+ const kids = /* @__PURE__ */ new Map();
120
+ for (const p of [...P].sort(back)) {
121
+ const l = kids.get(p.parent) ?? [];
122
+ l.push(p);
123
+ kids.set(p.parent, l);
124
+ }
125
+ const out = [], seen = /* @__PURE__ */ new Set();
126
+ const add = (p) => {
127
+ if (seen.has(p.id)) return;
128
+ const up = p.parent ? byId.get(p.parent) : void 0;
129
+ if (up) add(up);
130
+ seen.add(p.id);
131
+ out.push(p);
132
+ };
133
+ if (order === "by-region") {
134
+ const walk = (id) => {
135
+ for (const p of kids.get(id) ?? []) {
136
+ add(p);
137
+ walk(p.id);
138
+ }
139
+ };
140
+ walk();
141
+ return out;
142
+ }
143
+ const next = /* @__PURE__ */ new Map(), into = /* @__PURE__ */ new Set();
144
+ for (const r of m.routes) if (r.from && r.to) {
145
+ next.set(r.from, [...next.get(r.from) ?? [], r.to]);
146
+ into.add(r.to);
147
+ }
148
+ const start = o.source ?? m.routes.find((r) => r.from && !into.has(r.from))?.from ?? m.routes[0]?.from;
149
+ const queue = start ? [start] : [], queued = new Set(queue);
150
+ while (queue.length) {
151
+ const id = queue.shift(), p = byId.get(id);
152
+ if (p) add(p);
153
+ for (const n of next.get(id) ?? []) if (!queued.has(n)) {
154
+ queued.add(n);
155
+ queue.push(n);
156
+ }
157
+ }
158
+ for (const p of [...P].sort(back)) add(p);
159
+ return out;
160
+ }
161
+ const STYLE_EASE = {
162
+ edge: "settle",
163
+ drop: "settle",
164
+ rise: "ease-out",
165
+ fly: "settle",
166
+ pop: "spring",
167
+ scatter: "settle",
168
+ build: "ease-out",
169
+ fade: "linear",
170
+ explode: "ease-in",
171
+ "lift-off": "ease-in",
172
+ sink: "ease-in",
173
+ "fly-out": "ease-in",
174
+ collapse: "ease-in"
175
+ };
176
+ const OUT_AS = { "lift-off": "lift", sink: "rise", scatter: "scatter", "fly-out": "edge", fade: "fade", explode: "explode", collapse: "collapse" };
177
+ function planOf(m, kind, opts = {}) {
178
+ const seed = opts.seed ?? 1, leaving = kind === "disassemble";
179
+ const reverse = leaving && opts.style === "reverse";
180
+ const own = (p) => {
181
+ if (!leaving) return p.motion?.style ?? opts.style ?? "edge";
182
+ const out = p.motion?.out ?? opts.style ?? "fly-out";
183
+ return out === "reverse" ? p.motion?.style ?? opts.of ?? "edge" : OUT_AS[out];
184
+ };
185
+ const named = (p) => leaving ? p.motion?.out ?? opts.style ?? "fly-out" : own(p);
186
+ const asIntro = !leaving && !opts.order && opts.stagger === void 0 && opts.piece === void 0 && opts.overlap === void 0 && (opts.style ?? "edge") === "edge";
187
+ const order = opts.order ?? (opts.style === "build" ? "by-layer" : "by-depth");
188
+ const timed = [];
189
+ let routes, labels;
190
+ const mk = (p, delay, dur) => {
191
+ const style = own(p), e = p.motion?.easing ?? opts.easing ?? STYLE_EASE[leaving && named(p) !== "reverse" ? named(p) : style] ?? "ease";
192
+ return { p, delay: delay + (p.motion?.delay ?? 0), dur: Math.max(1, p.motion?.duration ?? dur), style, curve: CURVES[e], from: p.motion?.from ?? opts.from ?? "left" };
193
+ };
194
+ if (asIntro) {
195
+ const by = new Map(m.pieces.map((p) => [p.id, p]));
196
+ for (const a of m.intro.pieces) {
197
+ const p = by.get(a.id);
198
+ if (p) timed.push(mk(p, a.delay, a.dur));
199
+ }
200
+ routes = m.intro.routes.map((r) => ({ ...r }));
201
+ labels = { ...m.intro.labels };
202
+ } else {
203
+ const seq = motionOrder(m, order, { seed, source: opts.source });
204
+ const dur = opts.piece ?? 600, step = opts.stagger ?? Math.min(70, 900 / Math.max(1, seq.length)), over = clamp(opts.overlap ?? 0.5);
205
+ const list = reverse || leaving ? [...seq].reverse() : seq;
206
+ const rStep = Math.min(30, 300 / Math.max(1, m.routes.length)), rDur = 400, lDur = 400;
207
+ if (!leaving) {
208
+ list.forEach((p, i) => timed.push(mk(p, i * step, dur)));
209
+ const pEnd = Math.max(0, ...timed.map((x) => x.delay + x.dur));
210
+ const rAt = Math.max(0, pEnd - over * dur);
211
+ routes = m.routes.map((r, i) => ({ id: r.id, delay: rAt + i * rStep, dur: rDur }));
212
+ const rEnd = Math.max(rAt, ...routes.map((r) => r.delay + r.dur));
213
+ labels = { delay: Math.max(0, rEnd - over * lDur), dur: lDur };
214
+ } else {
215
+ labels = { delay: 0, dur: lDur };
216
+ const rAt = lDur * (1 - over);
217
+ routes = [...m.routes].reverse().map((r, i) => ({ id: r.id, delay: rAt + i * rStep, dur: rDur }));
218
+ const rEnd = Math.max(rAt, ...routes.map((r) => r.delay + r.dur));
219
+ const pAt = Math.max(0, rEnd - over * rDur);
220
+ list.forEach((p, i) => timed.push(mk(p, pAt + i * step, dur)));
221
+ }
222
+ }
223
+ const pulse = opts.pulses && m.routes.length ? 700 : 0;
224
+ if (pulse && leaving) {
225
+ for (const x of [...timed, ...routes, labels]) x.delay += pulse;
226
+ }
227
+ let total = Math.max(1, ...timed.map((x) => x.delay + x.dur), ...routes.map((r) => r.delay + r.dur), labels.delay + labels.dur);
228
+ const pulseAt = leaving ? 0 : total;
229
+ if (pulse && !leaving) total += pulse;
230
+ const k = opts.duration !== void 0 ? Math.max(1, opts.duration) / total : 1;
231
+ for (const x of [...timed, ...routes, labels]) {
232
+ x.delay = r2(x.delay * k);
233
+ x.dur = Math.max(0.01, r2(x.dur * k));
234
+ }
235
+ const duration = opts.duration !== void 0 ? Math.max(1, opts.duration) : Math.max(1, r2(total));
236
+ const byPiece = new Map(timed.map((x) => [x.p.id, x])), byRoute = new Map(routes.map((r) => [r.id, r]));
237
+ const [vx, vy, vw, vh] = m.viewBox, middle = [vx + vw / 2, vy + vh / 2];
238
+ const middleOf = (b) => [b.x + b.w / 2, b.y + b.h / 2];
239
+ const pointOf = (p, what) => {
240
+ const pt = opts.point;
241
+ if (Array.isArray(pt)) return pt;
242
+ if (pt === "region" || what === "collapse" && pt === void 0 && p.parent) {
243
+ const up = m.pieces.find((x) => x.id === p.parent);
244
+ return up ? up.foot : middle;
245
+ }
246
+ if (typeof pt === "string") {
247
+ const at2 = m.pieces.find((x) => x.id === pt);
248
+ if (at2) return at2.foot;
249
+ }
250
+ return middle;
251
+ };
252
+ function piece(x, e, f) {
253
+ const { p } = x, b = p.bbox, away = 1 - e;
254
+ const move = (dx, dy) => ({ dx: r2(dx * away), dy: r2(dy * away) });
255
+ switch (x.style) {
256
+ case "drop":
257
+ return move(...side(b, m.viewBox, "top"));
258
+ case "lift":
259
+ return move(...side(b, m.viewBox, "top"));
260
+ case "fly":
261
+ return move(...side(b, m.viewBox, x.from));
262
+ case "rise": {
263
+ const dy = b.h * away, [l, fr, r] = p.base, top = b.y - b.h - 4;
264
+ return { ...move(0, b.h), clip: [[l[0], l[1] - dy], [fr[0], fr[1] - dy], [r[0], r[1] - dy], [r[0] + 2, top - dy], [l[0] - 2, top - dy]].map((q) => [r2(q[0]), r2(q[1])]) };
265
+ }
266
+ case "pop":
267
+ return { scale: r4(Math.max(0, e)), about: p.foot, opacity: r4(clamp(f * 4)) };
268
+ case "scatter": {
269
+ const next = stream((seed ^ hashOf(p.id)) >>> 0), a = next() * Math.PI * 2, d = 0.35 + next() * 0.65;
270
+ return { ...move(Math.cos(a) * vw * d * 0.6, Math.sin(a) * vh * d * 0.6), opacity: r4(clamp(e * 1.6)) };
271
+ }
272
+ case "build":
273
+ return { ...move(0, -18), opacity: r4(clamp(e)) };
274
+ case "fade":
275
+ return { opacity: r4(clamp(e)) };
276
+ case "explode": {
277
+ const c = pointOf(p, "explode"), q = middleOf(b), dx = q[0] - c[0], dy = q[1] - c[1], l = Math.hypot(dx, dy);
278
+ const next = stream((seed ^ hashOf(p.id)) >>> 0), a = next() * Math.PI * 2, u = l > 1 ? [dx / l, dy / l] : [Math.cos(a), Math.sin(a)];
279
+ const far = Math.hypot(vw, vh) + Math.max(b.w, b.h);
280
+ return move(u[0] * far, u[1] * far);
281
+ }
282
+ case "collapse": {
283
+ const c = pointOf(p, "collapse");
284
+ return { ...move(c[0] - p.foot[0], c[1] - p.foot[1]), scale: r4(Math.max(0, e)), about: p.foot, opacity: r4(clamp(e * 3)) };
285
+ }
286
+ default: {
287
+ const o = offscreen(b, m.viewBox);
288
+ return move(o.dx, o.dy);
289
+ }
290
+ }
291
+ }
292
+ const share = (t, delay, dur) => {
293
+ const f = clamp((t - delay) / dur);
294
+ return leaving ? 1 - f : f;
295
+ };
296
+ const HIDDEN = { opacity: 0 };
297
+ const connectors = opts.connectors ?? "draw", labelling = opts.labels ?? "fade";
298
+ const labelled = [...m.pieces.map((p) => p.id), ...m.routes.map((r) => r.id), ...Object.keys(m.notes)];
299
+ function at(t0) {
300
+ const t = Math.max(0, Math.min(duration, Number.isFinite(t0) ? t0 : 0));
301
+ if (leaving ? t <= 0 : t >= duration) return {};
302
+ const empty = leaving ? t >= duration : t <= 0;
303
+ const items = {}, routeStates = {}, labelStates = {};
304
+ for (const x of timed) {
305
+ const f = empty ? 0 : share(t, x.delay, x.dur);
306
+ if (f <= 0) items[x.p.id] = HIDDEN;
307
+ else if (f < 1) items[x.p.id] = piece(x, leaving ? 1 - x.curve(1 - f) : x.curve(f), f);
308
+ }
309
+ for (const r of m.routes) {
310
+ const w = byRoute.get(r.id), f = empty || !w ? 0 : share(t, w.delay, w.dur);
311
+ if (f <= 0) routeStates[r.id] = { opacity: 0, drawn: 0 };
312
+ else if (f < 1) routeStates[r.id] = connectors === "fade" ? { opacity: r4(f) } : connectors === "grow" ? { drawn: r4(f), by: "grow" } : { drawn: r4(CURVES["ease-out"](f)) };
313
+ }
314
+ const lf = empty ? 0 : share(t, labels.delay, labels.dur);
315
+ const all = lf <= 0 ? HIDDEN : lf >= 1 ? void 0 : labelling === "type" ? { typed: r4(lf) } : { opacity: r4(lf) };
316
+ for (const id of labelled) {
317
+ let c = all;
318
+ if (labelling === "with-piece") {
319
+ const on = Object.hasOwn(m.notes, id) ? m.notes[id] : id, w = on === void 0 ? void 0 : byPiece.get(on) ?? byRoute.get(on);
320
+ if (w) {
321
+ const f = empty ? 0 : share(t, w.delay, w.dur);
322
+ c = f <= 0 ? HIDDEN : f >= 1 ? void 0 : { opacity: r4(f) };
323
+ }
324
+ }
325
+ if (c) labelStates[id] = c;
326
+ }
327
+ const tokens = [];
328
+ if (pulse && !empty) {
329
+ const f = (t - pulseAt * k) / (pulse * k);
330
+ if (f > 0 && f < 1) for (const r of m.routes) tokens.push({ route: r.id, at: r4(CURVES.ease(f)) });
331
+ }
332
+ const some = (o) => Object.keys(o).length > 0;
333
+ return {
334
+ ...some(items) ? { items } : {},
335
+ ...some(labelStates) ? { labels: labelStates } : {},
336
+ ...some(routeStates) ? { routes: routeStates } : {},
337
+ ...lf < 1 ? { marks: { opacity: r4(Math.max(0, lf)) } } : {},
338
+ ...tokens.length ? { tokens } : {}
339
+ };
340
+ }
341
+ return { kind, duration, pieces: timed.map((x) => ({ id: x.p.id, delay: x.delay, dur: x.dur, style: named(x.p) })), routes, labels, at };
342
+ }
343
+ const n2 = (v) => {
344
+ const s = v.toFixed(2);
345
+ return s === "-0.00" ? "0" : String(Number(s));
346
+ };
347
+ const scaleAt = (c, s) => `translate(${n2(c[0])} ${n2(c[1])}) scale(${Number(s.toFixed(4))}) translate(${n2(-c[0])} ${n2(-c[1])})`;
348
+ const pointsOf = (d) => [...d.matchAll(/(-?[\d.]+),(-?[\d.]+)/g)].map((m) => [Number(m[1]), Number(m[2])]);
349
+ const lengthOf = (pts) => pts.slice(1).reduce((n, p, i) => n + Math.hypot(p[0] - pts[i][0], p[1] - pts[i][1]), 0);
350
+ function alongPath(d, f) {
351
+ const pts = pointsOf(d);
352
+ let left = lengthOf(pts) * f;
353
+ for (let i = 0; i + 1 < pts.length; i++) {
354
+ const seg = Math.hypot(pts[i + 1][0] - pts[i][0], pts[i + 1][1] - pts[i][1]);
355
+ if (left <= seg && seg > 0) {
356
+ const t = left / seg;
357
+ return [pts[i][0] + (pts[i + 1][0] - pts[i][0]) * t, pts[i][1] + (pts[i + 1][1] - pts[i][1]) * t];
358
+ }
359
+ left -= seg;
360
+ }
361
+ return pts[pts.length - 1] ?? [0, 0];
362
+ }
363
+ function partOf(d, f) {
364
+ const pts = pointsOf(d), end = alongPath(d, f), out = [];
365
+ let left = lengthOf(pts) * f;
366
+ for (let i = 0; i < pts.length; i++) {
367
+ out.push(pts[i]);
368
+ const seg = i + 1 < pts.length ? Math.hypot(pts[i + 1][0] - pts[i][0], pts[i + 1][1] - pts[i][1]) : Infinity;
369
+ if (left <= seg) break;
370
+ left -= seg;
371
+ }
372
+ return "M" + [...out, end].map((q) => `${n2(q[0])},${n2(q[1])}`).join(" L");
373
+ }
374
+ const num = (v) => String(Number(v.toFixed(4)));
375
+ function typed(s, f) {
376
+ const ch = s.match(/&[^;\s]+;|[\s\S]/gu) ?? [];
377
+ return ch.slice(0, Math.round(ch.length * f)).join("");
378
+ }
379
+ function drawnItem(i) {
380
+ const t = [];
381
+ if (i.dx || i.dy) t.push(`translate(${n2(i.dx ?? 0)} ${n2(i.dy ?? 0)})`);
382
+ if (i.scale !== void 0 && i.scale !== 1) t.push(scaleAt(i.about ?? [0, 0], i.scale));
383
+ return { attrs: { ...i.opacity === void 0 || i.opacity === 1 ? {} : { opacity: num(i.opacity) }, ...t.length ? { transform: t.join(" ") } : {} }, clip: i.clip };
384
+ }
385
+ function drawnRoute(r, d) {
386
+ const out = { attrs: r.opacity === void 0 || r.opacity === 1 ? {} : { opacity: num(r.opacity) } };
387
+ if (r.drawn !== void 0 && r.drawn < 1 && d !== void 0) {
388
+ const f = clamp(r.drawn), len = lengthOf(pointsOf(d));
389
+ out.path = r.by === "grow" ? { d: partOf(d, f), head: false } : { style: `stroke-dasharray:${n2(len)} ${n2(len)};stroke-dashoffset:${n2(len * (1 - f))}`, head: false };
390
+ } else if (r.ants) out.path = { style: `stroke-dasharray:${n2(r.ants.dash[0])} ${n2(r.ants.dash[1])};stroke-dashoffset:${n2(-r.ants.offset)}`, head: true };
391
+ return out;
392
+ }
393
+ function wrapperState(S, what, id, part, pathOf) {
394
+ if (part === "label" || part === "note") {
395
+ const l = S.labels && Object.hasOwn(S.labels, id) ? S.labels[id] : void 0;
396
+ return l && { attrs: { ...l.opacity === void 0 || l.opacity === 1 ? {} : { opacity: num(l.opacity) }, ...l.dx || l.dy ? { transform: `translate(${n2(l.dx ?? 0)} ${n2(l.dy ?? 0)})` } : {} }, ...l.typed === void 0 || l.typed >= 1 ? {} : { typed: l.typed } };
397
+ }
398
+ if (what === "route") {
399
+ const r = S.routes && Object.hasOwn(S.routes, id) ? S.routes[id] : void 0;
400
+ return r && drawnRoute(r, pathOf(id));
401
+ }
402
+ const i = S.items && Object.hasOwn(S.items, id) ? S.items[id] : void 0;
403
+ return i && drawnItem(i);
404
+ }
405
+ function overlay(S, pathOf, inks) {
406
+ const out = [];
407
+ for (const o of S.outlines ?? []) out.push(["polygon", { points: o.points.map((q) => q.map(n2).join(",")).join(" "), class: "ring", fill: "none", "stroke-width": "2.5", "stroke-linejoin": "round", ...o.opacity === void 0 ? {} : { opacity: num(o.opacity) }, ...inks ? { stroke: o.color ?? inks.control } : { style: `stroke:${o.color ?? "var(--ring,var(--control,currentColor))"}` } }]);
408
+ for (const p of S.pulses ?? []) out.push(["ellipse", { cx: n2(p.x), cy: n2(p.y), rx: n2(p.rx), ry: n2(p.ry), class: "ring", fill: "none", "stroke-width": "2.5", ...p.opacity === void 0 ? {} : { opacity: num(p.opacity) }, ...inks ? { stroke: p.color ?? inks.control } : { style: `stroke:${p.color ?? "currentColor"}` } }]);
409
+ for (const k of S.tokens ?? []) {
410
+ const d = pathOf(k.route);
411
+ if (d === void 0) continue;
412
+ const [x, y] = alongPath(d, clamp(k.at));
413
+ out.push(["circle", { cx: n2(x), cy: n2(y), r: "7", class: "tok", ...inks ? { fill: k.color ?? (k.kind !== void 0 ? inks.kinds?.[k.kind] : void 0) ?? inks.control, stroke: inks.surface, "stroke-width": "2.5" } : { style: `fill:${k.color ?? (k.kind !== void 0 ? `var(--${k.kind},var(--control,currentColor))` : "var(--control,currentColor)")}` } }]);
414
+ }
415
+ return out;
416
+ }
417
+ const isStill = (S) => Object.keys(S).every((k) => k === "morph" || S[k] === void 0);
418
+ const MARK_LAYERS = /* @__PURE__ */ new Set(["marks", "badges"]);
419
+ const attrText = (v) => v.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
420
+ function insides(svg) {
421
+ const out = /* @__PURE__ */ new Map(), open = [];
422
+ for (const m of svg.matchAll(/<(\/?)([a-zA-Z]+)((?:\s+[\w:-]+="[^"]*")*)\s*(\/?)>/g)) {
423
+ if (m[1]) {
424
+ const o = open.pop();
425
+ if (o?.key !== void 0) out.set(o.key, [o.from, m.index]);
426
+ continue;
427
+ }
428
+ if (m[4]) continue;
429
+ const id = /\sdata-item="([^"]*)"/.exec(m[3])?.[1], part = /\sdata-part="([^"]*)"/.exec(m[3])?.[1];
430
+ open.push({ ...open.length === 2 && id !== void 0 && part !== void 0 ? { key: `${unescape(id)}
431
+ ${part}` } : {}, from: m.index + m[0].length });
432
+ }
433
+ return out;
434
+ }
435
+ const lookCache = /* @__PURE__ */ new WeakMap();
436
+ function lookParts(r, name) {
437
+ const all = lookCache.get(r) ?? /* @__PURE__ */ new Map();
438
+ lookCache.set(r, all);
439
+ let parts = all.get(name);
440
+ if (!parts) {
441
+ all.set(name, parts = /* @__PURE__ */ new Map());
442
+ const v = r.variants?.[name], own = insides(r.svg);
443
+ if (v !== void 0) for (const [key, [a, b]] of insides(v)) {
444
+ const o = own.get(key);
445
+ if (o && r.looks?.[key.split("\n")[0]]?.includes(name) && r.svg.slice(...o) !== v.slice(a, b)) parts.set(key, v.slice(a, b));
446
+ }
447
+ }
448
+ return parts;
449
+ }
450
+ function mixOf(r, id, part, want) {
451
+ const looks = [];
452
+ for (const name of r.looks?.[id] ?? []) {
453
+ const a = clamp(want[name] ?? 0), inside = a > 0 ? lookParts(r, name).get(`${id}
454
+ ${part}`) : void 0;
455
+ if (inside !== void 0) looks.push([inside, a]);
456
+ }
457
+ return looks.length ? { own: clamp(1 - looks.reduce((n, [, a]) => n + a, 0)), looks } : void 0;
458
+ }
459
+ const layers = (own, m) => (m.own > 0 ? `<g opacity="${num(m.own)}">${own}</g>` : "") + m.looks.map(([x, a]) => `<g opacity="${num(a)}">${x}</g>`).join("");
460
+ function withLooks(r, want) {
461
+ let out = "", at = 0;
462
+ for (const [key, [a, b]] of [...insides(r.svg)].sort((x, y) => x[1][0] - y[1][0])) {
463
+ const [id, part] = key.split("\n"), m = Object.hasOwn(want, id) ? mixOf(r, id, part, want[id]) : void 0;
464
+ if (!m) continue;
465
+ out += r.svg.slice(at, a) + layers(r.svg.slice(a, b), m);
466
+ at = b;
467
+ }
468
+ return out + r.svg.slice(at);
469
+ }
470
+ function svgAt(scene, S) {
471
+ if (isStill(S)) return scene.rendered.svg;
472
+ const svg = S.variants ? withLooks(scene.rendered, S.variants) : scene.rendered.svg;
473
+ const pathOf = (id) => Object.hasOwn(scene.rendered.paths, id) ? scene.rendered.paths[id] : void 0;
474
+ let depth = 0, layer = "", clips = "", nClips = 0;
475
+ let inside;
476
+ let out = svg.replace(/<(\/?)([a-zA-Z]+)((?:\s+[\w:-]+="[^"]*")*)\s*(\/?)>|([^<]+)/g, (m, close, name, attrs, self, text) => {
477
+ if (text !== void 0) return inside?.w.typed === void 0 ? m : typed(text, inside.w.typed);
478
+ if (close) {
479
+ depth--;
480
+ if (inside && depth === inside.at) inside = void 0;
481
+ if (depth === 1) layer = "";
482
+ return m;
483
+ }
484
+ const at = depth;
485
+ if (!self) depth++;
486
+ const a = {};
487
+ for (const [, k, v] of attrs.matchAll(/([\w:-]+)="([^"]*)"/g)) a[k] = v;
488
+ if (at === 0) {
489
+ if (!S.camera) return m;
490
+ a.viewBox = S.camera.map(n2).join(" ");
491
+ } else if (at === 1) {
492
+ layer = name === "g" && a.class?.startsWith("g-") ? a.class.slice(2) : "";
493
+ if (MARK_LAYERS.has(layer) && S.marks && S.marks.opacity !== 1) a.opacity = num(S.marks.opacity);
494
+ else return m;
495
+ } else if (at === 2 && a["data-part"] !== void 0) {
496
+ const what = a["data-route"] === void 0 ? "item" : "route", id = unescape(a["data-route"] ?? a["data-item"] ?? "");
497
+ const w = wrapperState(S, what, id, a["data-part"], pathOf);
498
+ if (!w) return m;
499
+ for (const [k, v] of Object.entries(w.attrs)) a[k] = attrText(v);
500
+ if (w.clip) {
501
+ const c = `${scene.id}-mc${nClips++}`;
502
+ clips += `<clipPath id="${c}"><polygon points="${w.clip.map((q) => q.join(",")).join(" ")}"/></clipPath>`;
503
+ a["clip-path"] = `url(#${c})`;
504
+ }
505
+ if (!self && (w.path || w.typed !== void 0)) inside = { at, w };
506
+ if (!Object.keys(w.attrs).length && !w.clip) return m;
507
+ } else if (inside?.w.path && name === "path" && a.class?.split(" ")[0] === "flow") {
508
+ const p = inside.w.path;
509
+ if (p.d !== void 0) a.d = p.d;
510
+ if (p.style !== void 0) a.style = p.style;
511
+ if (!p.head) delete a["marker-end"];
512
+ } else return m;
513
+ return `<${name}${Object.entries(a).map(([k, v]) => ` ${k}="${v}"`).join("")}${self ? "/" : ""}>`;
514
+ });
515
+ if (clips) out = out.replace(/<defs>|<defs\/>/, (d) => d === "<defs/>" ? `<defs>${clips}</defs>` : d + clips);
516
+ const over = overlay(S, pathOf, scene.rendered.inks);
517
+ if (over.length) out = out.replace('<g class="g-anim">', (g) => g + over.map(([n, at]) => `<${n}${Object.entries(at).map(([k, v]) => ` ${k}="${attrText(v)}"`).join("")}/>`).join(""));
518
+ return out;
519
+ }
520
+ const unescape = (s) => s.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&amp;/g, "&");
521
+ const keptOf = /* @__PURE__ */ new WeakMap();
522
+ function paint(svg, S, rendered) {
523
+ const was = keptOf.get(svg);
524
+ if (was) {
525
+ for (const [el, kids] of was.mixed) el.replaceChildren(...kids);
526
+ for (const [el, attrs] of was.attrs) for (const [k, v] of attrs) {
527
+ if (v === null) el.removeAttribute(k);
528
+ else el.setAttribute(k, v);
529
+ }
530
+ for (const [n, up, next] of was.gone.reverse()) up.insertBefore(n, next);
531
+ for (const [n, t] of was.texts) n.data = t;
532
+ for (const e of was.made) e.remove();
533
+ keptOf.delete(svg);
534
+ }
535
+ if (isStill(S)) return;
536
+ const kept = { attrs: /* @__PURE__ */ new Map(), texts: /* @__PURE__ */ new Map(), gone: [], made: [], mixed: [] };
537
+ keptOf.set(svg, kept);
538
+ const set = (el, k, v) => {
539
+ const m = kept.attrs.get(el) ?? /* @__PURE__ */ new Map();
540
+ if (!m.has(k)) m.set(k, el.getAttribute(k));
541
+ kept.attrs.set(el, m);
542
+ if (v === null) el.removeAttribute(k);
543
+ else el.setAttribute(k, v);
544
+ };
545
+ const doc = svg.ownerDocument;
546
+ const make = (name, attrs, into) => {
547
+ const e = doc.createElementNS("http://www.w3.org/2000/svg", name);
548
+ for (const [k, v] of Object.entries(attrs)) e.setAttribute(k, v);
549
+ into.append(e);
550
+ kept.made.push(e);
551
+ return e;
552
+ };
553
+ const layers2 = Array.from(svg.children), layer = (name) => layers2.find((g) => g.getAttribute("class") === `g-${name}`);
554
+ const paths = /* @__PURE__ */ new Map();
555
+ for (const w of Array.from(layer("flows")?.children ?? [])) {
556
+ const id = w.getAttribute("data-route"), d = w.querySelector("path.flow")?.getAttribute("d");
557
+ if (id !== null && d) paths.set(id, d);
558
+ }
559
+ const pathOf = (id) => paths.get(id);
560
+ if (S.camera) set(svg, "viewBox", S.camera.map(n2).join(" "));
561
+ for (const name of MARK_LAYERS) {
562
+ const g = layer(name);
563
+ if (g && S.marks && S.marks.opacity !== 1) set(g, "opacity", num(S.marks.opacity));
564
+ }
565
+ let nClips = 0;
566
+ const defs = layers2.find((e) => e.localName === "defs"), first = defs?.firstChild ?? null, id0 = svg.getAttribute("data-scene") ?? "scene";
567
+ for (const g of layers2) for (const el of Array.from(g.children)) {
568
+ const part = el.getAttribute("data-part");
569
+ if (part === null) continue;
570
+ const rid = el.getAttribute("data-route");
571
+ const iid = rid === null ? el.getAttribute("data-item") ?? "" : "", mix = rendered && S.variants && Object.hasOwn(S.variants, iid) ? mixOf(rendered, iid, part, S.variants[iid]) : void 0;
572
+ if (mix) {
573
+ const own = Array.from(el.childNodes), put = (a, kids) => {
574
+ const g2 = doc.createElementNS("http://www.w3.org/2000/svg", "g");
575
+ g2.setAttribute("opacity", num(a));
576
+ g2.append(...kids);
577
+ return g2;
578
+ };
579
+ const inside = (text) => Array.from(doc.importNode(new DOMParser().parseFromString(`<svg xmlns="http://www.w3.org/2000/svg">${text}</svg>`, "image/svg+xml").documentElement, true).childNodes);
580
+ kept.mixed.push([el, own]);
581
+ el.replaceChildren(...mix.own > 0 ? [put(mix.own, own)] : [], ...mix.looks.map(([x, a]) => put(a, inside(x))));
582
+ }
583
+ const w = wrapperState(S, rid === null ? "item" : "route", rid ?? el.getAttribute("data-item") ?? "", part, pathOf);
584
+ if (!w) continue;
585
+ for (const [k, v] of Object.entries(w.attrs)) set(el, k, v);
586
+ if (w.clip && defs) {
587
+ const c = make("clipPath", { id: `${id0}-mc${nClips++}` }, defs);
588
+ defs.insertBefore(c, first);
589
+ const p = doc.createElementNS("http://www.w3.org/2000/svg", "polygon");
590
+ p.setAttribute("points", w.clip.map((q) => q.join(",")).join(" "));
591
+ c.append(p);
592
+ set(el, "clip-path", `url(#${c.id})`);
593
+ }
594
+ if (w.typed !== void 0) {
595
+ const runs = [];
596
+ for (const tw = doc.createTreeWalker(el, 4); tw.nextNode(); ) runs.push(tw.currentNode);
597
+ for (const n of runs) {
598
+ kept.texts.set(n, n.data);
599
+ n.data = Array.from(n.data).slice(0, Math.round(Array.from(n.data).length * w.typed)).join("");
600
+ if (!n.data) {
601
+ kept.gone.push([n, n.parentNode, n.nextSibling]);
602
+ n.remove();
603
+ }
604
+ }
605
+ }
606
+ const path = w.path && el.querySelector("path.flow");
607
+ if (path && w.path) {
608
+ if (w.path.d !== void 0) set(path, "d", w.path.d);
609
+ if (w.path.style !== void 0) set(path, "style", w.path.style);
610
+ if (!w.path.head) set(path, "marker-end", null);
611
+ }
612
+ }
613
+ const anim = layer("anim");
614
+ if (anim) for (const [n, at] of overlay(S, pathOf)) make(n, at, anim);
615
+ }
616
+ const ofScene = (s, p) => ({ ...p, frame: (t) => svgAt(s, p.at(t)) });
617
+ const assemble = (s, o = {}) => ofScene(s, planOf(motionModel(s), "assemble", o));
618
+ const disassemble = (s, o = {}) => ofScene(s, planOf(motionModel(s), "disassemble", o));
619
+ const introSvg = (s, t) => assemble(s).frame(t);
620
+ export {
621
+ ASSEMBLE_STYLES,
622
+ DISASSEMBLE_STYLES,
623
+ MOTION_EASINGS,
624
+ MOTION_ORDERS,
625
+ alongPath,
626
+ assemble,
627
+ disassemble,
628
+ introSvg,
629
+ motionModel,
630
+ motionOrder,
631
+ paint,
632
+ planOf,
633
+ svgAt,
634
+ typed
635
+ };