1// Time-trace plots for the physics view: long channels drawn as min/max per pixel column, a
2// playhead, shaded dense-recording window, and click/drag to seek.
4export interface Trace {
5 data: Float32Array;
6 /** sample j is at t = t0 + j dt */
7 dt: number;
8 t0?: number;
9 color: string;
10 label: string;
11 /** map a sample value to the plotted value (e.g. dB) */
12 map?: (v: number) => number;
13}
15export interface TraceAxes { t0: number; t1: number; ymin?: number; ymax?: number; ylabel: string; playhead: number; shade?: [number, number] | null; marks?: { t: number; label: string }[] }
17const css = (name: string) => getComputedStyle(document.documentElement).getPropertyValue(name).trim() || "#888";
19const fmtT = (t: number, span: number) => (span < 0.02 ? `${(t * 1000).toFixed(span < 0.002 ? 2 : 1)}` : span < 2 ? `${(t * 1000).toFixed(0)}` : `${t.toFixed(1)}s`);
21export function tracePlot(c: HTMLCanvasElement, traces: Trace[], ax: TraceAxes): void {
22 const dpr = window.devicePixelRatio || 1;
23 const w = c.clientWidth, h = c.clientHeight;
24 if (c.width !== Math.round(w * dpr) || c.height !== Math.round(h * dpr)) { c.width = Math.round(w * dpr); c.height = Math.round(h * dpr); }
25 const ctx = c.getContext("2d")!;
26 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
27 ctx.clearRect(0, 0, w, h);
28 const padL = 48, padR = 8, padT = 14, padB = 16;
29 const pw = w - padL - padR, ph = h - padT - padB;
30 const { t0, t1 } = ax;
31 const X = (t: number) => padL + (pw * (t - t0)) / (t1 - t0);
32 // per-column min/max
33 const cols = Math.max(1, Math.floor(pw));
34 const env = traces.map((tr) => {
35 const lo = new Float32Array(cols).fill(Infinity), hi = new Float32Array(cols).fill(-Infinity);
36 const tb = tr.t0 ?? 0, n = tr.data.length, map = tr.map;
37 for (let px = 0; px < cols; px++) {
38 const ta = t0 + ((t1 - t0) * px) / cols, tc = t0 + ((t1 - t0) * (px + 1)) / cols;
39 let j0 = Math.floor((ta - tb) / tr.dt), j1 = Math.ceil((tc - tb) / tr.dt);
40 j0 = Math.max(0, j0); j1 = Math.min(n - 1, Math.max(j1, j0 + 1));
41 if (j0 >= n) break;
42 for (let j = j0; j <= j1; j++) { const v = map ? map(tr.data[j]) : tr.data[j]; if (!isFinite(v)) continue; if (v < lo[px]) lo[px] = v; if (v > hi[px]) hi[px] = v; }
43 }
44 return { lo, hi };
45 });
46 let ymin = ax.ymin ?? Infinity, ymax = ax.ymax ?? -Infinity;
47 if (ax.ymin === undefined || ax.ymax === undefined)
48 for (const e of env) for (let px = 0; px < cols; px++) {
49 if (ax.ymin === undefined && isFinite(e.lo[px])) ymin = Math.min(ymin, e.lo[px]);
50 if (ax.ymax === undefined && isFinite(e.hi[px])) ymax = Math.max(ymax, e.hi[px]);
51 }
52 if (!isFinite(ymin) || !isFinite(ymax)) { ymin = -1; ymax = 1; }
53 if (ymax - ymin < 1e-30) { ymax += 1e-12; ymin -= 1e-12; }
54 const pad = 0.06 * (ymax - ymin);
55 if (ax.ymax === undefined) ymax += pad;
56 if (ax.ymin === undefined) ymin -= pad;
57 const Y = (v: number) => padT + ph * (1 - (v - ymin) / (ymax - ymin));
58 ctx.font = "11px system-ui, sans-serif";
59 // shaded dense window
60 if (ax.shade) {
61 const a = Math.max(padL, X(ax.shade[0])), b = Math.min(padL + pw, X(ax.shade[1]));
62 if (b > a) { ctx.fillStyle = css("--grid"); ctx.fillRect(a, padT, b - a, ph); }
63 }
64 // grid + ticks
65 ctx.strokeStyle = css("--grid"); ctx.fillStyle = css("--muted"); ctx.lineWidth = 1;
66 const ticks = (lo: number, hi: number, n: number) => {
67 const span = hi - lo, st0 = span / n, mag = Math.pow(10, Math.floor(Math.log10(st0)));
68 const st = [1, 2, 5, 10].map((m) => m * mag).find((s) => s >= st0) ?? mag * 10;
69 const out: number[] = [];
70 for (let v = Math.ceil(lo / st) * st; v <= hi + 1e-9 * st; v += st) out.push(+v.toPrecision(12));
71 return out;
72 };
73 for (const t of ticks(t0, t1, 8)) {
74 const x = X(t);
75 ctx.beginPath(); ctx.moveTo(x, padT); ctx.lineTo(x, padT + ph); ctx.stroke();
76 ctx.textAlign = "center"; ctx.fillText(fmtT(t, t1 - t0), x, h - 3);
77 }
78 for (const v of ticks(ymin, ymax, 4)) {
79 const y = Y(v);
80 ctx.beginPath(); ctx.moveTo(padL, y); ctx.lineTo(padL + pw, y); ctx.stroke();
81 ctx.textAlign = "right"; ctx.fillText(Math.abs(v) >= 1000 || (Math.abs(v) < 0.01 && v !== 0) ? v.toExponential(0) : String(+v.toPrecision(3)), padL - 4, y + 4);
82 }
83 ctx.textAlign = "left"; ctx.fillText(`${ax.ylabel} (t in ${t1 - t0 < 2 ? "ms" : "s"})`, padL, 10);
84 // traces
85 ctx.save();
86 ctx.beginPath(); ctx.rect(padL, padT, pw, ph); ctx.clip();
87 for (const m of ax.marks ?? []) {
88 if (m.t < t0 || m.t > t1) continue;
89 ctx.strokeStyle = css("--mark"); ctx.setLineDash([2, 3]);
90 ctx.beginPath(); ctx.moveTo(X(m.t), padT); ctx.lineTo(X(m.t), padT + ph); ctx.stroke(); ctx.setLineDash([]);
91 ctx.fillStyle = css("--muted"); ctx.fillText(m.label, X(m.t) + 3, padT + 10);
92 }
93 traces.forEach((tr, q) => {
94 const { lo, hi } = env[q];
95 ctx.strokeStyle = tr.color; ctx.fillStyle = tr.color; ctx.lineWidth = 1.3;
96 ctx.beginPath();
97 let pen = false;
98 for (let px = 0; px < cols; px++) {
99 if (!isFinite(lo[px])) { pen = false; continue; }
100 const x = padL + px + 0.5, ya = Y(hi[px]), yb = Y(lo[px]);
101 if (pen) ctx.lineTo(x, (ya + yb) / 2); else { ctx.moveTo(x, (ya + yb) / 2); pen = true; }
102 if (yb - ya > 1.5) { ctx.moveTo(x, ya); ctx.lineTo(x, yb); ctx.moveTo(x, (ya + yb) / 2); }
103 }
104 ctx.stroke();
105 });
106 // playhead
107 if (ax.playhead >= t0 && ax.playhead <= t1) {
108 ctx.strokeStyle = css("--accent"); ctx.lineWidth = 1.5;
109 ctx.beginPath(); ctx.moveTo(X(ax.playhead), padT); ctx.lineTo(X(ax.playhead), padT + ph); ctx.stroke();
110 }
111 ctx.restore();
112 // legend, right-aligned above the plot
113 ctx.font = "11px system-ui, sans-serif";
114 let lx = w - padR - traces.reduce((s, tr) => s + 22 + ctx.measureText(tr.label).width, 0);
115 for (const tr of traces) {
116 ctx.fillStyle = tr.color; ctx.fillRect(lx, 4, 10, 3);
117 ctx.fillStyle = css("--fg"); ctx.textAlign = "left"; ctx.fillText(tr.label, lx + 13, 10);
118 lx += 22 + ctx.measureText(tr.label).width;
119 }
120}
122/** Time under the pointer for a plot drawn with tracePlot over [t0, t1]. */
123export function traceTimeAt(c: HTMLCanvasElement, clientX: number, t0: number, t1: number): number {
124 const r = c.getBoundingClientRect();
125 const padL = 48, padR = 8, pw = r.width - padL - padR;
126 return t0 + ((t1 - t0) * (clientX - r.left - padL)) / pw;
127}