// THE CHAT PAGE (module `chat`, modules/chat.mjs): a column beside the footage // that scrolls a stream's chat for the clips that ask for it. A cue page // (chrome-cues.mjs): each STATE of the column (the last `lines` messages up to // the newest) is its own block, shown over exactly its span, so a changed // message recaptures only the frames it is up. PURE: a schedule and a render // block in, geometry and HTML out. import { rgba } from "./chrome-deck.mjs"; import { compileCues, cuePageHtml, esc, INSTANT, joinSpans, spanEvents } from "./chrome-cues.mjs"; import { chatStates, resolveChat } from "./chat.mjs"; import { footageBox } from "./chrome-story.mjs"; const scaleOf = (render) => (render?.width ?? 1920) / 1920; const pxOf = (render) => (v) => Math.max(1, Math.round(v * scaleOf(render))); const even = (v) => 2 * Math.round(v / 2); /** The chat column's region: a strip down one side of the footage box, `width` wide. */ export function chatGeometry(render, { feed = false } = {}) { const f = footageBox(render, { feed }); const C = resolveChat(render); const px = pxOf(render); const inset = px(18); const width = even(Math.min(px(C.width), f.width - 2 * inset)); const height = even(f.height - 2 * inset); return { x: C.position === "left" ? f.x + inset : f.x + f.width - inset - width, y: f.y + inset, width, height, }; } /** The chat page: `{ html, items }`, each state an item gated by its own autoAlpha. */ export function chatHtml(schedule, render, opts = {}) { const C = resolveChat(render); const clips = schedule?.chat?.clips ?? []; const lines = schedule?.chat?.lines ?? C.lines; const states = clips.flatMap((c) => chatStates(c, lines)); if (!states.length) throw new Error("chat: the cut plays none of the chat's clips"); const geo = chatGeometry(render, { feed: schedule.layout === "feed" }); const pal = render.palette; const px = pxOf(render); const fps = schedule.fps ?? render.fps ?? 30; const init = { chat: { autoAlpha: 0 } }; const ev = []; // The column is up over each clip's whole span, even between its messages. for (const sp of joinSpans(clips.map((c) => ({ in: c.in, out: c.out })), 1e-3)) { ev.push(...spanEvents("chat", sp.in, sp.out, { fadeIn: 0.35, fadeOut: 0.35 })); } const items = []; const blocks = states.map((st, j) => { const k = `s${j}`; init[k] = { autoAlpha: 0 }; ev.push({ k, at: st.at, dur: INSTANT, to: { autoAlpha: 1 }, ease: "none" }); ev.push({ k, at: st.until, dur: INSTANT, to: { autoAlpha: 0 }, ease: "none" }); const rows = st.rows.map((m, i) => { const age = st.rows.length - 1 - i; const dim = age >= 6 ? " old" : ""; return `