// THE CUE PAGE (release 39, the video modules): the one shape a small chrome // page takes -- markup whose every element starts hidden, and a list of cues // each stating its FROM, played by a paused GSAP timeline a renderer seeks // frame by frame. chrome-stats.mjs is the pattern; the pair, chapter and // status modules write their pages through this file so they do not each // carry a copy of the interpreter (and the frame-key model reads all three // the same way: frame-keys.mjs, `frameKeys` on each module). // // Two placements: // - WHOLE: a page as long as the cut (or a window of it, `from`/`duration`), // its data `{ total, window, ids, init, cues }`. // - WINDOW: a page for one window of the cut, its data `{ from, dur, init, // cues }`, the cues in the cut's clock (the frame keys' `offset: "from"`). // // PURE: data in, an HTML string out. import { pageDuration } from "./deck.mjs"; import { settleFirstFrame } from "./cue-start.mjs"; /** Instant cues still take a millisecond: a zero-duration tween has its own seek rules. */ export const INSTANT = 0.001; export const r4 = (v) => Math.round(v * 10000) / 10000; export const esc = (s) => String(s ?? "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); /** * Events -> cues: ordered by time, clamped so a cue never starts before the * last on its element ends, every FROM stated (the last TO on its element, * or its init). `events` are `{ k, at, dur, to, ease }`. * * @returns {{ init: Record, cues: Array<{ k: string, at: number, dur: number, from: object, to: object, ease: string }> }} */ export function compileCues(init, events) { const ev = events.map((e, n) => ({ ...e, at: r4(e.at), dur: r4(Math.max(INSTANT, e.dur)), 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) }; } /** * Show `k` over [a, b]: in over `fadeIn`, out over `fadeOut` before b (each * at most half the span). The events compileCues takes. */ export function spanEvents(k, a, b, { fadeIn = 0.35, fadeOut = 0.3, prop = "autoAlpha" } = {}) { const len = Math.max(0, b - a); if (!(len > 0)) return []; return [ { k, at: a, dur: Math.min(fadeIn, len / 2), to: { [prop]: 1 }, ease: "power2.out" }, { k, at: Math.max(a, b - fadeOut), dur: Math.min(fadeOut, len / 2), to: { [prop]: 0 }, ease: "power1.in" }, ]; } /** Spans `{in, out}` with the ones that touch (or overlap) joined. */ export function joinSpans(spans, gap = 1e-6) { const out = []; for (const s of [...spans].sort((x, y) => x.in - y.in)) { const last = out[out.length - 1]; if (last && s.in - last.out <= gap) last.out = Math.max(last.out, s.out); else out.push({ in: s.in, out: s.out }); } return out; } /** The deck's two faces, under the deck's private names (docs/quirks.md). */ function fontFaces(fonts) { return `@font-face { font-family: 'DeckSans'; font-weight: 400; font-style: normal; src: url('${esc(fonts.regular ?? "")}'); } @font-face { font-family: 'DeckSansBold'; font-weight: 400; font-style: normal; src: url('${esc(fonts.bold ?? "")}'); }`; } /** * A cue page's HTML. * * @param {{ id: string, W: number, H: number, fps: number, fonts?: {regular?: string, bold?: string}, * gsapSrc?: string, css: string, body: string, init: object, cues: object[], * whole?: { total: number, from?: number, duration?: number | null, ids?: string[] }, * window?: { from: number, dur: number }, primeWindow?: boolean, fit?: { loads: string[], run: string } }} p * * `fit` (opt-in, the gap mark's): `{ loads, run }` -- once the faces in * `loads` (FontFace load strings, "26px DeckSans") are in, `run` (script) runs * before the page says it is fitted: what a page measures in its own faces. * Without it the page is what it always was. * * `primeWindow` (opt-in, the measure extras'): a WINDOW of a whole page is * rendered once at its end and back at its start before anything seeks it. A * window's timeline seeked to exactly 0 -- the renderer's first frame -- is * no change from where it stands, so GSAP renders nothing and the frame shows * init (everything hidden) instead of the cut's state at `from`; with frame * keys, every frame keyed like it is reused from that empty capture. */ export function cuePageHtml(p) { const { id, W, H, fps, css, body, init, cues } = p; const fonts = p.fonts ?? {}; const gsapSrc = p.gsapSrc ?? "assets/gsap.min.js"; let data; let dur; if (p.window) { dur = r4(p.window.dur); data = { from: r4(p.window.from), dur, init, cues }; } else { const total = p.whole.total; const from = Number(p.whole.from ?? 0); dur = p.whole.duration != null ? r4(Number(p.whole.duration)) : r4(total - from); if (!(dur > 0)) throw new Error(`${id}: nothing to render from ${from}s of a ${total}s cut`); const windowed = from > 0 || Math.abs(dur - total) > 1e-6; data = { total: r4(total), window: windowed ? { from: r4(from), dur } : null, ids: p.whole.ids ?? [], init, cues }; } const json = JSON.stringify(data).replace(/ Math.max(0, Math.min(S.dur, (Number(t) || 0) - S.from));` : `inner.set({}, {}, S.total); let tl = inner; if (S.window) { tl = gsap.timeline({ paused: true }); tl.add(inner.tweenFromTo(S.window.from, S.window.from + S.window.dur, { duration: S.window.dur, ease: "none" }), 0); } const local = (t) => { const v = Number(t) || 0; return S.window ? Math.max(0, Math.min(S.window.dur, v - S.window.from)) : Math.max(0, v); };`; return `
${body}
`; } /** * Each segment's span in the cut, by id: `{ in, out }` -- its start to its * end less the dissolve into the next (the frame a viewer reads it on). */ export function segmentSpans(schedule) { const out = new Map(); for (const s of schedule?.segments ?? []) out.set(s.id, { in: s.start, out: s.end }); return out; } /** * A date as the cut writes it: "2012" | "Sep 2012" | "29 Sep 2012" from * YYYY, YYYY-MM or YYYY-MM-DD (`format` "iso" keeps it; "year" the year). * Anything else is written as given. */ export function dateText(d, format = "long") { if (d === undefined || d === null || d === "") return ""; const s = String(d); const m = /^(\d{4})(?:-(\d{2}))?(?:-(\d{2}))?$/.exec(s); if (!m || format === "iso") return s; if (format === "year") return m[1]; const MON = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; if (!m[2]) return m[1]; const mon = MON[Number(m[2]) - 1] ?? m[2]; return m[3] ? `${Number(m[3])} ${mon} ${m[1]}` : `${mon} ${m[1]}`; } /** A YYYY / YYYY-MM / YYYY-MM-DD date (what pair, chapter and status take). */ export const DATE_RE = /^\d{4}(?:-(?:0[1-9]|1[0-2])(?:-(?:0[1-9]|[12]\d|3[01]))?)?$/;