// Time-trace plots for the physics view: long channels drawn as min/max per pixel column, a // playhead, shaded dense-recording window, and click/drag to seek. export interface Trace { data: Float32Array; /** sample j is at t = t0 + j dt */ dt: number; t0?: number; color: string; label: string; /** map a sample value to the plotted value (e.g. dB) */ map?: (v: number) => number; } export interface TraceAxes { t0: number; t1: number; ymin?: number; ymax?: number; ylabel: string; playhead: number; shade?: [number, number] | null; marks?: { t: number; label: string }[] } const css = (name: string) => getComputedStyle(document.documentElement).getPropertyValue(name).trim() || "#888"; const 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`); export function tracePlot(c: HTMLCanvasElement, traces: Trace[], ax: TraceAxes): void { const dpr = window.devicePixelRatio || 1; const w = c.clientWidth, h = c.clientHeight; 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); } const ctx = c.getContext("2d")!; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, w, h); const padL = 48, padR = 8, padT = 14, padB = 16; const pw = w - padL - padR, ph = h - padT - padB; const { t0, t1 } = ax; const X = (t: number) => padL + (pw * (t - t0)) / (t1 - t0); // per-column min/max const cols = Math.max(1, Math.floor(pw)); const env = traces.map((tr) => { const lo = new Float32Array(cols).fill(Infinity), hi = new Float32Array(cols).fill(-Infinity); const tb = tr.t0 ?? 0, n = tr.data.length, map = tr.map; for (let px = 0; px < cols; px++) { const ta = t0 + ((t1 - t0) * px) / cols, tc = t0 + ((t1 - t0) * (px + 1)) / cols; let j0 = Math.floor((ta - tb) / tr.dt), j1 = Math.ceil((tc - tb) / tr.dt); j0 = Math.max(0, j0); j1 = Math.min(n - 1, Math.max(j1, j0 + 1)); if (j0 >= n) break; 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; } } return { lo, hi }; }); let ymin = ax.ymin ?? Infinity, ymax = ax.ymax ?? -Infinity; if (ax.ymin === undefined || ax.ymax === undefined) for (const e of env) for (let px = 0; px < cols; px++) { if (ax.ymin === undefined && isFinite(e.lo[px])) ymin = Math.min(ymin, e.lo[px]); if (ax.ymax === undefined && isFinite(e.hi[px])) ymax = Math.max(ymax, e.hi[px]); } if (!isFinite(ymin) || !isFinite(ymax)) { ymin = -1; ymax = 1; } if (ymax - ymin < 1e-30) { ymax += 1e-12; ymin -= 1e-12; } const pad = 0.06 * (ymax - ymin); if (ax.ymax === undefined) ymax += pad; if (ax.ymin === undefined) ymin -= pad; const Y = (v: number) => padT + ph * (1 - (v - ymin) / (ymax - ymin)); ctx.font = "11px system-ui, sans-serif"; // shaded dense window if (ax.shade) { const a = Math.max(padL, X(ax.shade[0])), b = Math.min(padL + pw, X(ax.shade[1])); if (b > a) { ctx.fillStyle = css("--grid"); ctx.fillRect(a, padT, b - a, ph); } } // grid + ticks ctx.strokeStyle = css("--grid"); ctx.fillStyle = css("--muted"); ctx.lineWidth = 1; const ticks = (lo: number, hi: number, n: number) => { const span = hi - lo, st0 = span / n, mag = Math.pow(10, Math.floor(Math.log10(st0))); const st = [1, 2, 5, 10].map((m) => m * mag).find((s) => s >= st0) ?? mag * 10; const out: number[] = []; for (let v = Math.ceil(lo / st) * st; v <= hi + 1e-9 * st; v += st) out.push(+v.toPrecision(12)); return out; }; for (const t of ticks(t0, t1, 8)) { const x = X(t); ctx.beginPath(); ctx.moveTo(x, padT); ctx.lineTo(x, padT + ph); ctx.stroke(); ctx.textAlign = "center"; ctx.fillText(fmtT(t, t1 - t0), x, h - 3); } for (const v of ticks(ymin, ymax, 4)) { const y = Y(v); ctx.beginPath(); ctx.moveTo(padL, y); ctx.lineTo(padL + pw, y); ctx.stroke(); 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); } ctx.textAlign = "left"; ctx.fillText(`${ax.ylabel} (t in ${t1 - t0 < 2 ? "ms" : "s"})`, padL, 10); // traces ctx.save(); ctx.beginPath(); ctx.rect(padL, padT, pw, ph); ctx.clip(); for (const m of ax.marks ?? []) { if (m.t < t0 || m.t > t1) continue; ctx.strokeStyle = css("--mark"); ctx.setLineDash([2, 3]); ctx.beginPath(); ctx.moveTo(X(m.t), padT); ctx.lineTo(X(m.t), padT + ph); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = css("--muted"); ctx.fillText(m.label, X(m.t) + 3, padT + 10); } traces.forEach((tr, q) => { const { lo, hi } = env[q]; ctx.strokeStyle = tr.color; ctx.fillStyle = tr.color; ctx.lineWidth = 1.3; ctx.beginPath(); let pen = false; for (let px = 0; px < cols; px++) { if (!isFinite(lo[px])) { pen = false; continue; } const x = padL + px + 0.5, ya = Y(hi[px]), yb = Y(lo[px]); if (pen) ctx.lineTo(x, (ya + yb) / 2); else { ctx.moveTo(x, (ya + yb) / 2); pen = true; } if (yb - ya > 1.5) { ctx.moveTo(x, ya); ctx.lineTo(x, yb); ctx.moveTo(x, (ya + yb) / 2); } } ctx.stroke(); }); // playhead if (ax.playhead >= t0 && ax.playhead <= t1) { ctx.strokeStyle = css("--accent"); ctx.lineWidth = 1.5; ctx.beginPath(); ctx.moveTo(X(ax.playhead), padT); ctx.lineTo(X(ax.playhead), padT + ph); ctx.stroke(); } ctx.restore(); // legend, right-aligned above the plot ctx.font = "11px system-ui, sans-serif"; let lx = w - padR - traces.reduce((s, tr) => s + 22 + ctx.measureText(tr.label).width, 0); for (const tr of traces) { ctx.fillStyle = tr.color; ctx.fillRect(lx, 4, 10, 3); ctx.fillStyle = css("--fg"); ctx.textAlign = "left"; ctx.fillText(tr.label, lx + 13, 10); lx += 22 + ctx.measureText(tr.label).width; } } /** Time under the pointer for a plot drawn with tracePlot over [t0, t1]. */ export function traceTimeAt(c: HTMLCanvasElement, clientX: number, t0: number, t1: number): number { const r = c.getBoundingClientRect(); const padL = 48, padR = 8, pw = r.width - padL - padR; return t0 + ((t1 - t0) * (clientX - r.left - padL)) / pw; }