@intentius/behold 0.29.40 → 0.29.42
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.
- package/AGENTS.md +180 -10
- package/README.md +6 -2
- package/dist/cli.js +16121 -13141
- package/example-writes/README.md +1 -1
- package/example-writes/chant.config.ts +0 -6
- package/package.json +5 -2
- package/web/app.js +327 -21
- package/web/carve-export.js +142 -0
- package/web/carve-narration.js +294 -0
- package/web/carve-narration.test.js +48 -0
- package/web/change-set-overlay.js +147 -0
- package/web/change-set-overlay.test.js +83 -0
- package/web/dots-border.js +66 -0
- package/web/dots-border.test.js +27 -0
- package/web/dots-kiosk.js +279 -0
- package/web/dots-kiosk.test.js +60 -0
- package/web/dots-layout.d.ts +10 -0
- package/web/dots-layout.js +201 -0
- package/web/dots-live.js +316 -0
- package/web/dots-live.test.js +220 -0
- package/web/dots-lod.js +122 -0
- package/web/dots-lod.test.js +78 -0
- package/web/dots-recording.d.ts +7 -0
- package/web/dots-recording.js +247 -0
- package/web/dots-recording.test.js +71 -0
- package/web/dots-scene.js +294 -0
- package/web/dots-scene.test.js +118 -0
- package/web/dots-timeline.js +128 -0
- package/web/dots-timeline.test.js +83 -0
- package/web/dots-tour.js +153 -0
- package/web/dots.js +2124 -0
- package/web/dots.test.js +15 -0
- package/web/event-help-modal.js +1 -1
- package/web/index.html +215 -121
- package/web/kiosk-carve.js +225 -0
- package/web/kiosk-carve.test.js +26 -0
- package/web/kiosk-event-help-ui.js +7 -1
- package/web/kiosk-link.js +68 -0
- package/web/kiosk-link.test.js +69 -0
- package/web/kiosk-metrics.js +2 -2
- package/web/kiosk-trace.js +1 -1
- package/web/kiosk.html +186 -135
- package/web/kiosk.js +65 -7
- package/web/motion.js +42 -0
- package/web/motion.test.js +64 -0
- package/web/operator.js +1 -1
- package/web/run-playhead.js +102 -19
- package/web/run-playhead.test.js +81 -11
- package/web/terralith.html +2 -2
- package/web/theme.js +99 -26
- package/web/theme.test.js +70 -6
- package/web/tooltip.js +212 -0
- package/web/tooltip.test.js +193 -0
- package/web/tour-player.js +204 -0
- package/web/tour-player.test.js +115 -0
- package/web/tours/behold.tour.v1.schema.json +182 -0
- package/web/tours/terralith.json +330 -0
- package/web/trunk-lane.js +406 -0
- package/web/trunk-lane.test.js +224 -0
- package/web/workspace-gates.js +29 -4
- package/web/workspace-gates.test.js +23 -2
package/web/dots.js
ADDED
|
@@ -0,0 +1,2124 @@
|
|
|
1
|
+
// The dots layer (#712, #715): the zoom past the cards, where each resource
|
|
2
|
+
// is a dot in its estate's box and each estate, further out, a disc. It is
|
|
3
|
+
// mounted by two drivers and nothing else:
|
|
4
|
+
//
|
|
5
|
+
// - live (#715): the estate has choudoufu roots or estate rosters, and
|
|
6
|
+
// /api/terragucci/carve answers its rosters and migration progress
|
|
7
|
+
// (web/dots-live.js). The layer polls that route and plays each line as it
|
|
8
|
+
// arrives. In the kiosk's frame it also directs the camera while a
|
|
9
|
+
// migration runs and tells the kiosk what to say (web/kiosk-carve.js).
|
|
10
|
+
// - a recording: the local preview server's meta carries `dots`
|
|
11
|
+
// (scripts/dots-preview.ts), or the static carve export hands one in
|
|
12
|
+
// (web/carve-export.js).
|
|
13
|
+
//
|
|
14
|
+
// With neither, every export here is a no-op and the app behaves as it always
|
|
15
|
+
// has.
|
|
16
|
+
//
|
|
17
|
+
// Cards stay in pinhole's SVG. Dots, estate boxes and discs are drawn on one
|
|
18
|
+
// Canvas 2D layer under it, through the same viewBox camera. Each mark picks
|
|
19
|
+
// its form from its size on screen (web/dots-lod.js), and the morph follows
|
|
20
|
+
// the zoom. The replay plays a real recording (web/dots-recording.js) through
|
|
21
|
+
// the choreography in web/dots-scene.js.
|
|
22
|
+
|
|
23
|
+
import { readCarve, carveShow, walkthroughShow, phaseAt } from "./dots-recording.js";
|
|
24
|
+
import { parentsOf } from "./dots-layout.js";
|
|
25
|
+
import { buildScene, takeoffsBy, firstLandings, flightPoint, easeInOut, replayClock, BOX } from "./dots-scene.js";
|
|
26
|
+
import { cardMorph, discMorph, discRadius, levelOf, caption, railMarks, railPos, lerp } from "./dots-lod.js";
|
|
27
|
+
import { perimeter, pointAt, nearestS, segment } from "./dots-border.js";
|
|
28
|
+
import { getTokens, onThemeChange, contrast, hexToOklch, oklchToHex } from "./theme.js";
|
|
29
|
+
import { LOOP, carveStory, beatAt, tipsBetween, directorFocus, shortName as shortOf, liveBeat, liveTipsBetween, liveEndTip, LIVE_HOLD_MS } from "./dots-kiosk.js";
|
|
30
|
+
import { liveShow, stampArrivals, showKey, currentMigration, boxName, playedCounts, landedWrite } from "./dots-live.js";
|
|
31
|
+
import { provenanceText, migrationTool, stepMsFor } from "./tour-player.js";
|
|
32
|
+
import { tourNow, tourFocus, tourFx, fxKey, tourMessage } from "./dots-tour.js";
|
|
33
|
+
import { wallTicks, recordedLabel, recordedMarks, speedWords, skippedWords, clockWords, RECORDED_SPEEDS } from "./dots-timeline.js";
|
|
34
|
+
|
|
35
|
+
/** #715: how often the live layer asks for the carve. The route shares one read for 1.5 s while a migration runs. */
|
|
36
|
+
export const LIVE_POLL_MS = 2_000;
|
|
37
|
+
/** The live carve as last read, kept across a remount (a graph re-render), so arrivals keep their times. */
|
|
38
|
+
let liveMemo = null;
|
|
39
|
+
/** Slowed playback (the kiosk's "slowed ×N"): 1 is real time, the default; kept across a remount, never stored. */
|
|
40
|
+
let liveSlowed = 1;
|
|
41
|
+
|
|
42
|
+
let S = null;
|
|
43
|
+
let styleAdded = false;
|
|
44
|
+
|
|
45
|
+
const RIPPLE_MS = 650;
|
|
46
|
+
const PULSE_MS = 520;
|
|
47
|
+
const DRAW_MS = 650;
|
|
48
|
+
const LINGER_MS = 2600;
|
|
49
|
+
|
|
50
|
+
export function dotsActive() {
|
|
51
|
+
return !!S;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** How far past the fit the zoom may go out, as a multiple of the fit's width; null when not mounted. */
|
|
55
|
+
export function dotsZoomOutFactor() {
|
|
56
|
+
return S ? S.zoomOut : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** #715: whether the live carve's director holds the camera (a migration is running in the kiosk's frame). */
|
|
60
|
+
export function dotsCarving() {
|
|
61
|
+
return !!(S && S.live && S.carving);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The camera moved: draw at the new zoom. */
|
|
65
|
+
export function dotsCamera() {
|
|
66
|
+
if (S) S.dirty = true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function unmountDots() {
|
|
70
|
+
if (!S) return;
|
|
71
|
+
cancelAnimationFrame(S.raf);
|
|
72
|
+
S.off.forEach((f) => f());
|
|
73
|
+
for (const el of [S.canvas, S.ui]) el.remove();
|
|
74
|
+
S.host.classList.remove("dots-on");
|
|
75
|
+
S.host.style.removeProperty("--dots-card-a");
|
|
76
|
+
if (S.svg) {
|
|
77
|
+
S.svg.style.visibility = "";
|
|
78
|
+
S.svg.style.display = "";
|
|
79
|
+
}
|
|
80
|
+
S = null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function addStyle() {
|
|
84
|
+
if (styleAdded) return;
|
|
85
|
+
styleAdded = true;
|
|
86
|
+
const st = document.createElement("style");
|
|
87
|
+
st.textContent = `
|
|
88
|
+
.dots-on > svg { position: relative; z-index: 1; background: transparent; }
|
|
89
|
+
.dots-on > svg > rect[fill^="url(#pin-"] { display: none; }
|
|
90
|
+
.dots-on > svg [data-node-id], .dots-on > svg rect[data-group-id], .dots-on > svg text { opacity: var(--dots-card-a, 1); }
|
|
91
|
+
#dots-canvas { position: absolute; left: 0; top: 0; z-index: 0; pointer-events: none; }
|
|
92
|
+
#dots-ui { position: absolute; inset: 0; pointer-events: none; z-index: 3; font: var(--t-body, 13px)/1.3 var(--font-ui, system-ui); color: var(--fg); }
|
|
93
|
+
#dots-ui > * { pointer-events: auto; }
|
|
94
|
+
.dots-rail { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); width: 132px; height: 190px; padding: 0 0 0 0; border-radius: 8px; background: color-mix(in srgb, var(--panel) 88%, transparent); border: 1px solid var(--line); box-sizing: content-box; padding: 6px 8px 6px 4px; }
|
|
95
|
+
.dots-rail-track { position: absolute; right: 8px; top: 8px; bottom: 8px; width: 3px; border-radius: 2px; background: var(--line); }
|
|
96
|
+
.dots-rail-cam { position: absolute; right: 2px; width: 15px; height: 15px; margin-top: -7.5px; border-radius: 50%; border: 2px solid var(--focus); background: var(--panel); pointer-events: none; }
|
|
97
|
+
.dots-rail-mark { position: absolute; right: 0; transform: translateY(-50%); display: flex; align-items: center; gap: 6px; background: none; border: 0; color: var(--fg); cursor: pointer; padding: 3px 0; text-align: right; font: inherit; }
|
|
98
|
+
.dots-rail-mark span { display: block; }
|
|
99
|
+
.dots-rail-mark b { display: block; font-weight: 600; }
|
|
100
|
+
.dots-rail-mark small { display: block; color: var(--muted); font-size: 11.5px; }
|
|
101
|
+
.dots-rail-mark i { width: 11px; height: 11px; border-radius: 50%; border: 2px solid var(--muted); background: var(--bg); flex: none; margin-right: 4px; }
|
|
102
|
+
.dots-rail-mark:hover i, .dots-rail-mark:focus-visible i { border-color: var(--focus); }
|
|
103
|
+
.dots-bottom { position: absolute; left: 50%; transform: translateX(-50%); bottom: 12px; display: flex; flex-direction: column; align-items: center; gap: 6px; max-width: calc(100% - 24px); pointer-events: none; }
|
|
104
|
+
.dots-bottom > * { pointer-events: auto; }
|
|
105
|
+
.dots-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; max-width: calc(100% - 24px); background: color-mix(in srgb, var(--panel) 92%, transparent); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; box-sizing: border-box; }
|
|
106
|
+
.dots-bar button, .dots-bar select { background: var(--well); color: var(--fg); border: 1px solid var(--line); border-radius: 5px; padding: 3px 8px; font: inherit; cursor: pointer; }
|
|
107
|
+
.dots-bar input[type=range] { width: min(320px, 40vw); accent-color: var(--focus); }
|
|
108
|
+
.dots-time { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 12em; }
|
|
109
|
+
.dots-caption { padding: 3px 8px; border-radius: 6px; background: color-mix(in srgb, var(--bg) 80%, transparent); color: var(--fg); white-space: nowrap; max-width: calc(100% - 24px); overflow: hidden; text-overflow: ellipsis; box-sizing: border-box; }
|
|
110
|
+
.dots-kiosk .dots-caption { display: none; }
|
|
111
|
+
/* #715: a live carve has no playback head: it plays as it is written. */
|
|
112
|
+
.dots-live .dots-bar { display: none; }
|
|
113
|
+
.dots-live.dots-carving .dots-caption { display: none; }
|
|
114
|
+
/* The kiosk's replay runs and loops by itself, like a live scenario: no playback head. */
|
|
115
|
+
.dots-kiosk:not(.dots-keep-bar) .dots-bar { display: none; }
|
|
116
|
+
.dots-keep-bar .dots-bar { opacity: 0.55; transition: opacity 0.2s; }
|
|
117
|
+
.dots-rail { transition: opacity 0.4s; }
|
|
118
|
+
.dots-directing .dots-rail { opacity: 0; pointer-events: none; }
|
|
119
|
+
.dots-keep-bar .dots-bar:hover, .dots-keep-bar .dots-bar:focus-within { opacity: 1; }
|
|
120
|
+
/* In Watch the director holds the camera; the rail is Inspect's. */
|
|
121
|
+
.dots-kiosk.dots-watch:not(.dots-keep-bar) .dots-rail { display: none; }
|
|
122
|
+
.dots-tip { position: absolute; pointer-events: none; padding: 3px 6px; border-radius: 5px; background: var(--panel); border: 1px solid var(--line); white-space: nowrap; display: none; font-size: 12px; max-width: min(560px, calc(100% - 16px)); }
|
|
123
|
+
.dots-tip .dots-prov { white-space: normal; color: var(--ink2, var(--muted)); font: 11.5px/1.35 var(--font-mono, monospace); margin-top: 2px; }
|
|
124
|
+
/* A recording's timeline (#711): the label on the timeline itself, wall-clock ticks, the marks the records give, the breaks it skips. */
|
|
125
|
+
.dots-bar-rec { flex-direction: column; align-items: stretch; gap: 4px; width: min(860px, calc(100vw - 48px)); }
|
|
126
|
+
.dots-tl-label { font: 600 12px var(--font-ui, system-ui); color: var(--fg); }
|
|
127
|
+
.dots-tl-short { display: none; }
|
|
128
|
+
.dots-tl-row { display: flex; align-items: flex-end; gap: 16px; }
|
|
129
|
+
/* Break labels sit in their own row above the track; tick labels sit in the track, so the two never meet. */
|
|
130
|
+
.dots-tl-track { position: relative; flex: 1; height: 30px; margin-top: 16px; border-bottom: 1px solid var(--line); }
|
|
131
|
+
.dots-bar input.dots-tl-range { position: absolute; left: 0; right: 0; bottom: -8px; width: 100%; margin: 0; accent-color: var(--focus); background: transparent; }
|
|
132
|
+
.dots-tl-tick { position: absolute; bottom: 10px; transform: translateX(-50%); font: 11.5px var(--font-mono, monospace); color: var(--muted); white-space: nowrap; pointer-events: none; }
|
|
133
|
+
.dots-tl-tick::after { content: ""; position: absolute; left: 50%; top: 100%; width: 1px; height: 4px; background: var(--line); }
|
|
134
|
+
.dots-tl-break { position: absolute; top: 0; bottom: 0; width: 8px; transform: translateX(-50%); background: repeating-linear-gradient(135deg, var(--muted) 0 1.5px, transparent 1.5px 4px); opacity: 0.8; cursor: help; z-index: 1; }
|
|
135
|
+
.dots-tl-break small { position: absolute; bottom: calc(100% + 2px); left: 50%; transform: translateX(-50%); font: 11.5px var(--font-mono, monospace); color: var(--muted); white-space: nowrap; }
|
|
136
|
+
.dots-tl-break-end small { left: auto; right: 0; transform: none; }
|
|
137
|
+
.dots-bar .dots-tl-mark { position: absolute; top: 2px; width: 9px; height: 9px; padding: 0; margin-left: -4.5px; transform: rotate(45deg); border: 1.5px solid var(--focus); background: var(--focus); border-radius: 2px; cursor: pointer; z-index: 2; }
|
|
138
|
+
.dots-bar .dots-tl-keyframe { border-color: var(--muted); background: var(--muted); }
|
|
139
|
+
/* The reminder that this is a recording stays readable: the timeline never fades with the quiet controls. */
|
|
140
|
+
.dots-keep-bar .dots-bar.dots-bar-rec { opacity: 1; background: var(--panel); }
|
|
141
|
+
.dots-bar .dots-tl-first, .dots-bar .dots-tl-last { border-color: var(--bar-managed, var(--focus)); }
|
|
142
|
+
.dots-speed { white-space: nowrap; font-variant-numeric: tabular-nums; }
|
|
143
|
+
.dots-tip.dots-tip-go { pointer-events: auto; cursor: pointer; border-color: var(--focus); }
|
|
144
|
+
.dots-prov-go { font: 11.5px var(--font-ui, system-ui); color: var(--focus); margin-top: 2px; }
|
|
145
|
+
@media (max-width: 640px) { .dots-tl-long { display: none; } .dots-tl-short { display: inline; } .dots-tl-break small { display: none; } .dots-bar-rec { width: calc(100vw - 24px); } .dots-tl-row { flex-wrap: wrap; } .dots-tl-track { order: -1; flex: 1 1 100%; } }
|
|
146
|
+
.dots-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
|
147
|
+
.dots-keys { position: absolute; left: 8px; top: 8px; opacity: 0; pointer-events: none; font: 12px var(--font-ui, system-ui); background: var(--panel); color: var(--fg); border: 1px solid var(--focus); border-radius: 6px; padding: 3px 8px; }
|
|
148
|
+
.dots-keys:focus-visible { opacity: 1; pointer-events: auto; }
|
|
149
|
+
.dots-prov-link { position: absolute; left: 8px; bottom: 8px; font: 12px var(--font-ui, system-ui); color: var(--focus); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; }
|
|
150
|
+
.dots-prov-link[hidden] { display: none; }
|
|
151
|
+
@media (max-width: 640px) {
|
|
152
|
+
.dots-rail { top: 52px; transform: none; width: 84px; height: 112px; right: 6px; padding: 4px 6px 4px 2px; }
|
|
153
|
+
.dots-rail-mark { font-size: 11px; }
|
|
154
|
+
.dots-rail-mark small { display: none; }
|
|
155
|
+
.dots-bottom { bottom: 6px; }
|
|
156
|
+
.dots-bar { gap: 6px; padding: 4px 8px; }
|
|
157
|
+
.dots-bar input[type=range] { width: 100%; order: 9; }
|
|
158
|
+
.dots-time { min-width: 0; }
|
|
159
|
+
.dots-caption { font-size: 12px; white-space: normal; text-align: center; }
|
|
160
|
+
}`;
|
|
161
|
+
document.head.appendChild(st);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// --- colours ---------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
function cssVar(name) {
|
|
167
|
+
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A colour moved in OKLCH lightness until it clears `floor` against bg. */
|
|
171
|
+
function clear(hex, bg, floor = 3) {
|
|
172
|
+
if (!/^#[0-9a-f]{6}$/i.test(hex) || !/^#[0-9a-f]{6}$/i.test(bg)) return hex;
|
|
173
|
+
const c = hexToOklch(hex);
|
|
174
|
+
const up = hexToOklch(bg).L < 0.6;
|
|
175
|
+
let out = hex;
|
|
176
|
+
for (let i = 0; i < 40 && contrast(out, bg) < floor; i++) out = oklchToHex({ ...c, L: Math.max(0, Math.min(1, c.L + (up ? 1 : -1) * 0.02 * (i + 1))) });
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The estates' colours, from the theme: the source estate wears the neutral
|
|
182
|
+
* bar; the others take hues spread round OKLCH at the lightness and chroma of
|
|
183
|
+
* the theme's own state colours, skipping the reds the failure flare uses,
|
|
184
|
+
* each lifted to 3:1 on the page.
|
|
185
|
+
*/
|
|
186
|
+
function estateColours(estates, source) {
|
|
187
|
+
const t = getTokens();
|
|
188
|
+
const bg = cssVar("--bg") || (t && t.bg) || "#000000";
|
|
189
|
+
const out = new Map([[source, cssVar("--bar-neutral") || (t && t.neutral)]]);
|
|
190
|
+
const refs = t ? [t.managed, t.pending, t.runtime, t.foreign].filter(Boolean).map(hexToOklch) : [];
|
|
191
|
+
const L = refs.length ? refs.reduce((s, c) => s + c.L, 0) / refs.length : 0.7;
|
|
192
|
+
const C = refs.length ? Math.max(0.09, refs.reduce((s, c) => s + c.C, 0) / refs.length) : 0.12;
|
|
193
|
+
const others = estates.filter((e) => e !== source);
|
|
194
|
+
others.forEach((e, i) => {
|
|
195
|
+
// 60..340 degrees: everything but red.
|
|
196
|
+
const H = 60 + (280 * i) / Math.max(1, others.length);
|
|
197
|
+
out.set(e, clear(oklchToHex({ L, C, H }), bg, 3));
|
|
198
|
+
});
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function readColours() {
|
|
203
|
+
const bg = cssVar("--bg") || "#000000";
|
|
204
|
+
const st = {
|
|
205
|
+
bg,
|
|
206
|
+
fg: cssVar("--fg"),
|
|
207
|
+
muted: cssVar("--muted-ink") || cssVar("--muted"),
|
|
208
|
+
surface: cssVar("--surface-box") || cssVar("--panel"),
|
|
209
|
+
pending: cssVar("--bar-pending"),
|
|
210
|
+
managed: cssVar("--bar-managed"),
|
|
211
|
+
degraded: cssVar("--bar-degraded"),
|
|
212
|
+
runtime: cssVar("--bar-runtime"),
|
|
213
|
+
foreign: cssVar("--bar-foreign"),
|
|
214
|
+
neutral: cssVar("--bar-neutral"),
|
|
215
|
+
};
|
|
216
|
+
st.estate = estateColours(S.scene.estates, S.scene.source);
|
|
217
|
+
return st;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function rgba(hex, a) {
|
|
221
|
+
const m = /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex || "");
|
|
222
|
+
if (!m) return hex;
|
|
223
|
+
return `rgba(${parseInt(m[1], 16)},${parseInt(m[2], 16)},${parseInt(m[3], 16)},${a})`;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// --- mount -----------------------------------------------------------------
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* host: #graph; svg: the card SVG on the page; meta: /api/graph's meta;
|
|
230
|
+
* getVB / setVB: the app's viewBox camera.
|
|
231
|
+
*/
|
|
232
|
+
export async function mountDots({ host, svg, meta, getVB, setVB, vbInit, source = null, auto = null, live = null, kiosk = false, onCarving = null }) {
|
|
233
|
+
const name = live ? "live" : meta.dots.source;
|
|
234
|
+
let show = S && S.name === name && !live ? S.show : null;
|
|
235
|
+
const keep = S && S.name === name && !live ? { t: S.t, playing: S.playing, speed: S.speed } : null;
|
|
236
|
+
unmountDots();
|
|
237
|
+
// A remount (the graph re-rendered) starts from the newest read, not the one the page was given.
|
|
238
|
+
if (live && liveMemo && liveMemo.answer) live = liveMemo.answer;
|
|
239
|
+
if (live) {
|
|
240
|
+
// Arrivals keep the times they were first seen across a remount; the first read's lines are the past.
|
|
241
|
+
const now = Date.now();
|
|
242
|
+
const seen = stampArrivals(live, liveMemo ? liveMemo.seen : new Map(), now, { first: !liveMemo, stepMs: stepMsFor(liveSlowed) });
|
|
243
|
+
show = liveShow(live, seen, now, { t0: liveMemo ? liveMemo.t0 : null });
|
|
244
|
+
liveMemo = { seen, answer: live, t0: show.t0 };
|
|
245
|
+
} else if (!show) {
|
|
246
|
+
// A static export hands the source in; the preview server answers it.
|
|
247
|
+
let src = source;
|
|
248
|
+
if (!src) {
|
|
249
|
+
const res = await fetch(`/api/dots/source?name=${encodeURIComponent(name)}`);
|
|
250
|
+
src = await res.json();
|
|
251
|
+
if (!res.ok) throw new Error(src.error || "dots source unreadable");
|
|
252
|
+
}
|
|
253
|
+
if (src.kind === "carve") {
|
|
254
|
+
const rec = readCarve(src.text);
|
|
255
|
+
show = carveShow(rec, parentsOf(rec.roster));
|
|
256
|
+
} else show = walkthroughShow(src.model);
|
|
257
|
+
}
|
|
258
|
+
addStyle();
|
|
259
|
+
const prefix = `${show.source}/`;
|
|
260
|
+
// Seeds: each card's centre, read from where pinhole painted it. A live
|
|
261
|
+
// carve's cards are the served roots' (`<member>/<address>`), joined by address.
|
|
262
|
+
const cards = new Map();
|
|
263
|
+
const widths = [];
|
|
264
|
+
for (const g of svg.querySelectorAll("[data-node-id]")) {
|
|
265
|
+
const id = g.getAttribute("data-node-id");
|
|
266
|
+
const r = g.querySelector("rect");
|
|
267
|
+
if (!r) continue;
|
|
268
|
+
const slash = id.indexOf("/");
|
|
269
|
+
if (live ? slash < 0 : !id.startsWith(prefix)) continue;
|
|
270
|
+
const addr = live ? id.slice(slash + 1) : id.slice(prefix.length);
|
|
271
|
+
if (live && cards.has(addr)) continue;
|
|
272
|
+
const x = +r.getAttribute("x");
|
|
273
|
+
const y = +r.getAttribute("y");
|
|
274
|
+
const w = +r.getAttribute("width");
|
|
275
|
+
const h = +r.getAttribute("height");
|
|
276
|
+
cards.set(addr, { x: x + w / 2, y: y + h / 2 });
|
|
277
|
+
widths.push(w);
|
|
278
|
+
}
|
|
279
|
+
widths.sort((a, b) => a - b);
|
|
280
|
+
const cardW = widths[widths.length >> 1] || 200;
|
|
281
|
+
const boxEl = svg.querySelector(`rect[data-group-id="${CSS.escape(show.source)}"]`) || svg.querySelector("rect[data-group-id]");
|
|
282
|
+
const mbox = boxEl
|
|
283
|
+
? { x: +boxEl.getAttribute("x"), y: +boxEl.getAttribute("y"), w: +boxEl.getAttribute("width"), h: +boxEl.getAttribute("height") }
|
|
284
|
+
: { x: vbInit[0], y: vbInit[1], w: vbInit[2], h: vbInit[3] };
|
|
285
|
+
// The source estate's box takes its cards' box's place and shape on the page.
|
|
286
|
+
const scene = buildScene(show, { sourceAspect: mbox.w / Math.max(1, mbox.h), ...labelRoom(host) });
|
|
287
|
+
const src = scene.sourceBox;
|
|
288
|
+
// One dot pitch is a fixed share of a card's width, so a dot is legible exactly
|
|
289
|
+
// when its card stops being: at the dot size a pitch is about 8 px, whatever
|
|
290
|
+
// the estate's size. The source box sits centred on its cards' box.
|
|
291
|
+
const pitch = cardW * 0.35;
|
|
292
|
+
const origin = { x: mbox.x + (mbox.w - src.w * pitch) / 2, y: mbox.y + (mbox.h - src.h * pitch) / 2 };
|
|
293
|
+
if (!show.roster.length) return;
|
|
294
|
+
|
|
295
|
+
const canvas = document.createElement("canvas");
|
|
296
|
+
canvas.id = "dots-canvas";
|
|
297
|
+
host.prepend(canvas);
|
|
298
|
+
host.classList.add("dots-on");
|
|
299
|
+
const ui = document.createElement("div");
|
|
300
|
+
ui.id = "dots-ui";
|
|
301
|
+
host.appendChild(ui);
|
|
302
|
+
|
|
303
|
+
const reducedQ = typeof matchMedia === "function" ? matchMedia("(prefers-reduced-motion: reduce)") : null;
|
|
304
|
+
const n = show.roster.length;
|
|
305
|
+
S = {
|
|
306
|
+
name,
|
|
307
|
+
host,
|
|
308
|
+
svg,
|
|
309
|
+
canvas,
|
|
310
|
+
ui,
|
|
311
|
+
ctx: canvas.getContext("2d"),
|
|
312
|
+
scene,
|
|
313
|
+
show,
|
|
314
|
+
clock: replayClock(show),
|
|
315
|
+
cards,
|
|
316
|
+
cardW,
|
|
317
|
+
pitch,
|
|
318
|
+
origin,
|
|
319
|
+
mbox,
|
|
320
|
+
live: !!live,
|
|
321
|
+
answer: live,
|
|
322
|
+
onCarving,
|
|
323
|
+
carving: false,
|
|
324
|
+
getVB,
|
|
325
|
+
setVB,
|
|
326
|
+
vbInit,
|
|
327
|
+
zoomOut: 3,
|
|
328
|
+
t: keep ? keep.t : 0,
|
|
329
|
+
playing: keep ? keep.playing : false,
|
|
330
|
+
speed: keep ? keep.speed : 1,
|
|
331
|
+
reduced: !!(reducedQ && reducedQ.matches),
|
|
332
|
+
dirty: true,
|
|
333
|
+
lastWall: 0,
|
|
334
|
+
raf: 0,
|
|
335
|
+
off: [],
|
|
336
|
+
disp: new Map(),
|
|
337
|
+
boxDisp: new Map(),
|
|
338
|
+
phase: new Map(),
|
|
339
|
+
activity: new Map(),
|
|
340
|
+
firsts: firstLandings(scene),
|
|
341
|
+
addrs: show.roster.map((r) => r.address),
|
|
342
|
+
idx: new Map(show.roster.map((r, i) => [r.address, i])),
|
|
343
|
+
px: new Float32Array(n),
|
|
344
|
+
py: new Float32Array(n),
|
|
345
|
+
pvis: new Uint8Array(n),
|
|
346
|
+
stats: { draw: [], frame: [] },
|
|
347
|
+
snap: true,
|
|
348
|
+
fly: null,
|
|
349
|
+
hover: null,
|
|
350
|
+
svgHidden: false,
|
|
351
|
+
cardA: 1,
|
|
352
|
+
};
|
|
353
|
+
S.col = readColours();
|
|
354
|
+
// The page's own UI face; a canvas cannot read var() in its font.
|
|
355
|
+
S.font = (cssVar("--font-ui") || "system-ui, sans-serif").replace(/;$/, "");
|
|
356
|
+
S.off.push(onThemeChange(() => {
|
|
357
|
+
if (!S) return;
|
|
358
|
+
S.col = readColours();
|
|
359
|
+
S.dirty = true;
|
|
360
|
+
}));
|
|
361
|
+
if (reducedQ) {
|
|
362
|
+
const onRM = () => {
|
|
363
|
+
if (S) {
|
|
364
|
+
S.reduced = reducedQ.matches;
|
|
365
|
+
S.dirty = true;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
reducedQ.addEventListener("change", onRM);
|
|
369
|
+
S.off.push(() => reducedQ.removeEventListener("change", onRM));
|
|
370
|
+
}
|
|
371
|
+
// The zoom goes out until the whole scene is a handful of discs.
|
|
372
|
+
const ext = scene.extent;
|
|
373
|
+
S.zoomOut = Math.max(3, (Math.max(ext.w, ext.h) * pitch * 40) / Math.max(1, vbInit[2]) / 8);
|
|
374
|
+
buildUi();
|
|
375
|
+
const onMove = (e) => {
|
|
376
|
+
if (S && S.selected) return;
|
|
377
|
+
hoverAt(e);
|
|
378
|
+
};
|
|
379
|
+
host.addEventListener("mousemove", onMove);
|
|
380
|
+
S.off.push(() => host.removeEventListener("mousemove", onMove));
|
|
381
|
+
// A tap (or a click) on a landed dot selects it: its tip stays, and its line is read out. A tap elsewhere lets it go.
|
|
382
|
+
let down = null;
|
|
383
|
+
const onDown = (e) => {
|
|
384
|
+
down = { x: e.clientX, y: e.clientY };
|
|
385
|
+
};
|
|
386
|
+
const onUp = (e) => {
|
|
387
|
+
if (!S || !down || Math.hypot(e.clientX - down.x, e.clientY - down.y) > 6 || S.cardA > 0.5) return;
|
|
388
|
+
const i = nearestDot(e, 144);
|
|
389
|
+
if (i < 0) {
|
|
390
|
+
S.selected = null;
|
|
391
|
+
S.tip.style.display = "none";
|
|
392
|
+
if (S.provLink) S.provLink.hidden = true;
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
selectDot(S.addrs[i]);
|
|
396
|
+
};
|
|
397
|
+
host.addEventListener("pointerdown", onDown);
|
|
398
|
+
host.addEventListener("pointerup", onUp);
|
|
399
|
+
S.off.push(() => {
|
|
400
|
+
host.removeEventListener("pointerdown", onDown);
|
|
401
|
+
host.removeEventListener("pointerup", onUp);
|
|
402
|
+
});
|
|
403
|
+
const params = new URLSearchParams(location.search);
|
|
404
|
+
if (S.live) startLive(kiosk);
|
|
405
|
+
// Past a few hundred cards the fit is already below card size, so a large
|
|
406
|
+
// source opens on its resources; the rail and the fit button still go to cards.
|
|
407
|
+
// #712: in the kiosk's frame the replay loops by itself and the director frames it.
|
|
408
|
+
// `auto` (the static carve export): the same loop and director, narrated in the page itself.
|
|
409
|
+
S.kiosk = !S.live && (params.get("dotsKiosk") === "1" || !!auto);
|
|
410
|
+
S.narrate = auto && auto.narrate ? auto.narrate : null;
|
|
411
|
+
S.auto = !!auto;
|
|
412
|
+
S.keepBar = !!(auto && auto.bar);
|
|
413
|
+
if (S.kiosk) startKiosk();
|
|
414
|
+
else if (!S.live && !keep && !params.has("dotsAt") && show.roster.length > 300) flyNow("resources");
|
|
415
|
+
if (params.has("dotsAt")) flyNow(params.get("dotsAt"));
|
|
416
|
+
if (params.has("dotsT")) seek(Number(params.get("dotsT")) || 0);
|
|
417
|
+
if (params.get("dotsPlay") === "1") play(true);
|
|
418
|
+
if (params.has("dotsSpeed")) setSpeed(Number(params.get("dotsSpeed")) || 1);
|
|
419
|
+
// A frame-exact step for recording frame sequences: advance the replay by ms and draw with that dt.
|
|
420
|
+
const step = (ms) => {
|
|
421
|
+
if (!S) return;
|
|
422
|
+
S.playing = false;
|
|
423
|
+
S.t = Math.min(S.clock.length, S.t + ms);
|
|
424
|
+
draw(ms);
|
|
425
|
+
S.dirty = false;
|
|
426
|
+
};
|
|
427
|
+
// Zoom to a viewBox width about the scene's centre, for screenshots of a morph halfway.
|
|
428
|
+
const zoomTo = (w, onSource = false) => {
|
|
429
|
+
const vb = S.getVB();
|
|
430
|
+
const P = S.pitch;
|
|
431
|
+
const ext = onSource ? S.scene.sourceBox : S.scene.extent;
|
|
432
|
+
const cx = S.origin.x + (ext.w * P) / 2;
|
|
433
|
+
const cy = S.origin.y + (ext.h * P) / 2;
|
|
434
|
+
const h = (w * vb[3]) / vb[2];
|
|
435
|
+
S.setVB([cx - w / 2, cy - h / 2, w, h]);
|
|
436
|
+
};
|
|
437
|
+
// The same, by the size a card would be on screen.
|
|
438
|
+
const zoomCardPx = (px) => zoomTo((paneRect().width * S.cardW) / px, true);
|
|
439
|
+
const setInsets = (ins) => {
|
|
440
|
+
if (S) S.insets = { top: Number(ins.top) || 0, bottom: Number(ins.bottom) || 0 };
|
|
441
|
+
};
|
|
442
|
+
window.__dots = { select: (id) => selectDot(id), landed: () => S && landedIds(), seek, play, step, zoomTo, zoomCardPx, setInsets, pause: () => play(false), fly, setSpeed, stats: () => S && S.stats, length: () => S && S.clock.length, marks: () => S && currentMarks(), state: () => S && { t: S.t, level: S.level, reduced: S.reduced, cardPx: S.cardPx, pitchPx: S.pitchPx }, debug: () => S && { band: (({ left, right, w }) => ({ left, right, w }))(band()), svg: S.svg.getBoundingClientRect().toJSON(), rail: S.ui.querySelector('.dots-rail').getBoundingClientRect().toJSON(), vb: S.getVB(), dir: S.dir, directing: S.directing, focusBox: S.dir && focusBox(S.dir.focus), want: S.dir && frameBox(focusBox(S.dir.focus)), pitch: S.pitch, origin: S.origin }, story: () => S && S.story && { start: S.story.start, end: S.story.end, firstRefusal: S.story.firstRefusal ? S.story.firstRefusal.end : null } };
|
|
443
|
+
const loop = (wall) => {
|
|
444
|
+
if (!S) return;
|
|
445
|
+
S.raf = requestAnimationFrame(loop);
|
|
446
|
+
const dt = S.lastWall ? Math.min(100, wall - S.lastWall) : 16;
|
|
447
|
+
S.lastWall = wall;
|
|
448
|
+
if (S.fly) stepFly(wall);
|
|
449
|
+
if (S.live) {
|
|
450
|
+
// The live clock is the wall's: a line flies from when it was seen.
|
|
451
|
+
S.t = Date.now() - S.show.t0;
|
|
452
|
+
S.clock.length = Math.max(S.clock.length, S.t);
|
|
453
|
+
if (S.t <= S.busyUntil) S.dirty = true;
|
|
454
|
+
liveTick(wall, dt);
|
|
455
|
+
} else if (S.playing) {
|
|
456
|
+
S.t = Math.min(S.clock.length, S.t + dt * S.speed);
|
|
457
|
+
if (S.t >= S.clock.length) play(false);
|
|
458
|
+
S.dirty = true;
|
|
459
|
+
}
|
|
460
|
+
if (S.kiosk) kioskTick(wall, dt);
|
|
461
|
+
if (!S.dirty && !S.settling) return;
|
|
462
|
+
S.stats.frame.push(dt);
|
|
463
|
+
const t0 = performance.now();
|
|
464
|
+
draw(dt);
|
|
465
|
+
S.stats.draw.push(performance.now() - t0);
|
|
466
|
+
if (S.stats.draw.length > 4000) {
|
|
467
|
+
S.stats.draw.splice(0, 2000);
|
|
468
|
+
S.stats.frame.splice(0, 2000);
|
|
469
|
+
}
|
|
470
|
+
S.dirty = false;
|
|
471
|
+
};
|
|
472
|
+
S.raf = requestAnimationFrame(loop);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// --- playback --------------------------------------------------------------
|
|
476
|
+
|
|
477
|
+
function seek(t) {
|
|
478
|
+
if (!S) return;
|
|
479
|
+
// In the kiosk a seek keeps the loop playing.
|
|
480
|
+
if (S.kiosk && t < S.clock.length) {
|
|
481
|
+
S.holdUntil = null;
|
|
482
|
+
S.playing = true;
|
|
483
|
+
}
|
|
484
|
+
S.t = Math.max(0, Math.min(S.clock.length, t));
|
|
485
|
+
// Tips fire for what plays, not for what a seek skips.
|
|
486
|
+
S.tipFrom = S.t;
|
|
487
|
+
S.snap = true;
|
|
488
|
+
S.dirty = true;
|
|
489
|
+
syncUi();
|
|
490
|
+
}
|
|
491
|
+
function play(on) {
|
|
492
|
+
if (!S) return;
|
|
493
|
+
if (on && S.t >= S.clock.length) seek(0);
|
|
494
|
+
S.playing = !!on;
|
|
495
|
+
S.dirty = true;
|
|
496
|
+
syncUi();
|
|
497
|
+
}
|
|
498
|
+
function setSpeed(v) {
|
|
499
|
+
if (!S) return;
|
|
500
|
+
S.speed = v;
|
|
501
|
+
syncUi();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// --- camera ----------------------------------------------------------------
|
|
505
|
+
|
|
506
|
+
/** Where the card SVG sits on the page. Read off the SVG while it shows; once it is
|
|
507
|
+
* out of the paint the host stands in for it (the SVG fills it), so a moving camera
|
|
508
|
+
* never makes the browser lay a 1,041-card SVG out just to be measured. */
|
|
509
|
+
/**
|
|
510
|
+
* What the scene's layout needs to leave each box's label room (web/dots-scene.js): the label's width on screen, in
|
|
511
|
+
* the face the boxes are labelled in, and the screen area the director frames the boxes in.
|
|
512
|
+
*/
|
|
513
|
+
function labelRoom(host) {
|
|
514
|
+
if (typeof document === "undefined" || !host || !host.getBoundingClientRect) return {};
|
|
515
|
+
const r = host.getBoundingClientRect();
|
|
516
|
+
if (!r.width || !r.height) return {};
|
|
517
|
+
const cv = document.createElement("canvas").getContext("2d");
|
|
518
|
+
if (!cv) return {};
|
|
519
|
+
const font = (S && S.font) || getComputedStyle(document.body).fontFamily || "sans-serif";
|
|
520
|
+
cv.font = `700 11.5px ${font}`;
|
|
521
|
+
const ins = S && S.insets ? S.insets : { top: 0, bottom: 0 };
|
|
522
|
+
return {
|
|
523
|
+
labelPx: (e) => cv.measureText(`${e} 000`).width + 12,
|
|
524
|
+
labelH: 18,
|
|
525
|
+
avail: { w: r.width * 0.72, h: Math.max(80, r.height - (ins.top || 0) - (ins.bottom || 0) - 32) },
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function paneRect() {
|
|
530
|
+
return S.svgHidden && S.svgGone ? S.host.getBoundingClientRect() : S.svg.getBoundingClientRect();
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function camera() {
|
|
534
|
+
const r = paneRect();
|
|
535
|
+
const hr = S.host.getBoundingClientRect();
|
|
536
|
+
const vb = S.getVB();
|
|
537
|
+
const s = Math.min(r.width / vb[2], r.height / vb[3]);
|
|
538
|
+
const ox = r.left - hr.left + S.host.scrollLeft + (r.width - vb[2] * s) / 2 - vb[0] * s;
|
|
539
|
+
const oy = r.top - hr.top + S.host.scrollTop + (r.height - vb[3] * s) / 2 - vb[1] * s;
|
|
540
|
+
return { s, ox, oy, r, vb, left: r.left - hr.left + S.host.scrollLeft, top: r.top - hr.top + S.host.scrollTop };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/** The pane's width not covered by the floating panel, and how much it covers. */
|
|
544
|
+
function band() {
|
|
545
|
+
const r = paneRect();
|
|
546
|
+
const pe = document.getElementById("panel");
|
|
547
|
+
let left = 0;
|
|
548
|
+
if (pe && !pe.classList.contains("collapsed") && getComputedStyle(pe).display !== "none") {
|
|
549
|
+
const pr = pe.getBoundingClientRect();
|
|
550
|
+
if (pr.width && pr.right > r.left && pr.left < r.left + r.width / 2) left = Math.min(r.width * 0.45, pr.right - r.left);
|
|
551
|
+
}
|
|
552
|
+
// The rail sits on the right edge; keep the scene clear of it too.
|
|
553
|
+
const rail = S.ui && S.ui.querySelector(".dots-rail");
|
|
554
|
+
const right = rail ? Math.min(r.width * 0.3, r.right - rail.getBoundingClientRect().left + 8) : 0;
|
|
555
|
+
return { r, left, right, w: Math.max(1, r.width - left - right) };
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function currentMarks() {
|
|
559
|
+
const { r, w: bw } = band();
|
|
560
|
+
let big = 0;
|
|
561
|
+
let small = Infinity;
|
|
562
|
+
// Every box the replay will draw: the ones on screen now and each estate's final one.
|
|
563
|
+
const sizes = [];
|
|
564
|
+
for (const b of S.scene.layoutAt(takeoffsBy(S.scene, S.t)).boxes.values()) if (b.members) sizes.push(Math.max(b.w, b.h));
|
|
565
|
+
for (const [e, f] of S.scene.finalSize) if (e !== S.scene.source) sizes.push(Math.max(f.w, f.h));
|
|
566
|
+
for (const v of sizes) {
|
|
567
|
+
big = Math.max(big, v);
|
|
568
|
+
small = Math.min(small, v);
|
|
569
|
+
}
|
|
570
|
+
const sorted = [...sizes].sort((a, b) => a - b);
|
|
571
|
+
const typical = sorted.length ? sorted[sorted.length >> 1] : 0;
|
|
572
|
+
const P = S.pitch;
|
|
573
|
+
return railMarks({ paneW: bw, paneH: r.height || 1, cardW: S.cardW, sceneW: S.scene.extent.w * P, sceneH: S.scene.extent.h * P, smallestBox: Number.isFinite(small) ? small * P : 0, largestBox: big * P, typicalBox: typical * P });
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** Fly the camera to a rail mark, centred on the scene (or the cards for Cards). */
|
|
577
|
+
function fly(mark) {
|
|
578
|
+
if (!S) return;
|
|
579
|
+
const marks = currentMarks();
|
|
580
|
+
const w = marks[mark];
|
|
581
|
+
if (!w) return;
|
|
582
|
+
const vb = S.getVB();
|
|
583
|
+
const ext = S.scene.extent;
|
|
584
|
+
const P = S.pitch;
|
|
585
|
+
const cx = mark === "cards" ? Math.max(S.origin.x + w / 2, Math.min(vb[0] + vb[2] / 2, S.origin.x + S.scene.sourceBox.w * P - w / 2)) : S.origin.x + (ext.w * P) / 2;
|
|
586
|
+
const cy = mark === "cards" ? Math.max(S.origin.y + (w * vb[3]) / vb[2] / 2, Math.min(vb[1] + vb[3] / 2, S.origin.y + S.scene.sourceBox.h * P)) : S.origin.y + (ext.h * P) / 2;
|
|
587
|
+
// Centre in the part of the pane the floating panel leaves uncovered.
|
|
588
|
+
const { r, left, right, w: bw } = band();
|
|
589
|
+
const ww = mark === "cards" ? w : (w * r.width) / bw;
|
|
590
|
+
const shift = (((left - right) / 2) * ww) / Math.max(1, r.width);
|
|
591
|
+
const to = [cx - ww / 2 - shift, cy - (ww * vb[3]) / vb[2] / 2, ww, (ww * vb[3]) / vb[2]];
|
|
592
|
+
if (S.reduced) {
|
|
593
|
+
S.setVB(to);
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
S.fly = { from: vb.slice(), to, start: performance.now(), dur: 900 };
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function flyNow(mark) {
|
|
600
|
+
fly(mark);
|
|
601
|
+
if (S && S.fly) {
|
|
602
|
+
S.setVB(S.fly.to);
|
|
603
|
+
S.fly = null;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function stepFly(wall) {
|
|
608
|
+
const f = S.fly;
|
|
609
|
+
const u = easeInOut((wall - f.start) / f.dur);
|
|
610
|
+
// Width interpolates in log space so the zoom feels even; the centre linearly.
|
|
611
|
+
const w = Math.exp(lerp(Math.log(f.from[2]), Math.log(f.to[2]), u));
|
|
612
|
+
const h = (w * f.from[3]) / f.from[2];
|
|
613
|
+
const cx = lerp(f.from[0] + f.from[2] / 2, f.to[0] + f.to[2] / 2, u);
|
|
614
|
+
const cy = lerp(f.from[1] + f.from[3] / 2, f.to[1] + f.to[3] / 2, u);
|
|
615
|
+
S.setVB([cx - w / 2, cy - h / 2, w, h]);
|
|
616
|
+
if (u >= 1) S.fly = null;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// --- UI ----------------------------------------------------------------------
|
|
620
|
+
|
|
621
|
+
function el(tag, cls, text) {
|
|
622
|
+
const e = document.createElement(tag);
|
|
623
|
+
if (cls) e.className = cls;
|
|
624
|
+
if (text !== undefined) e.textContent = text;
|
|
625
|
+
return e;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function buildUi() {
|
|
629
|
+
const ui = S.ui;
|
|
630
|
+
const rail = el("div", "dots-rail");
|
|
631
|
+
rail.setAttribute("role", "group");
|
|
632
|
+
rail.setAttribute("aria-label", "Zoom levels");
|
|
633
|
+
rail.appendChild(el("div", "dots-rail-track"));
|
|
634
|
+
S.railCam = el("div", "dots-rail-cam");
|
|
635
|
+
rail.appendChild(S.railCam);
|
|
636
|
+
S.railMarks = {};
|
|
637
|
+
for (const [k, label, pos] of [["cards", "Cards", 0], ["resources", "Resources", 0.5], ["estates", "Estates", 1]]) {
|
|
638
|
+
const b = el("button", "dots-rail-mark");
|
|
639
|
+
b.type = "button";
|
|
640
|
+
b.style.top = `calc(8px + ${pos} * (100% - 16px))`;
|
|
641
|
+
const txt = el("span");
|
|
642
|
+
txt.append(el("b", "", label), el("small", "", ""));
|
|
643
|
+
b.append(txt, el("i"));
|
|
644
|
+
b.title = `Fly to ${label.toLowerCase()}`;
|
|
645
|
+
b.addEventListener("click", (e) => {
|
|
646
|
+
e.stopPropagation();
|
|
647
|
+
fly(k);
|
|
648
|
+
});
|
|
649
|
+
b.addEventListener("mousedown", (e) => e.stopPropagation());
|
|
650
|
+
rail.appendChild(b);
|
|
651
|
+
S.railMarks[k] = b;
|
|
652
|
+
}
|
|
653
|
+
ui.appendChild(rail);
|
|
654
|
+
const bottom = el("div", "dots-bottom");
|
|
655
|
+
S.capEl = el("div", "dots-caption");
|
|
656
|
+
S.capEl.setAttribute("aria-live", "polite");
|
|
657
|
+
bottom.appendChild(S.capEl);
|
|
658
|
+
ui.appendChild(bottom);
|
|
659
|
+
const bar = el("div", "dots-bar");
|
|
660
|
+
bar.addEventListener("mousedown", (e) => e.stopPropagation());
|
|
661
|
+
S.playBtn = el("button", "", "Play");
|
|
662
|
+
S.playBtn.type = "button";
|
|
663
|
+
S.playBtn.addEventListener("click", () => play(!S.playing));
|
|
664
|
+
S.speedSel = el("select");
|
|
665
|
+
S.speedSel.setAttribute("aria-label", "Replay speed");
|
|
666
|
+
for (const v of [0.5, 1, 2, 4]) {
|
|
667
|
+
const o = el("option", "", `${v}×`);
|
|
668
|
+
o.value = String(v);
|
|
669
|
+
S.speedSel.appendChild(o);
|
|
670
|
+
}
|
|
671
|
+
S.speedSel.addEventListener("change", () => setSpeed(Number(S.speedSel.value)));
|
|
672
|
+
S.range = el("input");
|
|
673
|
+
S.range.type = "range";
|
|
674
|
+
S.range.min = "0";
|
|
675
|
+
S.range.max = String(Math.round(S.clock.length));
|
|
676
|
+
S.range.step = "10";
|
|
677
|
+
S.range.setAttribute("aria-label", "Replay position");
|
|
678
|
+
S.range.addEventListener("input", () => seek(Number(S.range.value)));
|
|
679
|
+
S.timeEl = el("span", "dots-time");
|
|
680
|
+
if (S.clock.recorded) {
|
|
681
|
+
// A recording of a real event stream: the timeline says so, on itself, with the recording's wall-clock times,
|
|
682
|
+
// its marks (each a line of the recording), its breaks (each idle stretch it compresses, with the real gap), and the
|
|
683
|
+
// speed beside the playhead, always in words.
|
|
684
|
+
bar.classList.add("dots-bar-rec");
|
|
685
|
+
const rec = S.show.records || {};
|
|
686
|
+
const lab = el("div", "dots-tl-label");
|
|
687
|
+
const facts = { t0: S.show.timeline.t0, t1: S.show.timeline.t1, source: rec.header ? rec.header.source : null, plan: rec.header ? rec.header.plan : null };
|
|
688
|
+
lab.append(el("span", "dots-tl-long", recordedLabel(facts)), el("span", "dots-tl-short", recordedLabel(facts, { short: true })));
|
|
689
|
+
const track = el("div", "dots-tl-track");
|
|
690
|
+
const L = S.clock.length;
|
|
691
|
+
const at = (p) => `${((p / L) * 100).toFixed(3)}%`;
|
|
692
|
+
const narrow = typeof innerWidth === "number" && innerWidth < 640;
|
|
693
|
+
// The track's width on screen, near enough to keep labels apart: the bar less the play and speed buttons.
|
|
694
|
+
const trackW = Math.max(160, Math.min(860, (typeof innerWidth === "number" ? innerWidth : 1200) - 48) - (narrow ? 24 : 230));
|
|
695
|
+
const px = (p) => (p / L) * trackW;
|
|
696
|
+
const breaks = S.show.timeline.breaks;
|
|
697
|
+
// A tick label that would sit under a break's hatch or label is left out; its time is still on the track.
|
|
698
|
+
for (const tk of wallTicks(S.show.timeline, narrow ? 3 : 7)) {
|
|
699
|
+
if (breaks.some((b) => Math.abs(px(tk.p) - px(b.p)) < 26)) continue;
|
|
700
|
+
const t = el("span", "dots-tl-tick", tk.label);
|
|
701
|
+
t.style.left = at(tk.p);
|
|
702
|
+
track.appendChild(t);
|
|
703
|
+
}
|
|
704
|
+
for (const b of breaks) {
|
|
705
|
+
const k = el("span", `dots-tl-break${b.p / L > 0.9 ? " dots-tl-break-end" : ""}`);
|
|
706
|
+
k.style.left = at(b.p);
|
|
707
|
+
k.title = `${skippedWords(b.skipped)}: from ${clockWords(b.real0, true)} to ${clockWords(b.real1, true)} UTC nothing was written, and the replay skips it`;
|
|
708
|
+
k.appendChild(el("small", "", skippedWords(b.skipped).replace(/ skipped$/, "")));
|
|
709
|
+
track.appendChild(k);
|
|
710
|
+
}
|
|
711
|
+
const file = S.name ? `${String(S.name).replace(/^carve:/, "")}.jsonl` : "the recording";
|
|
712
|
+
for (const m of recordedMarks(S.show, S.show.timeline, file)) {
|
|
713
|
+
const b = el("button", `dots-tl-mark dots-tl-${m.kind}`);
|
|
714
|
+
b.type = "button";
|
|
715
|
+
b.style.left = at(m.p);
|
|
716
|
+
b.title = `${m.words} · ${m.record}`;
|
|
717
|
+
b.setAttribute("aria-label", `${m.words}, ${m.record}`);
|
|
718
|
+
b.addEventListener("click", () => seek(m.p));
|
|
719
|
+
track.appendChild(b);
|
|
720
|
+
}
|
|
721
|
+
S.range.classList.add("dots-tl-range");
|
|
722
|
+
track.appendChild(S.range);
|
|
723
|
+
S.speedBtn = el("button", "dots-speed", speedWords(S.speed));
|
|
724
|
+
S.speedBtn.type = "button";
|
|
725
|
+
S.speedBtn.setAttribute("aria-label", "Replay speed");
|
|
726
|
+
S.speedBtn.addEventListener("click", () => {
|
|
727
|
+
const i = RECORDED_SPEEDS.indexOf(S.speed);
|
|
728
|
+
setSpeed(RECORDED_SPEEDS[(i + 1) % RECORDED_SPEEDS.length]);
|
|
729
|
+
});
|
|
730
|
+
const row = el("div", "dots-tl-row");
|
|
731
|
+
row.append(S.playBtn, track, S.speedBtn);
|
|
732
|
+
bar.append(lab, row, S.timeEl);
|
|
733
|
+
S.speedSel.hidden = true;
|
|
734
|
+
} else bar.append(S.playBtn, S.speedSel, S.range, S.timeEl);
|
|
735
|
+
bottom.appendChild(bar);
|
|
736
|
+
S.tip = el("div", "dots-tip");
|
|
737
|
+
S.tip.addEventListener("click", (e) => {
|
|
738
|
+
if (!S || S.tipSeek === null || S.tipSeek === undefined) return;
|
|
739
|
+
e.stopPropagation();
|
|
740
|
+
seek(S.clock.at(S.tipSeek));
|
|
741
|
+
play(false);
|
|
742
|
+
});
|
|
743
|
+
S.tip.addEventListener("pointerup", (e) => e.stopPropagation());
|
|
744
|
+
ui.appendChild(S.tip);
|
|
745
|
+
// A landed dot's provenance without a pointer: the arrow keys step through the writes, the live region reads each out.
|
|
746
|
+
S.sr = el("div", "dots-sr");
|
|
747
|
+
S.sr.setAttribute("aria-live", "polite");
|
|
748
|
+
S.sr.setAttribute("role", "status");
|
|
749
|
+
ui.appendChild(S.sr);
|
|
750
|
+
const keys = el("button", "dots-keys", "← → the writes that landed");
|
|
751
|
+
keys.type = "button";
|
|
752
|
+
keys.setAttribute("aria-label", "Step through the writes that landed with the left and right arrow keys; each is read out");
|
|
753
|
+
keys.addEventListener("keydown", (e) => {
|
|
754
|
+
if (!S || (e.key !== "ArrowLeft" && e.key !== "ArrowRight")) return;
|
|
755
|
+
e.preventDefault();
|
|
756
|
+
const ids = landedIds();
|
|
757
|
+
if (!ids.length) {
|
|
758
|
+
S.sr.textContent = "No write has landed yet in this cycle.";
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
const at = ids.indexOf(S.selected);
|
|
762
|
+
const next = at < 0 ? (e.key === "ArrowRight" ? 0 : ids.length - 1) : (at + (e.key === "ArrowRight" ? 1 : -1) + ids.length) % ids.length;
|
|
763
|
+
selectDot(ids[next]);
|
|
764
|
+
});
|
|
765
|
+
ui.appendChild(keys);
|
|
766
|
+
S.provLink = el("a", "dots-prov-link", "Open the job");
|
|
767
|
+
S.provLink.target = "_blank";
|
|
768
|
+
S.provLink.rel = "noopener";
|
|
769
|
+
S.provLink.hidden = true;
|
|
770
|
+
ui.appendChild(S.provLink);
|
|
771
|
+
syncUi();
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
const fmt = (n) => n.toLocaleString("en-US");
|
|
775
|
+
|
|
776
|
+
function syncUi() {
|
|
777
|
+
if (!S || !S.playBtn) return;
|
|
778
|
+
S.playBtn.textContent = S.playing ? "Pause" : S.t >= S.clock.length ? "Replay" : "Play";
|
|
779
|
+
S.speedSel.value = String(S.speed);
|
|
780
|
+
if (S.speedBtn && S.speedBtn.textContent !== speedWords(S.speed)) S.speedBtn.textContent = speedWords(S.speed);
|
|
781
|
+
S.range.value = String(Math.round(S.t));
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/** The show's own time at replay time `t`: the wall clock for a live carve and for a recording with its own clock. */
|
|
785
|
+
function showTime(t) {
|
|
786
|
+
if (S.live) return S.show.t0 + t;
|
|
787
|
+
if (S.clock.real) return S.clock.real(t);
|
|
788
|
+
return S.show.t0 + (t / S.clock.length) * (S.show.t1 - S.show.t0);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
function shortName(e) {
|
|
792
|
+
const src = S.scene.source;
|
|
793
|
+
// An adopted stock root's box takes its estate's name once the first stamp plays (web/dots-live.js).
|
|
794
|
+
const nm = boxName(S.show, e, S.show.t0 + S.t);
|
|
795
|
+
if (nm !== e) return nm;
|
|
796
|
+
return e !== src && e.startsWith(`${src}-`) ? e.slice(src.length + 1) : e;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
function updateUiFrame(counts, level) {
|
|
800
|
+
const sc = S.scene;
|
|
801
|
+
const moves = sc.flights.filter((f) => !f.parent);
|
|
802
|
+
const done = moves.filter((f) => f.end <= S.t);
|
|
803
|
+
const refused = done.filter((f) => !f.ok).length;
|
|
804
|
+
const estatesNow = [...counts.values()].filter((c) => c > 0).length;
|
|
805
|
+
let label = `move ${fmt(done.length)} of ${fmt(moves.length)}`;
|
|
806
|
+
if (moves.some((f) => !f.ok)) label += ` · ${refused} refused`;
|
|
807
|
+
if (S.show.kind === "walkthrough") {
|
|
808
|
+
const p = phaseAt(S.show, S.show.t0 + (S.t / S.clock.length) * (S.show.t1 - S.show.t0));
|
|
809
|
+
label = `${p ? p.id : "stock"} · ${label}`;
|
|
810
|
+
}
|
|
811
|
+
if (S.timeEl.textContent !== label) S.timeEl.textContent = label;
|
|
812
|
+
if (S.playing && Math.abs(Number(S.range.value) - S.t) > 100) S.range.value = String(Math.round(S.t));
|
|
813
|
+
const marks = currentMarks();
|
|
814
|
+
// The zoom goes out a little past the estates mark and no further.
|
|
815
|
+
S.zoomOut = Math.max(3, (marks.estates * 1.4) / Math.max(1, S.vbInit[2]));
|
|
816
|
+
const sub = { cards: `${fmt(S.show.roster.length)} cards`, resources: `${fmt(S.show.roster.length)} resources`, estates: `${estatesNow} estate${estatesNow === 1 ? "" : "s"}` };
|
|
817
|
+
for (const k of Object.keys(S.railMarks)) {
|
|
818
|
+
const sm = S.railMarks[k].querySelector("small");
|
|
819
|
+
if (sm.textContent !== sub[k]) sm.textContent = sub[k];
|
|
820
|
+
}
|
|
821
|
+
const b = band();
|
|
822
|
+
const pos = railPos((S.getVB()[2] * b.w) / Math.max(1, b.r.width), marks);
|
|
823
|
+
S.railCam.style.top = `calc(8px + ${pos.toFixed(4)} * (100% - 16px))`;
|
|
824
|
+
const capText = caption(level);
|
|
825
|
+
if (S.capEl.textContent !== capText) S.capEl.textContent = capText;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
function hoverAt(e) {
|
|
829
|
+
if (!S) return;
|
|
830
|
+
const hr = S.host.getBoundingClientRect();
|
|
831
|
+
const x = e.clientX - hr.left + S.host.scrollLeft;
|
|
832
|
+
const y = e.clientY - hr.top + S.host.scrollTop;
|
|
833
|
+
if (S.cardA > 0.5) {
|
|
834
|
+
S.tip.style.display = "none";
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
837
|
+
let best = -1;
|
|
838
|
+
let bd = 100;
|
|
839
|
+
for (let i = 0; i < S.addrs.length; i++) {
|
|
840
|
+
if (!S.pvis[i]) continue;
|
|
841
|
+
const d = (S.px[i] - x) ** 2 + (S.py[i] - y) ** 2;
|
|
842
|
+
if (d < bd) {
|
|
843
|
+
bd = d;
|
|
844
|
+
best = i;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
if (best < 0) {
|
|
848
|
+
S.tip.style.display = "none";
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
showTip(S.addrs[best], x, y);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/** A landed dot's provenance: what its newest write was, from its progress line and the migration's run; null for a dot no line names. */
|
|
855
|
+
function provenanceOf(id) {
|
|
856
|
+
if (!S) return null;
|
|
857
|
+
if (!S.live) return recordedProvenance(id);
|
|
858
|
+
const w = landedWrite(S.show, id, S.show.t0 + S.t);
|
|
859
|
+
if (!w) return null;
|
|
860
|
+
const m = ((S.answer && S.answer.migrations) || []).find((x) => x.name === w.migration);
|
|
861
|
+
const run = m && m.run ? m.run : null;
|
|
862
|
+
return { text: provenanceText({ ...w, tool: run ? run.tool : migrationTool(m && m.change), job: run ? run.job : null }), url: run && run.job_url ? run.job_url : null };
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/** A recorded dot's provenance: the move line of the recording that landed it, with its line and time, and where the playhead goes on a click. */
|
|
866
|
+
function recordedProvenance(id) {
|
|
867
|
+
if (!S.clock.recorded) return null;
|
|
868
|
+
const now = showTime(S.t);
|
|
869
|
+
let best = null;
|
|
870
|
+
for (const ev of S.show.events) {
|
|
871
|
+
if (ev.kind !== "write" || ev.t > now) continue;
|
|
872
|
+
if (ev.address !== id && !(ev.followers || []).includes(id)) continue;
|
|
873
|
+
if (!best || ev.t >= best.t) best = ev;
|
|
874
|
+
}
|
|
875
|
+
if (!best) return null;
|
|
876
|
+
const file = S.name ? `${String(S.name).replace(/^carve:/, "")}.jsonl` : "the recording";
|
|
877
|
+
const text = provenanceText({ outcome: best.ok ? "moved" : "refused", tool: "live-mv", address: best.address === id ? id : `${id} (with ${best.address})`, from: best.from, to: best.to, at: best.t, reason: best.refusalSummary || null });
|
|
878
|
+
return { text: `${text} · ${file}${best.line ? ` line ${best.line}` : ""}`, url: null, seek: best.t };
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
function showTip(a, x, y) {
|
|
882
|
+
const prov = provenanceOf(a);
|
|
883
|
+
S.tip.replaceChildren(el("div", "", `${a} · ${shortName(S.ownersNow.get(a) ?? S.scene.source)}`));
|
|
884
|
+
if (prov) {
|
|
885
|
+
const p = el("div", "dots-prov", prov.text);
|
|
886
|
+
S.tip.appendChild(p);
|
|
887
|
+
if (prov.seek !== undefined && S.selected === a) S.tip.appendChild(el("div", "dots-prov-go", "Click to move the playhead to this write"));
|
|
888
|
+
}
|
|
889
|
+
// A selected recorded dot's tip is a control: a click moves the playhead to its write.
|
|
890
|
+
S.tip.classList.toggle("dots-tip-go", !!(prov && prov.seek !== undefined && S.selected === a));
|
|
891
|
+
S.tipSeek = prov && prov.seek !== undefined ? prov.seek : null;
|
|
892
|
+
S.tip.style.display = "block";
|
|
893
|
+
S.tip.style.left = `${Math.max(4, Math.min(x + 12, S.host.clientWidth - S.tip.offsetWidth - 4))}px`;
|
|
894
|
+
S.tip.style.top = `${y + 14}px`;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/** The dots that a write of this cycle landed, in the order they were written: what the keys step through. */
|
|
898
|
+
function landedIds() {
|
|
899
|
+
const out = [];
|
|
900
|
+
const t = showTime(S.t);
|
|
901
|
+
const seen = new Set();
|
|
902
|
+
for (const ev of S.show.events) {
|
|
903
|
+
if (ev.before || (S.live ? !ev.line || ev.start > t : ev.kind !== "write" || ev.t > t)) continue;
|
|
904
|
+
for (const id of [ev.address, ...(ev.followers || [])]) {
|
|
905
|
+
if (seen.has(id)) continue;
|
|
906
|
+
seen.add(id);
|
|
907
|
+
out.push(id);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
return out;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/** Select a dot: its tip stays up, and its provenance is read out in the live region, for a reader without a pointer. */
|
|
914
|
+
function selectDot(id) {
|
|
915
|
+
if (!S) return;
|
|
916
|
+
S.selected = id;
|
|
917
|
+
const i = S.idx.get(id);
|
|
918
|
+
const prov = provenanceOf(id);
|
|
919
|
+
const words = prov ? prov.text : `${id} · ${shortName(S.ownersNow.get(id) ?? S.scene.source)}`;
|
|
920
|
+
if (S.sr && S.sr.textContent !== words) S.sr.textContent = words;
|
|
921
|
+
if (S.provLink) {
|
|
922
|
+
S.provLink.hidden = !(prov && prov.url);
|
|
923
|
+
if (prov && prov.url) S.provLink.href = prov.url;
|
|
924
|
+
}
|
|
925
|
+
if (i !== undefined && S.pvis[i]) showTip(id, S.px[i], S.py[i]);
|
|
926
|
+
S.dirty = true;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
function nearestDot(e, within = 100) {
|
|
930
|
+
const hr = S.host.getBoundingClientRect();
|
|
931
|
+
const x = e.clientX - hr.left + S.host.scrollLeft;
|
|
932
|
+
const y = e.clientY - hr.top + S.host.scrollTop;
|
|
933
|
+
let best = -1;
|
|
934
|
+
let bd = within;
|
|
935
|
+
for (let i = 0; i < S.addrs.length; i++) {
|
|
936
|
+
if (!S.pvis[i]) continue;
|
|
937
|
+
const d = (S.px[i] - x) ** 2 + (S.py[i] - y) ** 2;
|
|
938
|
+
if (d < bd) {
|
|
939
|
+
bd = d;
|
|
940
|
+
best = i;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return best;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
// --- drawing -----------------------------------------------------------------
|
|
947
|
+
|
|
948
|
+
/** Flights near `t`: in the air, or landed within the ripple, or refused (they stay lit). */
|
|
949
|
+
function flightsNear(t) {
|
|
950
|
+
const fl = S.scene.flights;
|
|
951
|
+
let lo = 0;
|
|
952
|
+
let hi = fl.length;
|
|
953
|
+
const from = t - 1100 - RIPPLE_MS - 50;
|
|
954
|
+
while (lo < hi) {
|
|
955
|
+
const m = (lo + hi) >> 1;
|
|
956
|
+
if (fl[m].start < from) lo = m + 1;
|
|
957
|
+
else hi = m;
|
|
958
|
+
}
|
|
959
|
+
const out = [];
|
|
960
|
+
for (let i = lo; i < fl.length && fl[i].start <= t; i++) out.push(fl[i]);
|
|
961
|
+
return out;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
function roundRect(ctx, x, y, w, h, r) {
|
|
965
|
+
const rr = Math.max(0, Math.min(r, w / 2, h / 2));
|
|
966
|
+
ctx.beginPath();
|
|
967
|
+
if (ctx.roundRect) ctx.roundRect(x, y, w, h, rr);
|
|
968
|
+
else ctx.rect(x, y, w, h);
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
function polyline(ctx, pts) {
|
|
972
|
+
if (pts.length < 2) return;
|
|
973
|
+
ctx.beginPath();
|
|
974
|
+
ctx.moveTo(pts[0].x, pts[0].y);
|
|
975
|
+
for (let i = 1; i < pts.length; i++) ctx.lineTo(pts[i].x, pts[i].y);
|
|
976
|
+
ctx.stroke();
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
function draw(dt) {
|
|
980
|
+
const { ctx, canvas, scene, pitch: P, origin: O, col } = S;
|
|
981
|
+
const cam = camera();
|
|
982
|
+
const dpr = window.devicePixelRatio || 1;
|
|
983
|
+
const W = Math.round(cam.r.width);
|
|
984
|
+
const H = Math.round(cam.r.height);
|
|
985
|
+
if (canvas.width !== W * dpr || canvas.height !== H * dpr) {
|
|
986
|
+
canvas.width = W * dpr;
|
|
987
|
+
canvas.height = H * dpr;
|
|
988
|
+
canvas.style.width = `${W}px`;
|
|
989
|
+
canvas.style.height = `${H}px`;
|
|
990
|
+
}
|
|
991
|
+
canvas.style.left = `${cam.left}px`;
|
|
992
|
+
canvas.style.top = `${cam.top}px`;
|
|
993
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
994
|
+
ctx.clearRect(0, 0, W, H);
|
|
995
|
+
const s = cam.s;
|
|
996
|
+
// Scene pitch units -> canvas px.
|
|
997
|
+
const sx = (u) => (O.x + u * P) * s + cam.ox - cam.left;
|
|
998
|
+
const sy = (u) => (O.y + u * P) * s + cam.oy - cam.top;
|
|
999
|
+
// viewBox units -> canvas px (the cards' centres).
|
|
1000
|
+
const vx = (x) => x * s + cam.ox - cam.left;
|
|
1001
|
+
const vy = (y) => y * s + cam.oy - cam.top;
|
|
1002
|
+
|
|
1003
|
+
const t = S.t;
|
|
1004
|
+
const reduced = S.reduced;
|
|
1005
|
+
const tc = cardMorph(S.cardW * s, { reduced });
|
|
1006
|
+
S.cardPx = S.cardW * s;
|
|
1007
|
+
S.pitchPx = P * s;
|
|
1008
|
+
S.cardA = 1 - tc;
|
|
1009
|
+
// Cards fade faster than the dots grow, so the dots under them read by the middle of the morph.
|
|
1010
|
+
const cardA = Math.pow(1 - tc, 2).toFixed(3);
|
|
1011
|
+
if (S.host.style.getPropertyValue("--dots-card-a") !== cardA) S.host.style.setProperty("--dots-card-a", cardA);
|
|
1012
|
+
const hide = tc >= 1;
|
|
1013
|
+
if (hide !== S.svgHidden) {
|
|
1014
|
+
// The export owns its camera and measures the host, so it takes the SVG out of
|
|
1015
|
+
// layout altogether; the app's own wheel handler measures the SVG, so there it stays in layout.
|
|
1016
|
+
if (S.auto) S.svg.style.display = hide ? "none" : "";
|
|
1017
|
+
else S.svg.style.visibility = hide ? "hidden" : "";
|
|
1018
|
+
S.svgHidden = hide;
|
|
1019
|
+
S.svgGone = hide && !!S.auto;
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
const k = takeoffsBy(scene, t);
|
|
1023
|
+
const L = scene.layoutAt(k);
|
|
1024
|
+
const near = flightsNear(t);
|
|
1025
|
+
const inAir = new Map();
|
|
1026
|
+
for (const f of near) if (t < f.end) inAir.set(f.address, f);
|
|
1027
|
+
const owners = new Map(scene.show.owners0);
|
|
1028
|
+
for (const f of scene.landing) if (f.end <= t) owners.set(f.address, f.to);
|
|
1029
|
+
S.ownersNow = owners;
|
|
1030
|
+
// A dot counts where it is until it lands.
|
|
1031
|
+
const counts = new Map();
|
|
1032
|
+
for (const e of owners.values()) counts.set(e, (counts.get(e) ?? 0) + 1);
|
|
1033
|
+
|
|
1034
|
+
// Smoothing toward the layout: boxes and resting dots slide when a box re-packs.
|
|
1035
|
+
const ease = S.snap || reduced ? 1 : 1 - Math.exp(-dt / 140);
|
|
1036
|
+
let moving = false;
|
|
1037
|
+
const boxes = new Map();
|
|
1038
|
+
for (const [e, b] of L.boxes) {
|
|
1039
|
+
const prev = S.boxDisp.get(e);
|
|
1040
|
+
const cur = prev && !S.snap ? { x: lerp(prev.x, b.x, ease), y: lerp(prev.y, b.y, ease), w: lerp(prev.w, b.w, ease), h: lerp(prev.h, b.h, ease) } : { ...b };
|
|
1041
|
+
if (prev && Math.abs(cur.h - b.h) + Math.abs(cur.y - b.y) + Math.abs(cur.w - b.w) > 0.01) moving = true;
|
|
1042
|
+
S.boxDisp.set(e, cur);
|
|
1043
|
+
boxes.set(e, cur);
|
|
1044
|
+
}
|
|
1045
|
+
for (const e of [...S.boxDisp.keys()]) if (!L.boxes.has(e)) S.boxDisp.delete(e);
|
|
1046
|
+
|
|
1047
|
+
// Per-estate disc morph from the box's size on screen.
|
|
1048
|
+
const td = new Map();
|
|
1049
|
+
// A new estate's form is judged by the box it is growing into, so it does not
|
|
1050
|
+
// flip from disc to dots as its first dots land.
|
|
1051
|
+
for (const [e, b] of boxes) {
|
|
1052
|
+
const fin = e === scene.source ? null : scene.finalSize.get(e);
|
|
1053
|
+
const size = Math.max(b.w, b.h, fin ? Math.max(fin.w, fin.h) : 0);
|
|
1054
|
+
td.set(e, discMorph(size * P * s, { reduced }));
|
|
1055
|
+
}
|
|
1056
|
+
const level = levelOf(tc, td.values());
|
|
1057
|
+
// Reaching the estate level by hand fits the estates, so they are not a speck in a corner.
|
|
1058
|
+
if (level === "discs" && S.level && S.level !== "discs" && !S.fly && !S.directing) queueMicrotask(() => fly("estates"));
|
|
1059
|
+
S.level = level;
|
|
1060
|
+
const dotLayerA = tc;
|
|
1061
|
+
|
|
1062
|
+
// Activity per estate: dots in the air toward it (or leaving it), for the running light.
|
|
1063
|
+
const act = new Map();
|
|
1064
|
+
for (const f of inAir.values()) {
|
|
1065
|
+
if (!f.ok) continue;
|
|
1066
|
+
act.set(f.to, (act.get(f.to) ?? 0) + 1);
|
|
1067
|
+
act.set(f.from, (act.get(f.from) ?? 0) - 1);
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
const boxPx = (b) => ({ x: sx(b.x), y: sy(b.y), w: b.w * P * s, h: b.h * P * s });
|
|
1071
|
+
const centre = (e) => {
|
|
1072
|
+
const b = boxes.get(e);
|
|
1073
|
+
return b ? { x: b.x + b.w / 2, y: b.y + (BOX.title + b.h) / 2 } : { x: 0, y: 0 };
|
|
1074
|
+
};
|
|
1075
|
+
const rDot = Math.max(1.2, Math.min(6, P * s * 0.36));
|
|
1076
|
+
const firsts = S.firsts;
|
|
1077
|
+
|
|
1078
|
+
// Labels claim their space first come, first served; one that would overlap is left out.
|
|
1079
|
+
const taken = [];
|
|
1080
|
+
const claim = (x, y, w, h) => {
|
|
1081
|
+
for (const q of taken) if (x < q.x + q.w && x + w > q.x && y < q.y + q.h && y + h > q.y) return false;
|
|
1082
|
+
taken.push({ x, y, w, h });
|
|
1083
|
+
return true;
|
|
1084
|
+
};
|
|
1085
|
+
// Discs hold their ground before any label is placed.
|
|
1086
|
+
for (const [e] of boxes) {
|
|
1087
|
+
const tdE = td.get(e) ?? 0;
|
|
1088
|
+
const cnt = counts.get(e) ?? 0;
|
|
1089
|
+
if (tdE < 0.5 || !cnt) continue;
|
|
1090
|
+
const d = centre(e);
|
|
1091
|
+
const r = Math.max(2.5, discRadius(cnt, P) * s);
|
|
1092
|
+
taken.push({ x: sx(d.x) - r, y: sy(d.y) - r, w: 2 * r, h: 2 * r, disc: e });
|
|
1093
|
+
}
|
|
1094
|
+
// --- boxes: fill, outline, the lights on their borders ---
|
|
1095
|
+
const titlePx = BOX.title * P * s;
|
|
1096
|
+
// Where each box's label went, so a Tips bubble keeps off it (web/carve-narration.js).
|
|
1097
|
+
const labelPx = new Map();
|
|
1098
|
+
for (const [e, b] of boxes) {
|
|
1099
|
+
const a = dotLayerA * (1 - (td.get(e) ?? 0));
|
|
1100
|
+
if (a <= 0.01) continue;
|
|
1101
|
+
const c = col.estate.get(e) || col.neutral;
|
|
1102
|
+
const bp = boxPx(b);
|
|
1103
|
+
const first = firsts.get(e);
|
|
1104
|
+
// The draw-in: from the moment before its first landing, a head traces the outline.
|
|
1105
|
+
let drawIn = 1;
|
|
1106
|
+
if (e !== scene.source && first !== undefined) drawIn = Math.max(0, Math.min(1, (t - (first - DRAW_MS)) / DRAW_MS));
|
|
1107
|
+
if (e !== scene.source && first !== undefined && t < first - DRAW_MS) continue;
|
|
1108
|
+
ctx.globalAlpha = a * (0.35 + 0.65 * drawIn);
|
|
1109
|
+
roundRect(ctx, bp.x, bp.y, bp.w, bp.h, Math.min(16, 1.2 * P * s));
|
|
1110
|
+
ctx.fillStyle = rgba(col.surface, 0.55);
|
|
1111
|
+
ctx.fill();
|
|
1112
|
+
const P0 = perimeter(bp);
|
|
1113
|
+
ctx.lineWidth = 1.3;
|
|
1114
|
+
ctx.strokeStyle = rgba(c, 0.6);
|
|
1115
|
+
if (drawIn >= 1 || reduced) {
|
|
1116
|
+
ctx.stroke();
|
|
1117
|
+
} else {
|
|
1118
|
+
// From the border point nearest the first dot's slot, clockwise.
|
|
1119
|
+
const anyLanding = scene.landing.find((f) => f.to === e);
|
|
1120
|
+
const slot = anyLanding && L.slots.get(anyLanding.address);
|
|
1121
|
+
const s0 = slot ? nearestS(bp, { x: sx(slot.x), y: sy(slot.y) }) : 0;
|
|
1122
|
+
ctx.strokeStyle = c;
|
|
1123
|
+
polyline(ctx, segment(bp, s0, P0 * drawIn));
|
|
1124
|
+
const head = pointAt(bp, s0 + P0 * drawIn);
|
|
1125
|
+
ctx.fillStyle = col.fg;
|
|
1126
|
+
ctx.beginPath();
|
|
1127
|
+
ctx.arc(head.x, head.y, 2.6, 0, Math.PI * 2);
|
|
1128
|
+
ctx.fill();
|
|
1129
|
+
}
|
|
1130
|
+
// The running light: speed follows the dots in the air; the source runs the other way.
|
|
1131
|
+
if (!reduced && drawIn >= 1) {
|
|
1132
|
+
const target = Math.min(12, Math.abs(act.get(e) ?? 0));
|
|
1133
|
+
const cur = S.snap ? target : lerp(S.activity.get(e) ?? 0, target, 1 - Math.exp(-dt / 400));
|
|
1134
|
+
S.activity.set(e, cur);
|
|
1135
|
+
if (cur > 0.05) {
|
|
1136
|
+
moving = true;
|
|
1137
|
+
// Clockwise while receiving; the other way while giving dots up.
|
|
1138
|
+
const dir = (act.get(e) ?? 0) < 0 || (e === scene.source && (act.get(e) ?? 0) === 0) ? -1 : 1;
|
|
1139
|
+
const speed = (0.08 + 0.05 * cur) * P0; // perimeter fractions per second
|
|
1140
|
+
const ph = (S.phase.get(e) ?? 0) + (dir * speed * dt) / 1000;
|
|
1141
|
+
S.phase.set(e, ph);
|
|
1142
|
+
ctx.strokeStyle = col.pending;
|
|
1143
|
+
ctx.lineWidth = 2.4;
|
|
1144
|
+
ctx.globalAlpha = a * Math.min(1, cur);
|
|
1145
|
+
const len = Math.min(P0 * 0.09, 120);
|
|
1146
|
+
for (const off of [0, P0 / 2]) polyline(ctx, segment(bp, ph + off, -dir * len));
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
// Labels: the name, then the live count. Inside the title row when the row
|
|
1150
|
+
// and the box have room, else just above the outline (it may run past a
|
|
1151
|
+
// narrow box's edge), so a label never sits on the dots.
|
|
1152
|
+
const fs = 11.5;
|
|
1153
|
+
if (tc > 0.6 && (td.get(e) ?? 0) < 0.5) {
|
|
1154
|
+
ctx.font = `700 ${fs}px ${S.font}`;
|
|
1155
|
+
ctx.textBaseline = "middle";
|
|
1156
|
+
ctx.textAlign = "left";
|
|
1157
|
+
let nm = shortName(e);
|
|
1158
|
+
const cnt = fmt(Math.max(0, counts.get(e) ?? 0));
|
|
1159
|
+
let nw = ctx.measureText(nm).width;
|
|
1160
|
+
const cw = ctx.measureText(cnt).width;
|
|
1161
|
+
const tw = nw + fs * 0.5 + cw;
|
|
1162
|
+
const inside = titlePx >= fs * 1.5 && tw + fs * 1.6 <= bp.w;
|
|
1163
|
+
const lx = bp.x + (inside ? fs * 0.8 : 1);
|
|
1164
|
+
// Outside the box, a label runs no further than the next box along its row, with a gap, so two never touch.
|
|
1165
|
+
let room = Infinity;
|
|
1166
|
+
if (!inside) {
|
|
1167
|
+
for (const [o, ob] of boxes) {
|
|
1168
|
+
if (o === e) continue;
|
|
1169
|
+
const op = boxPx(ob);
|
|
1170
|
+
if (op.x > bp.x + 1 && op.y < bp.y + fs && op.y + op.h > bp.y - fs * 2.4) room = Math.min(room, op.x - lx - fs);
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
// Too wide for its room: the count goes under the name, and a name still too wide is shortened.
|
|
1174
|
+
const stacked = !inside && tw > room;
|
|
1175
|
+
if (stacked && nw > room) {
|
|
1176
|
+
while (nm.length > 4 && ctx.measureText(`${nm}…`).width > room) nm = nm.slice(0, -1);
|
|
1177
|
+
nm = `${nm}…`;
|
|
1178
|
+
nw = ctx.measureText(nm).width;
|
|
1179
|
+
}
|
|
1180
|
+
const lw = stacked ? Math.max(nw, cw) : tw;
|
|
1181
|
+
const lh = stacked ? fs * 2.4 : fs * 1.2;
|
|
1182
|
+
let ly = inside ? bp.y + titlePx * 0.55 : bp.y - fs * 0.75 - (stacked ? fs * 1.2 : 0);
|
|
1183
|
+
let ok = claim(lx, ly - fs * 0.6, lw, lh);
|
|
1184
|
+
// No room above: try just below the outline.
|
|
1185
|
+
if (!ok && !inside) {
|
|
1186
|
+
ly = bp.y + bp.h + fs * 0.8;
|
|
1187
|
+
ok = claim(lx, ly - fs * 0.6, lw, lh);
|
|
1188
|
+
}
|
|
1189
|
+
if (ok) {
|
|
1190
|
+
ctx.globalAlpha = a * drawIn;
|
|
1191
|
+
ctx.fillStyle = c;
|
|
1192
|
+
ctx.fillText(nm, lx, ly);
|
|
1193
|
+
ctx.fillStyle = col.muted;
|
|
1194
|
+
if (stacked) ctx.fillText(cnt, lx, ly + fs * 1.2);
|
|
1195
|
+
else ctx.fillText(cnt, lx + nw + fs * 0.5, ly);
|
|
1196
|
+
labelPx.set(e, { x: lx, y: ly - fs * 0.6, w: lw, h: lh });
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
ctx.globalAlpha = 1;
|
|
1201
|
+
|
|
1202
|
+
// --- threads between estates (cross-estate reads), dashes marching reader -> source ---
|
|
1203
|
+
const tShow = showTime(t);
|
|
1204
|
+
const threadsOn = S.show.threads.length && S.show.events.some((ev) => ev.kind === "handoff" && ev.start <= tShow);
|
|
1205
|
+
if (threadsOn) {
|
|
1206
|
+
ctx.lineWidth = 1.2;
|
|
1207
|
+
for (const th of S.show.threads) {
|
|
1208
|
+
if (!boxes.has(th.from) || !boxes.has(th.to)) continue;
|
|
1209
|
+
const a = centre(th.to);
|
|
1210
|
+
const b = centre(th.from);
|
|
1211
|
+
ctx.globalAlpha = 0.55 * Math.max(dotLayerA, 0.5);
|
|
1212
|
+
ctx.strokeStyle = col.runtime;
|
|
1213
|
+
ctx.setLineDash([6, 6]);
|
|
1214
|
+
ctx.lineDashOffset = reduced ? 0 : -((performance.now() / 40) % 12);
|
|
1215
|
+
ctx.beginPath();
|
|
1216
|
+
ctx.moveTo(sx(a.x), sy(a.y));
|
|
1217
|
+
ctx.lineTo(sx(b.x), sy(b.y));
|
|
1218
|
+
ctx.stroke();
|
|
1219
|
+
ctx.setLineDash([]);
|
|
1220
|
+
if (!reduced) moving = true;
|
|
1221
|
+
}
|
|
1222
|
+
// A hand-off: one bright pulse from the source (wave 1) along each thread to its reader (wave 2).
|
|
1223
|
+
for (const ev of S.show.events) {
|
|
1224
|
+
if (ev.kind !== "handoff" || tShow < ev.start || tShow > ev.t + (ev.t - ev.start)) continue;
|
|
1225
|
+
const u = Math.min(1, (tShow - ev.start) / Math.max(1, ev.t - ev.start));
|
|
1226
|
+
const a = centre(ev.from);
|
|
1227
|
+
const b = centre(ev.to);
|
|
1228
|
+
const x = sx(lerp(a.x, b.x, u));
|
|
1229
|
+
const y = sy(lerp(a.y, b.y, u));
|
|
1230
|
+
ctx.globalAlpha = 1 - Math.max(0, u - 0.85) / 0.15;
|
|
1231
|
+
ctx.fillStyle = col.runtime;
|
|
1232
|
+
ctx.beginPath();
|
|
1233
|
+
ctx.arc(x, y, 4.5, 0, Math.PI * 2);
|
|
1234
|
+
ctx.fill();
|
|
1235
|
+
}
|
|
1236
|
+
ctx.globalAlpha = 1;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
// --- dots ---
|
|
1240
|
+
const addrs = S.addrs;
|
|
1241
|
+
const discAt = (e) => centre(e);
|
|
1242
|
+
const slotOf = (Lx, a) => Lx.slots.get(a);
|
|
1243
|
+
const lit = new Map(); // estate -> resources lit now (for a disc's brightness)
|
|
1244
|
+
const markLit = (e) => lit.set(e, (lit.get(e) ?? 0) + 1);
|
|
1245
|
+
const events = S.show.events;
|
|
1246
|
+
const glow = new Map(); // address -> {colour, a}
|
|
1247
|
+
const rings = new Set();
|
|
1248
|
+
for (const ev of events) {
|
|
1249
|
+
if (!ev.address || ev.start > tShow) continue;
|
|
1250
|
+
if (ev.kind === "adopt") rings.add(ev.address);
|
|
1251
|
+
if ((ev.kind === "read" || ev.kind === "planned") && tShow <= ev.t + LINGER_MS) {
|
|
1252
|
+
const u = Math.min(1, (tShow - ev.start) / 300);
|
|
1253
|
+
const fade = tShow > ev.t ? 1 - (tShow - ev.t) / LINGER_MS : 1;
|
|
1254
|
+
const prev = glow.get(ev.address);
|
|
1255
|
+
const g = { colour: ev.kind === "planned" ? col.foreign : col.runtime, a: (ev.kind === "planned" ? 0.9 : 0.45) * u * fade };
|
|
1256
|
+
if (!prev || g.a > prev.a) glow.set(ev.address, g);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
// The tour's lights: each resource the chapter's plan planned, brighter where the plan changes it.
|
|
1260
|
+
const fx = currentFx(owners);
|
|
1261
|
+
if (fx) for (const [id, l] of fx.lights) {
|
|
1262
|
+
const prev = glow.get(id);
|
|
1263
|
+
const g = { colour: col.foreign, a: l.changed ? 1 : 0.85, ring: l.changed };
|
|
1264
|
+
if (!prev || g.a >= prev.a) glow.set(id, g);
|
|
1265
|
+
}
|
|
1266
|
+
ctx.globalAlpha = 1;
|
|
1267
|
+
const refusedLit = [];
|
|
1268
|
+
for (let i = 0; i < addrs.length; i++) {
|
|
1269
|
+
const a = addrs[i];
|
|
1270
|
+
const f = inAir.get(a);
|
|
1271
|
+
const owner = owners.get(a) ?? scene.source;
|
|
1272
|
+
let p;
|
|
1273
|
+
let colour = col.estate.get(owner) || col.neutral;
|
|
1274
|
+
let alpha = 1;
|
|
1275
|
+
let est = owner;
|
|
1276
|
+
if (f && f.ok && !reduced) {
|
|
1277
|
+
const Lb = scene.layoutAt(f.k - 1);
|
|
1278
|
+
const La = scene.layoutAt(f.k);
|
|
1279
|
+
const from = slotOf(Lb, a) ?? slotOf(L, a);
|
|
1280
|
+
const to = slotOf(La, a) ?? from;
|
|
1281
|
+
const u = (t - f.start) / (f.end - f.start);
|
|
1282
|
+
if (f.parent && inAir.get(f.parent)) {
|
|
1283
|
+
// A child keeps orbiting its parent along the parent's arc.
|
|
1284
|
+
const pf = inAir.get(f.parent);
|
|
1285
|
+
const pFrom = slotOf(scene.layoutAt(pf.k - 1), f.parent) ?? from;
|
|
1286
|
+
const pTo = slotOf(scene.layoutAt(pf.k), f.parent) ?? to;
|
|
1287
|
+
const pu = (t - pf.start) / (pf.end - pf.start);
|
|
1288
|
+
const pp = flightPoint(pFrom, pTo, pu);
|
|
1289
|
+
const e = easeInOut(u);
|
|
1290
|
+
const ox = lerp(from.x - pFrom.x, to.x - pTo.x, e);
|
|
1291
|
+
const oy = lerp(from.y - pFrom.y, to.y - pTo.y, e);
|
|
1292
|
+
const th = Math.sin(Math.PI * e) * Math.PI * 0.9;
|
|
1293
|
+
p = { x: pp.x + ox * Math.cos(th) - oy * Math.sin(th), y: pp.y + ox * Math.sin(th) + oy * Math.cos(th) };
|
|
1294
|
+
} else p = flightPoint(from, to, u);
|
|
1295
|
+
colour = u < 0.5 ? col.estate.get(f.from) || col.neutral : col.estate.get(f.to) || col.neutral;
|
|
1296
|
+
est = u < 0.5 ? f.from : f.to;
|
|
1297
|
+
// The trail: where it was a moment ago, fading, in the destination's colour.
|
|
1298
|
+
if (dotLayerA > 0.05 && !f.parent) {
|
|
1299
|
+
ctx.strokeStyle = col.estate.get(f.to) || col.neutral;
|
|
1300
|
+
ctx.lineCap = "round";
|
|
1301
|
+
let prev = p;
|
|
1302
|
+
for (let j = 1; j <= 4; j++) {
|
|
1303
|
+
const q = flightPoint(from, to, Math.max(0, u - j * 0.025));
|
|
1304
|
+
ctx.globalAlpha = dotLayerA * (1 - j / 5) * 0.7;
|
|
1305
|
+
ctx.lineWidth = rDot * (1.4 - j * 0.2);
|
|
1306
|
+
ctx.beginPath();
|
|
1307
|
+
ctx.moveTo(sx(prev.x), sy(prev.y));
|
|
1308
|
+
ctx.lineTo(sx(q.x), sy(q.y));
|
|
1309
|
+
ctx.stroke();
|
|
1310
|
+
prev = q;
|
|
1311
|
+
}
|
|
1312
|
+
ctx.lineCap = "butt";
|
|
1313
|
+
}
|
|
1314
|
+
moving = true;
|
|
1315
|
+
} else if (f && f.ok && reduced) {
|
|
1316
|
+
// No flight: out of the old slot, into the new one.
|
|
1317
|
+
const u = (t - f.start) / (f.end - f.start);
|
|
1318
|
+
const from = slotOf(scene.layoutAt(f.k - 1), a) ?? slotOf(L, a);
|
|
1319
|
+
const to = slotOf(scene.layoutAt(f.k), a) ?? from;
|
|
1320
|
+
p = u < 0.5 ? from : to;
|
|
1321
|
+
alpha = u < 0.5 ? 1 - u * 2 : (u - 0.5) * 2;
|
|
1322
|
+
colour = col.estate.get(u < 0.5 ? f.from : f.to) || col.neutral;
|
|
1323
|
+
} else if (f && !f.ok && !reduced) {
|
|
1324
|
+
const here = slotOf(L, a);
|
|
1325
|
+
const there = boxes.get(f.to) ? centre(f.to) : here;
|
|
1326
|
+
p = flightPoint(here, there, (t - f.start) / (f.end - f.start), { ok: false });
|
|
1327
|
+
moving = true;
|
|
1328
|
+
} else {
|
|
1329
|
+
const target = slotOf(L, a);
|
|
1330
|
+
if (!target) continue;
|
|
1331
|
+
const prev = S.disp.get(a);
|
|
1332
|
+
if (prev && !S.snap && ease < 1) {
|
|
1333
|
+
const nx = lerp(prev.x, target.x, ease);
|
|
1334
|
+
const ny = lerp(prev.y, target.y, ease);
|
|
1335
|
+
if (Math.abs(nx - target.x) + Math.abs(ny - target.y) > 0.01) moving = true;
|
|
1336
|
+
p = { x: nx, y: ny };
|
|
1337
|
+
} else p = target;
|
|
1338
|
+
}
|
|
1339
|
+
S.disp.set(a, p);
|
|
1340
|
+
const tdE = td.get(est) ?? 0;
|
|
1341
|
+
// Card centre -> slot -> disc, by the zoom.
|
|
1342
|
+
const card = S.cards.get(a);
|
|
1343
|
+
const d = discAt(est);
|
|
1344
|
+
const slotPx = { x: sx(p.x), y: sy(p.y) };
|
|
1345
|
+
const cardPx = card ? { x: vx(card.x), y: vy(card.y) } : slotPx;
|
|
1346
|
+
const discPx = { x: sx(d.x), y: sy(d.y) };
|
|
1347
|
+
const q = {
|
|
1348
|
+
x: lerp(lerp(cardPx.x, slotPx.x, tc), discPx.x, tdE),
|
|
1349
|
+
y: lerp(lerp(cardPx.y, slotPx.y, tc), discPx.y, tdE),
|
|
1350
|
+
};
|
|
1351
|
+
const vis = tdE > 0.97 ? 0 : dotLayerA * (1 - tdE) * alpha;
|
|
1352
|
+
S.px[i] = q.x;
|
|
1353
|
+
S.py[i] = q.y;
|
|
1354
|
+
S.pvis[i] = vis > 0.2 ? 1 : 0;
|
|
1355
|
+
if (q.x < -20 || q.y < -20 || q.x > W + 20 || q.y > H + 20) continue;
|
|
1356
|
+
const g = glow.get(a);
|
|
1357
|
+
if (f || g) markLit(est);
|
|
1358
|
+
if (vis <= 0.01) continue;
|
|
1359
|
+
if (g) {
|
|
1360
|
+
ctx.globalAlpha = vis * g.a * 0.6;
|
|
1361
|
+
ctx.fillStyle = g.colour;
|
|
1362
|
+
ctx.beginPath();
|
|
1363
|
+
ctx.arc(q.x, q.y, rDot * 2.6, 0, Math.PI * 2);
|
|
1364
|
+
ctx.fill();
|
|
1365
|
+
}
|
|
1366
|
+
const flying = f && f.ok && !reduced;
|
|
1367
|
+
if (flying) {
|
|
1368
|
+
// A dot in the air is lifted: larger, with a soft halo in the colour it is turning.
|
|
1369
|
+
ctx.globalAlpha = vis * 0.25;
|
|
1370
|
+
ctx.fillStyle = colour;
|
|
1371
|
+
ctx.beginPath();
|
|
1372
|
+
ctx.arc(q.x, q.y, rDot * 3, 0, Math.PI * 2);
|
|
1373
|
+
ctx.fill();
|
|
1374
|
+
}
|
|
1375
|
+
ctx.globalAlpha = vis;
|
|
1376
|
+
ctx.fillStyle = colour;
|
|
1377
|
+
ctx.beginPath();
|
|
1378
|
+
ctx.arc(q.x, q.y, flying ? rDot * 1.6 : rDot, 0, Math.PI * 2);
|
|
1379
|
+
ctx.fill();
|
|
1380
|
+
if (g && g.ring) {
|
|
1381
|
+
ctx.globalAlpha = vis;
|
|
1382
|
+
ctx.strokeStyle = g.colour;
|
|
1383
|
+
ctx.lineWidth = Math.max(1.2, rDot * 0.45);
|
|
1384
|
+
ctx.beginPath();
|
|
1385
|
+
ctx.arc(q.x, q.y, rDot * 2.3, 0, Math.PI * 2);
|
|
1386
|
+
ctx.stroke();
|
|
1387
|
+
}
|
|
1388
|
+
if (a === S.selected) {
|
|
1389
|
+
ctx.globalAlpha = 1;
|
|
1390
|
+
ctx.strokeStyle = col.fg;
|
|
1391
|
+
ctx.lineWidth = 1.6;
|
|
1392
|
+
ctx.beginPath();
|
|
1393
|
+
ctx.arc(q.x, q.y, rDot * 2.8, 0, Math.PI * 2);
|
|
1394
|
+
ctx.stroke();
|
|
1395
|
+
ctx.globalAlpha = vis;
|
|
1396
|
+
}
|
|
1397
|
+
if (rings.has(a)) {
|
|
1398
|
+
ctx.strokeStyle = col.managed;
|
|
1399
|
+
ctx.lineWidth = Math.max(1, rDot * 0.35);
|
|
1400
|
+
ctx.beginPath();
|
|
1401
|
+
ctx.arc(q.x, q.y, rDot * 1.7, 0, Math.PI * 2);
|
|
1402
|
+
ctx.stroke();
|
|
1403
|
+
}
|
|
1404
|
+
// Takeoff: a ring in the in-progress colour while the write starts.
|
|
1405
|
+
if (f && f.ok && !reduced) {
|
|
1406
|
+
const u = (t - f.start) / (f.end - f.start);
|
|
1407
|
+
if (u < 0.2) {
|
|
1408
|
+
ctx.strokeStyle = col.pending;
|
|
1409
|
+
ctx.globalAlpha = vis * (1 - u / 0.2);
|
|
1410
|
+
ctx.lineWidth = 1.4;
|
|
1411
|
+
ctx.beginPath();
|
|
1412
|
+
ctx.arc(q.x, q.y, rDot * (1.6 + u * 4), 0, Math.PI * 2);
|
|
1413
|
+
ctx.stroke();
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
// Landings: a small pulse at the dot and a ripple along the nearest border.
|
|
1418
|
+
for (const f of near) {
|
|
1419
|
+
if (t < f.end || t > f.end + RIPPLE_MS) continue;
|
|
1420
|
+
const u = (t - f.end) / RIPPLE_MS;
|
|
1421
|
+
const i = S.idx.get(f.address);
|
|
1422
|
+
if (f.ok) {
|
|
1423
|
+
const tdE = td.get(f.to) ?? 0;
|
|
1424
|
+
const a = dotLayerA * (1 - tdE);
|
|
1425
|
+
if (a < 0.02) continue;
|
|
1426
|
+
const qx = S.px[i];
|
|
1427
|
+
const qy = S.py[i];
|
|
1428
|
+
if (u < PULSE_MS / RIPPLE_MS && !f.parent) {
|
|
1429
|
+
ctx.strokeStyle = col.estate.get(f.to) || col.neutral;
|
|
1430
|
+
ctx.globalAlpha = a * (1 - u);
|
|
1431
|
+
ctx.lineWidth = 1.5;
|
|
1432
|
+
ctx.beginPath();
|
|
1433
|
+
ctx.arc(qx, qy, rDot * (1.2 + u * 5), 0, Math.PI * 2);
|
|
1434
|
+
ctx.stroke();
|
|
1435
|
+
}
|
|
1436
|
+
const b = boxes.get(f.to);
|
|
1437
|
+
if (b && !f.parent && !reduced) {
|
|
1438
|
+
const bp = boxPx(b);
|
|
1439
|
+
const s0 = nearestS(bp, { x: qx, y: qy });
|
|
1440
|
+
const len = Math.min(perimeter(bp) * 0.12, 90) * Math.sqrt(u);
|
|
1441
|
+
ctx.strokeStyle = col.estate.get(f.to) || col.neutral;
|
|
1442
|
+
ctx.lineWidth = 2.2;
|
|
1443
|
+
ctx.globalAlpha = a * (1 - u) * 0.9;
|
|
1444
|
+
polyline(ctx, segment(bp, s0, len));
|
|
1445
|
+
polyline(ctx, segment(bp, s0, -len));
|
|
1446
|
+
}
|
|
1447
|
+
moving = true;
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
// Refused moves stay lit: a red flare at the dot and a red segment on its box's border.
|
|
1451
|
+
for (const f of scene.flights) {
|
|
1452
|
+
if (f.ok || f.end > t) continue;
|
|
1453
|
+
const i = S.idx.get(f.address);
|
|
1454
|
+
if (i < 0 || !S.pvis[i]) continue;
|
|
1455
|
+
refusedLit.push(f);
|
|
1456
|
+
const qx = S.px[i];
|
|
1457
|
+
const qy = S.py[i];
|
|
1458
|
+
const u = Math.min(1, (t - f.end) / PULSE_MS);
|
|
1459
|
+
ctx.globalAlpha = dotLayerA * (0.35 + 0.4 * (1 - u));
|
|
1460
|
+
ctx.fillStyle = col.degraded;
|
|
1461
|
+
ctx.beginPath();
|
|
1462
|
+
ctx.arc(qx, qy, rDot * (2.2 + 1.5 * (1 - u)), 0, Math.PI * 2);
|
|
1463
|
+
ctx.fill();
|
|
1464
|
+
ctx.globalAlpha = dotLayerA;
|
|
1465
|
+
ctx.beginPath();
|
|
1466
|
+
ctx.arc(qx, qy, rDot, 0, Math.PI * 2);
|
|
1467
|
+
ctx.fill();
|
|
1468
|
+
const b = boxes.get(owners.get(f.address) ?? scene.source);
|
|
1469
|
+
if (b) {
|
|
1470
|
+
const bp = boxPx(b);
|
|
1471
|
+
const s0 = nearestS(bp, { x: qx, y: qy });
|
|
1472
|
+
ctx.strokeStyle = col.degraded;
|
|
1473
|
+
ctx.lineWidth = 2.4;
|
|
1474
|
+
ctx.globalAlpha = dotLayerA * (1 - (td.get(owners.get(f.address)) ?? 0)) * 0.9;
|
|
1475
|
+
polyline(ctx, segment(bp, s0 - 6, 12));
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
// --- discs ---
|
|
1480
|
+
for (const [e, b] of boxes) {
|
|
1481
|
+
const tdE = td.get(e) ?? 0;
|
|
1482
|
+
const a = tdE * Math.max(dotLayerA, 0);
|
|
1483
|
+
const cnt = Math.max(0, counts.get(e) ?? 0);
|
|
1484
|
+
if (a <= 0.01 || cnt === 0) continue;
|
|
1485
|
+
const c = col.estate.get(e) || col.neutral;
|
|
1486
|
+
const d = centre(e);
|
|
1487
|
+
const x = sx(d.x);
|
|
1488
|
+
const y = sy(d.y);
|
|
1489
|
+
const r = Math.max(2.5, discRadius(cnt, P) * s);
|
|
1490
|
+
const share = Math.min(1, (lit.get(e) ?? 0) / Math.max(1, cnt));
|
|
1491
|
+
ctx.globalAlpha = a * (0.45 + 0.5 * Math.min(1, share * 6));
|
|
1492
|
+
ctx.fillStyle = c;
|
|
1493
|
+
ctx.beginPath();
|
|
1494
|
+
ctx.arc(x, y, r, 0, Math.PI * 2);
|
|
1495
|
+
ctx.fill();
|
|
1496
|
+
if (share > 0) {
|
|
1497
|
+
ctx.globalAlpha = a * Math.min(0.6, share * 4);
|
|
1498
|
+
ctx.strokeStyle = col.pending;
|
|
1499
|
+
ctx.lineWidth = 2;
|
|
1500
|
+
ctx.beginPath();
|
|
1501
|
+
ctx.arc(x, y, r + 3, 0, Math.PI * 2);
|
|
1502
|
+
ctx.stroke();
|
|
1503
|
+
}
|
|
1504
|
+
const fs = 10.5;
|
|
1505
|
+
if (tdE >= 0.5) {
|
|
1506
|
+
ctx.globalAlpha = a;
|
|
1507
|
+
ctx.font = `600 ${fs}px ${S.font}`;
|
|
1508
|
+
ctx.textAlign = "center";
|
|
1509
|
+
ctx.textBaseline = "top";
|
|
1510
|
+
const lw = Math.max(ctx.measureText(shortName(e)).width, ctx.measureText(fmt(cnt)).width);
|
|
1511
|
+
// Big enough to carry its own label, at a size that fits inside it.
|
|
1512
|
+
const fin = Math.min(fs, (fs * r * 1.85) / Math.max(1, lw));
|
|
1513
|
+
if (fin >= 8 && r >= fin * 1.3) {
|
|
1514
|
+
ctx.font = `600 ${fin}px ${S.font}`;
|
|
1515
|
+
ctx.textBaseline = "middle";
|
|
1516
|
+
ctx.fillStyle = col.fg;
|
|
1517
|
+
ctx.fillText(shortName(e), x, y - fin * 0.55);
|
|
1518
|
+
ctx.globalAlpha = a * 0.75;
|
|
1519
|
+
ctx.fillText(fmt(cnt), x, y + fin * 0.65);
|
|
1520
|
+
} else {
|
|
1521
|
+
// Below it, unless that would sit on another disc or label.
|
|
1522
|
+
const mine = taken.findIndex((q) => q.disc === e);
|
|
1523
|
+
const saved = mine >= 0 ? taken.splice(mine, 1)[0] : null;
|
|
1524
|
+
if (claim(x - lw / 2, y + r + 3, lw, fs * 2 + 3)) {
|
|
1525
|
+
ctx.fillStyle = col.fg;
|
|
1526
|
+
ctx.fillText(shortName(e), x, y + r + 3);
|
|
1527
|
+
ctx.fillStyle = col.muted;
|
|
1528
|
+
ctx.fillText(fmt(cnt), x, y + r + 4 + fs);
|
|
1529
|
+
}
|
|
1530
|
+
if (saved) taken.push(saved);
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
// At the disc level a move is a comet from one disc to the other.
|
|
1535
|
+
for (const f of inAir.values()) {
|
|
1536
|
+
if (!f.ok || f.parent || reduced) continue;
|
|
1537
|
+
const a = Math.min(td.get(f.from) ?? 0, td.get(f.to) ?? 0) * dotLayerA;
|
|
1538
|
+
if (a < 0.05) continue;
|
|
1539
|
+
const u = easeInOut((t - f.start) / (f.end - f.start));
|
|
1540
|
+
const A = centre(f.from);
|
|
1541
|
+
const B = centre(f.to);
|
|
1542
|
+
const pt = (v) => flightPoint(A, B, v);
|
|
1543
|
+
const head = pt(u);
|
|
1544
|
+
ctx.strokeStyle = col.estate.get(f.to) || col.neutral;
|
|
1545
|
+
ctx.lineCap = "round";
|
|
1546
|
+
let prev = head;
|
|
1547
|
+
for (let j = 1; j <= 5; j++) {
|
|
1548
|
+
const q = pt(Math.max(0, u - j * 0.05));
|
|
1549
|
+
ctx.globalAlpha = a * (1 - j / 6);
|
|
1550
|
+
ctx.lineWidth = 3 - j * 0.4;
|
|
1551
|
+
ctx.beginPath();
|
|
1552
|
+
ctx.moveTo(sx(prev.x), sy(prev.y));
|
|
1553
|
+
ctx.lineTo(sx(q.x), sy(q.y));
|
|
1554
|
+
ctx.stroke();
|
|
1555
|
+
prev = q;
|
|
1556
|
+
}
|
|
1557
|
+
ctx.lineCap = "butt";
|
|
1558
|
+
ctx.globalAlpha = a;
|
|
1559
|
+
ctx.fillStyle = col.fg;
|
|
1560
|
+
ctx.beginPath();
|
|
1561
|
+
ctx.arc(sx(head.x), sy(head.y), 2.2, 0, Math.PI * 2);
|
|
1562
|
+
ctx.fill();
|
|
1563
|
+
moving = true;
|
|
1564
|
+
}
|
|
1565
|
+
drawTour(ctx, fx, boxes, boxPx, centre, sx, sy, dotLayerA, td, (e) => Math.max(2.5, discRadius(Math.max(1, counts.get(e) ?? 0), P) * s));
|
|
1566
|
+
ctx.globalAlpha = 1;
|
|
1567
|
+
S.snap = false;
|
|
1568
|
+
S.settling = moving;
|
|
1569
|
+
S.boxPx = new Map([...boxes].map(([e, b]) => [e, boxPx(b)]));
|
|
1570
|
+
S.labelPx = labelPx;
|
|
1571
|
+
updateUiFrame(counts, level);
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
// --- the kiosk (#712) ----------------------------------------------------------
|
|
1575
|
+
|
|
1576
|
+
/** A box and its label on the page, in the kiosk frame's pixels: a Tips bubble points at it and keeps off it. */
|
|
1577
|
+
function boxRect(e, b) {
|
|
1578
|
+
const l = S.labelPx && S.labelPx.get(e);
|
|
1579
|
+
const x0 = l ? Math.min(b.x, l.x) : b.x;
|
|
1580
|
+
const y0 = l ? Math.min(b.y, l.y) : b.y;
|
|
1581
|
+
const x1 = l ? Math.max(b.x + b.w, l.x + l.w) : b.x + b.w;
|
|
1582
|
+
const y1 = l ? Math.max(b.y + b.h, l.y + l.h) : b.y + b.h;
|
|
1583
|
+
return { left: x0 + (S.canvas.offsetLeft || 0), top: y0 + (S.canvas.offsetTop || 0), width: x1 - x0, height: y1 - y0 };
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
function toParent(msg) {
|
|
1587
|
+
if (S && S.narrate) return S.narrate(msg);
|
|
1588
|
+
try {
|
|
1589
|
+
if (window.parent && window.parent !== window) window.parent.postMessage(msg, location.origin);
|
|
1590
|
+
} catch {
|
|
1591
|
+
/* no parent */
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
function startKiosk() {
|
|
1596
|
+
S.story = carveStory(S.scene);
|
|
1597
|
+
S.mode = "watch";
|
|
1598
|
+
S.insets = { top: 0, bottom: 0 };
|
|
1599
|
+
S.ui.classList.add("dots-kiosk", "dots-watch");
|
|
1600
|
+
if (S.keepBar) S.ui.classList.add("dots-keep-bar");
|
|
1601
|
+
S.userUntil = 0;
|
|
1602
|
+
S.holdUntil = null;
|
|
1603
|
+
S.tipFrom = -1;
|
|
1604
|
+
S.lastPost = 0;
|
|
1605
|
+
S.dir = null;
|
|
1606
|
+
const onMsg = (e) => {
|
|
1607
|
+
if (!S || e.origin !== location.origin || !e.data) return;
|
|
1608
|
+
if (e.data.type === "behold:dots-speed") {
|
|
1609
|
+
setSpeed(Number(e.data.speed) || 1);
|
|
1610
|
+
return;
|
|
1611
|
+
}
|
|
1612
|
+
if (e.data.type !== "behold:dots-mode") return;
|
|
1613
|
+
S.mode = e.data.mode === "inspect" ? "inspect" : "watch";
|
|
1614
|
+
S.ui.classList.toggle("dots-watch", S.mode === "watch");
|
|
1615
|
+
if (e.data.insets) S.insets = { top: Number(e.data.insets.top) || 0, bottom: Number(e.data.insets.bottom) || 0 };
|
|
1616
|
+
S.dirty = true;
|
|
1617
|
+
};
|
|
1618
|
+
window.addEventListener("message", onMsg);
|
|
1619
|
+
S.off.push(() => window.removeEventListener("message", onMsg));
|
|
1620
|
+
// The director never moves while a person has the graph in hand.
|
|
1621
|
+
const touched = () => {
|
|
1622
|
+
if (S) S.userUntil = performance.now() + 8_000;
|
|
1623
|
+
};
|
|
1624
|
+
for (const ev of ["wheel", "mousedown", "touchstart"]) {
|
|
1625
|
+
S.host.addEventListener(ev, touched, { passive: true });
|
|
1626
|
+
S.off.push(() => S && S.host.removeEventListener(ev, touched));
|
|
1627
|
+
}
|
|
1628
|
+
S.camAt = null;
|
|
1629
|
+
play(true);
|
|
1630
|
+
toParent({ type: "behold:dots-ready", source: S.name });
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
/** A box in scene pitches, as the viewBox that frames it clear of the rail and the parent's caption. */
|
|
1634
|
+
function frameBox(b) {
|
|
1635
|
+
const { r, left, right } = band();
|
|
1636
|
+
const P = S.pitch;
|
|
1637
|
+
const pad = 3.5;
|
|
1638
|
+
const bx = S.origin.x + (b.x - pad) * P;
|
|
1639
|
+
const by = S.origin.y + (b.y - pad) * P;
|
|
1640
|
+
const bw = (b.w + 2 * pad) * P;
|
|
1641
|
+
const bh = (b.h + 2 * pad) * P;
|
|
1642
|
+
const top = S.insets.top + 8;
|
|
1643
|
+
// The quiet controls at the foot, when the page keeps them, are furniture the boxes stay clear of.
|
|
1644
|
+
const foot = S.keepBar ? (S.ui.querySelector(".dots-bottom")?.offsetHeight ?? 0) + 12 : 0;
|
|
1645
|
+
const bottom = S.insets.bottom + 8 + foot;
|
|
1646
|
+
const aw = Math.max(40, r.width - left - right - 16);
|
|
1647
|
+
const ah = Math.max(40, r.height - top - bottom);
|
|
1648
|
+
// Never so close that a card shows again: dots stay dots.
|
|
1649
|
+
let s = Math.min(aw / bw, ah / bh, 20 / S.cardW);
|
|
1650
|
+
const vw = r.width / s;
|
|
1651
|
+
const vh = r.height / s;
|
|
1652
|
+
const vx = bx + bw / 2 - (left + 8 + aw / 2) / s;
|
|
1653
|
+
const vy = by + bh / 2 - (top + ah / 2) / s;
|
|
1654
|
+
return [vx, vy, vw, vh];
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
function unionBox(a, b) {
|
|
1658
|
+
if (!a) return b;
|
|
1659
|
+
if (!b) return a;
|
|
1660
|
+
const x = Math.min(a.x, b.x);
|
|
1661
|
+
const y = Math.min(a.y, b.y);
|
|
1662
|
+
return { x, y, w: Math.max(a.x + a.w, b.x + b.w) - x, h: Math.max(a.y + a.h, b.y + b.h) - y };
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
function focusBox(focus) {
|
|
1666
|
+
const sc = S.scene;
|
|
1667
|
+
const now = sc.layoutAt(takeoffsBy(sc, S.t)).boxes;
|
|
1668
|
+
const fin = sc.layoutAt(sc.landing.length).boxes;
|
|
1669
|
+
const src = now.get(sc.source) || sc.layoutAt(0).boxes.get(sc.source);
|
|
1670
|
+
if (focus.kind === "all") return { x: 0, y: 0, w: sc.extent.w, h: sc.extent.h };
|
|
1671
|
+
if (focus.kind === "source" || focus.estate === sc.source) return src;
|
|
1672
|
+
// The estate receiving the flock, with the box it leaves, so the flight is in the shot.
|
|
1673
|
+
return unionBox(src, fin.get(focus.estate) || now.get(focus.estate));
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
function kioskTick(wall, dt) {
|
|
1677
|
+
// The loop: play, hold on the result, start again.
|
|
1678
|
+
if (S.t >= S.clock.length) {
|
|
1679
|
+
if (S.holdUntil === null) S.holdUntil = wall + LOOP.holdMs;
|
|
1680
|
+
else if (wall >= S.holdUntil) {
|
|
1681
|
+
S.holdUntil = null;
|
|
1682
|
+
seek(0);
|
|
1683
|
+
S.tipFrom = -1;
|
|
1684
|
+
play(true);
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
const beat = beatAt(S.story, S.scene, S.t);
|
|
1688
|
+
// On a phone the scene is framed whole, so no estate sits under the timeline (estates compact to discs there).
|
|
1689
|
+
if (typeof innerWidth === "number" && innerWidth < 640) beat.focus = { kind: "all" };
|
|
1690
|
+
// The director: what the beat is about, eased in, never against a person's hand.
|
|
1691
|
+
S.directing = wall >= S.userUntil;
|
|
1692
|
+
// While the director holds the camera the rail steps aside, so no box is ever under it;
|
|
1693
|
+
// it comes back the moment a person takes the camera.
|
|
1694
|
+
S.ui.classList.toggle("dots-directing", S.directing);
|
|
1695
|
+
if (S.directing) {
|
|
1696
|
+
S.dir = directorFocus(S.dir, beat.focus, wall);
|
|
1697
|
+
const want = frameBox(focusBox(S.dir.focus));
|
|
1698
|
+
const cur = S.getVB();
|
|
1699
|
+
const k = S.camAt === null || S.reduced ? 1 : 1 - Math.exp(-dt / 650);
|
|
1700
|
+
S.camAt = wall;
|
|
1701
|
+
const w = Math.exp(lerp(Math.log(cur[2]), Math.log(want[2]), k));
|
|
1702
|
+
const h = (w * cur[3]) / cur[2];
|
|
1703
|
+
const cx = lerp(cur[0] + cur[2] / 2, want[0] + want[2] / 2, k);
|
|
1704
|
+
const cy = lerp(cur[1] + cur[3] / 2, want[1] + want[3] / 2, k);
|
|
1705
|
+
if (Math.abs(w - cur[2]) / cur[2] > 0.0005 || Math.abs(cx - cur[0] - cur[2] / 2) > cur[2] * 0.0005 || Math.abs(cy - cur[1] - cur[3] / 2) > cur[3] * 0.0005) {
|
|
1706
|
+
S.setVB([cx - w / 2, cy - h / 2, w, h]);
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
// Tips for what has just played.
|
|
1710
|
+
const tips = S.playing || S.t >= S.clock.length ? tipsBetween(S.story, S.tipFrom, S.t) : [];
|
|
1711
|
+
S.tipFrom = S.t;
|
|
1712
|
+
if (wall - S.lastPost < 200 && !tips.length) return;
|
|
1713
|
+
S.lastPost = wall;
|
|
1714
|
+
const rects = {};
|
|
1715
|
+
for (const [e, b] of S.boxPx || []) rects[e] = boxRect(e, b);
|
|
1716
|
+
const counts = {};
|
|
1717
|
+
for (const [e, c] of countsNowMap()) counts[e] = c;
|
|
1718
|
+
const moves = S.story.moves;
|
|
1719
|
+
const done = moves.filter((f) => f.end <= S.t);
|
|
1720
|
+
toParent({
|
|
1721
|
+
type: "behold:dots-state",
|
|
1722
|
+
source: S.name,
|
|
1723
|
+
src: S.scene.source,
|
|
1724
|
+
t: S.t,
|
|
1725
|
+
length: S.clock.length,
|
|
1726
|
+
playing: S.playing,
|
|
1727
|
+
speed: S.speed,
|
|
1728
|
+
holdLeft: S.holdUntil === null ? null : Math.max(0, Math.ceil((S.holdUntil - wall) / 1000)),
|
|
1729
|
+
beat,
|
|
1730
|
+
counts,
|
|
1731
|
+
names: Object.fromEntries(S.scene.estates.map((e) => [e, shortOf(e, S.scene.source)])),
|
|
1732
|
+
recording: { kind: S.show.kind, resources: S.show.roster.length, recordedAt: S.show.recordedAt || null },
|
|
1733
|
+
colours: Object.fromEntries([...S.col.estate]),
|
|
1734
|
+
totals: { moves: moves.length, done: done.length, refused: done.filter((f) => !f.ok).length, estates: S.story.dests.length },
|
|
1735
|
+
log: done.slice(-14).reverse().map((f) => ({ address: f.address, to: shortOf(f.to, S.scene.source), ok: f.ok, refusal: f.refusal || null })),
|
|
1736
|
+
rects,
|
|
1737
|
+
tips,
|
|
1738
|
+
});
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
function countsNowMap() {
|
|
1742
|
+
return S.ownersNow ? [...S.ownersNow.values()].reduce((m, e) => m.set(e, (m.get(e) ?? 0) + 1), new Map()) : new Map();
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
// --- the live carve (#715) -------------------------------------------------------
|
|
1746
|
+
|
|
1747
|
+
/** Start following /api/terragucci/carve. `kiosk`: in the kiosk's frame, direct the camera while a migration runs. */
|
|
1748
|
+
function startLive(kiosk) {
|
|
1749
|
+
S.ui.classList.add("dots-live");
|
|
1750
|
+
S.tipFrom = S.t = Date.now() - S.show.t0;
|
|
1751
|
+
S.clock.length = Math.max(S.clock.length, S.t);
|
|
1752
|
+
S.kioskLive = !!kiosk;
|
|
1753
|
+
S.liveEndAt = null;
|
|
1754
|
+
// What the first read holds already ended happened before the page looked: no end tip for it.
|
|
1755
|
+
S.ended = new Set((S.answer.migrations || []).filter((m) => !m.running).map((m) => m.name));
|
|
1756
|
+
S.wasActive = false;
|
|
1757
|
+
S.wasRunning = new Map((S.answer.migrations || []).map((m) => [m.name, m.running]));
|
|
1758
|
+
S.userUntil = 0;
|
|
1759
|
+
S.insets = { top: 0, bottom: 0 };
|
|
1760
|
+
S.mode = "watch";
|
|
1761
|
+
S.dir = null;
|
|
1762
|
+
S.camAt = null;
|
|
1763
|
+
S.lastPost = 0;
|
|
1764
|
+
S.pendingTips = [];
|
|
1765
|
+
busy();
|
|
1766
|
+
const poll = setInterval(() => void readLive(), LIVE_POLL_MS);
|
|
1767
|
+
S.off.push(() => clearInterval(poll));
|
|
1768
|
+
if (S.kioskLive) {
|
|
1769
|
+
const onMsg = (e) => {
|
|
1770
|
+
if (!S || e.origin !== location.origin || !e.data) return;
|
|
1771
|
+
if (e.data.type === "behold:dots-slowed") {
|
|
1772
|
+
liveSlowed = Number(e.data.n) || 1;
|
|
1773
|
+
return;
|
|
1774
|
+
}
|
|
1775
|
+
if (e.data.type !== "behold:dots-mode") return;
|
|
1776
|
+
S.mode = e.data.mode === "inspect" ? "inspect" : "watch";
|
|
1777
|
+
if (e.data.insets) S.insets = { top: Number(e.data.insets.top) || 0, bottom: Number(e.data.insets.bottom) || 0 };
|
|
1778
|
+
S.dirty = true;
|
|
1779
|
+
};
|
|
1780
|
+
window.addEventListener("message", onMsg);
|
|
1781
|
+
S.off.push(() => window.removeEventListener("message", onMsg));
|
|
1782
|
+
// The director never moves while a person has the graph in hand.
|
|
1783
|
+
const touched = () => {
|
|
1784
|
+
if (S) S.userUntil = performance.now() + 8_000;
|
|
1785
|
+
};
|
|
1786
|
+
for (const ev of ["wheel", "mousedown", "touchstart"]) {
|
|
1787
|
+
S.host.addEventListener(ev, touched, { passive: true });
|
|
1788
|
+
S.off.push(() => S && S.host.removeEventListener(ev, touched));
|
|
1789
|
+
}
|
|
1790
|
+
toParent({ type: "behold:dots-ready", source: S.name, live: true });
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
/** Until when something is in the air or about to be: the loop draws every frame until then. */
|
|
1795
|
+
function busy() {
|
|
1796
|
+
let until = 0;
|
|
1797
|
+
for (const f of S.scene.flights) until = Math.max(until, f.end + 700);
|
|
1798
|
+
for (const e of S.show.events) if (e.kind === "adopt" && !e.before) until = Math.max(until, e.t - S.show.t0 + 400);
|
|
1799
|
+
S.busyUntil = until;
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
let liveReading = false;
|
|
1803
|
+
async function readLive() {
|
|
1804
|
+
if (!S || !S.live || liveReading) return;
|
|
1805
|
+
liveReading = true;
|
|
1806
|
+
try {
|
|
1807
|
+
const res = await fetch("/api/terragucci/carve", { headers: { accept: "application/json" } });
|
|
1808
|
+
const a = await res.json();
|
|
1809
|
+
if (S && S.live && a && a.live === true) takeLive(a);
|
|
1810
|
+
} catch {
|
|
1811
|
+
/* the next poll asks again */
|
|
1812
|
+
} finally {
|
|
1813
|
+
liveReading = false;
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
/** A new read of the carve: stamp what arrived, and rebuild the scene when the show changed. */
|
|
1818
|
+
function takeLive(answer) {
|
|
1819
|
+
const now = Date.now();
|
|
1820
|
+
const seen = stampArrivals(answer, liveMemo ? liveMemo.seen : new Map(), now, { stepMs: stepMsFor(liveSlowed) });
|
|
1821
|
+
const show = liveShow(answer, seen, now, { t0: S.show.t0 });
|
|
1822
|
+
liveMemo = { seen, answer, t0: show.t0 };
|
|
1823
|
+
S.answer = answer;
|
|
1824
|
+
if (!show.roster.length) return;
|
|
1825
|
+
if (showKey(show) === showKey(S.show) && show.t0 === S.show.t0) {
|
|
1826
|
+
S.show.t1 = now;
|
|
1827
|
+
return;
|
|
1828
|
+
}
|
|
1829
|
+
const scene = buildScene(show, { sourceAspect: S.mbox.w / Math.max(1, S.mbox.h), ...labelRoom(S.host) });
|
|
1830
|
+
const n = show.roster.length;
|
|
1831
|
+
const src = scene.sourceBox;
|
|
1832
|
+
S.show = show;
|
|
1833
|
+
S.scene = scene;
|
|
1834
|
+
S.clock = replayClock(show);
|
|
1835
|
+
S.t = now - show.t0;
|
|
1836
|
+
S.clock.length = Math.max(S.clock.length, S.t);
|
|
1837
|
+
S.origin = { x: S.mbox.x + (S.mbox.w - src.w * S.pitch) / 2, y: S.mbox.y + (S.mbox.h - src.h * S.pitch) / 2 };
|
|
1838
|
+
S.firsts = firstLandings(scene);
|
|
1839
|
+
S.addrs = show.roster.map((r) => r.address);
|
|
1840
|
+
S.idx = new Map(show.roster.map((r, i) => [r.address, i]));
|
|
1841
|
+
S.px = new Float32Array(n);
|
|
1842
|
+
S.py = new Float32Array(n);
|
|
1843
|
+
S.pvis = new Uint8Array(n);
|
|
1844
|
+
S.col = readColours();
|
|
1845
|
+
busy();
|
|
1846
|
+
S.dirty = true;
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
/** Each frame of a live carve: whether the director has the camera, and what the kiosk is told. */
|
|
1850
|
+
function liveTick(wall, dt) {
|
|
1851
|
+
const answer = S.answer || {};
|
|
1852
|
+
const m = currentMigration(answer);
|
|
1853
|
+
const now = Date.now();
|
|
1854
|
+
// A migration that stopped running ends its beat: Watch holds on the result, then hands the camera back.
|
|
1855
|
+
for (const x of answer.migrations || []) {
|
|
1856
|
+
const was = S.wasRunning.get(x.name);
|
|
1857
|
+
if (was && !x.running && !S.ended.has(x.name)) {
|
|
1858
|
+
S.ended.add(x.name);
|
|
1859
|
+
S.liveEndAt = now;
|
|
1860
|
+
S.pendingTips.push(liveEndTip(x, S.t));
|
|
1861
|
+
}
|
|
1862
|
+
S.wasRunning.set(x.name, x.running);
|
|
1863
|
+
}
|
|
1864
|
+
const running = !!(answer.running || (m && m.running));
|
|
1865
|
+
// Lines still playing count as running: an adoption on floci writes its 82 lines in under a second, and the record
|
|
1866
|
+
// says applied by the read that brings them, so the beat is the 50 ms pacing of those lines (web/dots-live.js).
|
|
1867
|
+
const playing = S.t < (S.busyUntil ?? 0);
|
|
1868
|
+
const active = running || playing;
|
|
1869
|
+
// When the last of it has played, Watch holds on the result, then hands the camera back.
|
|
1870
|
+
if (S.wasActive && !active) {
|
|
1871
|
+
S.liveEndAt = now;
|
|
1872
|
+
// A migration that was done by the read that brought its lines ends here, once they have played.
|
|
1873
|
+
for (const x of answer.migrations || []) {
|
|
1874
|
+
if (x.running || S.ended.has(x.name) || !x.lines.length) continue;
|
|
1875
|
+
S.ended.add(x.name);
|
|
1876
|
+
S.pendingTips.push(liveEndTip(x, S.t));
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
S.wasActive = active;
|
|
1880
|
+
// The tour (behold.tour/v1), in Watch: the chapter the cycle's events have reached. While it runs, the carve keeps the camera.
|
|
1881
|
+
const tn = tourNow(answer);
|
|
1882
|
+
S.tourOn = !!(S.kioskLive && tn && tn.at && S.mode === "watch");
|
|
1883
|
+
S.tn = tn;
|
|
1884
|
+
const carving = S.kioskLive && (S.tourOn || active || (S.liveEndAt !== null && now - S.liveEndAt < LIVE_HOLD_MS));
|
|
1885
|
+
if (carving !== S.carving) {
|
|
1886
|
+
S.carving = carving;
|
|
1887
|
+
S.ui.classList.toggle("dots-carving", carving);
|
|
1888
|
+
S.dir = null;
|
|
1889
|
+
S.camAt = null;
|
|
1890
|
+
if (S.onCarving) S.onCarving(carving);
|
|
1891
|
+
}
|
|
1892
|
+
if (!S.kioskLive) return;
|
|
1893
|
+
// The counts the page has played so far: the caption, the next line and the tally all follow the paced lines,
|
|
1894
|
+
// and show the record's own once every line has played.
|
|
1895
|
+
const shown = m ? playedCounts(m, liveMemo ? liveMemo.seen : new Map(), S.show.t0 + S.t) : null;
|
|
1896
|
+
const beat = liveBeat(S.scene, S.t, { demo: answer.demo, migration: m, shown });
|
|
1897
|
+
// In Inspect too: while a migration runs its boxes and dots are the story, so the director frames them there as well,
|
|
1898
|
+
// unless a person has the graph in hand.
|
|
1899
|
+
S.directing = carving && performance.now() >= S.userUntil;
|
|
1900
|
+
S.ui.classList.toggle("dots-directing", S.directing);
|
|
1901
|
+
if (S.directing) {
|
|
1902
|
+
// On a tour, the chapter says what to frame, except while a migration's writes are landing: the flock leads then.
|
|
1903
|
+
const focus = S.tourOn && !active ? tourFocus(tn, S.scene.estates, answer.adopted) : beat.focus;
|
|
1904
|
+
S.dir = directorFocus(S.dir, focus, wall);
|
|
1905
|
+
const want = frameBox(focusBox(S.dir.focus));
|
|
1906
|
+
const cur = S.getVB();
|
|
1907
|
+
const k = S.camAt === null || S.reduced ? 1 : 1 - Math.exp(-dt / 650);
|
|
1908
|
+
S.camAt = wall;
|
|
1909
|
+
const w = Math.exp(lerp(Math.log(cur[2]), Math.log(want[2]), k));
|
|
1910
|
+
const h = (w * cur[3]) / cur[2];
|
|
1911
|
+
const cx = lerp(cur[0] + cur[2] / 2, want[0] + want[2] / 2, k);
|
|
1912
|
+
const cy = lerp(cur[1] + cur[3] / 2, want[1] + want[3] / 2, k);
|
|
1913
|
+
if (Math.abs(w - cur[2]) / cur[2] > 0.0005 || Math.abs(cx - cur[0] - cur[2] / 2) > cur[2] * 0.0005 || Math.abs(cy - cur[1] - cur[3] / 2) > cur[3] * 0.0005) {
|
|
1914
|
+
S.setVB([cx - w / 2, cy - h / 2, w, h]);
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
// A tour's pulses and travelling values animate: draw every frame while one is on.
|
|
1918
|
+
if (S.tourOn && S.fx && (S.fx.pulses.length || S.fx.travel)) S.dirty = true;
|
|
1919
|
+
// A chapter or visual that has just fired is drawn now, not at the next camera move.
|
|
1920
|
+
const tk = S.tourOn ? fxKey(tn) : "";
|
|
1921
|
+
if (tk !== S.tourKey) {
|
|
1922
|
+
S.tourKey = tk;
|
|
1923
|
+
S.dirty = true;
|
|
1924
|
+
}
|
|
1925
|
+
// Tips for what has just been written: never for what the first read already held.
|
|
1926
|
+
const tips = [...liveTipsBetween(S.scene, S.tipFrom, S.t), ...S.pendingTips.splice(0)];
|
|
1927
|
+
S.tipFrom = S.t;
|
|
1928
|
+
if (wall - S.lastPost < 250 && !tips.length) return;
|
|
1929
|
+
S.lastPost = wall;
|
|
1930
|
+
const rects = {};
|
|
1931
|
+
for (const [e, b] of S.boxPx || []) rects[e] = boxRect(e, b);
|
|
1932
|
+
const counts = {};
|
|
1933
|
+
for (const [e, c] of countsNowMap()) counts[e] = c;
|
|
1934
|
+
const story = carveStory(S.scene);
|
|
1935
|
+
const done = story.moves.filter((f) => f.end <= S.t);
|
|
1936
|
+
const mc = shown ? shown.counts : null;
|
|
1937
|
+
toParent({
|
|
1938
|
+
type: "behold:dots-state",
|
|
1939
|
+
live: true,
|
|
1940
|
+
carving,
|
|
1941
|
+
source: S.name,
|
|
1942
|
+
src: S.scene.source,
|
|
1943
|
+
t: S.t,
|
|
1944
|
+
length: S.clock.length,
|
|
1945
|
+
playing: true,
|
|
1946
|
+
speed: 1,
|
|
1947
|
+
holdLeft: null,
|
|
1948
|
+
beat,
|
|
1949
|
+
counts,
|
|
1950
|
+
names: Object.fromEntries(S.scene.estates.map((e) => [e, shortOf(e, S.scene.source)])),
|
|
1951
|
+
recording: { kind: "live", resources: S.show.roster.length, recordedAt: null, from: answer.source || null, migration: m ? m.name : null },
|
|
1952
|
+
colours: Object.fromEntries([...S.col.estate]),
|
|
1953
|
+
totals: mc
|
|
1954
|
+
? { moves: mc.moved + mc.stamped + mc.refused + mc.skipped + (mc.left ?? 0), done: mc.moved + mc.stamped + mc.refused + mc.skipped, refused: mc.refused, estates: story.dests.length, moved: mc.moved, stamped: mc.stamped, skipped: mc.skipped }
|
|
1955
|
+
: { moves: story.moves.length, done: done.length, refused: done.filter((f) => !f.ok).length, estates: story.dests.length },
|
|
1956
|
+
log: done.slice(-14).reverse().map((f) => ({ address: f.address, to: shortOf(f.to, S.scene.source), ok: f.ok, refusal: f.refusal || null })),
|
|
1957
|
+
rects,
|
|
1958
|
+
tips,
|
|
1959
|
+
// The live badge's lag: the newest line's time, and whether a migration runs (web/tour-player.js lagText).
|
|
1960
|
+
lastWriteAt: newestLineAt(answer),
|
|
1961
|
+
running,
|
|
1962
|
+
slowed: liveSlowed,
|
|
1963
|
+
...(S.tourOn ? { tour: tourMessage(tn, { counts: countsNowMap(), names: S.scene.estates, adopted: answer.adopted, source: S.scene.source, short: tourShort }) } : {}),
|
|
1964
|
+
});
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
/** A box's name in the tour's notes: as the box shows it, less the prefix every box's name shares (a cycle's `tl56-`). */
|
|
1968
|
+
function tourShort(e) {
|
|
1969
|
+
const shown = S.scene.estates.map((x) => shortName(x));
|
|
1970
|
+
const nm = shortName(e);
|
|
1971
|
+
let pre = shown.length > 1 ? shown[0] : "";
|
|
1972
|
+
for (const x of shown) while (pre && !x.startsWith(pre)) pre = pre.slice(0, -1);
|
|
1973
|
+
const cut = pre.lastIndexOf("-");
|
|
1974
|
+
return cut > 0 && nm.length > cut + 1 ? nm.slice(cut + 1) : nm;
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
/** The newest progress line's time across the cycle's migrations, in ms; null with none. */
|
|
1978
|
+
function newestLineAt(answer) {
|
|
1979
|
+
let at = null;
|
|
1980
|
+
for (const m of (answer && answer.migrations) || [])
|
|
1981
|
+
for (const l of m.lines) {
|
|
1982
|
+
const t = Date.parse(l.at);
|
|
1983
|
+
if (!Number.isNaN(t) && (at === null || t > at)) at = t;
|
|
1984
|
+
}
|
|
1985
|
+
return at;
|
|
1986
|
+
}
|
|
1987
|
+
|
|
1988
|
+
/** The tour's visuals for this frame, rebuilt only when the chapter, its visuals or the dots' owners change. */
|
|
1989
|
+
function currentFx(owners) {
|
|
1990
|
+
if (!S.tourOn || !S.tn) return null;
|
|
1991
|
+
const key = fxKey(S.tn, `${S.show.events.length}|${S.scene.estates.join(",")}|${[...countsNowOf(owners)].join(",")}`);
|
|
1992
|
+
if (S.fxKey !== key) {
|
|
1993
|
+
S.fx = tourFx(S.tn, { ids: S.addrs, owners, names: S.scene.estates, adopted: S.answer && S.answer.adopted, source: S.scene.source });
|
|
1994
|
+
S.fxKey = key;
|
|
1995
|
+
}
|
|
1996
|
+
return S.fx;
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1999
|
+
function countsNowOf(owners) {
|
|
2000
|
+
const m = new Map();
|
|
2001
|
+
for (const e of owners.values()) m.set(e, (m.get(e) ?? 0) + 1);
|
|
2002
|
+
return m;
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
/** A small chip in the canvas: a rounded box with words, in a colour; returns its width. */
|
|
2006
|
+
function chip(ctx, x, y, words, colour, { align = "left", fg = null } = {}) {
|
|
2007
|
+
ctx.font = `600 11px ${S.font}`;
|
|
2008
|
+
const w = ctx.measureText(words).width + 14;
|
|
2009
|
+
const h = 18;
|
|
2010
|
+
const x0 = align === "right" ? x - w : x;
|
|
2011
|
+
ctx.globalAlpha = 0.92;
|
|
2012
|
+
roundRect(ctx, x0, y, w, h, 5);
|
|
2013
|
+
ctx.fillStyle = S.col.bg;
|
|
2014
|
+
ctx.fill();
|
|
2015
|
+
ctx.strokeStyle = colour;
|
|
2016
|
+
ctx.lineWidth = 1.2;
|
|
2017
|
+
ctx.stroke();
|
|
2018
|
+
ctx.globalAlpha = 1;
|
|
2019
|
+
ctx.fillStyle = fg || colour;
|
|
2020
|
+
ctx.textBaseline = "middle";
|
|
2021
|
+
ctx.textAlign = "left";
|
|
2022
|
+
ctx.fillText(words, x0 + 7, y + h / 2 + 0.5);
|
|
2023
|
+
return w;
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
/** The tour's own marks over the boxes: a wave's pulse, the value along the read threads, the state file, the checks. */
|
|
2027
|
+
function drawTour(ctx, fx, boxes, boxPx, centre, sx, sy, dotLayerA, td, discR) {
|
|
2028
|
+
if (!fx || dotLayerA < 0.3) return;
|
|
2029
|
+
const col = S.col;
|
|
2030
|
+
const now = Date.now();
|
|
2031
|
+
// A box drawn as its disc (far out, or a phone) takes its marks beside the disc.
|
|
2032
|
+
const disc = (e) => (td.get(e) ?? 0) >= 0.5;
|
|
2033
|
+
const discSpot = (e) => {
|
|
2034
|
+
const c = centre(e);
|
|
2035
|
+
return { x: sx(c.x) + discR(e) + 6, y: sy(c.y) - 9 };
|
|
2036
|
+
};
|
|
2037
|
+
// A wave's pulse: the box's outline breathes in the in-progress colour while its plan runs.
|
|
2038
|
+
for (const e of fx.pulses) {
|
|
2039
|
+
const b = boxes.get(e);
|
|
2040
|
+
if (!b) continue;
|
|
2041
|
+
const bp = boxPx(b);
|
|
2042
|
+
const u = S.reduced ? 0.6 : 0.5 + 0.5 * Math.sin(now / 260);
|
|
2043
|
+
if (disc(e)) {
|
|
2044
|
+
const c = centre(e);
|
|
2045
|
+
ctx.globalAlpha = 0.35 + 0.55 * u;
|
|
2046
|
+
ctx.strokeStyle = col.pending;
|
|
2047
|
+
ctx.lineWidth = 2 + 2 * u;
|
|
2048
|
+
ctx.beginPath();
|
|
2049
|
+
ctx.arc(sx(c.x), sy(c.y), discR(e) + 5, 0, Math.PI * 2);
|
|
2050
|
+
ctx.stroke();
|
|
2051
|
+
continue;
|
|
2052
|
+
}
|
|
2053
|
+
ctx.globalAlpha = 0.35 + 0.55 * u;
|
|
2054
|
+
ctx.strokeStyle = col.pending;
|
|
2055
|
+
ctx.lineWidth = 2 + 2.5 * u;
|
|
2056
|
+
roundRect(ctx, bp.x - 3, bp.y - 3, bp.w + 6, bp.h + 6, 14);
|
|
2057
|
+
ctx.stroke();
|
|
2058
|
+
}
|
|
2059
|
+
// The read threads, reader to source, and the output's planned value travelling along them to each reader.
|
|
2060
|
+
if (fx.travel) {
|
|
2061
|
+
for (const th of fx.travel.threads) {
|
|
2062
|
+
if (!boxes.has(th.from) || !boxes.has(th.to)) continue;
|
|
2063
|
+
const a = centre(th.from);
|
|
2064
|
+
const b = centre(th.to);
|
|
2065
|
+
ctx.globalAlpha = 0.6;
|
|
2066
|
+
ctx.strokeStyle = col.runtime;
|
|
2067
|
+
ctx.lineWidth = 1.4;
|
|
2068
|
+
ctx.setLineDash([6, 6]);
|
|
2069
|
+
ctx.lineDashOffset = S.reduced ? 0 : -((performance.now() / 40) % 12);
|
|
2070
|
+
ctx.beginPath();
|
|
2071
|
+
ctx.moveTo(sx(a.x), sy(a.y));
|
|
2072
|
+
ctx.lineTo(sx(b.x), sy(b.y));
|
|
2073
|
+
ctx.stroke();
|
|
2074
|
+
ctx.setLineDash([]);
|
|
2075
|
+
if (fx.travel.value === null || fx.travel.value === undefined) continue;
|
|
2076
|
+
// From when the wave's job ended (its event), 1.8 s along the thread; then it rests at the reader.
|
|
2077
|
+
const u = S.reduced || fx.travel.at === null ? 1 : Math.max(0, Math.min(1, (now - fx.travel.at) / 1800));
|
|
2078
|
+
const e = easeInOut(u);
|
|
2079
|
+
const x = sx(lerp(a.x, b.x, e));
|
|
2080
|
+
const y = sy(lerp(a.y, b.y, e));
|
|
2081
|
+
ctx.globalAlpha = 1;
|
|
2082
|
+
ctx.fillStyle = col.runtime;
|
|
2083
|
+
ctx.beginPath();
|
|
2084
|
+
ctx.arc(x, y, 4.5, 0, Math.PI * 2);
|
|
2085
|
+
ctx.fill();
|
|
2086
|
+
chip(ctx, x + 8, y - 9, String(fx.travel.value), col.runtime, { fg: col.fg });
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
// The state file the adoption bound: its version id, and "unchanged" once the stamps are done.
|
|
2090
|
+
if (fx.state) {
|
|
2091
|
+
const b = boxes.get(fx.state.box);
|
|
2092
|
+
if (b) {
|
|
2093
|
+
const bp = boxPx(b);
|
|
2094
|
+
const words = `state file · ${String(fx.state.version).slice(0, 8)}${fx.state.unchanged ? " · unchanged" : ""}`;
|
|
2095
|
+
if (disc(fx.state.box)) {
|
|
2096
|
+
const p = discSpot(fx.state.box);
|
|
2097
|
+
chip(ctx, p.x, p.y, words, fx.state.unchanged ? col.managed : col.muted, { fg: col.fg });
|
|
2098
|
+
} else {
|
|
2099
|
+
// Above the box's right end when there is room beside its label; else under the box, so it never covers the label.
|
|
2100
|
+
ctx.font = `600 11px ${S.font}`;
|
|
2101
|
+
const room = ctx.measureText(words).width + 14 <= bp.w * 0.55;
|
|
2102
|
+
if (room) chip(ctx, bp.x + bp.w, bp.y - 24, words, fx.state.unchanged ? col.managed : col.muted, { align: "right", fg: col.fg });
|
|
2103
|
+
else chip(ctx, bp.x, bp.y + bp.h + 6, words, fx.state.unchanged ? col.managed : col.muted, { fg: col.fg });
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
// The post-write check: each root that plans no change after the write.
|
|
2108
|
+
for (const [e, ok] of fx.checks) {
|
|
2109
|
+
const b = boxes.get(e);
|
|
2110
|
+
if (!b) continue;
|
|
2111
|
+
const bp = boxPx(b);
|
|
2112
|
+
if (disc(e)) {
|
|
2113
|
+
const p = discSpot(e);
|
|
2114
|
+
chip(ctx, p.x, p.y + 20, ok ? "✓" : "✗", ok ? col.managed : col.degraded, { fg: col.fg });
|
|
2115
|
+
continue;
|
|
2116
|
+
}
|
|
2117
|
+
// Inside the box's foot, so it never sits on the box below; a box too narrow for the words takes the mark alone.
|
|
2118
|
+
ctx.font = `600 11px ${S.font}`;
|
|
2119
|
+
const words = ok ? "✓ plans no change" : "plans a change";
|
|
2120
|
+
const fits = ctx.measureText(words).width + 26 <= bp.w;
|
|
2121
|
+
chip(ctx, bp.x + 6, bp.y + bp.h - 24, fits ? words : ok ? "✓" : "✗", ok ? col.managed : col.degraded, { fg: col.fg });
|
|
2122
|
+
}
|
|
2123
|
+
ctx.globalAlpha = 1;
|
|
2124
|
+
}
|