// Drawing helpers for the Poincaré section plot: canvas sizing, the "fit energy" zoom, and axes. import type { Params } from "../../core/params.ts"; import type { PendulumConfig } from "../../physics/doublePendulum.ts"; export function fitCanvas(c: HTMLCanvasElement): CanvasRenderingContext2D { const dpr = window.devicePixelRatio || 1; const w = Math.round(c.clientWidth * dpr); const h = Math.round(c.clientHeight * dpr); if (c.width !== w || c.height !== h) { c.width = w; c.height = h; } const g = c.getContext("2d")!; g.setTransform(dpr, 0, 0, dpr, 0, 0); return g; } export function cssColor(el: Element, name: string, fallback = "#ccc"): string { return getComputedStyle(el).getPropertyValue(name).trim() || fallback; } export interface SectionFrame { g: CanvasRenderingContext2D; w: number; h: number; sx: (th2: number) => number; sy: (p2: number) => number; } /** The section's axes, with a zoom that eases toward the energetically allowed region. */ export class SectionPlot { private thR = Math.PI; // half-ranges (smoothed) private pR = 1; private lastWall = 0; canvas: HTMLCanvasElement; constructor(canvas: HTMLCanvasElement) { this.canvas = canvas; } /** * Clear, update the zoom, draw the axes; returns the coordinate mapping. * e: normalized energy that sets the zoom (with "fit energy"); cfg/vTop: the pendulum's. */ begin(P: Params, e: number | null, cfg: PendulumConfig | null, vTop: number, dim: string): SectionFrame { const wall = performance.now() / 1000; const dtw = wall - this.lastWall; this.lastWall = wall; const c = this.canvas; const g = fitCanvas(c); const w = c.clientWidth, h = c.clientHeight; g.clearRect(0, 0, w, h); let thT = Math.PI, pT = P.plotP as number; if (P.plotZoom === "fit energy" && e !== null && cfg) { // exact extent of the energetically allowed region of the section (θ1 = 0): // p2² ≤ 2·m2·l2²·(E − V(θ2)), so |p2|max = √(2e) in normalized units, // and |θ2| ≤ acos(1 − E/(m2·g·l2)). const en = Math.max(1e-6, e); const E = en * vTop; const th = E >= 2 * cfg.m2 * cfg.g * cfg.l2 ? Math.PI : Math.acos(1 - E / (cfg.m2 * cfg.g * cfg.l2)); thT = Math.min(Math.PI, 1.08 * th); pT = 1.08 * Math.sqrt(2 * en); } // ease toward the target range with a time constant, in log space so zooming in and out feel alike const ease = dtw > 0 && dtw < 0.5 ? 1 - Math.exp(-dtw / Math.max(0.01, P.zoomSmooth as number)) : 1; this.thR *= Math.exp(ease * Math.log(thT / this.thR)); this.pR *= Math.exp(ease * Math.log(pT / this.pR)); const pad = 8; const kx = (w / 2 - pad) / this.thR, ky = (h / 2 - pad) / this.pR; g.strokeStyle = dim; g.lineWidth = 1; g.beginPath(); g.moveTo(pad, h / 2); g.lineTo(w - pad, h / 2); g.moveTo(w / 2, pad); g.lineTo(w / 2, h - pad); g.stroke(); return { g, w, h, sx: (th) => w / 2 + kx * th, sy: (p) => h / 2 - ky * p }; } } /** A crossing dot that fades with age, with a brief expanding ring when new. */ export function drawCrossing(f: SectionFrame, th2: number, p2: number, age: number, fade: number, size: number, color: string) { const { g, sx, sy } = f; g.fillStyle = color; g.globalAlpha = Math.exp(-age / fade); g.beginPath(); g.arc(sx(th2), sy(p2), size / 2, 0, 2 * Math.PI); g.fill(); if (age < 0.4) { g.globalAlpha = 1 - age / 0.4; g.strokeStyle = color; g.beginPath(); g.arc(sx(th2), sy(p2), size / 2 + 3 + 12 * age, 0, 2 * Math.PI); g.stroke(); } g.globalAlpha = 1; }