@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,340 @@
1
+ import { paint } from "../iso-objects/motion.js";
2
+ import { h } from "./dom.js";
3
+ import { md } from "./page.js";
4
+ import { transport } from "./transport.js";
5
+ const mounted = /* @__PURE__ */ new Map();
6
+ function pick() {
7
+ let best, most = 0.35;
8
+ for (const [el, m] of mounted) if (m.wants && m.seen >= most && (m.seen > most || !best)) {
9
+ best = el;
10
+ most = m.seen;
11
+ }
12
+ for (const [el, m] of mounted) if (m.wants) m.auto(el === best);
13
+ }
14
+ const svgOf = (text) => {
15
+ const doc = new DOMParser().parseFromString(text, "image/svg+xml");
16
+ if (doc.documentElement.localName !== "svg") throw new Error("the drawing is not SVG");
17
+ return document.importNode(doc.documentElement, true);
18
+ };
19
+ const clockText = (ms) => {
20
+ const s = Math.round(ms / 1e3);
21
+ return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
22
+ };
23
+ const NOTHING = { ok: true, diags: [], duration: 0, beats: [], markers: {}, ambient: "on", loops: false, at: () => ({}), cues: () => void 0, beatAt: () => void 0, startOf: () => void 0 };
24
+ function lit(S) {
25
+ const out = { ...S.camera ? { camera: S.camera } : {}, ...S.variants ? { variants: S.variants } : {} };
26
+ for (const k of ["items", "labels", "routes"]) {
27
+ const gone = Object.entries(S[k] ?? {}).filter(([, v]) => v.opacity === 0).map(([id]) => [id, { opacity: 0 }]);
28
+ if (gone.length) out[k] = Object.fromEntries(gone);
29
+ }
30
+ return out;
31
+ }
32
+ function mountPlayer(host, o) {
33
+ if (o.timeline && !o.timeline.ok) throw new Error(`the motion has faults: ${o.timeline.diags.map((d) => `${d.path}: ${d.message}`).join("; ")}`);
34
+ let scene = o.scene, svg = svgOf(scene.rendered.svg), frozen, stepTo, userPaused = false, gone = false;
35
+ const tr = transport(o.timeline ?? NOTHING, { loop: o.loop, rate: o.rate, ambient: o.ambient, clock: o.clock });
36
+ const controls = o.controls ?? true;
37
+ const calm = o.reducedMotion === "respect" && typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
38
+ let resting = calm;
39
+ const btn = (cls, label, text2, f) => h("button", { type: "button", class: `iso-play-btn ${cls}`, "aria-label": label, title: label, on: { click: f } }, text2);
40
+ const stage = h("div", { class: "iso-play-stage" }, svg);
41
+ const back = btn("back", "Back", "\u2039", () => byHand(() => step(Math.max(0, at() - 1))));
42
+ const play = btn("play", "Play", "\u25B6", () => toggle());
43
+ const next = btn("next", "Next", "\u203A", () => byHand(() => step(Math.min(tr.timeline.beats.length - 1, at() + 1))));
44
+ const again = btn("again", "Reset", "\u21BA", () => byHand(() => {
45
+ stop();
46
+ tr.restart();
47
+ }));
48
+ const amb = btn("ambient", "Ambient motion", "\u223F", () => {
49
+ wake();
50
+ tr.ambient = !tr.ambient;
51
+ show();
52
+ });
53
+ const bar = h("input", { type: "range", class: "iso-play-bar", min: 0, step: 1, "aria-label": "Time", on: { input: (() => byHand(() => {
54
+ stop();
55
+ tr.seek(Number(bar.value));
56
+ })) } });
57
+ const time = h("span", { class: "iso-play-time" });
58
+ const dots = h("div", { class: "iso-play-dots" });
59
+ const num = h("span", { class: "iso-play-num" }), title = h("span", { class: "iso-play-title" }), chapter = h("span", { class: "iso-play-chapter" }), count = h("span", { class: "iso-play-count" });
60
+ const text = h("div", { class: "iso-play-text" }), code = h("pre", { class: "iso-play-code" }), facts = h("dl", { class: "iso-play-facts" });
61
+ const say = h("div", { class: "iso-play-say", "aria-live": "polite" }, h("div", { class: "iso-play-head" }, num, title, chapter, count), text, code, facts);
62
+ const list = h("details", { class: "iso-play-list" });
63
+ const row = h("div", { class: "iso-play-row" }, back, play, next, again, amb, bar, time);
64
+ const root = h(
65
+ "div",
66
+ { class: "iso-play", role: "group", "aria-roledescription": "animated diagram", "aria-label": o.label ?? "Diagram", tabindex: controls ? 0 : void 0, "data-mode": o.mode ?? "light" },
67
+ stage,
68
+ controls && row,
69
+ controls && dots,
70
+ controls && say,
71
+ controls && list
72
+ );
73
+ host.append(root);
74
+ let dotOf = [];
75
+ function beats() {
76
+ const all = tr.timeline.beats, some = all.length > 0, said = all.some((b) => b.say);
77
+ dotOf = [];
78
+ dots.replaceChildren();
79
+ let group, last;
80
+ for (const b of all) {
81
+ if (!group || b.say?.chapter !== last) {
82
+ group = h("span", { class: "iso-play-group", role: "group", "aria-label": b.say?.chapter, title: b.say?.chapter });
83
+ dots.append(group);
84
+ last = b.say?.chapter;
85
+ }
86
+ const d = h("button", { type: "button", class: "iso-play-dot", "aria-label": `Step ${b.index + 1}: ${b.say?.title ?? b.name}`, title: b.say?.title ?? b.name, on: { click: (() => byHand(() => step(b.index))) } });
87
+ group.append(d);
88
+ dotOf.push(d);
89
+ }
90
+ list.replaceChildren(h("summary", null, `All ${all.length} steps as text`), h("ol", null, ...all.map((b) => h("li", null, ...words(b)))));
91
+ for (const el of [back, next, dots]) el.hidden = !some;
92
+ say.hidden = !said;
93
+ list.hidden = !said;
94
+ amb.hidden = !tr.timeline.loops;
95
+ bar.max = String(Math.round(tr.duration));
96
+ for (const el of [play, again, bar, time]) el.hidden = tr.duration <= 0;
97
+ told = void 0;
98
+ }
99
+ function words(b) {
100
+ const s = b.say, out = [h("b", null, s?.title ?? b.name)];
101
+ if (s?.text) {
102
+ const p = h("div", null);
103
+ p.innerHTML = md(s.text);
104
+ out.push(p);
105
+ }
106
+ if (s?.code) out.push(h("code", null, s.code));
107
+ if (s?.facts?.length) out.push(h("span", null, s.facts.map(([k, v]) => `${k}: ${v}`).join(" \xB7 ")));
108
+ return out;
109
+ }
110
+ let told;
111
+ function marks(b) {
112
+ const n = String((b?.index ?? -1) + 1);
113
+ for (const e of svg.querySelectorAll("[data-badge]")) {
114
+ e.classList.toggle("on", e.getAttribute("data-badge") === n);
115
+ if (o.steps) {
116
+ if (e.getAttribute("data-badge") === n || resting) e.removeAttribute("opacity");
117
+ else e.setAttribute("opacity", "0.14");
118
+ }
119
+ }
120
+ if (o.steps) for (const e of svg.querySelectorAll("[data-step]")) e.setAttribute("visibility", e.getAttribute("data-step") === n && !resting ? "visible" : "hidden");
121
+ }
122
+ function caption() {
123
+ const b = tr.beat;
124
+ if (b === told) return;
125
+ told = b;
126
+ marks(b);
127
+ if (!controls) return;
128
+ const s = b?.say;
129
+ num.textContent = b ? String(b.index + 1) : "";
130
+ num.style.backgroundColor = s?.color ?? "";
131
+ title.textContent = s?.title ?? b?.name ?? "";
132
+ chapter.textContent = s?.chapter ?? "";
133
+ count.textContent = b ? `${b.index + 1} / ${tr.timeline.beats.length}` : "";
134
+ text.innerHTML = md(s?.text);
135
+ code.textContent = s?.code ?? "";
136
+ facts.replaceChildren(...(s?.facts ?? []).flatMap(([k, v]) => [h("dt", null, k), h("dd", null, v)]));
137
+ code.hidden = !s?.code;
138
+ facts.hidden = !s?.facts?.length;
139
+ dotOf.forEach((d, i) => {
140
+ d.classList.toggle("on", i === b?.index);
141
+ d.classList.toggle("done", b !== void 0 && i < b.index);
142
+ if (i === b?.index) d.setAttribute("aria-current", "step");
143
+ else d.removeAttribute("aria-current");
144
+ });
145
+ }
146
+ function show() {
147
+ const S = frozen !== void 0 ? tr.timeline.at(frozen, { ambient: frozen }) : resting ? lit(tr.timeline.at(tr.duration, { ambient: "off" })) : tr.state();
148
+ paint(svg, S, scene.rendered, { styles: "cssom" });
149
+ root.dataset.time = String(Math.round(frozen ?? tr.time));
150
+ if (frozen !== void 0) marks(tr.timeline.beatAt(frozen));
151
+ else caption();
152
+ if (!controls) return;
153
+ bar.value = String(Math.round(tr.time));
154
+ bar.setAttribute("aria-valuetext", `${clockText(tr.time)} of ${clockText(tr.duration)}${tr.beat ? `, ${tr.beat.say?.title ?? tr.beat.name}` : ""}`);
155
+ time.textContent = `${clockText(tr.time)} / ${clockText(tr.duration)}`;
156
+ }
157
+ function buttons() {
158
+ const on = going();
159
+ play.textContent = on ? "\u275A\u275A" : "\u25B6";
160
+ play.setAttribute("aria-label", on ? "Pause" : "Play");
161
+ play.title = on ? "Pause" : "Play";
162
+ amb.setAttribute("aria-pressed", String(tr.ambient));
163
+ say.setAttribute("aria-live", on ? "off" : "polite");
164
+ root.dataset.playing = String(on);
165
+ }
166
+ const going = () => tr.playing && stepTo === void 0;
167
+ const at = () => tr.beat?.index ?? 0;
168
+ function wake() {
169
+ if (resting) {
170
+ resting = false;
171
+ told = void 0;
172
+ }
173
+ }
174
+ function byHand(f) {
175
+ wake();
176
+ userPaused = true;
177
+ f();
178
+ buttons();
179
+ show();
180
+ }
181
+ function stop() {
182
+ stepTo = void 0;
183
+ tr.pause();
184
+ }
185
+ function step(i) {
186
+ const b = tr.timeline.beats[i];
187
+ if (!b || gone) return;
188
+ tr.pause();
189
+ tr.seek(b.start, { join: true });
190
+ const nextStart = tr.timeline.beats[i + 1]?.start ?? Infinity;
191
+ stepTo = Math.max(b.start, Math.min(b.end, nextStart, tr.duration) - 1);
192
+ tr.play({ until: stepTo });
193
+ }
194
+ function toggle() {
195
+ wake();
196
+ if (going()) {
197
+ userPaused = true;
198
+ stop();
199
+ } else {
200
+ userPaused = false;
201
+ stepTo = void 0;
202
+ tr.pause();
203
+ tr.play();
204
+ }
205
+ buttons();
206
+ show();
207
+ }
208
+ tr.on("time", show);
209
+ tr.on("play", buttons);
210
+ tr.on("ambient", buttons);
211
+ tr.on("pause", () => {
212
+ stepTo = void 0;
213
+ buttons();
214
+ });
215
+ root.addEventListener("keydown", (e) => {
216
+ const k = e.key, t = e.target;
217
+ if (e.altKey || e.ctrlKey || e.metaKey || t === bar) return;
218
+ if (k === "ArrowRight" || k === "ArrowLeft") {
219
+ e.preventDefault();
220
+ (k === "ArrowRight" ? next : back).click();
221
+ } else if (k === "Home") {
222
+ e.preventDefault();
223
+ again.click();
224
+ } else if ((k === " " || k === "k") && t === root) {
225
+ e.preventDefault();
226
+ toggle();
227
+ }
228
+ });
229
+ const onBadge = (e) => {
230
+ const b = e.target.closest?.("[data-go]"), key = e.key;
231
+ if (!b || e.type === "keydown" && key !== "Enter" && key !== " ") return;
232
+ e.preventDefault();
233
+ e.stopPropagation();
234
+ byHand(() => step(Number(b.getAttribute("data-go")) - 1));
235
+ };
236
+ stage.addEventListener("click", onBadge);
237
+ stage.addEventListener("keydown", onBadge);
238
+ const me = {
239
+ seen: 0,
240
+ wants: o.autoplay === "visible" && !calm,
241
+ auto(on) {
242
+ if (on) {
243
+ if (!userPaused && !going()) {
244
+ stepTo = void 0;
245
+ tr.play();
246
+ }
247
+ } else if (going()) {
248
+ stepTo = void 0;
249
+ tr.pause();
250
+ }
251
+ }
252
+ };
253
+ mounted.set(root, me);
254
+ const io = typeof IntersectionObserver === "function" ? new IntersectionObserver((es) => {
255
+ for (const e of es) {
256
+ me.seen = e.isIntersecting ? e.intersectionRatio : 0;
257
+ if (frozen === void 0) tr.active = e.isIntersecting;
258
+ }
259
+ pick();
260
+ }, { threshold: [0, 0.2, 0.35, 0.5, 0.7, 0.9, 1] }) : void 0;
261
+ io?.observe(root);
262
+ beats();
263
+ buttons();
264
+ show();
265
+ if (o.autoplay === true && !calm) tr.play();
266
+ return {
267
+ root,
268
+ transport: tr,
269
+ step: (i) => byHand(() => step(i)),
270
+ set(s, tl = NOTHING, mode) {
271
+ if (gone || !tl.ok) return false;
272
+ const fresh = svgOf(s.rendered.svg);
273
+ scene = s;
274
+ svg.replaceWith(fresh);
275
+ svg = fresh;
276
+ if (mode) root.dataset.mode = mode;
277
+ if (stepTo !== void 0) stop();
278
+ tr.rebind(tl);
279
+ beats();
280
+ buttons();
281
+ show();
282
+ return true;
283
+ },
284
+ freeze(ms) {
285
+ frozen = ms;
286
+ tr.pause();
287
+ tr.active = false;
288
+ root.classList.add("frozen");
289
+ show();
290
+ },
291
+ dispose() {
292
+ gone = true;
293
+ io?.disconnect();
294
+ mounted.delete(root);
295
+ tr.dispose();
296
+ root.remove();
297
+ pick();
298
+ }
299
+ };
300
+ }
301
+ const PLAY_CSS = `
302
+ .iso-play{--ip-ink:#1c2026;--ip-muted:#68707c;--ip-line:#dfe3e8;--ip-bg:#fff;--ip-code:#f2f4f7;--ip-focus:#2a64d6;container:iso-play/inline-size;display:block;color:var(--ip-ink);background:var(--ip-bg);font:14px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif;border-radius:10px}
303
+ .iso-play[data-mode="dark"]{--ip-ink:#e6e9ee;--ip-muted:#98a1ae;--ip-line:#333a44;--ip-bg:#15181d;--ip-code:#1f242b;--ip-focus:#7aa7ff;color-scheme:dark}
304
+ .iso-play:focus-visible,.iso-play button:focus-visible,.iso-play input:focus-visible,.iso-play summary:focus-visible{outline:2px solid var(--ip-focus);outline-offset:2px}
305
+ .iso-play [hidden]{display:none}
306
+ .iso-play-stage svg{display:block;width:100%;height:auto}
307
+ .iso-play-stage [data-go]{cursor:pointer}
308
+ .iso-play-row{display:flex;align-items:center;gap:6px;padding:8px 12px 4px}
309
+ .iso-play-btn{font:inherit;min-width:32px;height:32px;padding:0 8px;border:1px solid var(--ip-line);border-radius:8px;background:none;color:inherit;cursor:pointer}
310
+ .iso-play-btn:hover{border-color:var(--ip-muted)}
311
+ .iso-play-btn.play{background:var(--ip-ink);color:var(--ip-bg);border-color:var(--ip-ink);min-width:44px}
312
+ .iso-play-btn.ambient[aria-pressed="false"]{color:var(--ip-muted);text-decoration:line-through}
313
+ .iso-play-bar{flex:1 1 60px;min-width:0;accent-color:var(--ip-ink)}
314
+ .iso-play-time{color:var(--ip-muted);font-size:12px;font-variant-numeric:tabular-nums;white-space:nowrap}
315
+ .iso-play-dots{display:flex;flex-wrap:wrap;gap:4px 12px;padding:2px 12px}
316
+ .iso-play-group{display:inline-flex;gap:0}
317
+ .iso-play-dot{width:24px;height:24px;padding:0;border:0;background:none;cursor:pointer;display:grid;place-items:center}
318
+ .iso-play-dot::before{content:"";width:9px;height:9px;border-radius:50%;background:var(--ip-line)}
319
+ .iso-play-dot.done::before{background:var(--ip-muted)}
320
+ .iso-play-dot.on::before{background:var(--ip-ink);width:11px;height:11px}
321
+ .iso-play-say{padding:4px 12px 10px;min-height:5.2em}
322
+ .iso-play-head{display:flex;align-items:baseline;gap:8px;font-weight:700}
323
+ .iso-play-num{flex:none;display:inline-grid;place-items:center;min-width:22px;height:22px;border-radius:11px;background:var(--ip-ink);color:var(--ip-bg);font-size:11px;align-self:center}
324
+ .iso-play-chapter,.iso-play-count{color:var(--ip-muted);font-size:12px;font-weight:500}
325
+ .iso-play-count{margin-left:auto;white-space:nowrap}
326
+ .iso-play-text{max-width:80ch}
327
+ .iso-play-text a{color:inherit}
328
+ .iso-play-code{margin:6px 0 0;padding:6px 10px;border-radius:8px;background:var(--ip-code);font:12px/1.5 ui-monospace,Menlo,monospace;white-space:pre-wrap;overflow-wrap:anywhere}
329
+ .iso-play-facts{display:flex;flex-wrap:wrap;gap:2px 16px;margin:6px 0 0;font-size:12px}
330
+ .iso-play-facts dt{color:var(--ip-muted)}
331
+ .iso-play-facts dd{margin:0 8px 0 -10px;font-weight:600}
332
+ .iso-play-list{padding:0 12px 10px;font-size:13px}
333
+ .iso-play-list summary{color:var(--ip-muted);cursor:pointer}
334
+ .iso-play.frozen>:not(.iso-play-stage){display:none}
335
+ @container iso-play (max-width:420px){.iso-play-time{display:none}.iso-play-row{flex-wrap:wrap}.iso-play-bar{flex-basis:100%;order:9}}
336
+ `;
337
+ export {
338
+ PLAY_CSS,
339
+ mountPlayer
340
+ };