// 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 `
${m.author ? `${esc(m.author)} ` : ""}${esc(m.text)}
`; }).join(""); const html = `
${rows}
`; items.push({ key: k, gate: [k, "autoAlpha"], html }); return html; }); const { init: i2, cues } = compileCues(init, ev); const css = ` #chat { position: absolute; inset: 0; visibility: hidden; opacity: 0; } /* Each state is a column of its own, bottom-aligned: the newest message at the foot, the oldest pushed off the top. */ .st { position: absolute; inset: 0; visibility: hidden; opacity: 0; display: flex; flex-direction: column; justify-content: flex-end; gap: ${px(6)}px; overflow: hidden; -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 ${px(70)}px); mask-image: linear-gradient(to bottom, transparent 0, #000 ${px(70)}px); } .m { flex: none; padding: ${px(7)}px ${px(12)}px; border-radius: ${px(8)}px; background: ${rgba(pal.bg, 0.78)}; font-size: ${px(22)}px; line-height: ${px(28)}px; color: ${pal.fg}; overflow: hidden; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; max-height: ${px(4 * 28 + 14)}px; } .m.old { opacity: 0.72; } .au { font-family: 'DeckSansBold', sans-serif; color: ${pal.accent}; } .tx { color: ${pal.fg}; }`; const body = `
${blocks.join("\n ")}
`; const html = cuePageHtml({ id: "chat", W: geo.width, H: geo.height, fps, fonts: opts.fonts, gsapSrc: opts.gsap, css, body, init: i2, cues, whole: { total: schedule.total, from: opts.from, duration: opts.duration, ids: states.map((_, j) => `s${j}`) }, }); return { html, items }; }