// Composition 5: ensemble — drawing at the playhead: the six pendulums hanging in one row, left to // right as listed (lowest pitch first), each given room in proportion to its drawn size. A push // brightens a pendulum briefly; a faint trail follows each bob over the last few milliseconds. import type { Params } from "../../core/params.ts"; import type { View } from "../../stream/piece.ts"; import { cssColor, fitCanvas } from "../shared/section.ts"; import { N, physics } from "./score.ts"; const TRAIL_S = 0.012; // the bobs' paths over this long /** Each pendulum's pivot, and drawing units → px (most of a pendulum's sideways reach as room). */ function rowLayout(dr: number[], w: number, h: number) { const ROOM = 1.5, GAP = 0.2; const span = dr.reduce((a, d) => a + ROOM * d, 0) + GAP * (dr.length - 1); const unit = Math.min((0.92 * w) / span, (0.5 * h) / Math.max(...dr)); const y = h / 2 - (Math.max(...dr) * unit) / 2; // the resting row sits centred let x = (w - span * unit) / 2; const at = dr.map((d) => { const p = { x: x + (ROOM / 2) * d * unit, y }; x += (ROOM * d + GAP) * unit; return p; }); return { unit, at }; } export function ensembleView(el: HTMLElement, params: () => Params): View { el.classList.add("c05"); const canvas = document.createElement("canvas"); el.append(canvas); const joints = new Float64Array(4 * N); let physKey = ""; let built = physics(params()).built; return { keep: () => 1.5, draw(t, store) { const P = params(); const key = JSON.stringify([P.basePitch, ...Array.from({ length: N }, (_, i) => [P[`ratio${i + 1}`], P[`partial${i + 1}`], P[`shape${i + 1}`]])]); if (key !== physKey) { physKey = key; built = physics(P).built; } const g = fitCanvas(canvas); const w = canvas.clientWidth, h = canvas.clientHeight; g.clearRect(0, 0, w, h); const fg = cssColor(el, "--fg"), muted = cssColor(el, "--muted"); // compared sizes: length^sizeExp (true scale would make the highest a speck) const ex = P.sizeExp as number; const reach = built.map((b) => b.l1 + b.l2); const dr = reach.map((r) => Math.pow(r, ex)); const { unit, at } = rowLayout(dr, w, h); const s = store?.at(0, t, joints) ?? null; const trailA = P.trail as number; // when each pendulum was last pushed const pushed = Array(N).fill(-Infinity); if (store) for (const e of store.events) if (e.kind === "push" && e.t <= t) pushed[e.i as number] = e.t; g.lineCap = "round"; g.lineJoin = "round"; for (let i = 0; i < N; i++) { const { x: cx, y: cy } = at[i]; const f = (unit * dr[i]) / reach[i]; // physical units → px for this pendulum if (store && trailA > 0) { g.strokeStyle = muted; g.globalAlpha = trailA; g.lineWidth = 1; g.beginPath(); let started = false; store.rows(1, t - TRAIL_S, t, (d, o) => { const x = cx + f * d[o + 1 + 2 * i], y = cy + f * d[o + 2 + 2 * i]; if (started) g.lineTo(x, y); else g.moveTo(x, y), (started = true); }); g.stroke(); } const b = built[i]; const jt = s ? s.subarray(4 * i, 4 * i + 4) : [0, b.l1, 0, reach[i]]; const x1 = cx + f * jt[0], y1 = cy + f * jt[1], x2 = cx + f * jt[2], y2 = cy + f * jt[3]; // a push brightens the pendulum, fading over a second g.globalAlpha = 0.6 + 0.4 * Math.max(0, 1 - (t - pushed[i])); g.strokeStyle = fg; g.fillStyle = fg; g.lineWidth = 1.5; g.beginPath(); g.moveTo(cx, cy); g.lineTo(x1, y1); g.lineTo(x2, y2); g.stroke(); const r2 = 2.2 * Math.cbrt(b.m2) + 1; g.beginPath(); g.arc(x1, y1, 2, 0, 2 * Math.PI); g.moveTo(x2 + r2, y2); g.arc(x2, y2, r2, 0, 2 * Math.PI); g.moveTo(cx + 1.5, cy); g.arc(cx, cy, 1.5, 0, 2 * Math.PI); g.fill(); } g.globalAlpha = 1; }, readouts() { const { k } = physics(params()); const r: Record = { "time scale": `${k.toFixed(0)} sim s per s` }; built.forEach((b, i) => { r[`pendulum ${i + 1}`] = `${b.lower.toFixed(1)} + ${b.upper.toFixed(1)} Hz; l1 ${b.l1.toFixed(3)}, l2 ${b.l2.toFixed(3)}, m2/m1 ${b.m2.toFixed(2)}`; }); return r; }, dispose() { el.classList.remove("c05"); }, }; }