75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 1// Composition 4: descent — drawing, from the score's data at the playhead: the pendulum (with
2// motion blur), the outer bob's fading trail, and rings where extension notes and clicks happen.
4import type { Params } from "../../core/params.ts";
5import type { FrameStore } from "../../stream/frames.ts";
6import type { View } from "../../stream/piece.ts";
7import { cssColor, fitCanvas } from "../shared/section.ts";
9const BLUR_MAX_SAMPLES = 48;
10const BLUR_SPACING_PX = 2;
11const RING_S = 0.9; // extension-note ring lifetime
12const CLICK_RING_S = 0.35;
14export function descentView(el: HTMLElement, params: () => Params): View {
15 el.classList.add("c04");
16 const canvas = document.createElement("canvas");
17 el.append(canvas);
18 const st = new Float64Array(2);
19 let lastWall = 0;
20 let frameDt = 1 / 60;
22 return {
23 keep: (P) => Math.max(P.trailLength as number, RING_S) + 0.5,
24 draw(t: number, store: FrameStore | null, playing: boolean) {
25 const P = params();
26 const wall = performance.now() / 1000;
27 const dtw = wall - lastWall;
28 lastWall = wall;
29 if (dtw > 0 && dtw < 0.1) frameDt += (dtw - frameDt) * 0.1;
30 const g = fitCanvas(canvas);
31 const w = canvas.clientWidth, h = canvas.clientHeight;
32 g.clearRect(0, 0, w, h);
33 const fg = cssColor(el, "--fg");
34 const l1 = 1, l2 = P.lengthRatio as number;
35 const scale = (0.46 * Math.min(w, h)) / (l1 + l2);
36 const x0 = w / 2, y0 = h / 2;
37 const pos = (th1: number, th2: number) => {
38 const x1 = x0 + scale * l1 * Math.sin(th1), y1 = y0 + scale * l1 * Math.cos(th1);
39 return [x1, y1, x1 + scale * l2 * Math.sin(th2), y1 + scale * l2 * Math.cos(th2)];
40 };
41 // the state at a time (balanced straight up before anything is computed)
42 const at = (u: number): [number, number] => {
43 const s = store?.at(0, u, st);
44 return s ? [s[0], s[1]] : [Math.PI, Math.PI];
45 };
47 // trail of the outer bob, fading with age
48 if (P.trails && store) {
49 const len = P.trailLength as number;
50 const BANDS = 6;
51 g.strokeStyle = fg;
52 g.lineWidth = 1;
53 g.lineJoin = g.lineCap = "round";
54 for (let band = 0; band < BANDS; band++) {
55 const aNew = t - (band * len) / BANDS, aOld = t - ((band + 1) * len) / BANDS;
56 g.globalAlpha = 0.45 * (1 - (band + 0.5) / BANDS);
57 g.beginPath();
58 let started = false, k = 0;
59 store.rows(0, aOld - 0.01, Math.min(t, aNew + 0.01), (d, o) => {
60 if (k++ % 2) return; // every other row is plenty for the trail
61 const [, , bx, by] = pos(d[o + 1], d[o + 2]);
62 if (started) g.lineTo(bx, by);
63 else g.moveTo(bx, by), (started = true);
64 });
65 g.stroke();
66 }
67 g.globalAlpha = 1;
68 }
70 // rings, where each extension note (and click) happened
71 if (P.rings && store) {
72 for (const c of store.events) {
73 if (c.kind !== "ring") continue;
74 const age = t - c.t;
75 const click = c.click === 1;
76 const life = click ? CLICK_RING_S : RING_S;
77 if (age < 0 || age > life) continue;
78 const strength = c.strength as number;
79 const bx = x0 + scale * (c.x as number), by = y0 + scale * (c.y as number);
80 g.strokeStyle = fg;
81 g.globalAlpha = (1 - age / life) * (0.4 + 0.6 * strength);
82 g.lineWidth = click ? 1 : 1.2;
83 g.beginPath();
84 const grow = click ? 14 : 60 * (0.5 + strength);
85 g.arc(bx, by, (click ? 3 : 6) + grow * Math.sqrt(age / life), 0, 2 * Math.PI);
86 g.stroke();
87 }
88 g.globalAlpha = 1;
89 }
91 // the pendulum, with motion blur
92 const shutter = playing ? (P.motionBlur as number) * frameDt : 0;
93 let k = 1;
94 if (shutter > 0) {
95 let path = 0;
96 let a = at(t);
97 for (let j = 1; j <= 8; j++) {
98 const b = at(t - (shutter * j) / 8);
99 path += scale * (l1 * Math.abs(b[0] - a[0]) + l2 * Math.abs(b[1] - a[1]));
100 a = b;
101 }
102 k = Math.max(2, Math.min(BLUR_MAX_SAMPLES, Math.ceil(path / BLUR_SPACING_PX)));
103 }
104 const alpha = k === 1 ? 1 : 1 - Math.pow(0.03, 1 / k);
105 const r1 = 4, r2 = 4 * Math.cbrt(P.massRatio as number);
106 g.strokeStyle = fg;
107 g.fillStyle = fg;
108 g.lineWidth = 1.5;
109 g.lineCap = "round";
110 g.globalAlpha = alpha;
111 for (let i = 0; i < k; i++) {
112 const [th1, th2] = at(t - (shutter * i) / Math.max(1, k - 1));
113 const [x1, y1, x2, y2] = pos(th1, th2);
114 g.beginPath();
115 g.moveTo(x0, y0);
116 g.lineTo(x1, y1);
117 g.lineTo(x2, y2);
118 g.stroke();
119 g.beginPath();
120 g.arc(x1, y1, r1, 0, 2 * Math.PI);
121 g.moveTo(x2 + r2, y2);
122 g.arc(x2, y2, r2, 0, 2 * Math.PI);
123 g.fill();
124 }
125 g.globalAlpha = 1;
126 g.beginPath();
127 g.arc(x0, y0, 1.5, 0, 2 * Math.PI);
128 g.fill();
129 },
130 dispose() {
131 el.classList.remove("c04");
132 },
133 };
134}