/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / compositions / c05-ensemble / view.ts
112 lines · 4.4 KBBlameHistoryRaw
1// Composition 5: ensemble — drawing at the playhead: the six pendulums hanging in one row, left to
2// right as listed (lowest pitch first), each given room in proportion to its drawn size. A push
3// brightens a pendulum briefly; a faint trail follows each bob over the last few milliseconds.
5import type { Params } from "../../core/params.ts";
6import type { View } from "../../stream/piece.ts";
7import { cssColor, fitCanvas } from "../shared/section.ts";
8import { N, physics } from "./score.ts";
10const TRAIL_S = 0.012; // the bobs' paths over this long
12/** Each pendulum's pivot, and drawing units → px (most of a pendulum's sideways reach as room). */
13function rowLayout(dr: number[], w: number, h: number) {
14 const ROOM = 1.5, GAP = 0.2;
15 const span = dr.reduce((a, d) => a + ROOM * d, 0) + GAP * (dr.length - 1);
16 const unit = Math.min((0.92 * w) / span, (0.5 * h) / Math.max(...dr));
17 const y = h / 2 - (Math.max(...dr) * unit) / 2; // the resting row sits centred
18 let x = (w - span * unit) / 2;
19 const at = dr.map((d) => {
20 const p = { x: x + (ROOM / 2) * d * unit, y };
21 x += (ROOM * d + GAP) * unit;
22 return p;
23 });
24 return { unit, at };
27export function ensembleView(el: HTMLElement, params: () => Params): View {
28 el.classList.add("c05");
29 const canvas = document.createElement("canvas");
30 el.append(canvas);
31 const joints = new Float64Array(4 * N);
32 let physKey = "";
33 let built = physics(params()).built;
35 return {
36 keep: () => 1.5,
37 draw(t, store) {
38 const P = params();
39 const key = JSON.stringify([P.basePitch, ...Array.from({ length: N }, (_, i) => [P[`ratio${i + 1}`], P[`partial${i + 1}`], P[`shape${i + 1}`]])]);
40 if (key !== physKey) {
41 physKey = key;
42 built = physics(P).built;
43 }
44 const g = fitCanvas(canvas);
45 const w = canvas.clientWidth, h = canvas.clientHeight;
46 g.clearRect(0, 0, w, h);
47 const fg = cssColor(el, "--fg"), muted = cssColor(el, "--muted");
48 // compared sizes: length^sizeExp (true scale would make the highest a speck)
49 const ex = P.sizeExp as number;
50 const reach = built.map((b) => b.l1 + b.l2);
51 const dr = reach.map((r) => Math.pow(r, ex));
52 const { unit, at } = rowLayout(dr, w, h);
53 const s = store?.at(0, t, joints) ?? null;
54 const trailA = P.trail as number;
55 // when each pendulum was last pushed
56 const pushed = Array(N).fill(-Infinity);
57 if (store) for (const e of store.events) if (e.kind === "push" && e.t <= t) pushed[e.i as number] = e.t;
58 g.lineCap = "round";
59 g.lineJoin = "round";
60 for (let i = 0; i < N; i++) {
61 const { x: cx, y: cy } = at[i];
62 const f = (unit * dr[i]) / reach[i]; // physical units → px for this pendulum
63 if (store && trailA > 0) {
64 g.strokeStyle = muted;
65 g.globalAlpha = trailA;
66 g.lineWidth = 1;
67 g.beginPath();
68 let started = false;
69 store.rows(1, t - TRAIL_S, t, (d, o) => {
70 const x = cx + f * d[o + 1 + 2 * i], y = cy + f * d[o + 2 + 2 * i];
71 if (started) g.lineTo(x, y);
72 else g.moveTo(x, y), (started = true);
73 });
74 g.stroke();
75 }
76 const b = built[i];
77 const jt = s ? s.subarray(4 * i, 4 * i + 4) : [0, b.l1, 0, reach[i]];
78 const x1 = cx + f * jt[0], y1 = cy + f * jt[1], x2 = cx + f * jt[2], y2 = cy + f * jt[3];
79 // a push brightens the pendulum, fading over a second
80 g.globalAlpha = 0.6 + 0.4 * Math.max(0, 1 - (t - pushed[i]));
81 g.strokeStyle = fg;
82 g.fillStyle = fg;
83 g.lineWidth = 1.5;
84 g.beginPath();
85 g.moveTo(cx, cy);
86 g.lineTo(x1, y1);
87 g.lineTo(x2, y2);
88 g.stroke();
89 const r2 = 2.2 * Math.cbrt(b.m2) + 1;
90 g.beginPath();
91 g.arc(x1, y1, 2, 0, 2 * Math.PI);
92 g.moveTo(x2 + r2, y2);
93 g.arc(x2, y2, r2, 0, 2 * Math.PI);
94 g.moveTo(cx + 1.5, cy);
95 g.arc(cx, cy, 1.5, 0, 2 * Math.PI);
96 g.fill();
97 }
98 g.globalAlpha = 1;
99 },
100 readouts() {
101 const { k } = physics(params());
102 const r: Record<string, string> = { "time scale": `${k.toFixed(0)} sim s per s` };
103 built.forEach((b, i) => {
104 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)}`;
105 });
106 return r;
107 },
108 dispose() {
109 el.classList.remove("c05");
110 },
111 };
↑↓ move↵openescclose