// Composition 1: poincaré — drawing at the playhead: the Poincaré section (axes zoomed to the // energy, the crossings fading with age, a background portrait of the orbits at the current energy // computed by a throttled worker) with the pendulum as an inset in the upper-left corner. import type { Params } from "../../core/params.ts"; import type { FrameStore } from "../../stream/frames.ts"; import type { View } from "../../stream/piece.ts"; import { PortraitLayer, type PortraitSettings } from "../shared/portrait.ts"; import { SectionPlot, cssColor, drawCrossing } from "../shared/section.ts"; const BLUR_MAX_SAMPLES = 64; const BLUR_SPACING_PX = 2; const PORTRAIT_POST_MS = 50; export function vTopOf(m2: number, l2: number): number { return 2 * (1 + m2) + 2 * m2 * l2; // m1 = l1 = g = 1 } export function poincareView(el: HTMLElement, params: () => Params): View { el.classList.add("c01"); const canvas = document.createElement("canvas"); el.append(canvas); const plot = new SectionPlot(canvas); let now = 0, moving = false; const portrait = new PortraitLayer(() => now, () => moving); const settings = (P: Params): PortraitSettings => ({ show: P.orbits as boolean, count: P.orbitCount as number, cpu: P.orbitCpu as number, life: P.orbitLife as number, band: P.orbitBand as number, minHold: P.orbitMinHold as number, maxPoints: P.orbitMaxPoints as number, xfade: P.orbitXfade as number, alpha: P.orbitAlpha as number, size: P.orbitSize as number, tracks: false, trackMax: 0, trackAlpha: 0, trackSize: 1, }); const st = new Float64Array(3); let lastStore: FrameStore | null = null; let lastPost = 0, lastWall = 0, frameDt = 1 / 60; function drawPendulum(g: CanvasRenderingContext2D, w: number, h: number, P: Params, t: number, store: FrameStore | null) { const opacity = P.pendulumAlpha as number; if (opacity <= 0) return; const at = (u: number): [number, number] => { const s = store?.at(0, u, st); return s ? [s[0], s[1]] : [0, 0]; }; const shutter = moving ? (P.motionBlur as number) * frameDt : 0; const l1 = 1, l2 = P.lengthRatio as number; const R = ((P.pendulumSize as number) * Math.min(w, h)) / 2; const scale = (0.9 * R) / (l1 + l2); const x0 = 8 + R, y0 = 8 + R; 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 = opacity * (k === 1 ? 1 : 1 - Math.pow(0.03, 1 / k)); const r1 = 3, r2 = 3 * Math.cbrt(P.massRatio as number); const fg = cssColor(el, "--fg"); 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 = x0 + scale * l1 * Math.sin(th1), y1 = y0 + scale * l1 * Math.cos(th1); const x2 = x1 + scale * l2 * Math.sin(th2), y2 = y1 + scale * l2 * Math.cos(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 = opacity; g.beginPath(); g.arc(x0, y0, 1.5, 0, 2 * Math.PI); g.fill(); g.globalAlpha = 1; } return { keep: (P) => 4.6 * (P.fade as number) + 0.5, draw(t, store, playing) { const P = params(); now = t; moving = playing; const wall = performance.now(); const dtw = (wall - lastWall) / 1000; lastWall = wall; if (dtw > 0 && dtw < 0.1) frameDt += (dtw - frameDt) * 0.1; if (store !== lastStore) { lastStore = store; portrait.reset(); // a new performance: a fresh portrait } const s = store?.at(0, t, st); const e = s ? s[2] : null; const m2 = P.massRatio as number, l2 = P.lengthRatio as number; const fg = cssColor(el, "--fg"); if (wall - lastPost > PORTRAIT_POST_MS) { lastPost = wall; portrait.post(playing, settings(P), e ?? 0, m2, l2, []); } const f = plot.begin(P, e, { m1: 1, l1: 1, g: 1, m2, l2 }, vTopOf(m2, l2), cssColor(el, "--dim")); portrait.draw(f, t, settings(P), fg, () => fg); if (store) { const fade = P.fade as number, size = P.pointSize as number; for (const c of store.events) { const age = t - c.t; if (age < 0) break; // not sounded yet drawCrossing(f, c.th2 as number, c.p2 as number, age, fade, size, fg); } } drawPendulum(f.g, f.w, f.h, P, t, store); }, readouts: () => ({ "orbit portrait": portrait.describe() }), dispose() { portrait.dispose(); el.classList.remove("c01"); }, }; }