1// Composition 1: poincaré — drawing at the playhead: the Poincaré section (axes zoomed to the
2// energy, the crossings fading with age, a background portrait of the orbits at the current energy
3// computed by a throttled worker) with the pendulum as an inset in the upper-left corner.
5import type { Params } from "../../core/params.ts";
6import type { FrameStore } from "../../stream/frames.ts";
7import type { View } from "../../stream/piece.ts";
8import { PortraitLayer, type PortraitSettings } from "../shared/portrait.ts";
9import { SectionPlot, cssColor, drawCrossing } from "../shared/section.ts";
11const BLUR_MAX_SAMPLES = 64;
12const BLUR_SPACING_PX = 2;
13const PORTRAIT_POST_MS = 50;
15export function vTopOf(m2: number, l2: number): number {
16 return 2 * (1 + m2) + 2 * m2 * l2; // m1 = l1 = g = 1
17}
19export function poincareView(el: HTMLElement, params: () => Params): View {
20 el.classList.add("c01");
21 const canvas = document.createElement("canvas");
22 el.append(canvas);
23 const plot = new SectionPlot(canvas);
24 let now = 0, moving = false;
25 const portrait = new PortraitLayer(() => now, () => moving);
26 const settings = (P: Params): PortraitSettings => ({
27 show: P.orbits as boolean,
28 count: P.orbitCount as number,
29 cpu: P.orbitCpu as number,
30 life: P.orbitLife as number,
31 band: P.orbitBand as number,
32 minHold: P.orbitMinHold as number,
33 maxPoints: P.orbitMaxPoints as number,
34 xfade: P.orbitXfade as number,
35 alpha: P.orbitAlpha as number,
36 size: P.orbitSize as number,
37 tracks: false,
38 trackMax: 0,
39 trackAlpha: 0,
40 trackSize: 1,
41 });
42 const st = new Float64Array(3);
43 let lastStore: FrameStore | null = null;
44 let lastPost = 0, lastWall = 0, frameDt = 1 / 60;
46 function drawPendulum(g: CanvasRenderingContext2D, w: number, h: number, P: Params, t: number, store: FrameStore | null) {
47 const opacity = P.pendulumAlpha as number;
48 if (opacity <= 0) return;
49 const at = (u: number): [number, number] => {
50 const s = store?.at(0, u, st);
51 return s ? [s[0], s[1]] : [0, 0];
52 };
53 const shutter = moving ? (P.motionBlur as number) * frameDt : 0;
54 const l1 = 1, l2 = P.lengthRatio as number;
55 const R = ((P.pendulumSize as number) * Math.min(w, h)) / 2;
56 const scale = (0.9 * R) / (l1 + l2);
57 const x0 = 8 + R, y0 = 8 + R;
58 let k = 1;
59 if (shutter > 0) {
60 let path = 0;
61 let a = at(t);
62 for (let j = 1; j <= 8; j++) {
63 const b = at(t - (shutter * j) / 8);
64 path += scale * (l1 * Math.abs(b[0] - a[0]) + l2 * Math.abs(b[1] - a[1]));
65 a = b;
66 }
67 k = Math.max(2, Math.min(BLUR_MAX_SAMPLES, Math.ceil(path / BLUR_SPACING_PX)));
68 }
69 const alpha = opacity * (k === 1 ? 1 : 1 - Math.pow(0.03, 1 / k));
70 const r1 = 3, r2 = 3 * Math.cbrt(P.massRatio as number);
71 const fg = cssColor(el, "--fg");
72 g.strokeStyle = fg;
73 g.fillStyle = fg;
74 g.lineWidth = 1.5;
75 g.lineCap = "round";
76 g.globalAlpha = alpha;
77 for (let i = 0; i < k; i++) {
78 const [th1, th2] = at(t - (shutter * i) / Math.max(1, k - 1));
79 const x1 = x0 + scale * l1 * Math.sin(th1), y1 = y0 + scale * l1 * Math.cos(th1);
80 const x2 = x1 + scale * l2 * Math.sin(th2), y2 = y1 + scale * l2 * Math.cos(th2);
81 g.beginPath();
82 g.moveTo(x0, y0);
83 g.lineTo(x1, y1);
84 g.lineTo(x2, y2);
85 g.stroke();
86 g.beginPath();
87 g.arc(x1, y1, r1, 0, 2 * Math.PI);
88 g.moveTo(x2 + r2, y2);
89 g.arc(x2, y2, r2, 0, 2 * Math.PI);
90 g.fill();
91 }
92 g.globalAlpha = opacity;
93 g.beginPath();
94 g.arc(x0, y0, 1.5, 0, 2 * Math.PI);
95 g.fill();
96 g.globalAlpha = 1;
97 }
99 return {
100 keep: (P) => 4.6 * (P.fade as number) + 0.5,
101 draw(t, store, playing) {
102 const P = params();
103 now = t;
104 moving = playing;
105 const wall = performance.now();
106 const dtw = (wall - lastWall) / 1000;
107 lastWall = wall;
108 if (dtw > 0 && dtw < 0.1) frameDt += (dtw - frameDt) * 0.1;
109 if (store !== lastStore) {
110 lastStore = store;
111 portrait.reset(); // a new performance: a fresh portrait
112 }
113 const s = store?.at(0, t, st);
114 const e = s ? s[2] : null;
115 const m2 = P.massRatio as number, l2 = P.lengthRatio as number;
116 const fg = cssColor(el, "--fg");
117 if (wall - lastPost > PORTRAIT_POST_MS) {
118 lastPost = wall;
119 portrait.post(playing, settings(P), e ?? 0, m2, l2, []);
120 }
121 const f = plot.begin(P, e, { m1: 1, l1: 1, g: 1, m2, l2 }, vTopOf(m2, l2), cssColor(el, "--dim"));
122 portrait.draw(f, t, settings(P), fg, () => fg);
123 if (store) {
124 const fade = P.fade as number, size = P.pointSize as number;
125 for (const c of store.events) {
126 const age = t - c.t;
127 if (age < 0) break; // not sounded yet
128 drawCrossing(f, c.th2 as number, c.p2 as number, age, fade, size, fg);
129 }
130 }
131 drawPendulum(f.g, f.w, f.h, P, t, store);
132 },
133 readouts: () => ({ "orbit portrait": portrait.describe() }),
134 dispose() {
135 portrait.dispose();
136 el.classList.remove("c01");
137 },
138 };
139}