// Composition 4: descent — drawing, from the score's data at the playhead: the pendulum (with // motion blur), the outer bob's fading trail, and rings where extension notes and clicks happen. import type { Params } from "../../core/params.ts"; import type { FrameStore } from "../../stream/frames.ts"; import type { View } from "../../stream/piece.ts"; import { cssColor, fitCanvas } from "../shared/section.ts"; const BLUR_MAX_SAMPLES = 48; const BLUR_SPACING_PX = 2; const RING_S = 0.9; // extension-note ring lifetime const CLICK_RING_S = 0.35; export function descentView(el: HTMLElement, params: () => Params): View { el.classList.add("c04"); const canvas = document.createElement("canvas"); el.append(canvas); const st = new Float64Array(2); let lastWall = 0; let frameDt = 1 / 60; return { keep: (P) => Math.max(P.trailLength as number, RING_S) + 0.5, draw(t: number, store: FrameStore | null, playing: boolean) { const P = params(); const wall = performance.now() / 1000; const dtw = wall - lastWall; lastWall = wall; if (dtw > 0 && dtw < 0.1) frameDt += (dtw - frameDt) * 0.1; const g = fitCanvas(canvas); const w = canvas.clientWidth, h = canvas.clientHeight; g.clearRect(0, 0, w, h); const fg = cssColor(el, "--fg"); const l1 = 1, l2 = P.lengthRatio as number; const scale = (0.46 * Math.min(w, h)) / (l1 + l2); const x0 = w / 2, y0 = h / 2; const pos = (th1: number, th2: number) => { const x1 = x0 + scale * l1 * Math.sin(th1), y1 = y0 + scale * l1 * Math.cos(th1); return [x1, y1, x1 + scale * l2 * Math.sin(th2), y1 + scale * l2 * Math.cos(th2)]; }; // the state at a time (balanced straight up before anything is computed) const at = (u: number): [number, number] => { const s = store?.at(0, u, st); return s ? [s[0], s[1]] : [Math.PI, Math.PI]; }; // trail of the outer bob, fading with age if (P.trails && store) { const len = P.trailLength as number; const BANDS = 6; g.strokeStyle = fg; g.lineWidth = 1; g.lineJoin = g.lineCap = "round"; for (let band = 0; band < BANDS; band++) { const aNew = t - (band * len) / BANDS, aOld = t - ((band + 1) * len) / BANDS; g.globalAlpha = 0.45 * (1 - (band + 0.5) / BANDS); g.beginPath(); let started = false, k = 0; store.rows(0, aOld - 0.01, Math.min(t, aNew + 0.01), (d, o) => { if (k++ % 2) return; // every other row is plenty for the trail const [, , bx, by] = pos(d[o + 1], d[o + 2]); if (started) g.lineTo(bx, by); else g.moveTo(bx, by), (started = true); }); g.stroke(); } g.globalAlpha = 1; } // rings, where each extension note (and click) happened if (P.rings && store) { for (const c of store.events) { if (c.kind !== "ring") continue; const age = t - c.t; const click = c.click === 1; const life = click ? CLICK_RING_S : RING_S; if (age < 0 || age > life) continue; const strength = c.strength as number; const bx = x0 + scale * (c.x as number), by = y0 + scale * (c.y as number); g.strokeStyle = fg; g.globalAlpha = (1 - age / life) * (0.4 + 0.6 * strength); g.lineWidth = click ? 1 : 1.2; g.beginPath(); const grow = click ? 14 : 60 * (0.5 + strength); g.arc(bx, by, (click ? 3 : 6) + grow * Math.sqrt(age / life), 0, 2 * Math.PI); g.stroke(); } g.globalAlpha = 1; } // the pendulum, with motion blur const shutter = playing ? (P.motionBlur as number) * frameDt : 0; let k = 1; if (shutter > 0) { let path = 0; let a = at(t); for (let j = 1; j <= 8; j++) { const b = at(t - (shutter * j) / 8); path += scale * (l1 * Math.abs(b[0] - a[0]) + l2 * Math.abs(b[1] - a[1])); a = b; } k = Math.max(2, Math.min(BLUR_MAX_SAMPLES, Math.ceil(path / BLUR_SPACING_PX))); } const alpha = k === 1 ? 1 : 1 - Math.pow(0.03, 1 / k); const r1 = 4, r2 = 4 * Math.cbrt(P.massRatio as number); g.strokeStyle = fg; g.fillStyle = fg; g.lineWidth = 1.5; g.lineCap = "round"; g.globalAlpha = alpha; for (let i = 0; i < k; i++) { const [th1, th2] = at(t - (shutter * i) / Math.max(1, k - 1)); const [x1, y1, x2, y2] = pos(th1, th2); g.beginPath(); g.moveTo(x0, y0); g.lineTo(x1, y1); g.lineTo(x2, y2); g.stroke(); g.beginPath(); g.arc(x1, y1, r1, 0, 2 * Math.PI); g.moveTo(x2 + r2, y2); g.arc(x2, y2, r2, 0, 2 * Math.PI); g.fill(); } g.globalAlpha = 1; g.beginPath(); g.arc(x0, y0, 1.5, 0, 2 * Math.PI); g.fill(); }, dispose() { el.classList.remove("c04"); }, }; }