5c043e1Add composition 2: orbitsJeremy Magland 1// Drawing helpers for the Poincaré section plot: canvas sizing, the "fit energy" zoom, and axes.
2import type { Params } from "../../core/params.ts";
3import type { PendulumConfig } from "../../physics/doublePendulum.ts";
5export function fitCanvas(c: HTMLCanvasElement): CanvasRenderingContext2D {
6 const dpr = window.devicePixelRatio || 1;
7 const w = Math.round(c.clientWidth * dpr);
8 const h = Math.round(c.clientHeight * dpr);
9 if (c.width !== w || c.height !== h) {
10 c.width = w;
11 c.height = h;
12 }
13 const g = c.getContext("2d")!;
14 g.setTransform(dpr, 0, 0, dpr, 0, 0);
15 return g;
16}
18export function cssColor(el: Element, name: string, fallback = "#ccc"): string {
19 return getComputedStyle(el).getPropertyValue(name).trim() || fallback;
20}
22export interface SectionFrame {
23 g: CanvasRenderingContext2D;
24 w: number;
25 h: number;
26 sx: (th2: number) => number;
27 sy: (p2: number) => number;
28}
30/** The section's axes, with a zoom that eases toward the energetically allowed region. */
31export class SectionPlot {
32 private thR = Math.PI; // half-ranges (smoothed)
33 private pR = 1;
34 private lastWall = 0;
35 canvas: HTMLCanvasElement;
36 constructor(canvas: HTMLCanvasElement) {
37 this.canvas = canvas;
38 }
40 /**
41 * Clear, update the zoom, draw the axes; returns the coordinate mapping.
42 * e: normalized energy that sets the zoom (with "fit energy"); cfg/vTop: the pendulum's.
43 */
44 begin(P: Params, e: number | null, cfg: PendulumConfig | null, vTop: number, dim: string): SectionFrame {
45 const wall = performance.now() / 1000;
46 const dtw = wall - this.lastWall;
47 this.lastWall = wall;
48 const c = this.canvas;
49 const g = fitCanvas(c);
50 const w = c.clientWidth, h = c.clientHeight;
51 g.clearRect(0, 0, w, h);
52 let thT = Math.PI, pT = P.plotP as number;
53 if (P.plotZoom === "fit energy" && e !== null && cfg) {
54 // exact extent of the energetically allowed region of the section (θ1 = 0):
55 // p2² ≤ 2·m2·l2²·(E − V(θ2)), so |p2|max = √(2e) in normalized units,
56 // and |θ2| ≤ acos(1 − E/(m2·g·l2)).
57 const en = Math.max(1e-6, e);
58 const E = en * vTop;
59 const th = E >= 2 * cfg.m2 * cfg.g * cfg.l2 ? Math.PI : Math.acos(1 - E / (cfg.m2 * cfg.g * cfg.l2));
60 thT = Math.min(Math.PI, 1.08 * th);
61 pT = 1.08 * Math.sqrt(2 * en);
62 }
63 // ease toward the target range with a time constant, in log space so zooming in and out feel alike
64 const ease = dtw > 0 && dtw < 0.5 ? 1 - Math.exp(-dtw / Math.max(0.01, P.zoomSmooth as number)) : 1;
65 this.thR *= Math.exp(ease * Math.log(thT / this.thR));
66 this.pR *= Math.exp(ease * Math.log(pT / this.pR));
67 const pad = 8;
68 const kx = (w / 2 - pad) / this.thR, ky = (h / 2 - pad) / this.pR;
69 g.strokeStyle = dim;
70 g.lineWidth = 1;
71 g.beginPath();
72 g.moveTo(pad, h / 2);
73 g.lineTo(w - pad, h / 2);
74 g.moveTo(w / 2, pad);
75 g.lineTo(w / 2, h - pad);
76 g.stroke();
77 return { g, w, h, sx: (th) => w / 2 + kx * th, sy: (p) => h / 2 - ky * p };
78 }
79}
81/** A crossing dot that fades with age, with a brief expanding ring when new. */
82export function drawCrossing(f: SectionFrame, th2: number, p2: number, age: number, fade: number, size: number, color: string) {
83 const { g, sx, sy } = f;
84 g.fillStyle = color;
85 g.globalAlpha = Math.exp(-age / fade);
86 g.beginPath();
87 g.arc(sx(th2), sy(p2), size / 2, 0, 2 * Math.PI);
88 g.fill();
89 if (age < 0.4) {
90 g.globalAlpha = 1 - age / 0.4;
91 g.strokeStyle = color;
92 g.beginPath();
93 g.arc(sx(th2), sy(p2), size / 2 + 3 + 12 * age, 0, 2 * Math.PI);
94 g.stroke();
95 }
96 g.globalAlpha = 1;
97}