// The STATS region's composition (release 31, lane P3): one HyperFrames page // for a whole report cut, a small transparent region in a corner of the // footage box (deck.mjs statsGeometry) where a frame scan's running numbers // -- the stack, big blinds, players left, the level -- sit over each clip // that declares them, each number held until the scan's next point. // // PURE, like chrome-stamp.mjs: a schedule and a render block in, an HTML // string out. compose-chrome.mjs copies the assets in beside it, writes it and // renders it (`region: "stats"`); the build lays the frames over the cut as // it lays the stamps'. // // The timeline is cues, computed here, each stating its from -- a render is a // seek per frame, from parallel workers, in any order (chrome-deck.mjs says // why at length). Each STATE of a clip (its numbers between two points) is // its own block, shown over exactly its span: no text is rewritten by a // callback, so a seek backwards lands on the same pixels as one forwards. // The times are stats.mjs `statsSchedule`'s, untouched. import { pageDuration, statsGeometry } from "./deck.mjs"; import { rgba } from "./chrome-deck.mjs"; import { STATS_LABELS, STATS_MOTION } from "./stats.mjs"; import { settleFirstFrame } from "./cue-start.mjs"; /** Instant cues still take a millisecond: a zero-duration tween has its own seek rules. */ const INSTANT = 0.001; const esc = (s) => String(s ?? "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); const r4 = (v) => Math.round(v * 10000) / 10000; /** * A number as the panel shows it: chips grouped under 100,000, then K and M * ("323.2K", "1.25M"); big blinds to a tenth under 100; the rest whole. A * field not read yet is a dash. */ export function statsValue(field, v) { if (v === null || v === undefined || !Number.isFinite(v)) return "—"; const trim = (x, d) => x.toFixed(d).replace(/\.0+$/, "").replace(/(\.\d*?)0+$/, "$1"); if (field === "stack") { if (v >= 1e6) return `${trim(v / 1e6, 2)}M`; if (v >= 1e5) return `${trim(v / 1e3, 1)}K`; return Math.round(v).toLocaleString("en-US"); } if (field === "stackBB") return v >= 100 ? String(Math.round(v)) : trim(v, 1); return Math.round(v).toLocaleString("en-US"); } /** * The panel's spans: each clip's [in, out], with clips that touch joined, so * the panel stays up across a cut between two clips that both carry it. */ export function panelSpans(clips) { const out = []; for (const c of clips) { const last = out[out.length - 1]; if (last && c.in - last.out <= 1e-6) last.out = c.out; else out.push({ in: c.in, out: c.out }); } return out; } /** * Everything the timeline does, as data. The panel is `pn` (autoAlpha), in * over STATS_MOTION.in at each span's start, out over STATS_MOTION.out before * its end; state j of clip i is `s_` (autoAlpha), shown instantly at * its `at` and hidden at the next state's (or its clip's out). * * @returns {{ init: Record, cues: Array<{ k: string, at: number, * dur: number, from: object, to: object, ease: string }> }} */ export function statsCues(schedule) { const clips = schedule.stats?.clips ?? []; const m = STATS_MOTION; const init = { pn: { autoAlpha: 0 } }; const ev = []; const add = (k, at, dur, to, ease) => ev.push({ k, at: r4(at), dur: r4(Math.max(INSTANT, dur)), to, ease }); for (const sp of panelSpans(clips)) { add("pn", sp.in, Math.min(m.in, (sp.out - sp.in) / 2), { autoAlpha: 1 }, "power2.out"); add("pn", Math.max(sp.in, sp.out - m.out), Math.min(m.out, (sp.out - sp.in) / 2), { autoAlpha: 0 }, "power1.in"); } clips.forEach((c, i) => { c.states.forEach((st, j) => { const k = `s${i}_${j}`; init[k] = { autoAlpha: 0 }; const until = c.states[j + 1]?.at ?? c.out; add(k, st.at, INSTANT, { autoAlpha: 1 }, "none"); add(k, until, INSTANT, { autoAlpha: 0 }, "none"); }); }); // Order, clamp (a cue never starts before the last on its element ends), // and state every from: the last to on its element, or its init. ev.forEach((e, n) => { e.n = n; }); ev.sort((a, b) => a.at - b.at || a.n - b.n); const state = Object.fromEntries(Object.entries(init).map(([k, v]) => [k, { ...v }])); const freeAt = new Map(); const cues = []; for (const e of ev) { let { at, dur } = e; const free = freeAt.get(e.k) ?? 0; if (at < free) { const end = at + dur; at = r4(free); dur = r4(Math.max(INSTANT, end - at)); } const cur = state[e.k] ?? (state[e.k] = {}); const from = {}; for (const p of Object.keys(e.to)) from[p] = cur[p]; Object.assign(cur, e.to); freeAt.set(e.k, r4(at + dur)); cues.push({ k: e.k, at, dur, from, to: e.to, ease: e.ease }); } return { init, cues: settleFirstFrame(init, cues, schedule.fps) }; } /** * The stats composition's HTML, for the whole cut -- or a window of it * (`from`/`duration`, as the deck's). `fonts` = `{ regular, bold }` * asset-relative paths (DeckSans / DeckSansBold); `gsap` the vendored script. * The region is `statsGeometry(render, { feed })`, region-local, transparent * outside the panel. Colours are the cut's palette roles (bg, fg, muted, * accent). `?still=` and the preview's `deck:seek` take CUT seconds. */ export function statsHtml(schedule, render, opts = {}) { const st = schedule.stats; if (!st?.clips?.length) throw new Error("stats: the schedule draws no clip's stats"); const geo = statsGeometry(render, { feed: schedule.layout === "feed" }); const pal = render.palette; const W = geo.width, H = geo.height; const k = (render.width ?? 1920) / 1920; const px = (v) => Math.max(1, Math.round(v * k)); const fonts = opts.fonts ?? {}; const gsapSrc = opts.gsap ?? "assets/gsap.min.js"; const total = schedule.total; const from = Number(opts.from ?? 0); const dur = opts.duration != null ? r4(Number(opts.duration)) : r4(total - from); if (!(dur > 0)) throw new Error(`stats: nothing to render from ${from}s of a ${total}s cut`); const windowed = from > 0 || Math.abs(dur - total) > 1e-6; const { init, cues } = statsCues(schedule); const fields = st.fields; // The cells share the panel's width, the stack's a third wider (its // number is the longest: "323.2K", "41,000"). const pad = px(24); const weight = (f) => (f === "stack" ? 1.3 : 1); const unit = (W - 2 * pad) / fields.reduce((n, f) => n + weight(f), 0); const cellW = (f) => Math.floor(unit * weight(f)); const states = st.clips .flatMap((c, i) => c.states.map((s, j) => { const cells = fields .map((f) => `
${esc(STATS_LABELS[f] ?? f)}
${esc(statsValue(f, s.values[f]))}
`) .join(""); const name = st.name && c.name ? `
${esc(c.name)}
` : ""; return `
${name}
${cells}
`; }), ) .join("\n "); const data = { total: r4(total), window: windowed ? { from: r4(from), dur } : null, ids: st.clips.map((c) => c.segment), init, cues, }; // `` inside a JSON string would close the tag; a name is the scan's. const json = JSON.stringify(data).replace(/
${states}
`; }