// Composition 2: orbits — drawing at the playhead: the Poincaré section, each orbit's crossings in // its own color, over a background portrait at the current energy (with each orbit's trace). import type { Params } from "../../core/params.ts"; import type { FrameStore } from "../../stream/frames.ts"; import type { View } from "../../stream/piece.ts"; import { vTopOf } from "../c01-poincare/view.ts"; import { PortraitLayer, type PortraitSettings } from "../shared/portrait.ts"; import { SectionPlot, cssColor, drawCrossing } from "../shared/section.ts"; import { orbitCount } from "./layout.ts"; const PORTRAIT_POST_MS = 50; export function orbitsView(el: HTMLElement, params: () => Params): View { el.classList.add("c02"); 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.portrait as boolean, count: P.portraitCount as number, cpu: P.portraitCpu as number, life: P.portraitLife as number, band: P.portraitBand as number, minHold: P.portraitMinHold as number, maxPoints: P.portraitMaxPoints as number, xfade: P.portraitXfade as number, alpha: P.portraitAlpha as number, size: P.portraitSize as number, tracks: P.traces as boolean, trackMax: P.traceMax as number, trackAlpha: P.traceAlpha as number, trackSize: P.traceSize as number, }); const colors = (P: Params): string[] => { const list = String(P.colors).split(",").map((s) => s.trim()).filter(Boolean); return list.length ? list : [cssColor(el, "--fg")]; }; let row = new Float64Array(2); let lastStore: FrameStore | null = null; let lastPost = 0; return { keep: (P) => 4.6 * (P.fade as number) + 0.5, draw(t, store, playing) { const P = params(); now = t; moving = playing; if (store !== lastStore) { lastStore = store; portrait.reset(); } const n = orbitCount(P); if (row.length !== 2 + 5 * n) row = new Float64Array(2 + 5 * n); const s = store?.at(0, t, row) ?? null; const m2 = P.massRatio as number, l2 = P.lengthRatio as number; const wall = performance.now(); if (wall - lastPost > PORTRAIT_POST_MS) { lastPost = wall; // each active orbit's state, so the portrait worker can trace the orbit it is on const tracks = []; if (s) for (let i = 0; i < n; i++) if (s[2 + 5 * i + 4] > 0.5) tracks.push({ id: i, y: [s[2 + 5 * i], s[3 + 5 * i], s[4 + 5 * i], s[5 + 5 * i]] }); portrait.post(playing, settings(P), s ? s[0] : 0, m2, l2, tracks); } const f = plot.begin(P, s ? s[1] : null, { m1: 1, l1: 1, g: 1, m2, l2 }, vTopOf(m2, l2), cssColor(el, "--dim")); const cols = colors(P); portrait.draw(f, t, settings(P), cssColor(el, "--fg"), (id) => cols[id % cols.length]); 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, cols[(c.o as number) % cols.length]); } } }, readouts: () => ({ portrait: portrait.describe() }), dispose() { portrait.dispose(); el.classList.remove("c02"); }, }; }