/ interactive_tutorials / duffing_islands
Sign in
interactive_tutorials / duffing_islands
duffing_islands / app / main.js
535 lines · 19.8 KBCodeBlameHistory
a1f5e15Islands in a sea of chaos: Duffing phase space with sonificationJeremy Magland 1// Islands in a sea of chaos: the Poincaré section of the driven Duffing
2// oscillator (js/duffing.js), filled in live, with clickable orbits and a
3// sonification (js/voices.js).
4//
5// - Seed orbits, started on the axes of the view at drive phase 0, fill the
6// section in the background, a few thousand points per frame.
7// - A click adds an orbit from that point (drive phase 0), up to MAX_CLICKED;
8// clicking an orbit's start ring removes it. Its dots fill in fast and
9// larger, and a playhead walks it one drive period per beat: a live trail
10// traces the motion through the period, and at each return to the section
11// a note is played, its pitch set by x (left to right over three octaves
12// of the chosen scale). All the clicked orbits play together, on the same
13// beat, each in its own voice (harp, marimba, ...), listed in the panel.
14// - Rhythm 'natural' instead plays the motion in slowed time (still one
15// drive period per beat) and plucks wherever x' crosses 0 upward (the
16// left turning point of each swing), at that x: each orbit makes its own
17// rhythm, regular on an island, irregular in the sea.
18// - Changing a parameter or preset restarts everything from the same starts,
19// the clicked orbits included.
21import { createStepper, physicsOf, X_MAX } from './js/duffing.js';
22import { createSynth, DEFAULT_KEY, DEFAULT_SCALE, KEYS, pitchOf, SCALES, SUSTAIN, VOICES } from './js/voices.js';
23import { PRESETS } from './js/presets.js';
25const SEEDS_X = 24; // seed orbits along x' = 0
26const SEEDS_V = 8; // and along x = 0
27const SEED_POINTS = 1500;
28const CLICK_POINTS = 4000;
29const CLICK_PER_FRAME = 40; // so a clicked orbit visibly grows
30const MAX_CLICKED = 8;
31const HIT_RADIUS = 9; // px: a click this close to a start ring removes it
32const FRAME_BUDGET = 7; // ms of integration per frame
33const TAIL = 2; // trail length, in drive periods
34const LOOKAHEAD = 0.12; // s of notes scheduled ahead
35const RING = 0.35; // s a pluck's ring takes to fade (at most 3 beats)
36const MARGIN = { left: 56, right: 12, top: 12, bottom: 34 };
38const SLIDERS = [
39 { name: 'freq', label: 'f<sub>0</sub>', unit: 'Hz', min: -1000, max: 1000, step: 5, fmt: (v) => `${v < 0 ? '−' : ''}${Math.abs(v)} Hz` },
40 { name: 'alpha', label: 'α', min: -5, max: 10, step: 0.1, scale: 1e7, fmt: (v) => `${v.toFixed(1)}·10⁷` },
41 { name: 'delta', label: 'δ', min: 0, max: 50, step: 0.5, fmt: (v) => `${v.toFixed(1)} s⁻¹` },
42 { name: 'F', label: 'F', min: 0, max: 30, step: 0.1, scale: 1e4, fmt: (v) => `${v.toFixed(1)}·10⁴` },
43 { name: 'drive', label: 'Ω / 2π', min: 20, max: 2000, step: 5, fmt: (v) => `${v} Hz` },
44 { name: 'view', label: 'View ±x', min: 0.1, max: X_MAX, step: 0.05, fmt: (v) => v.toFixed(2) },
45];
47const $ = (id) => document.getElementById(id);
48const canvas = $('plot');
49const ctx = canvas.getContext('2d');
50// the section points, accumulated: seed orbits, and clicked ones on top
51const dots = document.createElement('canvas');
52const top = document.createElement('canvas');
53const layers = [dots, top];
55let params = { ...PRESETS[0].params };
56let stepper;
57let view; // { xr, vr }: half-ranges of x and x'
58let orbits = []; // seed and clicked orbits
59let clicked = []; // clicked orbits: { x, v, hue, voice, ph }, ph its playhead
60let nextBeat = 0; // clock time of the next beat not yet scheduled
61let synth = null;
62let key = DEFAULT_KEY; // 0 = C, ..., 11 = B
63let soundOn = false;
64let hueCounter = 0;
65let theme;
67// ---- physics ---------------------------------------------------------------
69// x' half-range: the speed at the bottom of the well of a free orbit that
70// just reaches x = +/-xr, so the view frames the motions of that size
71function speedRange(p, xr) {
72 const V = (x) => 0.5 * p.omega2 * x * x + 0.25 * p.alpha * x ** 4;
73 const vMin = p.omega2 < 0 && p.alpha > 0 ? -(p.omega2 ** 2) / (4 * p.alpha) : 0;
74 const v = Math.sqrt(Math.max(0, 2 * (V(xr) - vMin)));
75 return Math.max(v, 0.3 * xr * p.Omega, 1);
78// skip: drive periods to run before recording (a seed's transient)
79function newOrbit(x, v, cap, hue, isClicked, skip = 0) {
80 return { x0: x, v0: v, s: { x, v, escaped: false }, pts: new Float32Array(2 * cap), n: 0, cap, hue, clicked: isClicked, skip };
83function reset() {
84 const p = physicsOf(params);
85 stepper = createStepper(p);
86 view = { xr: params.view, vr: speedRange(p, params.view) };
87 // damped: the seeds settle onto the attractors before they are drawn,
88 // for about 5 / delta (clicked orbits show the way in)
89 const skip = p.delta > 0 ? Math.min(5000, Math.ceil((5 / p.delta) / stepper.T)) : 0;
90 orbits = [];
91 for (let i = 0; i < SEEDS_X; i++) {
92 const x = view.xr * (-1 + (2 * (i + 0.5)) / SEEDS_X);
93 orbits.push(newOrbit(x, 0, SEED_POINTS, (i * 0.618) % 1, false, skip));
94 }
95 for (let i = 0; i < SEEDS_V; i++) {
96 const v = 0.9 * view.vr * (-1 + (2 * (i + 0.5)) / SEEDS_V);
97 orbits.push(newOrbit(0, v, SEED_POINTS, ((i + SEEDS_X) * 0.618) % 1, false, skip));
98 }
99 for (const c of clicked) {
100 orbits.unshift(newOrbit(c.x, c.v, CLICK_POINTS, c.hue, true));
101 c.ph = newPlayhead(c.x, c.v);
102 }
103 clearDots();
106// ---- playhead and sound -----------------------------------------------------
108const clock = () => (synth ? synth.ctx.currentTime : performance.now() / 1000);
109const noteDt = () => 1 / Number($('tempo').value);
110const ringTime = () => Math.min(RING, 3 * noteDt());
112// a playhead: the orbit's state, and its scheduled beats ({ time, path,
113// last, notes }) still needed for the trail; notes: [{ t, x, v }]
114const newPlayhead = (x, v) => ({ s: { x, v, escaped: false }, queue: [] });
116// The upward crossings of x' = 0 along a drive period's path (N + 1 points,
117// evenly spaced in time over dt from time t0), interpolated linearly.
118function upwardCrossings(path, t0, dt) {
119 const N = path.length / 2 - 1;
120 const notes = [];
121 for (let j = 1; j <= N; j++) {
122 const v0 = path[2 * j - 1];
123 const v1 = path[2 * j + 1];
124 if (v0 < 0 && v1 >= 0) {
125 const f = v0 / (v0 - v1);
126 notes.push({ t: t0 + ((j - 1 + f) / N) * dt, x: path[2 * j - 2] + f * (path[2 * j] - path[2 * j - 2]), v: 0 });
127 }
128 }
129 return notes;
132// schedule the beats due within LOOKAHEAD: on each, every playhead advances
133// one drive period (keeping the path for the trail) and plucks its notes
134// (steady: one at the section point; natural: at each upward x' = 0),
135// softer the more orbits there are
136function schedule() {
137 const now = clock();
138 // fallen behind (a stalled frame, a hidden tab), or nothing playing: carry
139 // on from now rather than play the missed notes in a burst
140 if (nextBeat < now) nextBeat = now + 0.02;
141 if (!clicked.length) return;
142 const scale = SCALES[$('scale').selectedIndex];
143 const gain = 1 / Math.sqrt(clicked.length);
144 const natural = $('rhythm').value === 'natural';
145 const dt = noteDt();
146 while (nextBeat < now + LOOKAHEAD) {
147 for (const { ph, voice } of clicked) {
148 if (ph.s.escaped) continue;
149 const { x, v } = ph.s;
150 const path = new Float32Array(2 * (stepper.N + 1));
151 stepper.step(ph.s, path);
152 // (natural: none in the period it escapes, whose path ends at rest)
153 const notes = !natural ? [{ t: nextBeat, x, v }] : ph.s.escaped ? [] : upwardCrossings(path, nextBeat, dt);
154 ph.queue.push({ time: nextBeat, path, last: ph.s.escaped, notes });
155 if (soundOn && synth) {
156 for (const n of notes) {
157 const u = (n.x / view.xr + 1) / 2;
158 synth.play(voice, pitchOf(u, scale, key), n.t, Math.max(-1, Math.min(1, 1.4 * u - 0.7)), gain);
159 }
160 }
161 }
162 nextBeat += dt;
163 }
164 const keep = Math.max((TAIL + 1) * dt, ringTime() + dt);
165 for (const { ph } of clicked) {
166 while (ph.queue.length > 1 && ph.queue[1].time < now - keep) ph.queue.shift();
167 }
170function setSound(on) {
171 soundOn = on;
172 const btn = $('sound');
173 btn.setAttribute('aria-pressed', on);
174 btn.textContent = on ? '♪ Sound on' : '♪ Sound off';
175 if (on && !synth) {
176 // the clock switches to the audio context's
177 synth = createSynth();
178 synth.setSustain(Number($('sustain').value));
179 for (const { ph } of clicked) ph.queue = [];
180 nextBeat = 0;
181 }
182 if (synth) {
183 synth.ctx.resume();
184 synth.setVolume(on ? Number($('volume').value) : 0);
185 }
188// ---- drawing -----------------------------------------------------------------
190function readTheme() {
191 const cs = getComputedStyle(document.documentElement);
192 theme = {
193 bg: cs.getPropertyValue('--bg').trim(),
194 fg: cs.getPropertyValue('--fg').trim(),
195 muted: cs.getPropertyValue('--muted').trim(),
196 line: cs.getPropertyValue('--line').trim(),
197 accent: cs.getPropertyValue('--accent').trim(),
198 light: cs.getPropertyValue('--dot-light').trim(),
199 };
202const orbitColor = (hue, clickedOrbit, alpha = 1) =>
203 `hsla(${Math.round(hue * 360)}, ${clickedOrbit ? 85 : 50}%, ${theme.light}, ${alpha})`;
205function plotRect() {
206 const dpr = devicePixelRatio || 1;
207 const w = canvas.width / dpr;
208 const h = canvas.height / dpr;
209 return { x: MARGIN.left, y: MARGIN.top, w: Math.max(10, w - MARGIN.left - MARGIN.right), h: Math.max(10, h - MARGIN.top - MARGIN.bottom) };
212const toPx = (r, x, v) => [r.x + ((x / view.xr + 1) / 2) * r.w, r.y + ((1 - v / view.vr) / 2) * r.h];
214function drawPoints(o, from) {
215 const r = plotRect();
216 const dctx = (o.clicked ? top : dots).getContext('2d');
217 dctx.fillStyle = orbitColor(o.hue, o.clicked, o.clicked ? 0.95 : 0.7);
218 const size = o.clicked ? 2.2 : 1.4;
219 for (let i = from; i < o.n; i++) {
220 const [px, py] = toPx(r, o.pts[2 * i], o.pts[2 * i + 1]);
221 dctx.fillRect(px - size / 2, py - size / 2, size, size);
222 }
225function clearDots(which = layers) {
226 const dpr = devicePixelRatio || 1;
227 for (const layer of which) {
228 const c = layer.getContext('2d');
229 c.setTransform(1, 0, 0, 1, 0, 0);
230 c.clearRect(0, 0, layer.width, layer.height);
231 c.setTransform(dpr, 0, 0, dpr, 0, 0);
232 }
235function resize() {
236 const dpr = devicePixelRatio || 1;
237 const { width, height } = canvas.getBoundingClientRect();
238 canvas.width = dots.width = top.width = Math.round(width * dpr);
239 canvas.height = dots.height = top.height = Math.round(height * dpr);
240 clearDots();
241 for (const o of orbits) drawPoints(o, 0);
244// advance the orbits within the frame's budget, drawing their new points
245function fill() {
246 const deadline = performance.now() + FRAME_BUDGET;
247 const advance = (o, count) => {
248 const from = o.n;
249 for (let k = 0; k < count && o.n < o.cap && !o.s.escaped; k++) {
250 stepper.step(o.s);
251 if (o.s.escaped) break;
252 if (o.skip > 0) { o.skip--; continue; }
253 o.pts[2 * o.n] = o.s.x;
254 o.pts[2 * o.n + 1] = o.s.v;
255 o.n++;
256 }
257 drawPoints(o, from);
258 };
259 for (const o of orbits) if (o.clicked) advance(o, CLICK_PER_FRAME);
260 let busy = true;
261 while (busy && performance.now() < deadline) {
262 busy = false;
263 for (const o of orbits) {
264 if (o.clicked || o.n >= o.cap || o.s.escaped) continue;
265 advance(o, 10);
266 busy = true;
267 }
268 }
271function drawAxes(r) {
272 ctx.strokeStyle = theme.line;
273 ctx.lineWidth = 1;
274 ctx.strokeRect(r.x + 0.5, r.y + 0.5, r.w - 1, r.h - 1);
275 ctx.beginPath();
276 ctx.moveTo(r.x + r.w / 2, r.y);
277 ctx.lineTo(r.x + r.w / 2, r.y + r.h);
278 ctx.moveTo(r.x, r.y + r.h / 2);
279 ctx.lineTo(r.x + r.w, r.y + r.h / 2);
280 ctx.stroke();
281 ctx.fillStyle = theme.muted;
282 ctx.font = '13px "Hanken Grotesk", system-ui, sans-serif';
283 ctx.textBaseline = 'top';
284 ctx.textAlign = 'left';
285 ctx.fillText(`−${view.xr.toFixed(2)}`, r.x, r.y + r.h + 6);
286 ctx.textAlign = 'right';
287 ctx.fillText(view.xr.toFixed(2), r.x + r.w, r.y + r.h + 6);
288 ctx.textAlign = 'center';
289 ctx.fillText('displacement x', r.x + r.w / 2, r.y + r.h + 6);
290 ctx.save();
291 ctx.translate(r.x - 8, r.y + r.h / 2);
292 ctx.rotate(-Math.PI / 2);
293 ctx.textBaseline = 'bottom';
294 ctx.fillText('velocity ẋ (s⁻¹)', 0, 0);
295 ctx.textAlign = 'right';
296 ctx.fillText(`${Math.round(view.vr)}`, r.h / 2, 0);
297 ctx.textAlign = 'left';
298 ctx.fillText(`−${Math.round(view.vr)}`, -r.h / 2, 0);
299 ctx.restore();
302function drawPlayhead(r, ph, hue) {
303 if (!ph.queue.length) return;
304 const now = clock();
305 const dt = noteDt();
306 const N = stepper.N;
307 const q = ph.queue;
308 let cur = -1;
309 for (let k = 0; k < q.length; k++) if (q[k].time <= now) cur = k;
310 if (cur < 0) return;
311 ctx.save();
312 ctx.beginPath();
313 ctx.rect(r.x, r.y, r.w, r.h);
314 ctx.clip();
315 ctx.lineWidth = 1.6;
316 ctx.lineCap = 'round';
317 ctx.lineJoin = 'round';
318 const CHUNK = Math.max(4, Math.ceil(N / 24));
319 let head = null;
320 for (let k = 0; k <= cur; k++) {
321 const e = q[k];
322 const span = q[k + 1] ? q[k + 1].time - e.time : dt;
323 const f = Math.min(1, (now - e.time) / span);
324 const end = Math.floor(f * N);
325 for (let j0 = 0; j0 < end; j0 += CHUNK) {
326 const age = (now - (e.time + (j0 / N) * span)) / dt;
327 const alpha = 1 - age / TAIL;
328 if (alpha <= 0) continue;
329 ctx.strokeStyle = orbitColor(hue, true, 0.85 * alpha);
330 ctx.beginPath();
331 for (let j = j0; j <= Math.min(end, j0 + CHUNK); j++) {
332 const [px, py] = toPx(r, e.path[2 * j], e.path[2 * j + 1]);
333 if (j === j0) ctx.moveTo(px, py);
334 else ctx.lineTo(px, py);
335 }
336 ctx.stroke();
337 }
338 if (k === cur) {
339 const j = Math.min(N, f * N);
340 const i0 = Math.floor(j);
341 const i1 = Math.min(N, i0 + 1);
342 const w = j - i0;
343 head = toPx(r, (1 - w) * e.path[2 * i0] + w * e.path[2 * i1], (1 - w) * e.path[2 * i0 + 1] + w * e.path[2 * i1 + 1]);
344 if (e.last) head = null;
345 }
346 // each pluck: a ring growing and fading where it was played
347 for (const n of e.notes) {
348 const g = (now - n.t) / ringTime();
349 if (g < 0 || g >= 1) continue;
350 const [sx, sy] = toPx(r, n.x, n.v);
351 ctx.strokeStyle = orbitColor(hue, true);
352 ctx.globalAlpha = 1 - g;
353 ctx.lineWidth = 2;
354 ctx.beginPath();
355 ctx.arc(sx, sy, 4 + 14 * g, 0, 2 * Math.PI);
356 ctx.stroke();
357 ctx.globalAlpha = 1;
358 }
359 }
360 if (head) {
361 ctx.fillStyle = orbitColor(hue, true);
362 ctx.beginPath();
363 ctx.arc(head[0], head[1], 4.5, 0, 2 * Math.PI);
364 ctx.fill();
365 }
366 ctx.restore();
369function drawStarts(r) {
370 for (const o of orbits) {
371 if (!o.clicked) continue;
372 const [px, py] = toPx(r, o.x0, o.v0);
373 ctx.strokeStyle = orbitColor(o.hue, true);
374 ctx.lineWidth = 1.5;
375 ctx.beginPath();
376 if (o.s.escaped) {
377 // escaped: a cross at its start
378 ctx.moveTo(px - 5, py - 5); ctx.lineTo(px + 5, py + 5);
379 ctx.moveTo(px + 5, py - 5); ctx.lineTo(px - 5, py + 5);
380 } else {
381 ctx.arc(px, py, 5, 0, 2 * Math.PI);
382 }
383 ctx.stroke();
384 }
387function frame() {
388 requestAnimationFrame(frame);
389 if (!canvas.width || !canvas.height) return;
390 fill();
391 schedule();
392 const dpr = devicePixelRatio || 1;
393 const r = plotRect();
394 ctx.setTransform(1, 0, 0, 1, 0, 0);
395 ctx.fillStyle = theme.bg;
396 ctx.fillRect(0, 0, canvas.width, canvas.height);
397 ctx.drawImage(dots, 0, 0);
398 ctx.drawImage(top, 0, 0);
399 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
400 drawAxes(r);
401 drawStarts(r);
402 for (const c of clicked) drawPlayhead(r, c.ph, c.hue);
405// ---- controls ----------------------------------------------------------------
407function buildControls() {
408 const presetSel = $('preset');
409 presetSel.innerHTML = PRESETS.map((pr, i) => `<option value="${i}">${pr.name}</option>`).join('')
410 + '<option value="custom" hidden>Custom</option>';
412 $('sliders').innerHTML = SLIDERS.map((s) => `
413 <label class="row">
414 <span>${s.label}</span>
415 <input type="range" id="s-${s.name}" min="${s.min}" max="${s.max}" step="${s.step}">
416 <output id="o-${s.name}"></output>
417 </label>`).join('');
419 const show = () => {
420 for (const s of SLIDERS) {
421 const v = params[s.name] / (s.scale ?? 1);
422 $(`s-${s.name}`).value = v;
423 $(`o-${s.name}`).textContent = s.fmt(Number($(`s-${s.name}`).value));
424 }
425 };
426 for (const s of SLIDERS) {
427 $(`s-${s.name}`).addEventListener('input', (e) => {
428 params[s.name] = Number(e.target.value) * (s.scale ?? 1);
429 $(`o-${s.name}`).textContent = s.fmt(Number(e.target.value));
430 presetSel.value = 'custom';
431 reset();
432 });
433 }
434 presetSel.addEventListener('change', () => {
435 if (presetSel.value === 'custom') return;
436 // the clicked orbits stay, restarted from the same (x, x') under the new
437 // parameters (a start outside the new view still plays; its chip in the
438 // legend removes it)
439 params = { ...PRESETS[presetSel.value].params };
440 show();
441 reset();
442 });
443 show();
445 $('scale').innerHTML = SCALES.map((s) => `<option${s.name === DEFAULT_SCALE ? ' selected' : ''}>${s.name}</option>`).join('');
446 $('keys').innerHTML = KEYS.map((k, i) => `<button data-key="${i}" aria-pressed="${i === key}">${k}</button>`).join('');
447 $('keys').addEventListener('click', (e) => {
448 const btn = e.target.closest('button[data-key]');
449 if (!btn) return;
450 key = Number(btn.dataset.key);
451 for (const b of $('keys').children) b.setAttribute('aria-pressed', b === btn);
452 });
453 const sus = $('sustain');
454 Object.assign(sus, { min: SUSTAIN.min, max: SUSTAIN.max, step: SUSTAIN.step, value: SUSTAIN.default });
455 const susOut = () => { sus.nextElementSibling.textContent = `${Number(sus.value).toFixed(1)} s`; };
456 sus.addEventListener('input', () => {
457 susOut();
458 synth?.setSustain(Number(sus.value));
459 });
460 susOut();
461 const tempoOut = () => { $('tempo-out').textContent = `${$('tempo').value} /s`; };
462 $('tempo').addEventListener('input', tempoOut);
463 tempoOut();
464 const vol = $('volume');
465 const volOut = () => { vol.nextElementSibling.textContent = `${Math.round(100 * vol.value)}%`; };
466 vol.addEventListener('input', () => {
467 volOut();
468 if (synth && soundOn) synth.setVolume(Number(vol.value));
469 });
470 volOut();
471 $('sound').addEventListener('click', () => setSound(!soundOn));
472 $('clear').addEventListener('click', () => {
473 clicked = [];
474 reset();
475 renderLegend();
476 });
477 $('orbit-list').addEventListener('click', (e) => {
478 const btn = e.target.closest('button[data-hue]');
479 if (btn) removeOrbit(clicked.find((c) => String(c.hue) === btn.dataset.hue));
480 });
482 canvas.addEventListener('pointerdown', (e) => {
483 const rect = canvas.getBoundingClientRect();
484 const r = plotRect();
485 const px = e.clientX - rect.left;
486 const py = e.clientY - rect.top;
487 if (px < r.x || px > r.x + r.w || py < r.y || py > r.y + r.h) return;
488 // on a start ring: remove that orbit; otherwise add one from here
489 const hit = clicked.find((c) => {
490 const [cx, cy] = toPx(r, c.x, c.v);
491 return Math.hypot(cx - px, cy - py) <= HIT_RADIUS;
492 });
493 if (hit) {
494 removeOrbit(hit);
495 return;
496 }
497 const x = ((px - r.x) / r.w * 2 - 1) * view.xr;
498 const v = (1 - (py - r.y) / r.h * 2) * view.vr;
499 if (clicked.length >= MAX_CLICKED) removeOrbit(clicked[0]);
500 // the first voice no other orbit is playing
501 const voice = VOICES.findIndex((_, i) => !clicked.some((c) => c.voice === i));
502 const hue = (0.08 + hueCounter++ * 0.382) % 1;
503 clicked.push({ x, v, hue, voice, ph: newPlayhead(x, v) });
504 orbits.unshift(newOrbit(x, v, CLICK_POINTS, hue, true));
505 renderLegend();
506 });
509function removeOrbit(c) {
510 if (!c) return;
511 clicked = clicked.filter((q) => q !== c);
512 orbits = orbits.filter((o) => !o.clicked || o.hue !== c.hue);
513 // redraw the clicked layer, so its dots go
514 clearDots([top]);
515 for (const o of orbits) if (o.clicked) drawPoints(o, 0);
516 renderLegend();
519// the playing orbits: their color and voice, and a button to remove each
520function renderLegend() {
521 $('orbit-list').innerHTML = clicked.map((c) => `
522 <li>
523 <span class="swatch" style="background: ${orbitColor(c.hue, true)}"></span>
524 ${VOICES[c.voice].name}
525 <button data-hue="${c.hue}" aria-label="Remove the ${VOICES[c.voice].name} orbit">×</button>
526 </li>`).join('');
529readTheme();
530matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { readTheme(); resize(); renderLegend(); });
531buildControls();
532reset();
533new ResizeObserver(resize).observe(canvas);
534resize();
535requestAnimationFrame(frame);
↑↓ move↵openescclose