@johnoscott/isometric-engine 0.15.0 → 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 (66) hide show
  1. package/README.md +1 -1
  2. package/iso-editor.js +230 -193
  3. package/iso-render.js +184 -147
  4. package/iso-systems.js +183 -146
  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 +1 -1
  10. package/js/iso-objects/index.js +10 -2
  11. package/js/iso-objects/mono-paint.js +96 -0
  12. package/js/iso-objects/motion-doc.js +0 -79
  13. package/js/iso-objects/motion-entry.js +4 -2
  14. package/js/iso-objects/motion-schema.js +83 -0
  15. package/js/iso-objects/motion.js +9 -4
  16. package/js/iso-objects/timeline.js +51 -11
  17. package/js/iso-systems/compile.js +25 -1
  18. package/js/iso-systems/render.js +1 -1
  19. package/js/iso-systems/schema.json +1 -0
  20. package/js/iso-viewer/index.js +6 -0
  21. package/js/iso-viewer/page.js +40 -17
  22. package/js/iso-viewer/play.js +340 -0
  23. package/js/iso-viewer/player.js +147 -162
  24. package/js/iso-viewer/transport.js +289 -0
  25. package/package.json +39 -2
  26. package/src/iso-core/index.ts +1 -0
  27. package/src/iso-core/mono.ts +306 -0
  28. package/src/iso-core/theme.ts +2 -1
  29. package/src/iso-editor/ui/canvas.ts +14 -1
  30. package/src/iso-objects/ants.ts +2 -2
  31. package/src/iso-objects/index.ts +5 -3
  32. package/src/iso-objects/mono-paint.ts +97 -0
  33. package/src/iso-objects/motion-doc.ts +0 -52
  34. package/src/iso-objects/motion-entry.ts +4 -3
  35. package/src/iso-objects/motion-schema.ts +55 -0
  36. package/src/iso-objects/motion.ts +6 -5
  37. package/src/iso-objects/scene.ts +3 -0
  38. package/src/iso-objects/timeline.ts +63 -13
  39. package/src/iso-objects/types.ts +2 -0
  40. package/src/iso-systems/compile.ts +29 -2
  41. package/src/iso-systems/render.ts +5 -1
  42. package/src/iso-systems/schema.json +1 -0
  43. package/src/iso-viewer/index.ts +4 -0
  44. package/src/iso-viewer/page.ts +28 -12
  45. package/src/iso-viewer/play.ts +280 -0
  46. package/src/iso-viewer/player.ts +133 -134
  47. package/src/iso-viewer/transport.ts +272 -0
  48. package/types/iso-core/index.d.ts +1 -0
  49. package/types/iso-core/mono.d.ts +61 -0
  50. package/types/iso-core/theme.d.ts +2 -0
  51. package/types/iso-objects/ants.d.ts +1 -0
  52. package/types/iso-objects/index.d.ts +5 -3
  53. package/types/iso-objects/mono-paint.d.ts +20 -0
  54. package/types/iso-objects/motion-doc.d.ts +0 -594
  55. package/types/iso-objects/motion-entry.d.ts +4 -3
  56. package/types/iso-objects/motion-schema.d.ts +594 -0
  57. package/types/iso-objects/motion.d.ts +4 -2
  58. package/types/iso-objects/scene.d.ts +3 -0
  59. package/types/iso-objects/timeline.d.ts +22 -2
  60. package/types/iso-objects/types.d.ts +2 -0
  61. package/types/iso-systems/compile.d.ts +5 -0
  62. package/types/iso-systems/render.d.ts +4 -0
  63. package/types/iso-viewer/index.d.ts +4 -0
  64. package/types/iso-viewer/play.d.ts +37 -0
  65. package/types/iso-viewer/transport.d.ts +73 -0
  66. package/types/player/isometric-diagram.d.ts +24 -0
@@ -2,34 +2,37 @@
2
2
  // It reads the scene data from <script type="application/json" id="diagram-steps"> and the rendered SVG.
3
3
  //
4
4
  // Hash flags (for screenshots and links): #theme=light|dark, #still (no motion: tokens frozen mid-route),
5
- // #scene=ID&step=N (open a scene at a step), #t=0.6 (where frozen tokens sit, 0..1), #motion (ignore reduced motion),
5
+ // #scene=ID&step=N (open a scene at a step), #t=0.6 (where frozen tokens sit, 0..1), #motion (no Motion off button),
6
6
  // #grid (show the floor grid), #assemble (play the assemble intro) and #assemble&at=MS (freeze it at that moment).
