/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / compositions / c03-divergence / view.ts
92 lines · 3.4 KBCodeBlameHistory
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 1// Composition 3: divergence — drawing at the playhead: all the pendulums from one pivot, with the
2// bottom bobs' fading trails.
4import type { Params } from "../../core/params.ts";
5import type { View } from "../../stream/piece.ts";
6import { cssColor, fitCanvas } from "../shared/section.ts";
7import { releaseAngles } from "./score.ts";
9const FRAME_DT = 1 / 120;
11export function divergenceView(el: HTMLElement, params: () => Params): View {
12 el.classList.add("c03");
13 const canvas = document.createElement("canvas");
14 el.append(canvas);
15 let row = new Float64Array(0);
16 const colorOf = (P: Params, i: number, n: number) => `hsl(${((30 + (360 * i) / Math.max(1, n)) % 360).toFixed(0)} ${P.saturation}% 70%)`;
18 return {
19 keep: (P) => (P.trailLength as number) + 0.5,
20 draw(t, store) {
21 const P = params();
22 const g = fitCanvas(canvas);
23 const w = canvas.clientWidth, h = canvas.clientHeight;
24 g.clearRect(0, 0, w, h);
25 const n = Math.max(1, Math.round(P.count as number));
26 const l1 = 1, l2 = P.lengthRatio as number;
27 const scale = (0.46 * Math.min(w, h)) / (l1 + l2);
28 const x0 = w / 2, y0 = h / 2;
29 const px = (th1: number, th2: number): [number, number, number, number] => {
30 const x1 = x0 + scale * l1 * Math.sin(th1), y1 = y0 + scale * l1 * Math.cos(th1);
31 return [x1, y1, x1 + scale * l2 * Math.sin(th2), y1 + scale * l2 * Math.cos(th2)];
32 };
34 // trails: the bottom bobs' recent paths, fading with age (drawn in a few alpha bands)
35 if (P.trails && store) {
36 const len = P.trailLength as number;
37 const BANDS = 6;
38 g.lineWidth = P.trailWidth as number;
39 g.lineJoin = g.lineCap = "round";
40 for (let i = 0; i < n; i++) {
41 g.strokeStyle = colorOf(P, i, n);
42 for (let band = 0; band < BANDS; band++) {
43 const aNew = t - (band * len) / BANDS, aOld = t - ((band + 1) * len) / BANDS;
44 g.globalAlpha = 0.7 * (1 - (band + 0.5) / BANDS);
45 g.beginPath();
46 let started = false;
47 store.rows(0, aOld - FRAME_DT, Math.min(t, aNew + FRAME_DT), (d, o) => {
48 const [, , bx, by] = px(d[o + 1 + 2 * i], d[o + 2 + 2 * i]);
49 if (started) g.lineTo(bx, by);
50 else g.moveTo(bx, by), (started = true);
51 });
52 g.stroke();
53 }
54 }
55 g.globalAlpha = 1;
56 }
58 // pendulums, all from the one pivot (before the first play: at the nominal release)
59 if (row.length !== 2 * n) row = new Float64Array(2 * n);
60 const s = store?.at(0, t, row) ?? null;
61 const [a1, a2] = releaseAngles(P, null);
62 g.lineWidth = 1.5;
63 g.lineCap = "round";
64 const r = P.bobSize as number;
65 for (let i = n - 1; i >= 0; i--) {
66 const [x1, y1, x2, y2] = s ? px(s[2 * i], s[2 * i + 1]) : px(a1, a2);
67 const col = colorOf(P, i, n);
68 g.strokeStyle = col;
69 g.fillStyle = col;
70 g.globalAlpha = P.armAlpha as number;
71 g.beginPath();
72 g.moveTo(x0, y0);
73 g.lineTo(x1, y1);
74 g.lineTo(x2, y2);
75 g.stroke();
76 g.globalAlpha = 1;
77 g.beginPath();
78 g.arc(x1, y1, r * 0.8, 0, 2 * Math.PI);
79 g.moveTo(x2 + r, y2);
80 g.arc(x2, y2, r, 0, 2 * Math.PI);
81 g.fill();
82 }
83 g.fillStyle = cssColor(el, "--fg");
84 g.beginPath();
85 g.arc(x0, y0, 1.5, 0, 2 * Math.PI);
86 g.fill();
87 },
88 dispose() {
89 el.classList.remove("c03");
90 },
91 };
↑↓ move↵openescclose