@loadmap/react 0.8.0 → 0.10.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.
@@ -3,8 +3,214 @@ import { compact, fmt, monthOf, multiple, multiples, pct, secs } from "@loadmap/
3
3
  import { anchor, labelsAt, layout, legPath, localElk, pointAt, structureKey } from "@loadmap/layout";
4
4
  import { memo, useCallback, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
5
5
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6
- import { morph } from "@loadmap/layout/internal";
6
+ import { morph, shapesOf } from "@loadmap/layout/internal";
7
7
  import { createSim } from "@loadmap/sim";
8
+ // src/ranks.ts
9
+ /** Badges after the limit: the two next in line. */
10
+ const BADGES = 2;
11
+ const ORDINAL = [
12
+ "",
13
+ "limit",
14
+ "2nd",
15
+ "3rd"
16
+ ];
17
+ /** Where a component reaches the target, on the pessimistic edge the ranking uses. */
18
+ const edge = (c) => c.targetRange?.low ?? c.target;
19
+ /**
20
+ * The limit and the next two in line, by component: the multiple at which each reaches the target, the number the
21
+ * headline gives. The next ones only when they reach it within the load's range, so a box far away wears nothing.
22
+ */
23
+ function rankLabels(ev, model) {
24
+ const out = /* @__PURE__ */ new Map();
25
+ const first = ev.firstLimit;
26
+ if (!first) return out;
27
+ const at = (c) => multiple(edge(c), model.searchMax);
28
+ out.set(first.id, {
29
+ rank: 1,
30
+ text: `LIMIT · ${at(first)}`,
31
+ word: "limit"
32
+ });
33
+ ev.ranked.filter((c) => c !== first && c.util != null).slice(0, BADGES).forEach((c, i) => {
34
+ const x = edge(c);
35
+ if (x == null || !Number.isFinite(x) || x > model.load.max) return;
36
+ const rank = i + 2;
37
+ out.set(c.id, {
38
+ rank,
39
+ text: `${ORDINAL[rank]} · ${at(c)}`,
40
+ word: ORDINAL[rank]
41
+ });
42
+ });
43
+ return out;
44
+ }
45
+ const capital = (s) => s.charAt(0).toUpperCase() + s.slice(1);
46
+ /** The limit moves from one component to another between these values. */
47
+ const handsOver = (a, b) => !!a.firstLimit && !!b.firstLimit && a.firstLimit.id !== b.firstLimit.id;
48
+ /**
49
+ * What the story says when a step hands the limit to another component: "API gateway fixed: the worker pool is next,
50
+ * at 10×" when the old limit got further away, else "The push broker overtakes the position consumer: the limit is
51
+ * 18× now". Null when the limit stays.
52
+ */
53
+ function handoverText(a, b, model) {
54
+ const was = a.firstLimit;
55
+ const now = b.firstLimit;
56
+ if (!was || !now || !handsOver(a, b)) return null;
57
+ const at = multiple(edge(now), model.searchMax);
58
+ const before = edge(was);
59
+ const after = b.components[was.id] ? edge(b.components[was.id]) : null;
60
+ return before != null && (after == null || after > before) ? `${was.name} fixed: ${now.short} ${now.plural ? "are" : "is"} next, at ${at}` : `${capital(now.short)} ${now.plural ? "overtake" : "overtakes"} ${was.short}: the limit is ${at} now`;
61
+ }
62
+ // src/camera.ts
63
+ /**
64
+ * The shots of a story played, in seconds: the whole diagram first, then for each step a flight in to what it changes,
65
+ * a moment close, the change held close, a flight out, and the whole diagram for the step's own seconds. A step that
66
+ * hands the limit to another component adds `hand`: a flight to the new limit (`HAND_FLY`), whose ring then comes in.
67
+ * `back` is the flight out when the reader pauses.
68
+ */
69
+ const SHOTS = {
70
+ establish: 1.2,
71
+ in: 1,
72
+ close: .5,
73
+ hand: 1.8,
74
+ out: 1,
75
+ back: .4
76
+ };
77
+ /** The change is held close until the structure has moved and settled. */
78
+ const changeSeconds = (transition) => Math.max(1.6, transition + .7);
79
+ /** A step played, from the flight in to the end of its hold on the whole diagram; `hands` when it moves the limit. */
80
+ const stepSeconds = (st, transition, hands = false) => SHOTS.in + SHOTS.close + changeSeconds(transition) + (hands ? SHOTS.hand : 0) + SHOTS.out + st.seconds;
81
+ /** The whole story played from `start`, the values shown when it starts, from the first shot of the whole diagram. */
82
+ function storySeconds(model, start, transition) {
83
+ let ev = start;
84
+ let t = SHOTS.establish;
85
+ for (const st of model.story) {
86
+ const after = model.evaluate(st.values);
87
+ t += stepSeconds(st, transition, handsOver(ev, after));
88
+ ev = after;
89
+ }
90
+ return t;
91
+ }
92
+ const whole = (s) => ({
93
+ x: 0,
94
+ y: 0,
95
+ w: s.width,
96
+ h: s.height
97
+ });
98
+ /** The component a box stands for: its stages' component, or its own. */
99
+ function boxComponent(ev, n) {
100
+ return n.stages.map((s) => ev.stages[s]?.component).find(Boolean) ?? n.component ?? void 0;
101
+ }
102
+ /**
103
+ * The boxes a step's change is about, from the values before (`a`) to the step's (`b`): the box it points at and any box
104
+ * the change reshapes; else the boxes it reshapes or an edge that comes or goes ends at; else those whose component
105
+ * changes state; else the first limit after it.
106
+ */
107
+ function targets(scene, a, b, shapes, focus) {
108
+ const ids = new Set(scene.nodes.map((n) => n.id));
109
+ const reshaped = /* @__PURE__ */ new Set();
110
+ for (const id of ids) if (shapes.a.nodes[id] !== shapes.b.nodes[id]) reshaped.add(id);
111
+ const edgesA = new Set(shapes.a.edges.map((e) => e.id));
112
+ const edgesB = new Set(shapes.b.edges.map((e) => e.id));
113
+ for (const e of [...shapes.a.edges, ...shapes.b.edges]) if (edgesA.has(e.id) !== edgesB.has(e.id)) {
114
+ for (const end of [e.from, e.to]) if (ids.has(end)) reshaped.add(end);
115
+ }
116
+ const of = (c) => scene.nodes.filter((n) => c && boxComponent(b, n) === c).map((n) => n.id);
117
+ const order = (s) => scene.nodes.filter((n) => s.has(n.id)).map((n) => n.id);
118
+ if (focus) return order(/* @__PURE__ */ new Set([...of(focus), ...reshaped]));
119
+ if (reshaped.size) return order(reshaped);
120
+ const turned = scene.nodes.filter((n) => {
121
+ const c = boxComponent(b, n);
122
+ return c && a.components[c]?.state !== b.components[c]?.state;
123
+ });
124
+ if (turned.length) return turned.map((n) => n.id);
125
+ return of(b.firstLimit?.id);
126
+ }
127
+ /** The closest the camera comes: the boxes framed never fill more than this share of the board's width. */
128
+ const MAX_ZOOM = 2.5;
129
+ /** Boxes that take more than this share of the board's width are shown on the whole diagram. */
130
+ const WIDE = .8;
131
+ /**
132
+ * The part of the scene that shows these boxes with room around them, at the scene's own shape (the board's), at most
133
+ * 2.5× closer, inside the scene. The whole scene when there is nothing to frame or it would hardly be closer; with
134
+ * `hold`, as wide as it takes instead (a framing that follows boxes as they grow never jumps out).
135
+ */
136
+ function framing(scene, ids, o = {}) {
137
+ const all = whole(scene);
138
+ const ns = scene.nodes.filter((n) => ids.includes(n.id));
139
+ if (!ns.length || !(scene.width > 0 && scene.height > 0)) return all;
140
+ const x0 = Math.min(...ns.map((n) => n.x));
141
+ const y0 = Math.min(...ns.map((n) => n.y));
142
+ const x1 = Math.max(...ns.map((n) => n.x + n.w));
143
+ const y1 = Math.max(...ns.map((n) => n.y + n.h));
144
+ const aspect = scene.width / scene.height;
145
+ let w = Math.max(x1 - x0 + 60, (y1 - y0 + 60) * aspect, scene.width / MAX_ZOOM);
146
+ if (w > WIDE * scene.width && !o.hold) return all;
147
+ w = Math.min(w, scene.width);
148
+ const h = w / aspect;
149
+ const clamp = (v, hi) => Math.max(0, Math.min(hi, v));
150
+ return {
151
+ x: clamp((x0 + x1 - w) / 2, scene.width - w),
152
+ y: clamp((y0 + y1 - h) / 2, scene.height - h),
153
+ w,
154
+ h
155
+ };
156
+ }
157
+ /** Cubic in and out. */
158
+ const ease = (t) => t < .5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
159
+ /**
160
+ * The camera `t` of the way from `a` to `b` (eased): the centre moves evenly, the size changes by the same factor
161
+ * each moment, so a zoom feels even whatever its depth.
162
+ */
163
+ function between(a, b, t) {
164
+ const k = ease(Math.max(0, Math.min(1, t)));
165
+ const w = a.w * (b.w / a.w) ** k;
166
+ const h = a.h * (b.h / a.h) ** k;
167
+ const cx = a.x + a.w / 2 + (b.x + b.w / 2 - a.x - a.w / 2) * k;
168
+ const cy = a.y + a.h / 2 + (b.y + b.h / 2 - a.y - a.h / 2) * k;
169
+ return {
170
+ x: cx - w / 2,
171
+ y: cy - h / 2,
172
+ w,
173
+ h
174
+ };
175
+ }
176
+ /** A label's first words: "Users, × today" says "Users". */
177
+ const short = (label) => label.split(",")[0].trim();
178
+ /** At most this many changed values in a title; the rest are counted. */
179
+ const TITLE_VALUES = 2;
180
+ /**
181
+ * The few words the diagram shows for a step: its own title, or what it changes ("Proposed: Gateway instances 1 → 2,
182
+ * Worker pods 3 → 6"), numbered.
183
+ */
184
+ function stepTitle(model, from, st, i) {
185
+ if (st.title) return `${i + 1} · ${st.title}`;
186
+ const say = (k, v) => {
187
+ if (k === "load") return `${fmt(v)}×`;
188
+ const def = model.params.find((x) => x.id === k)?.def;
189
+ if (typeof v === "boolean") return v ? "on" : "off";
190
+ if (def && def.kind !== "load" && "format" in def && def.format) return def.format(v);
191
+ return typeof v === "number" ? fmt(v) : String(v);
192
+ };
193
+ const changed = Object.keys(st.values).filter((k) => from[k] !== st.values[k]);
194
+ const parts = changed.slice(0, TITLE_VALUES).map((k) => {
195
+ const label = k === "load" ? model.load.label : model.params.find((x) => x.id === k)?.def.label ?? k;
196
+ const to = st.values[k];
197
+ const was = from[k];
198
+ return typeof to === "boolean" ? `${short(label)} ${say(k, to)}` : `${short(label)} ${say(k, was)} → ${say(k, to)}`;
199
+ });
200
+ if (changed.length > TITLE_VALUES) parts.push(`${changed.length - TITLE_VALUES} more`);
201
+ const what = parts.join(", ") || "Nothing changes";
202
+ return `${i + 1} · ${st.scenario && changed.length ? `${st.scenario}: ${what}` : what}`;
203
+ }
204
+ /** What the framed components show: their utilization now, or before and after the change. */
205
+ function stepDetail(a, b, components) {
206
+ return components.slice(0, 2).map((c) => {
207
+ const was = a.components[c];
208
+ const now = b?.components[c];
209
+ if (!was) return "";
210
+ const u = (x) => x == null ? "off" : pct(x);
211
+ return now ? `${was.name} ${u(was.util)} → ${u(now.util)}` : `${was.name} ${u(was.util)}`;
212
+ }).filter(Boolean).join(" · ");
213
+ }
8
214
  // src/scenarios.tsx
9
215
  /** Scenario identity colours, in declaration order; a fifth and later scenario is drawn muted. */
10
216
  const SERIES = [
@@ -88,7 +294,7 @@ function Compare({ lm, title = "Scenarios compared" }) {
88
294
  }),
89
295
  /* @__PURE__ */ jsx("th", {
90
296
  scope: "col",
91
- children: "First limit"
297
+ children: "Limited by"
92
298
  }),
93
299
  /* @__PURE__ */ jsx("th", {
94
300
  scope: "col",
@@ -611,6 +817,7 @@ function Diagram({ lm, legend = true, onNodeClick, selected = null }) {
611
817
  const barRef = useRef(null);
612
818
  const boardRef = useRef(null);
613
819
  const buttonRef = useRef(null);
820
+ const titleRef = useRef(null);
614
821
  const full = lm.fullScreen;
615
822
  const [tip, setTip] = useState(null);
616
823
  const detailId = useId();
@@ -623,6 +830,7 @@ function Diagram({ lm, legend = true, onNodeClick, selected = null }) {
623
830
  ]);
624
831
  const frameId = useSyncExternalStore(engine.subscribe, () => engine.frameId);
625
832
  const frame = engine.frame;
833
+ const hand = useSyncExternalStore(engine.subscribe, () => engine.handover);
626
834
  useEffect(() => {
627
835
  engine.bind(svgRef.current, canvasRef.current);
628
836
  }, [
@@ -630,6 +838,10 @@ function Diagram({ lm, legend = true, onNodeClick, selected = null }) {
630
838
  frame,
631
839
  reducedMotion
632
840
  ]);
841
+ useLayoutEffect(() => {
842
+ engine.applyCamera();
843
+ engine.drawHalos();
844
+ });
633
845
  useEffect(() => {
634
846
  if (lm.root) engine.readColors(lm.root);
635
847
  }, [
@@ -649,6 +861,24 @@ function Diagram({ lm, legend = true, onNodeClick, selected = null }) {
649
861
  ro.observe(board);
650
862
  return () => ro.disconnect();
651
863
  }, [engine]);
864
+ const follow = () => {
865
+ const wrap = wrapRef.current;
866
+ const board = boardRef.current;
867
+ const c = engine.camera;
868
+ const title = titleRef.current;
869
+ if (!c && !engine.cue) return;
870
+ if (!wrap || !board || !engine.frame || wrap.scrollHeight <= wrap.clientHeight) {
871
+ title?.style.removeProperty("top");
872
+ title?.style.removeProperty("bottom");
873
+ return;
874
+ }
875
+ const k = board.clientHeight / engine.frame.height;
876
+ if (c) wrap.scrollTop = board.offsetTop + (c.y + c.h / 2) * k - wrap.clientHeight / 2;
877
+ if (title) {
878
+ title.style.top = `${Math.max(0, wrap.scrollTop - board.offsetTop + (barRef.current?.offsetHeight ?? 0))}px`;
879
+ title.style.bottom = "auto";
880
+ }
881
+ };
652
882
  useEffect(() => {
653
883
  let raf = 0;
654
884
  let last = 0;
@@ -658,6 +888,7 @@ function Diagram({ lm, legend = true, onNodeClick, selected = null }) {
658
888
  if (!engine.paused && engine.scene) {
659
889
  engine.step(dt, { particles: !reducedMotion });
660
890
  engine.draw({ particles: !reducedMotion });
891
+ follow();
661
892
  }
662
893
  raf = requestAnimationFrame(frame);
663
894
  };
@@ -772,20 +1003,31 @@ function Diagram({ lm, legend = true, onNodeClick, selected = null }) {
772
1003
  className: "lm-board",
773
1004
  ref: boardRef,
774
1005
  style: frame ? { aspectRatio: `${frame.width} / ${frame.height}` } : { minHeight: 240 },
775
- children: [frame && /* @__PURE__ */ jsx(Structure, {
776
- lm,
777
- scene: frame,
778
- svgRef,
779
- rates: reducedMotion,
780
- setTip,
781
- onClick: onNodeClick ? onClick : void 0,
782
- selected
783
- }), /* @__PURE__ */ jsx("canvas", {
784
- ref: canvasRef,
785
- hidden: reducedMotion
786
- })]
1006
+ children: [
1007
+ frame && /* @__PURE__ */ jsx(Structure, {
1008
+ lm,
1009
+ scene: frame,
1010
+ hand,
1011
+ svgRef,
1012
+ rates: reducedMotion,
1013
+ setTip,
1014
+ onClick: onNodeClick ? onClick : void 0,
1015
+ selected
1016
+ }),
1017
+ /* @__PURE__ */ jsx("canvas", {
1018
+ ref: canvasRef,
1019
+ hidden: reducedMotion
1020
+ }),
1021
+ lm.cue && /* @__PURE__ */ jsxs("div", {
1022
+ className: "lm-title",
1023
+ "data-top": lm.cue.top || void 0,
1024
+ "aria-hidden": "true",
1025
+ ref: titleRef,
1026
+ children: [/* @__PURE__ */ jsx("b", { children: lm.cue.title }), lm.cue.detail && /* @__PURE__ */ jsx("span", { children: lm.cue.detail })]
1027
+ })
1028
+ ]
787
1029
  }),
788
- frame && tip && /* @__PURE__ */ jsx(Detail, {
1030
+ frame && tip && !lm.storyPlaying && /* @__PURE__ */ jsx(Detail, {
789
1031
  lm,
790
1032
  scene: frame,
791
1033
  id: tip.id,
@@ -825,12 +1067,9 @@ function Legend({ lm }) {
825
1067
  ]
826
1068
  });
827
1069
  }
828
- /** The component a box stands for: its stages' component, or its own. */
829
- function componentOf(lm, n) {
830
- return n.stages.map((s) => lm.ev.stages[s]?.component).find(Boolean) ?? n.component ?? void 0;
831
- }
1070
+ const componentOf = (lm, n) => boxComponent(lm.ev, n);
832
1071
  /** The SVG. It does not read the detail's state, so a hover re-renders the detail, not every box and edge. */
833
- const Structure = memo(function Structure({ lm, scene, svgRef, rates, setTip, onClick, selected }) {
1072
+ const Structure = memo(function Structure({ lm, scene, hand, svgRef, rates, setTip, onClick, selected }) {
834
1073
  const marker = useId().replace(/:/g, "");
835
1074
  const { labels, ev } = lm;
836
1075
  const stateOf = (n) => {
@@ -892,6 +1131,14 @@ const Structure = memo(function Structure({ lm, scene, svgRef, rates, setTip, on
892
1131
  };
893
1132
  };
894
1133
  const summary = scene.nodes.map((n) => labels.nodes[n.id]?.title).join(", ");
1134
+ const ranks = rankLabels(ev, lm.model);
1135
+ const staying = lm.scene && new Set(lm.scene.nodes.map((n) => n.id));
1136
+ const boxes = (c) => scene.nodes.filter((n) => componentOf(lm, n) === c && (!staying || staying.has(n.id)));
1137
+ const first = ev.firstLimit;
1138
+ const was = hand && hand.from !== first?.id && ev.components[hand.from] ? ev.components[hand.from] : null;
1139
+ const wasBoxes = was ? boxes(was.id) : [];
1140
+ const wasRank = was && ranks.get(was.id);
1141
+ const note = was && (wasRank ? wasRank.text : `now ${multiple(edge(was), lm.model.searchMax)}`);
895
1142
  return /* @__PURE__ */ jsxs("svg", {
896
1143
  ref: svgRef,
897
1144
  viewBox: `0 0 ${scene.width} ${scene.height}`,
@@ -929,7 +1176,30 @@ const Structure = memo(function Structure({ lm, scene, svgRef, rates, setTip, on
929
1176
  focused: !!lm.focus && componentOf(lm, n) === lm.focus,
930
1177
  selected: selected === n.id,
931
1178
  handlers: handlers(n)
932
- }, n.id))
1179
+ }, n.id)),
1180
+ was && wasBoxes.map((n) => /* @__PURE__ */ jsx(Halo, {
1181
+ n,
1182
+ kind: "was",
1183
+ component: was.id
1184
+ }, `was-${n.id}`)),
1185
+ first && boxes(first.id).map((n, i) => /* @__PURE__ */ jsx(Halo, {
1186
+ n,
1187
+ kind: "limit",
1188
+ component: first.id,
1189
+ tag: i ? void 0 : ranks.get(first.id)?.text
1190
+ }, n.id)),
1191
+ [...ranks].map(([c, r]) => {
1192
+ const n = r.rank > 1 && c !== was?.id ? boxes(c)[0] : void 0;
1193
+ return n && /* @__PURE__ */ jsx(Badge, {
1194
+ n,
1195
+ text: r.text
1196
+ }, c);
1197
+ }),
1198
+ note && wasBoxes[0] && /* @__PURE__ */ jsx(Badge, {
1199
+ n: wasBoxes[0],
1200
+ text: note,
1201
+ was: !wasRank
1202
+ })
933
1203
  ]
934
1204
  });
935
1205
  });
@@ -1138,6 +1408,105 @@ function Node({ n, state, title, sub, lm, focused, selected, handlers }) {
1138
1408
  ]
1139
1409
  });
1140
1410
  }
1411
+ /** A tag's width in scene units for its text: wide enough for any digit, so the same text keeps its size. */
1412
+ const tagWidth = (text, perChar) => Math.ceil(12 + text.length * perChar);
1413
+ /**
1414
+ * A ring around the limit's box with a soft glow and, on one box of it, a "LIMIT · 18×" tag on its top edge. The
1415
+ * engine scales the ring and its glow on its own clock (`data-ring`) and fades the whole in and out; it is a layer of
1416
+ * its own, so it moves nothing else, and it fades with its box. The tag stays still: text drawn scaled by a fraction
1417
+ * is not rasterised the same twice, and a recording must be. `was`: the old limit's ring while it fades, without a tag.
1418
+ */
1419
+ function Halo({ n, kind, component, tag }) {
1420
+ const d = 7;
1421
+ const box = {
1422
+ x: n.x - d,
1423
+ y: n.y - d,
1424
+ width: n.w + 14,
1425
+ height: n.h + 14,
1426
+ rx: 10
1427
+ };
1428
+ const w = tag ? tagWidth(tag, 5.6) : 0;
1429
+ const tx = n.x + n.w + d - w - 8;
1430
+ return /* @__PURE__ */ jsxs("g", {
1431
+ className: "lm-halo",
1432
+ "data-halo": kind,
1433
+ "data-component": component,
1434
+ "data-fade": n.fade ?? 1,
1435
+ pointerEvents: "none",
1436
+ children: [/* @__PURE__ */ jsxs("g", {
1437
+ "data-ring": "",
1438
+ "data-cx": n.x + n.w / 2,
1439
+ "data-cy": n.y + n.h / 2,
1440
+ children: [/* @__PURE__ */ jsx("rect", {
1441
+ ...box,
1442
+ "data-glow": "",
1443
+ fill: "none",
1444
+ stroke: "var(--lm-drop)",
1445
+ strokeWidth: "10",
1446
+ strokeOpacity: ".22"
1447
+ }), /* @__PURE__ */ jsx("rect", {
1448
+ ...box,
1449
+ fill: "none",
1450
+ stroke: "var(--lm-drop)",
1451
+ strokeWidth: "2.5"
1452
+ })]
1453
+ }), tag && /* @__PURE__ */ jsxs("g", {
1454
+ "data-tag": tag,
1455
+ children: [/* @__PURE__ */ jsx("rect", {
1456
+ x: tx,
1457
+ y: box.y - 7.5,
1458
+ width: w,
1459
+ height: "15",
1460
+ rx: "3",
1461
+ fill: "var(--lm-drop)"
1462
+ }), /* @__PURE__ */ jsx("text", {
1463
+ x: tx + w / 2,
1464
+ y: box.y + 3.5,
1465
+ textAnchor: "middle",
1466
+ fontSize: "9.5",
1467
+ fontWeight: "600",
1468
+ letterSpacing: ".06em",
1469
+ fill: "var(--lm-surface)",
1470
+ children: tag
1471
+ })]
1472
+ })]
1473
+ });
1474
+ }
1475
+ /**
1476
+ * A quiet badge on a box's top edge: where the next in line reach the target ("2nd · 20×"), or, while the limit
1477
+ * passes on, where the old limit is now. No ring, no breathing. `was`: a note the engine fades in and out.
1478
+ */
1479
+ function Badge({ n, text, was }) {
1480
+ const w = tagWidth(text, 4.9);
1481
+ const tx = n.x + n.w - w - 6;
1482
+ return /* @__PURE__ */ jsx("g", {
1483
+ className: "lm-badge",
1484
+ "data-badge": text,
1485
+ "data-was": was || void 0,
1486
+ opacity: n.fade,
1487
+ pointerEvents: "none",
1488
+ children: /* @__PURE__ */ jsxs("g", {
1489
+ "data-show": "",
1490
+ children: [/* @__PURE__ */ jsx("rect", {
1491
+ x: tx,
1492
+ y: n.y - 7,
1493
+ width: w,
1494
+ height: "14",
1495
+ rx: "7",
1496
+ fill: "var(--lm-surface)",
1497
+ stroke: "var(--lm-muted)"
1498
+ }), /* @__PURE__ */ jsx("text", {
1499
+ x: tx + w / 2,
1500
+ y: n.y + 3,
1501
+ textAnchor: "middle",
1502
+ fontSize: "8.5",
1503
+ fontWeight: "600",
1504
+ fill: "var(--lm-muted)",
1505
+ children: text
1506
+ })]
1507
+ })
1508
+ });
1509
+ }
1141
1510
  /**
1142
1511
  * A box's component in words, over the board: utilization, demand against capacity and where it came from, where it
1143
1512
  * reaches the target and 100 % (with dates, given growth), and its target in each scenario. HTML, so exports leave it out.
@@ -1210,16 +1579,17 @@ function Detail({ lm, scene, id, board, detailId }) {
1210
1579
  const at = (r, x) => r ? reach(r, model.searchMax, model.dateAt) : multiple(x, model.searchMax);
1211
1580
  const num = (x) => Math.abs(x) >= 1e3 ? compact(x) : fmt(x);
1212
1581
  const cap = c.capacity;
1582
+ const rank = rankLabels(ev, model).get(c.id);
1213
1583
  return /* @__PURE__ */ jsxs("div", {
1214
1584
  id: detailId,
1215
1585
  className: "lm-tip lm-detail",
1216
1586
  role: "tooltip",
1217
1587
  ref: tip,
1218
1588
  children: [
1219
- /* @__PURE__ */ jsxs("b", { children: [c.name, c === ev.firstLimit && /* @__PURE__ */ jsx("span", {
1589
+ /* @__PURE__ */ jsxs("b", { children: [c.name, rank && /* @__PURE__ */ jsx("span", {
1220
1590
  className: "lm-tag",
1221
- "data-kind": "first",
1222
- children: "first limit"
1591
+ "data-kind": rank.rank === 1 ? "first" : "rank",
1592
+ children: rank.word
1223
1593
  })] }),
1224
1594
  c.util == null || !cap ? /* @__PURE__ */ jsx("span", { children: "switched off" }) : /* @__PURE__ */ jsxs(Fragment, { children: [
1225
1595
  /* @__PURE__ */ jsxs("span", { children: [
@@ -1454,7 +1824,7 @@ function Limits({ lm, title = "Where it runs out, in order" }) {
1454
1824
  c === ev.firstLimit && /* @__PURE__ */ jsx("span", {
1455
1825
  className: "lm-tag",
1456
1826
  "data-kind": "first",
1457
- children: "first limit"
1827
+ children: "limit"
1458
1828
  }),
1459
1829
  c.capacity?.provenance === "estimated" ? /* @__PURE__ */ jsxs("span", {
1460
1830
  className: "lm-tag",
@@ -1815,7 +2185,7 @@ function VerdictLine({ lm, verdict = true, extra = 0, open, assumptions }) {
1815
2185
  })
1816
2186
  ] }),
1817
2187
  " ",
1818
- "· first limit ",
2188
+ "· limited by ",
1819
2189
  /* @__PURE__ */ jsx("b", { children: first.name }),
1820
2190
  sure && /* @__PURE__ */ jsx("span", {
1821
2191
  className: "lm-tag",
@@ -2075,17 +2445,18 @@ function Sparkline({ lm }) {
2075
2445
  /**
2076
2446
  * The model's story, a step at a time: the step's text, where it is, play, back and next. ← and → move through it
2077
2447
  * while the bar has the keyboard focus (the sliders keep their own arrows). The text keeps the room of the longest
2078
- * step, so stepping through it never moves what is below.
2448
+ * step, so stepping through it never moves what is below. While it plays, the bar shows the step played from the
2449
+ * moment the camera heads for it, before its values change.
2079
2450
  */
2080
2451
  function Story({ lm }) {
2081
2452
  const bar = useRef(null);
2082
2453
  const { story } = lm.model;
2083
2454
  if (!story.length) return null;
2084
- const i = lm.step;
2455
+ const i = lm.cue?.step ?? lm.step;
2085
2456
  const last = story.length - 1;
2086
2457
  const go = (to) => {
2087
2458
  const next = Math.max(0, Math.min(last, to));
2088
- if (next !== i) lm.setStep(next);
2459
+ if (next !== i || lm.storyPlaying) lm.setStep(next);
2089
2460
  };
2090
2461
  const keys = (e) => {
2091
2462
  if (e.key === "ArrowRight") go(i == null ? 0 : i + 1);
@@ -2138,7 +2509,7 @@ function Story({ lm }) {
2138
2509
  "aria-hidden": "true",
2139
2510
  children: story.map((st, k) => /* @__PURE__ */ jsx("i", {
2140
2511
  "data-state": k < i ? "done" : k === i ? "now" : void 0,
2141
- style: k === i ? { "--lm-step": `${st.seconds}s` } : void 0
2512
+ style: k === i ? { "--lm-step": `${lm.cue?.seconds ?? stepSeconds(st, lm.engine.transition)}s` } : void 0
2142
2513
  }, k === i ? `${k}-${playing}` : k))
2143
2514
  }),
2144
2515
  /* @__PURE__ */ jsxs("span", {
@@ -2220,6 +2591,17 @@ const LOADMAP_CSS = `
2220
2591
  .lm-board { position: relative; background: var(--lm-surface); border: 1px solid var(--lm-line); border-radius: 8px; overflow: hidden; margin: 0 auto; width: 100%; }
2221
2592
  .lm-board svg { display: block; width: 100%; height: auto; font-family: var(--lm-font); }
2222
2593
  .lm-board canvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
2594
+ /* the story's title over the board while it plays: a lower third, at the top when what is framed is low; a strip
2595
+ across the top of a narrow board */
2596
+ .lm-title { position: absolute; left: 12px; bottom: 12px; z-index: 1; display: grid; gap: 1px; max-width: min(34rem, calc(100% - 24px)); padding: 7px 12px 8px; border-left: 4px solid var(--lm-accent); border-radius: 0 6px 6px 0; background: color-mix(in srgb, var(--lm-surface) 92%, transparent); box-shadow: 0 4px 16px rgb(0 0 0 / .12); pointer-events: none; overflow-wrap: anywhere; }
2597
+ .lm-title[data-top] { top: 12px; bottom: auto; }
2598
+ .lm-title b { font-size: .95rem; font-weight: 600; }
2599
+ .lm-title span { font: 500 .8rem var(--lm-mono); color: var(--lm-muted); }
2600
+ @container (max-width: 520px) {
2601
+ .lm-title, .lm-title[data-top] { top: 0; right: 0; bottom: auto; left: 0; max-width: none; padding: 4px 8px; border-radius: 0; box-shadow: none; }
2602
+ .lm-title b { font-size: .8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
2603
+ .lm-title span { display: none; }
2604
+ }
2223
2605
  .lm-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: .78rem; color: var(--lm-muted); }
2224
2606
  .lm-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: -1px; margin-right: 5px; }
2225
2607
 
@@ -2370,7 +2752,7 @@ const LOADMAP_CSS = `
2370
2752
  .lm-tag { font: 600 .64rem var(--lm-font); text-transform: uppercase; letter-spacing: .06em; border-radius: 3px; padding: 1px 5px; margin-left: 6px; vertical-align: 1px; }
2371
2753
  .lm-tag[data-kind="first"] { color: var(--lm-surface); background: var(--lm-drop); }
2372
2754
  .lm-tag[data-kind="estimated"] { color: var(--lm-warn); border: 1px solid currentColor; }
2373
- .lm-tag[data-kind="range"] { color: var(--lm-muted); border: 1px solid currentColor; }
2755
+ .lm-tag[data-kind="range"], .lm-tag[data-kind="rank"] { color: var(--lm-muted); border: 1px solid currentColor; }
2374
2756
  .lm-track { position: relative; height: 16px; background: var(--lm-bg); border-radius: 3px; overflow: hidden; }
2375
2757
  .lm-fill { position: absolute; inset: 0 auto 0 0; background: var(--lm-ok); transition: width .25s ease; }
2376
2758
  .lm-fill[data-state="warn"] { background: var(--lm-warn); } .lm-fill[data-state="over"] { background: var(--lm-drop); }
@@ -2477,6 +2859,19 @@ const TOKENS = [
2477
2859
  ];
2478
2860
  /** The stroke of a hollow dot, in scene units. */
2479
2861
  const RING = 1.4;
2862
+ /** Seconds a handover the reader made stays, its note on the old limit with it. */
2863
+ const HAND_KEEP = 2.5;
2864
+ /** Seconds the breathing of the first limit takes, in and out. */
2865
+ const BREATH = 2.4;
2866
+ /** Sets an attribute only when it changes: most frames change nothing. */
2867
+ function set(el, name, value) {
2868
+ if (el.getAttribute(name) !== value) el.setAttribute(name, value);
2869
+ }
2870
+ /** Sets a group's opacity, and none when it shows whole: the SVG of a ring that shows whole stays as it was. */
2871
+ function fade(el, show) {
2872
+ if (show >= 1) el.removeAttribute("opacity");
2873
+ else set(el, "opacity", show.toFixed(3));
2874
+ }
2480
2875
  /**
2481
2876
  * Everything that moves, without React: the sim, the dots on the canvas, the worker fills in the SVG, and the move from
2482
2877
  * one structure to the next. React renders the structure once per scene, and once per frame while it moves; the engine
@@ -2505,6 +2900,13 @@ var Engine = class {
2505
2900
  storyStep = null;
2506
2901
  drawnFocus = null;
2507
2902
  paused = false;
2903
+ /** Flights cut and the first limit holds still. */
2904
+ reducedMotion = false;
2905
+ /** The part of the scene the board shows while the story plays; null for all of it. */
2906
+ camera = null;
2907
+ /** What the diagram says about the step the story plays; null when it does not play. */
2908
+ cue = null;
2909
+ host = null;
2508
2910
  colors = {};
2509
2911
  svg = null;
2510
2912
  canvas = null;
@@ -2520,6 +2922,10 @@ var Engine = class {
2520
2922
  frame: -1,
2521
2923
  paths: /* @__PURE__ */ new Map()
2522
2924
  };
2925
+ /** Seconds the engine has run since the last reset: the first limit breathes on it. */
2926
+ #clock = 0;
2927
+ #story = null;
2928
+ #hand = null;
2523
2929
  constructor(model, view, ev, o = {}) {
2524
2930
  this.model = model;
2525
2931
  this.view = view;
@@ -2530,8 +2936,43 @@ var Engine = class {
2530
2936
  setEvaluation(ev) {
2531
2937
  if (ev === this.ev) return;
2532
2938
  const same = JSON.stringify(ev.values) === JSON.stringify(this.ev.values);
2939
+ const was = this.ev.firstLimit?.id;
2940
+ const now = ev.firstLimit?.id;
2533
2941
  this.ev = ev;
2534
2942
  if (!same) this.sim.update(ev);
2943
+ if (was === now) return;
2944
+ const story = this.#story?.shot === "change";
2945
+ const still = this.paused && !this.#driven;
2946
+ this.#setHand(was && now && this.frame && !still ? {
2947
+ from: was,
2948
+ to: now,
2949
+ t: 0,
2950
+ fade: this.#hand?.to === was ? this.#shows(this.#hand, false) : 1,
2951
+ ...story ? {
2952
+ out: .5,
2953
+ in: .6,
2954
+ inAt: Infinity
2955
+ } : {
2956
+ out: .3,
2957
+ in: .3,
2958
+ inAt: this.reducedMotion ? 0 : .3
2959
+ },
2960
+ story
2961
+ } : null);
2962
+ }
2963
+ /** How much of a handover's old ring (`was`) or new one shows now. */
2964
+ #shows(h, was) {
2965
+ const part = (t, d) => this.reducedMotion ? t >= 0 ? 1 : 0 : ease(Math.max(0, Math.min(1, t / d)));
2966
+ return was ? h.fade * (1 - part(h.t, h.out)) : part(h.t - h.inAt, h.in);
2967
+ }
2968
+ /** The limit passing to another component now, if it is: the diagram draws the old ring while it fades. */
2969
+ get handover() {
2970
+ return this.#hand;
2971
+ }
2972
+ #setHand(h) {
2973
+ if (h === this.#hand) return;
2974
+ this.#hand = h;
2975
+ for (const f of this.#listeners) f();
2535
2976
  }
2536
2977
  /**
2537
2978
  * Takes a new scene. A new structure moves in from what is drawn now, unless `cut`, the diagram turned (a new
@@ -2569,6 +3010,8 @@ var Engine = class {
2569
3010
  seed: seed ?? this.#o.seed
2570
3011
  });
2571
3012
  this.#paths.clear();
3013
+ this.#clock = 0;
3014
+ this.#setHand(null);
2572
3015
  if (this.#move && this.scene) {
2573
3016
  this.#move = null;
2574
3017
  this.#show(this.scene);
@@ -2589,6 +3032,7 @@ var Engine = class {
2589
3032
  });
2590
3033
  if (fresh) this.resize();
2591
3034
  this.drawFills();
3035
+ this.applyCamera();
2592
3036
  }
2593
3037
  /** Reads the theme tokens from the element that carries them. Call again when the theme changes. */
2594
3038
  readColors(root) {
@@ -2617,15 +3061,224 @@ var Engine = class {
2617
3061
  c.height = h;
2618
3062
  this.draw();
2619
3063
  }
2620
- /** Advances the sim, and the move to a new structure, by `dtReal` seconds. */
3064
+ /** Advances the sim, the move to a new structure and the story played, by `dtReal` seconds. */
2621
3065
  step(dtReal, o = {}) {
2622
3066
  this.#driven = this.paused;
2623
3067
  this.sim.step(dtReal, o);
3068
+ if (dtReal > 0) this.#clock += dtReal;
3069
+ const h = this.#hand;
3070
+ if (h && dtReal > 0 && this.#caughtUp()) {
3071
+ h.t += dtReal;
3072
+ if (!h.story && h.t >= HAND_KEEP) this.#setHand(null);
3073
+ }
2624
3074
  const m = this.#move;
2625
- if (!m || dtReal <= 0) return;
2626
- m.t += dtReal / m.seconds;
2627
- if (m.t >= 1) this.#move = null;
2628
- this.#show(m.at(m.t));
3075
+ if (m && dtReal > 0) {
3076
+ m.t += dtReal / m.seconds;
3077
+ if (m.t >= 1) this.#move = null;
3078
+ this.#show(m.at(m.t));
3079
+ }
3080
+ if (this.#story) this.#direct(dtReal);
3081
+ }
3082
+ /** The story plays: from the whole diagram, step `from` and the ones after it. */
3083
+ get playing() {
3084
+ return !!this.#story && this.#story.shot !== "back";
3085
+ }
3086
+ /**
3087
+ * Plays the story from step `from` on the engine's clock: the whole diagram, then for each step a flight in to what it
3088
+ * changes, the change, a flight out and the whole diagram for its seconds. The host shows each step's values. False
3089
+ * when it cannot: nothing is drawn yet, or there is no such step.
3090
+ */
3091
+ playStory(from) {
3092
+ if (!this.frame || !this.model.story[from]) return false;
3093
+ this.#story = {
3094
+ i: from - 1,
3095
+ shot: "establish",
3096
+ t: 0,
3097
+ from: whole(this.frame),
3098
+ close: whole(this.frame),
3099
+ zoom: false,
3100
+ targets: [],
3101
+ components: [],
3102
+ before: this.ev,
3103
+ waiting: false,
3104
+ hand: null
3105
+ };
3106
+ this.#setCamera(null);
3107
+ this.host?.playing(true);
3108
+ return true;
3109
+ }
3110
+ /** Stops the story: the camera flies back to the whole diagram (cuts with `cut`), and the step shown stays. */
3111
+ stopStory(o = {}) {
3112
+ const s = this.#story;
3113
+ if (!s) return;
3114
+ this.cue = null;
3115
+ this.host?.cue(null);
3116
+ if (this.#hand?.story) this.#setHand(null);
3117
+ if (s.shot !== "back") this.host?.playing(false);
3118
+ if (o.cut || !this.camera || this.reducedMotion) {
3119
+ this.#story = null;
3120
+ this.#setCamera(null);
3121
+ } else this.#story = {
3122
+ ...s,
3123
+ shot: "back",
3124
+ t: 0,
3125
+ from: this.camera
3126
+ };
3127
+ }
3128
+ /** The page has drawn the values and the structure the engine has: a step's change can run. */
3129
+ #caughtUp() {
3130
+ return !!this.scene && this.scene.key === this.wantKey && this.drawnValues === JSON.stringify(this.ev.values);
3131
+ }
3132
+ #length(s) {
3133
+ if (s.shot === "change") return changeSeconds(this.transition);
3134
+ if (s.shot === "hand") return SHOTS.hand;
3135
+ if (s.shot === "wide") return this.model.story[s.i]?.seconds ?? 0;
3136
+ return SHOTS[s.shot];
3137
+ }
3138
+ /** For tests and recordings: the shot the story is at. */
3139
+ get story() {
3140
+ const s = this.#story;
3141
+ return s && {
3142
+ i: s.i,
3143
+ shot: s.shot,
3144
+ t: s.t
3145
+ };
3146
+ }
3147
+ #direct(dt) {
3148
+ const s = this.#story;
3149
+ const frame = this.frame;
3150
+ if (!frame) return;
3151
+ if (s.waiting) {
3152
+ if (!this.#caughtUp()) return;
3153
+ s.waiting = false;
3154
+ }
3155
+ s.t += dt;
3156
+ while (this.#story === s && s.t >= this.#length(s) && !s.waiting) {
3157
+ s.t -= this.#length(s);
3158
+ this.#next(s);
3159
+ }
3160
+ if (this.#story !== s) return;
3161
+ const p = this.#length(s) > 0 ? s.t / this.#length(s) : 1;
3162
+ const cut = this.reducedMotion;
3163
+ const all = whole(frame);
3164
+ let cam;
3165
+ if (s.shot === "in") cam = cut ? s.close : between(all, s.close, p);
3166
+ else if (s.shot === "close") cam = s.close;
3167
+ else if (s.shot === "change") cam = s.zoom ? framing(frame, s.targets, { hold: true }) : all;
3168
+ else if (s.shot === "hand") {
3169
+ const to = framing(frame, this.#boxesOf(s.hand?.to));
3170
+ cam = cut ? to : between(s.from, to, s.t / 1);
3171
+ } else if (s.shot === "out" || s.shot === "back") cam = cut ? all : between(s.from, all, p);
3172
+ else cam = all;
3173
+ this.#setCamera(cam);
3174
+ const h = this.#hand;
3175
+ if (s.hand && h?.story && (cut || h.t >= h.out) && this.cue && this.cue.detail !== s.hand.text) this.#cue({
3176
+ ...this.cue,
3177
+ detail: s.hand.text
3178
+ });
3179
+ }
3180
+ /** The boxes a component is drawn as, in the frame. */
3181
+ #boxesOf(c) {
3182
+ return (this.frame?.nodes ?? []).filter((n) => c && boxComponent(this.ev, n) === c).map((n) => n.id);
3183
+ }
3184
+ /** The shot after `s.shot`. */
3185
+ #next(s) {
3186
+ const frame = this.frame;
3187
+ const story = this.model.story;
3188
+ if (s.shot === "back") {
3189
+ this.#story = null;
3190
+ this.#setCamera(null);
3191
+ return;
3192
+ }
3193
+ if (s.shot === "establish" || s.shot === "wide") {
3194
+ if (this.#hand?.story) this.#setHand(null);
3195
+ if (s.i + 1 >= story.length) {
3196
+ this.#story = null;
3197
+ this.#setCamera(null);
3198
+ this.cue = null;
3199
+ this.host?.cue(null);
3200
+ this.host?.playing(false);
3201
+ return;
3202
+ }
3203
+ const i = s.i + 1;
3204
+ const st = story[i];
3205
+ const after = this.model.evaluate(st.values);
3206
+ const host = this.host;
3207
+ const ids = host ? targets(frame, this.ev, after, {
3208
+ a: host.shapes(this.ev),
3209
+ b: host.shapes(after)
3210
+ }, st.focus) : [];
3211
+ const nodes = frame.nodes.filter((n) => ids.includes(n.id));
3212
+ const components = [...new Set(nodes.map((n) => boxComponent(after, n)).filter((c) => c != null))];
3213
+ const close = framing(frame, ids);
3214
+ const zoom = close.w < frame.width - 1e-6;
3215
+ const text = handoverText(this.ev, after, this.model);
3216
+ const hand = text ? {
3217
+ text,
3218
+ to: after.firstLimit.id
3219
+ } : null;
3220
+ Object.assign(s, {
3221
+ i,
3222
+ shot: "in",
3223
+ from: whole(frame),
3224
+ close,
3225
+ zoom,
3226
+ targets: ids,
3227
+ components,
3228
+ before: this.ev,
3229
+ hand
3230
+ });
3231
+ this.#cue({
3232
+ step: i,
3233
+ title: stepTitle(this.model, this.ev.values, st, i),
3234
+ detail: stepDetail(this.ev, null, components),
3235
+ after: false,
3236
+ top: close.y + close.h / 2 > frame.height * .55,
3237
+ seconds: stepSeconds(st, this.transition, !!hand)
3238
+ });
3239
+ return;
3240
+ }
3241
+ if (s.shot === "in") s.shot = "close";
3242
+ else if (s.shot === "close") {
3243
+ s.shot = "change";
3244
+ s.waiting = true;
3245
+ this.host?.show(s.i);
3246
+ this.#cue({
3247
+ ...this.cue,
3248
+ detail: stepDetail(s.before, this.ev, s.components),
3249
+ after: true
3250
+ });
3251
+ } else if (s.shot === "change" && s.hand) {
3252
+ s.shot = "hand";
3253
+ s.from = this.camera ?? whole(frame);
3254
+ const cam = s.from;
3255
+ const ids = this.#boxesOf(s.hand.to);
3256
+ const inView = frame.nodes.filter((n) => ids.includes(n.id)).every((n) => n.x >= cam.x && n.y >= cam.y && n.x + n.w <= cam.x + cam.w && n.y + n.h <= cam.y + cam.h);
3257
+ if (this.#hand?.story) this.#hand.inAt = this.#hand.t + (this.reducedMotion || inView ? 0 : 1);
3258
+ } else if (s.shot === "change" || s.shot === "hand") {
3259
+ s.shot = "out";
3260
+ s.from = this.camera ?? whole(frame);
3261
+ const h = this.#hand;
3262
+ if (h?.story && !Number.isFinite(h.inAt)) h.inAt = h.t;
3263
+ } else if (s.shot === "out") s.shot = "wide";
3264
+ }
3265
+ #cue(c) {
3266
+ this.cue = c;
3267
+ this.host?.cue(c);
3268
+ }
3269
+ #setCamera(cam) {
3270
+ const f = this.frame;
3271
+ const all = f && cam && Math.abs(cam.x) < 1e-6 && Math.abs(cam.y) < 1e-6 && Math.abs(cam.w - f.width) < 1e-6;
3272
+ this.camera = !cam || all ? null : cam;
3273
+ this.applyCamera();
3274
+ }
3275
+ /** Points the SVG at the camera: React draws the whole scene's viewBox, the camera replaces it after each render. */
3276
+ applyCamera() {
3277
+ const f = this.frame;
3278
+ if (!this.svg || !f) return;
3279
+ const c = this.camera ?? whole(f);
3280
+ const box = `${c.x} ${c.y} ${c.w} ${c.h}`;
3281
+ if (this.svg.getAttribute("viewBox") !== box) this.svg.setAttribute("viewBox", box);
2629
3282
  }
2630
3283
  /** Draws the dots on the overlay canvas and the worker fills in the SVG. */
2631
3284
  draw(o = {}) {
@@ -2639,6 +3292,7 @@ var Engine = class {
2639
3292
  this.paintParticles(ctx, c.width / this.frame.width);
2640
3293
  }
2641
3294
  drawFills() {
3295
+ this.drawHalos();
2642
3296
  for (const sl of this.#slots) {
2643
3297
  const b = this.sim.slotBusy(sl.stage, sl.index);
2644
3298
  const h = b < .02 ? 0 : (sl.s - 2) * b;
@@ -2646,6 +3300,34 @@ var Engine = class {
2646
3300
  sl.el.setAttribute("height", h.toFixed(2));
2647
3301
  }
2648
3302
  }
3303
+ /**
3304
+ * The rings: the limit breathes, on the engine's clock, while the story plays and once it is past the target, a scale
3305
+ * and a glow on its own layer, which moves nothing else; a handover fades the old ring and brings the new one in.
3306
+ * Decided here, not by a render, so a recording never waits on one. The page calls it after each render too, so a
3307
+ * new ring never shows a frame before the engine has placed it.
3308
+ */
3309
+ drawHalos() {
3310
+ const first = this.ev.firstLimit;
3311
+ const breathe = !this.reducedMotion && (this.playing || first?.state === "warn" || first?.state === "over");
3312
+ const b = breathe ? .5 - .5 * Math.cos(2 * Math.PI * this.#clock / BREATH) : .5;
3313
+ const h = this.#hand;
3314
+ for (const el of this.svg?.querySelectorAll("g[data-halo]") ?? []) {
3315
+ const was = el.dataset.halo === "was";
3316
+ const coming = !was && h?.to === el.dataset.component;
3317
+ const show = was ? h ? this.#shows(h, true) : 0 : coming ? this.#shows(h, false) : 1;
3318
+ fade(el, Number(el.dataset.fade ?? 1) * show);
3319
+ const ring = el.querySelector("[data-ring]");
3320
+ if (!ring) continue;
3321
+ const [cx, cy] = [Number(ring.dataset.cx), Number(ring.dataset.cy)];
3322
+ const k = (1 + .08 * (1 - show)) * (was || !breathe ? 1 : 1 + .035 * b);
3323
+ set(ring, "transform", k === 1 ? "" : `translate(${cx} ${cy}) scale(${k.toFixed(4)}) translate(${-cx} ${-cy})`);
3324
+ const glow = ring.querySelector("[data-glow]");
3325
+ if (glow) set(glow, "stroke-opacity", (.1 + .25 * b).toFixed(3));
3326
+ }
3327
+ if (!h) return;
3328
+ const part = (t, d) => this.reducedMotion ? t >= 0 ? 1 : 0 : ease(Math.max(0, Math.min(1, t / d)));
3329
+ for (const el of this.svg?.querySelectorAll("g[data-badge][data-was] [data-show]") ?? []) fade(el, part(h.t, h.out) * (h.story ? 1 : 1 - part(h.t - 2.1, .4)));
3330
+ }
2649
3331
  /** Every dot as it is drawn now, in scene units: for the canvas, and for an SVG export. */
2650
3332
  dots() {
2651
3333
  const scene = this.frame;
@@ -2669,11 +3351,18 @@ var Engine = class {
2669
3351
  for (const id of this.#paths.keys()) if (!live.has(id)) this.#paths.delete(id);
2670
3352
  return out;
2671
3353
  }
2672
- /** Paints the dots onto any 2D context, scene units × `scale`: the overlay, or an export frame. */
3354
+ /**
3355
+ * Paints the dots onto any 2D context, scene units × `scale`: the overlay, or an export frame. Under the camera they
3356
+ * go where the SVG shows their places (its viewBox, centred where its shape differs), so they keep matching the bars.
3357
+ */
2673
3358
  paintParticles(ctx, scale) {
2674
- if (!this.frame) return;
3359
+ const f = this.frame;
3360
+ if (!f) return;
3361
+ const c = this.camera ?? whole(f);
3362
+ const [bw, bh] = [f.width * scale, f.height * scale];
3363
+ const k = Math.min(bw / c.w, bh / c.h);
2675
3364
  ctx.save();
2676
- ctx.transform(scale, 0, 0, scale, 0, 0);
3365
+ ctx.transform(k, 0, 0, k, (bw - c.w * k) / 2 - c.x * k, (bh - c.h * k) / 2 - c.y * k);
2677
3366
  for (const d of this.dots()) {
2678
3367
  ctx.globalAlpha = d.alpha;
2679
3368
  ctx.beginPath();
@@ -2729,15 +3418,17 @@ function useLoadmap(model, view, o = {}) {
2729
3418
  model,
2730
3419
  values: init()
2731
3420
  }));
3421
+ const evaluated = useRef(null);
2732
3422
  let values = state.values;
2733
3423
  if (state.model !== model) {
2734
3424
  values = init();
3425
+ evaluated.current = null;
2735
3426
  setState({
2736
3427
  model,
2737
3428
  values
2738
3429
  });
2739
3430
  }
2740
- const evaluated = useRef(null);
3431
+ const fresh = !evaluated.current || evaluated.current.values === values;
2741
3432
  const ev = useMemo(() => evaluated.current?.values === values ? evaluated.current.ev : model.evaluate(values), [model, values]);
2742
3433
  const engineRef = useRef(null);
2743
3434
  if (!engineRef.current || engineRef.current.model !== model || engineRef.current.view !== view) engineRef.current = new Engine(model, view, ev, o);
@@ -2750,8 +3441,15 @@ function useLoadmap(model, view, o = {}) {
2750
3441
  const [laid, setLaid] = useState(null);
2751
3442
  const scene = laid?.view === view ? laid.scene : null;
2752
3443
  const reducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
3444
+ engine.reducedMotion = reducedMotion;
2753
3445
  const narrowBelow = o.narrowBelow ?? 640;
2754
- useEffect(() => engine.setEvaluation(ev), [engine, ev]);
3446
+ useEffect(() => {
3447
+ if (!evaluated.current || evaluated.current.values === values) engine.setEvaluation(ev);
3448
+ }, [
3449
+ engine,
3450
+ ev,
3451
+ values
3452
+ ]);
2755
3453
  useEffect(() => {
2756
3454
  if (!root) return;
2757
3455
  setMeasure(() => canvasMeasure(getComputedStyle(root).fontFamily));
@@ -2775,7 +3473,7 @@ function useLoadmap(model, view, o = {}) {
2775
3473
  direction,
2776
3474
  measure
2777
3475
  ]);
2778
- engine.wantKey = key;
3476
+ if (fresh) engine.wantKey = key;
2779
3477
  const elk = o.elk;
2780
3478
  useEffect(() => {
2781
3479
  if (!measure || !key) return;
@@ -2803,8 +3501,8 @@ function useLoadmap(model, view, o = {}) {
2803
3501
  ]);
2804
3502
  const labels = useMemo(() => labelsAt(view, ev), [view, ev]);
2805
3503
  const valuesRef = useRef(values);
2806
- valuesRef.current = values;
2807
- const setValues = useCallback((v) => {
3504
+ if (fresh) valuesRef.current = values;
3505
+ const apply = useCallback((v) => {
2808
3506
  const next = {
2809
3507
  ...valuesRef.current,
2810
3508
  ...v
@@ -2821,6 +3519,18 @@ function useLoadmap(model, view, o = {}) {
2821
3519
  values: next
2822
3520
  });
2823
3521
  }, [engine, model]);
3522
+ const [played, setPlayed] = useState({
3523
+ engine,
3524
+ on: false,
3525
+ cue: null
3526
+ });
3527
+ const playing = played.engine === engine && played.on;
3528
+ const cue = played.engine === engine ? played.cue : null;
3529
+ const stop = useCallback(() => engine.stopStory(), [engine]);
3530
+ const setValues = useCallback((v) => {
3531
+ stop();
3532
+ apply(v);
3533
+ }, [stop, apply]);
2824
3534
  const scenario = useMemo(() => model.scenarioOf(values), [model, values]);
2825
3535
  const setScenario = useCallback((label) => {
2826
3536
  const s = model.scenarios.find((x) => x.label === label);
@@ -2831,9 +3541,9 @@ function useLoadmap(model, view, o = {}) {
2831
3541
  i: null
2832
3542
  });
2833
3543
  const step = stepState.model === model && stepState.i != null && stepState.i < model.story.length ? stepState.i : null;
2834
- const setStep = useCallback((i) => {
3544
+ const show = useCallback((i) => {
2835
3545
  const st = i == null ? void 0 : model.story[i];
2836
- if (st) setValues(st.values);
3546
+ if (st) apply(st.values);
2837
3547
  engine.wantFocus = st?.focus ?? null;
2838
3548
  engine.storyStep = st ? i : null;
2839
3549
  setStepState({
@@ -2842,36 +3552,61 @@ function useLoadmap(model, view, o = {}) {
2842
3552
  });
2843
3553
  }, [
2844
3554
  model,
2845
- setValues,
3555
+ apply,
2846
3556
  engine
2847
3557
  ]);
3558
+ const setStep = useCallback((i) => {
3559
+ stop();
3560
+ show(i);
3561
+ }, [stop, show]);
2848
3562
  const shown = step == null ? void 0 : model.story[step];
2849
3563
  const stepChanged = !!shown && Object.entries(shown.values).some(([k, v]) => values[k] !== v);
2850
3564
  const focus = shown && !stepChanged ? shown.focus ?? null : null;
2851
3565
  engine.wantFocus = focus;
2852
3566
  engine.storyStep = step;
2853
- const [playing, setPlaying] = useState(false);
2854
- if (playing && (step == null || stepChanged)) setPlaying(false);
2855
- const storyPlaying = playing && step != null && !stepChanged;
3567
+ const measureRef = useRef(measure);
3568
+ measureRef.current = measure;
3569
+ engine.host = {
3570
+ playing: (on) => setPlayed((p) => ({
3571
+ engine,
3572
+ on,
3573
+ cue: on && p.engine === engine ? p.cue : null
3574
+ })),
3575
+ show,
3576
+ cue: (c) => setPlayed((p) => ({
3577
+ engine,
3578
+ on: p.engine === engine && p.on,
3579
+ cue: c
3580
+ })),
3581
+ shapes: (e) => shapesOf(view, e, {
3582
+ direction,
3583
+ measure: measureRef.current ?? void 0
3584
+ })
3585
+ };
2856
3586
  useEffect(() => {
2857
- if (!storyPlaying || step == null) return;
2858
- const last = model.story.length - 1;
2859
- const t = setTimeout(() => step < last ? setStep(step + 1) : setPlaying(false), model.story[step].seconds * 1e3);
2860
- return () => clearTimeout(t);
3587
+ if (playing && stepChanged) stop();
2861
3588
  }, [
2862
- storyPlaying,
2863
- step,
2864
- model,
2865
- setStep
3589
+ playing,
3590
+ stepChanged,
3591
+ stop
2866
3592
  ]);
3593
+ const storyPlaying = playing && model.story.length > 0;
2867
3594
  const setStoryPlaying = useCallback((on) => {
2868
- if (on && (step == null || stepChanged || step === model.story.length - 1)) setStep(step == null || step === model.story.length - 1 ? 0 : step);
2869
- setPlaying(on && model.story.length > 0);
3595
+ if (!on) return stop();
3596
+ const last = model.story.length - 1;
3597
+ if (last < 0) return;
3598
+ const from = step == null || step === last ? 0 : stepChanged ? step : step + 1;
3599
+ if (stepChanged) show(null);
3600
+ engine.playStory(from);
3601
+ if (!document.fullscreenElement && !root?.hasAttribute("data-full")) root?.querySelector(".lm-board")?.scrollIntoView({ block: "nearest" });
2870
3602
  }, [
2871
3603
  step,
2872
3604
  stepChanged,
2873
3605
  model,
2874
- setStep
3606
+ engine,
3607
+ stop,
3608
+ show,
3609
+ root
2875
3610
  ]);
2876
3611
  const [screen, setScreen] = useState(false);
2877
3612
  const [overlay, setOverlay] = useState(false);
@@ -2946,6 +3681,7 @@ function useLoadmap(model, view, o = {}) {
2946
3681
  focus,
2947
3682
  storyPlaying,
2948
3683
  setStoryPlaying,
3684
+ cue: storyPlaying ? cue : null,
2949
3685
  fullScreen,
2950
3686
  toggleFullScreen,
2951
3687
  fullScreenMode,
@@ -3200,6 +3936,11 @@ function handleOf(lm) {
3200
3936
  setValues: (v) => lm.setValues(v),
3201
3937
  setStoryStep: (i) => lm.setStep(i),
3202
3938
  storyStep: () => engine.storyStep,
3939
+ playStory: () => {
3940
+ const start = engine.ev;
3941
+ if (!engine.playStory(0)) throw new Error("loadmap: the story cannot play: nothing is drawn yet, or there is none");
3942
+ return storySeconds(engine.model, start, engine.transition);
3943
+ },
3203
3944
  reset: (seed) => engine.reset(seed),
3204
3945
  step: (dt) => {
3205
3946
  engine.step(dt, { particles: true });