7
7
 
8
- import { focusView } from '../iso-objects/focus.ts';
9
- import { paint, planOf, type MotionModel } from '../iso-objects/motion.ts';
8
+ //
9
+ // Motion plays, also for a reader who asked their machine for less (docs/design/MOTION.md, 6.2): they get a Motion off button in each figure.
10
+ //
11
+ // One clock: a transport (transport.ts) over the scene's Timeline, for the steps, their tokens, pulses and camera, and another over the
12
+ // assemble intro's plan. What a step dims, hides and lights is the page's stylesheet's, by classes, when its beat begins.
13
+
14
+ import { paint, planOf, type SceneState } from '../iso-objects/motion.ts';
15
+ import { bindMotion, type MotionSource, type Timeline } from '../iso-objects/timeline.ts';
16
+ import type { MotionDoc } from '../iso-objects/motion-doc.ts';
17
+ import { transport } from './transport.ts';
10
18
 
11
19
  interface Token { route: string; kind: string; color: string }
12
20
  interface Step {
13
21
  n: number; num: number; kind: string; color: string; heading: string; caption: string; command: string;
14
22
  phases: Token[][]; pulses: [number, number, number, number][]; lit: string[]; duration: number;
15
- focus?: [number, number, number, number];
16
23
  }
17
24
  interface SceneData {
18
25
  id: string; loop: boolean; dim: 'flows' | 'all'; steps: Step[]; vis: Record<string, boolean[]>;
19
- assemble: boolean; grid: boolean; motion: MotionModel;
26
+ assemble: boolean; grid: boolean; source: MotionSource;
27
+ /** the steps as a motion document, or the scene's own */
28
+ doc?: MotionDoc;
29
+ /** the scene's own motion: everything is the Timeline's, what is dimmed and hidden too */
30
+ own?: boolean;
20
31
  }
21
32
  interface Data { scenes: SceneData[] }
22
33
 
23
- const NS = 'http://www.w3.org/2000/svg';
24
- const TRAVEL = 1150;
25
-
26
- function svgEl(tag: string, attrs: Record<string, string | number>, parent: Element): SVGElement {
27
- const e = document.createElementNS(NS, tag);
28
- for (const k in attrs) e.setAttribute(k, String(attrs[k]));
29
- parent.appendChild(e);
30
- return e;
31
- }
32
34
  const ease = (f: number) => (f < 0.5 ? 2 * f * f : 1 - Math.pow(-2 * f + 2, 2) / 2);
35
+ const none: Timeline = { ok: true, diags: [], duration: 0, beats: [], markers: {}, ambient: 'on', loops: false, at: () => ({}), cues: () => undefined, beatAt: () => undefined, startOf: () => undefined };
33
36
 
34
37
  function init() {
35
38
  const dataEl = document.getElementById('diagram-steps');
@@ -37,8 +40,9 @@ function init() {
37
40
  const data = JSON.parse(dataEl.textContent || '{}') as Data;
38
41
  const hash = location.hash;
39
42
  const still = /still/.test(hash);
40
- const reduceMedia = matchMedia('(prefers-reduced-motion: reduce)').matches && !/motion/.test(hash);
41
- const reduce = still || reduceMedia;
43
+ const asked = matchMedia('(prefers-reduced-motion: reduce)').matches && !still && !/motion/.test(hash);
44
+ /** nothing moves: a still page, or Motion off pressed */
45
+ let reduce = still;
42
46
  const frozenT = Math.max(0, Math.min(1, Number((/[#&]t=([\d.]+)/.exec(hash) || [])[1] ?? 0.6)));
43
47
  const wantScene = (/[#&]scene=([\w-]+)/.exec(hash) || [])[1];
44
48
  const wantStep = Number((/[#&]step=?(\d+)/.exec(hash) || [])[1] || 0);
@@ -50,7 +54,8 @@ function init() {
50
54
  // #scene=ID opens the page at that scene (screenshots use #still, which keeps the page at the top)
51
55
  if (wantScene && !still) document.getElementById('scene-' + wantScene)?.scrollIntoView({ block: 'start' });
52
56
 
53
- const runners: { fig: Element; setActive(on: boolean): void }[] = [];
57
+ const runners: { fig: Element; setActive(on: boolean): void; setMotion(): void }[] = [];
58
+ const offBtns: HTMLButtonElement[] = [];
54
59
  for (const sd of data.scenes) {
55
60
  const sec = document.getElementById('scene-' + sd.id);
56
61
  if (sec) runners.push(makeRunner(sec, sd));
@@ -59,7 +64,6 @@ function init() {
59
64
  function makeRunner(sec: HTMLElement, sd: SceneData) {
60
65
  const STEPS = sd.steps, N = STEPS.length;
61
66
  const svg = sec.querySelector('svg')!, fig = sec.querySelector('.figure')!;
62
- const anim = svg.querySelector('.g-anim')!;
63
67
  const q = <T extends Element>(s: string) => sec.querySelector(s) as T | null;
64
68
  const num = q<HTMLElement>('.cap-h .num'), hEl = q<HTMLElement>('.cap-h .h'), cnt = q<HTMLElement>('.cap-h .cnt');
65
69
  const capT = q<HTMLElement>('.cap-t'), capC = q<HTMLElement>('.cap-c'), prog = q<HTMLElement>('.prog i'), dots = q<HTMLElement>('.dots');
@@ -71,12 +75,13 @@ function init() {
71
75
  const visEls = new Map<string, SVGElement[]>();
72
76
  for (const e of idEls) if (sd.vis[e.dataset.id!]) { const l = visEls.get(e.dataset.id!) ?? []; l.push(e); visEls.set(e.dataset.id!, l); }
73
77
  for (const e of hlEls) { const id = e.dataset.hl!; if (sd.vis[id] && !e.dataset.id) { const l = visEls.get(id) ?? []; l.push(e); visEls.set(id, l); } }
74
- const flowPath = (id: string) => svg.querySelector<SVGPathElement>(`[data-hl="${CSS.escape(id)}"] path.flow`);
75
78
  if (sd.dim === 'all') svg.classList.add('dim-all');
76
79
 
77
- let cur = 0, playing = false, userPaused = false, active = false, timer = 0, raf = 0, groups = 0, t0 = 0;
78
- let assembling = false, assembled = false, introTimer = 0, introDone: (() => void) | undefined;
79
- let tokens: { path: SVGPathElement; len: number; gi: number; stop: boolean; c: SVGElement }[] = [];
80
+ const bound = sd.doc ? bindMotion(sd.source, sd.doc) : none, tl = bound.ok ? bound : none;
81
+ const tr = transport(tl, { loop: sd.loop });
82
+ tr.active = false;
83
+ let cur = 0, userPaused = false, active = false, stepping = false;
84
+ let assembling = false, assembled = false;
80
85
 
81
86
  // ---- grid ----
82
87
  const setGrid = (on: boolean) => { svg.classList.toggle('show-grid', on); gridBtn?.setAttribute('aria-pressed', String(on)); };
@@ -86,164 +91,158 @@ function init() {
86
91
  STEPS.forEach((s, i) => {
87
92
  const b = document.createElement('button');
88
93
  b.className = 'dot'; b.type = 'button'; b.setAttribute('aria-label', `Step ${s.num}: ${s.heading}`);
89
- b.addEventListener('click', () => { userPause(); finishAssemble(); show(i); });
94
+ b.addEventListener('click', () => { userPause(); finishAssemble(); show(i, true); });
90
95
  dots?.appendChild(b);
91
96
  });
92
97
 
93
- function clearAnim() { anim.textContent = ''; tokens = []; if (raf) cancelAnimationFrame(raf); raf = 0; }
94
- function tick(now: number) {
95
- const per = TRAVEL * groups + 700, tt = (now - t0) % per;
96
- for (const tk of tokens) {
97
- // frozen: every phase's token sits at the same point, so one picture shows them all
98
- const f = reduce ? frozenT : Math.max(0, Math.min(1, (tt - tk.gi * TRAVEL) / TRAVEL));
99
- let e = ease(f);
100
- if (tk.stop) e = f < 0.7 ? f / 0.7 : 1 - ((f - 0.7) / 0.3) * 0.25; // a blocked token stalls and falls back
101
- const p = tk.path.getPointAtLength(Math.max(0, Math.min(1, e)) * tk.len);
102
- tk.c.setAttribute('cx', String(p.x)); tk.c.setAttribute('cy', String(p.y));
103
- tk.c.style.opacity = reduce ? '1' : f <= 0 || f >= 1 ? '0' : '1';
104
- }
105
- if (!reduce && active) raf = requestAnimationFrame(tick);
98
+ // ---- what is drawn now ----
99
+ const inkOf = new Map(STEPS.flatMap(s => s.phases.flat().map(t => [t.kind, t.color] as const)));
100
+ /** the Timeline's state as the page draws it: its tokens and pulses in the step's colours; the rest is its classes' */
101
+ function drawn(): SceneState {
102
+ const S: SceneState = reduce ? frozen() : tr.state(), s = STEPS[cur];
103
+ if (sd.own || !s) return S;
104
+ const out: SceneState = {};
105
+ if (S.camera && !svg.classList.contains('all')) out.camera = S.camera;
106
+ if (S.tokens) out.tokens = S.tokens.map(k => ({ ...k, color: k.color ?? (k.kind === undefined ? undefined : inkOf.get(k.kind)) }));
107
+ // (each ring grows from a third of its size to half as much again, and fades)
108
+ if (reduce) { if (s.pulses.length) out.pulses = s.pulses.map(([x, y, rx, ry]) => ({ x, y, rx, ry, opacity: 0.85, color: s.color })); }
109
+ else if (S.pulses) out.pulses = s.pulses.flatMap(([x, y, rx, ry], k) => { const p = S.pulses![k]; if (!p) return []; const f = 1 - Math.pow(p.opacity ?? 1, 2), z = 0.35 + 1.15 * f; return [{ x, y, rx: rx * z, ry: ry * z, opacity: 0.95 * (1 - f), color: s.color }]; });
110
+ return out;
106
111
  }
107
- // ---- focus: ease the viewBox to a step's focus box (keeping the drawing's proportions), and back ----
108
- const FULL = svg.getAttribute('viewBox')!.split(/\s+/).map(Number);
109
- let view = FULL.slice(), zoomTimer = 0;
110
- const viewFor = (s: Step) => focusView(FULL, s.focus);
111
- function zoomTo(target: number[]) {
112
- clearTimeout(zoomTimer);
113
- if (target.every((v, k) => Math.abs(v - view[k]) < 0.5)) return;
114
- const from = view.slice(), t0z = performance.now(), D = reduce ? 0 : 900;
115
- // a timer, not a frame callback, so it also finishes where no frames are drawn
116
- const step = () => {
117
- const f = D ? Math.min(1, (performance.now() - t0z) / D) : 1, e = ease(f);
118
- view = from.map((v, k) => v + (target[k] - v) * e);
119
- svg.setAttribute('viewBox', view.map(v => v.toFixed(1)).join(' '));
120
- if (f < 1) zoomTimer = window.setTimeout(step, 16);
121
- };
122
- step();
112
+ /** frozen: every phase's token at the same point, so one picture shows them all; the camera where the step leaves it */
113
+ function frozen(): SceneState {
114
+ const s = STEPS[cur], b = tl.beats[cur];
115
+ if (!s || !b) return {};
116
+ const S = tl.at(Math.max(b.start, b.end - 1), { ambient: 'off' });
117
+ if (sd.own) return S;
118
+ const at = (kind: string) => (kind === 'blocked' ? (frozenT < 0.7 ? frozenT / 0.7 : 1 - ((frozenT - 0.7) / 0.3) * 0.25) : ease(frozenT));
119
+ return { ...(S.camera ? { camera: S.camera } : {}), tokens: s.phases.flat().map(t => ({ route: t.route, at: at(t.kind), kind: t.kind })) };
123
120
  }
121
+ const draw = () => {
122
+ if (assembling) return;
123
+ paint(svg, drawn());
124
+ if (prog && N) prog.style.width = (going() ? (tl.cues(tr.time)?.progress ?? 0) * 100 : ((cur + 1) / N) * 100) + '%';
125
+ };
126
+ const going = () => tr.playing && !stepping;
124
127
 
125
- function show(i: number) {
126
- if (!N) return;
127
- clearAnim();
128
- cur = i;
128
+ /** the page at a step: what is lit, dimmed and hidden, the words; nothing moves by this */
129
+ function look(i: number) {
129
130
  const s = STEPS[i], n = s.n;
130
- zoomTo(svg.classList.contains("all") ? FULL : viewFor(s));
131
+ cur = i;
131
132
  const lit = new Set(s.lit);
132
- svg.classList.add('focus');
133
- for (const e of hlEls) e.classList.toggle('hl', e.dataset.hl!.split(' ').some(x => lit.has(x)));
134
- if (sd.dim === 'all') for (const e of idEls) if (e.hasAttribute('data-dim')) e.classList.toggle('hl', lit.has(e.dataset.id!));
133
+ if (!sd.own) {
134
+ svg.classList.add('focus');
135
+ for (const e of hlEls) e.classList.toggle('hl', e.dataset.hl!.split(' ').some(x => lit.has(x)));
136
+ if (sd.dim === 'all') for (const e of idEls) if (e.hasAttribute('data-dim')) e.classList.toggle('hl', lit.has(e.dataset.id!));
137
+ for (const [id, els] of visEls) { const v = sd.vis[id][i]; for (const e of els) e.classList.toggle('pre', !v); }
138
+ }
135
139
  for (const e of stepEls) e.classList.toggle('on', e.dataset.step === String(n));
136
140
  svg.querySelectorAll<SVGElement>('[data-badge]').forEach(b => b.classList.toggle('on', b.dataset.badge === String(n)));
137
- for (const [id, els] of visEls) { const v = sd.vis[id][i]; for (const e of els) e.classList.toggle('pre', !v); }
138
- for (const [x, y, rx, ry] of s.pulses) svgEl('ellipse', { cx: x, cy: y, rx, ry, class: 'pulse', style: `stroke:${s.color}` }, anim);
139
- groups = s.phases.length;
140
- s.phases.forEach((ph, gi) => ph.forEach(tk => {
141
- const path = flowPath(tk.route);
142
- if (!path) return;
143
- tokens.push({ path, len: path.getTotalLength(), gi, stop: tk.kind === 'blocked', c: svgEl('circle', { r: 7, class: 'tok', style: `fill:${tk.color}` }, anim) });
144
- }));
145
- t0 = performance.now(); tick(t0);
146
- if (!reduce && active && !raf) raf = requestAnimationFrame(tick);
147
141
  num!.textContent = String(s.num); num!.style.background = s.color;
148
142
  hEl!.textContent = s.heading; cnt!.textContent = `${i + 1} / ${N}`;
149
143
  capT!.innerHTML = s.caption; capC!.textContent = s.command;
150
144
  prog!.style.setProperty('--c', s.color);
151
145
  [...dots!.children].forEach((d, j) => { d.classList.toggle('on', j === i); d.classList.toggle('done', j < i); });
152
- restartProg();
153
146
  }
154
- function restartProg() {
155
- if (!prog) return;
156
- prog.style.transition = 'none';
157
- prog.style.width = playing ? '0%' : ((cur + 1) / N * 100) + '%';
158
- if (playing) { void prog.offsetWidth; prog.style.transition = `width ${STEPS[cur].duration}ms linear`; prog.style.width = '100%'; }
159
- }
160
- function schedule() {
161
- clearTimeout(timer);
162
- if (!playing) return;
163
- timer = window.setTimeout(() => {
164
- if (cur === N - 1 && !sd.loop) { stop(); return; }
165
- show((cur + 1) % N); schedule();
166
- }, STEPS[cur].duration);
147
+ /** to a step's start; `through`: and through it, to a stop in it */
148
+ function show(i: number, through = false) {
149
+ const b = tl.beats[i];
150
+ if (!N || !b) return;
151
+ tr.pause();
152
+ look(i);
153
+ tr.seek(b.start, { join: true });
154
+ if (through && !reduce && active) { stepping = true; tr.play({ until: Math.max(b.start + 1, b.end - 1) }); setBtn(); }
155
+ draw();
167
156
  }
168
157
  function setBtn() {
169
158
  if (!playBtn) return;
170
- playBtn.textContent = playing ? '❚❚ Pause' : '▶ Play';
171
- playBtn.setAttribute('aria-label', playing ? 'Pause' : 'Play');
172
- capBox?.setAttribute('aria-live', playing ? 'off' : 'polite');
159
+ playBtn.disabled = reduce;
160
+ if (reduce) { playBtn.textContent = 'Motion off'; return; }
161
+ const on = going();
162
+ playBtn.textContent = on ? '❚❚ Pause' : '▶ Play';
163
+ playBtn.setAttribute('aria-label', on ? 'Pause' : 'Play');
164
+ capBox?.setAttribute('aria-live', on ? 'off' : 'polite');
173
165
  }
174
- function play() { if (reduce || !N || assembling) return; playing = true; setBtn(); restartProg(); schedule(); }
175
- function stop() { playing = false; clearTimeout(timer); setBtn(); restartProg(); }
166
+ function play() { if (reduce || !N || assembling) return; if (stepping) tr.pause(); stepping = false; tr.play(); setBtn(); draw(); }
167
+ function stop() { tr.pause(); }
176
168
  function userPause() { userPaused = true; stop(); }
169
+ tr.on('time', draw);
170
+ tr.on('beat', b => { if (b.index !== cur) look(b.index); });
171
+ tr.on('pause', () => { stepping = false; setBtn(); draw(); });
177
172
 
178
- // ---- assemble: the motion core's plan (the same a video is made from), painted as time goes by ----
179
- const plan = planOf(sd.motion, 'assemble');
180
- function finishAssemble() { if (assembling) introDone?.(); }
173
+ // ---- assemble: the motion core's plan (the same a video is made from), on a transport of its own ----
174
+ const plan = planOf(sd.source.model, 'assemble');
175
+ let intro: ReturnType<typeof transport> | undefined;
176
+ function finishAssemble() {
177
+ if (!assembling || !intro) return;
178
+ intro.dispose(); intro = undefined;
179
+ assembling = false; assembled = true;
180
+ paint(svg, {});
181
+ if (N) show(cur);
182
+ if (active && !userPaused) play();
183
+ }
181
184
  function assemble(freezeAt?: number) {
182
- clearTimeout(introTimer);
183
- introDone = undefined;
185
+ intro?.dispose(); intro = undefined;
184
186
  stop();
185
187
  if (N) show(cur); // set what is visible at this step; focus comes back at the end
186
- clearAnim();
187
188
  svg.classList.remove('focus', 'pre-assemble', 'all');
188
189
  assembling = true;
189
190
  if (freezeAt !== undefined) { paint(svg, plan.at(freezeAt)); return; }
190
- const start = performance.now();
191
- introDone = () => {
192
- clearTimeout(introTimer);
193
- introDone = undefined;
194
- paint(svg, {});
195
- assembling = false; assembled = true;
196
- if (N) show(cur);
197
- if (active && !userPaused) play();
198
- };
199
- // a timer, not a frame callback, so it also finishes where no frames are drawn
200
- const step = () => {
201
- const t = performance.now() - start;
202
- if (t >= plan.duration) { introDone?.(); return; }
203
- paint(svg, plan.at(t));
204
- introTimer = window.setTimeout(step, 16);
205
- };
206
- step();
191
+ const run = intro = transport({ ...none, duration: plan.duration, at: t => plan.at(t) });
192
+ run.on('time', t => paint(svg, plan.at(t)));
193
+ run.on('end', finishAssemble);
194
+ paint(svg, plan.at(0));
195
+ run.play();
207
196
  }
208
197
  asmBtn?.addEventListener('click', () => { userPaused = false; assemble(); });
209
198
 
210
199
  function setActive(on: boolean) {
211
200
  if (on === active) return;
212
201
  active = on;
213
- if (on) {
214
- if (!reduce) { t0 = performance.now(); raf = requestAnimationFrame(tick); }
215
- if (!userPaused) play();
216
- } else { stop(); if (raf) cancelAnimationFrame(raf); raf = 0; }
202
+ tr.active = on && !reduce;
203
+ if (on) { if (!userPaused) play(); } else stop();
217
204
  }
218
205
  playBtn?.addEventListener('click', () => {
219
- if (playing) userPause();
220
- else { userPaused = false; finishAssemble(); if (cur === N - 1) show(0); play(); }
206
+ if (going()) userPause();
207
+ else { userPaused = false; finishAssemble(); if (cur === N - 1 && !going()) show(0); play(); }
221
208
  });
222
- q('[data-a=next]')?.addEventListener('click', () => { userPause(); finishAssemble(); show(Math.min(N - 1, cur + 1)); });
223
- q('[data-a=prev]')?.addEventListener('click', () => { userPause(); finishAssemble(); show(Math.max(0, cur - 1)); });
209
+ q('[data-a=next]')?.addEventListener('click', () => { userPause(); finishAssemble(); show(Math.min(N - 1, cur + 1), true); });
210
+ q('[data-a=prev]')?.addEventListener('click', () => { userPause(); finishAssemble(); show(Math.max(0, cur - 1), true); });
224
211
  fig.addEventListener('keydown', e => {
225
212
  const k = (e as KeyboardEvent).key;
226
213
  if (!N) return;
227
- if (k === 'ArrowRight') { e.preventDefault(); userPause(); finishAssemble(); show(Math.min(N - 1, cur + 1)); }
228
- if (k === 'ArrowLeft') { e.preventDefault(); userPause(); finishAssemble(); show(Math.max(0, cur - 1)); }
214
+ if (k === 'ArrowRight') { e.preventDefault(); userPause(); finishAssemble(); show(Math.min(N - 1, cur + 1), true); }
215
+ if (k === 'ArrowLeft') { e.preventDefault(); userPause(); finishAssemble(); show(Math.max(0, cur - 1), true); }
229
216
  });
230
217
  svg.querySelectorAll<SVGElement>('.badge').forEach(b => {
231
- const go = () => { userPause(); finishAssemble(); show(Number(b.dataset.go) - 1); };
218
+ const go = () => { userPause(); finishAssemble(); show(Number(b.dataset.go) - 1, true); };
232
219
  b.addEventListener('click', go);
233
220
  b.addEventListener('keydown', e => { const k = (e as KeyboardEvent).key; if (k === 'Enter' || k === ' ') { e.preventDefault(); go(); } });
234
221
  });
235
222
 
236
223
  const forced = wantStep && (!wantScene || wantScene === sd.id) ? Math.min(N, wantStep) - 1 : -1;
237
224
  const frozenAssemble = wantAssemble && assembleAt !== undefined && (!wantScene || wantScene === sd.id);
238
- if (reduce && forced < 0 && !still && !frozenAssemble) {
239
- // reduced motion: the whole diagram, everything lit, the step list open, no assembly
240
- if (N) { svg.classList.add('all'); show(N - 1); }
241
- if (playBtn) { playBtn.disabled = true; playBtn.textContent = 'Motion off'; }
242
- if (asmBtn) asmBtn.disabled = true;
243
- sec.querySelector('details.steps')?.setAttribute('open', '');
244
- } else {
245
- if (N) show(forced >= 0 ? forced : 0);
246
- if (reduce && playBtn) { playBtn.disabled = true; playBtn.textContent = 'Motion off'; }
225
+ if (N) show(forced >= 0 ? forced : 0);
226
+ setBtn();
227
+ /** off: the whole diagram, everything lit, the step list open, no intro; on again: from the first step */
228
+ function setMotion() {
229
+ svg.classList.remove('pre-assemble');
230
+ if (reduce) { assembled = true; finishAssemble(); stop(); }
231
+ svg.classList.toggle('all', reduce && N > 0);
232
+ tr.active = active && !reduce;
233
+ if (asmBtn) asmBtn.disabled = reduce;
234
+ if (reduce) sec.querySelector('details.steps')?.setAttribute('open', '');
235
+ if (N) show(reduce ? N - 1 : 0);
236
+ setBtn();
237
+ if (!reduce && active && !userPaused) play();
238
+ }
239
+ const tools = q<HTMLElement>('.tools');
240
+ if (asked && tools) {
241
+ const b = document.createElement('button');
242
+ b.type = 'button'; b.className = 'pbtn small'; b.dataset.a = 'motion'; b.textContent = 'Motion off'; b.title = 'Draw everything in place, with nothing moving';
243
+ b.setAttribute('aria-pressed', 'false');
244
+ b.addEventListener('click', () => { reduce = !reduce; for (const x of offBtns) x.setAttribute('aria-pressed', String(reduce)); for (const r of runners) r.setMotion(); });
245
+ offBtns.push(b); tools.appendChild(b);
247
246
  }
248
247
  if (frozenAssemble) assemble(Number(assembleAt));
249
248
  else if ((sd.assemble || wantAssemble) && !reduce && !(window as unknown as { __diagramNoIntro?: boolean }).__diagramNoIntro) {
@@ -267,11 +266,11 @@ function init() {
267
266
  addEventListener('scroll', check, { passive: true });
268
267
  setTimeout(check, 60);
269
268
  }
270
- return { fig, setActive };
269
+ return { fig, setActive, setMotion };
271
270
  }
272
271
 
273
272
  // only the most visible scene animates
274
- if (!reduce) {
273
+ if (!still) {
275
274
  const ratio = new Map<Element, number>();
276
275
  const pick = () => {
277
276
  let best: typeof runners[number] | undefined, br = 0;