// THE CUT'S FIRST FRAME (the frame-zero fix, 11 Nov): a cue that takes effect // at the cut's first instant belongs in a page's init, not in its cue list. // // Every cue page sets its init, then builds a paused timeline of fromTo tweens // (`immediateRender: false`) and is seeked frame by frame. The renderer's first // frame is a seek to 0 -- no change to GSAP, and a tween placed at 0 shows its // FROM there anyway -- so a cue at 0 shorter than a frame is drawn on frame 1, // never on frame 0. That is how the instruments' column (dial, odometer, trail: // chrome-instruments.mjs withDeck) showed for one frame over a cut that opens on // the intro: their root is up in init and hidden by an instant cue at 0. // // LEAF: no imports, so every cue compiler can call it -- and SELF-CONTAINED // (no module constant): the posts' page embeds postsPlan, and this with it, in // the browser, and hands this to it as a parameter (chrome-posts.mjs embedFn). /** * Fold every cue that has finished before frame 1 and starts at the cut's * first instant (at or before 0, or where an earlier folded cue on the same * element ended) into init. A cue a frame or longer, or one after a cue on * its element that stays, is left alone: frame 0 then shows its FROM, which is * init, as it should. * * `init` is changed IN PLACE (an entry replaced, never mutated -- entries may * be shared) because several callers read their own init after compiling; the * cue list is returned without the folded cues. * * @template {{ k: string, at: number, dur: number, to: object }} C * @param {Record} init * @param {C[]} cues in time order per element (every compiler's output) * @param {number} [fps] * @returns {C[]} */ export function settleFirstFrame(init, cues, fps = 30) { // At or before this, a time is the cut's first instant. const START_EPS = 1e-4; const frame = 1 / (Number(fps) > 0 ? Number(fps) : 30); /** element -> where its folded cues end; absent once a cue on it stays. */ const foldEnd = new Map(); const kept = new Set(); const out = []; for (const c of cues) { const start = foldEnd.has(c.k) ? foldEnd.get(c.k) : 0; const foldable = !kept.has(c.k) && Number(c.at) <= start + START_EPS && Number(c.at) + Number(c.dur) < frame - START_EPS; if (!foldable) { kept.add(c.k); out.push(c); continue; } init[c.k] = { ...(init[c.k] ?? {}), ...c.to }; foldEnd.set(c.k, Number(c.at) + Number(c.dur)); } return out; } /** * The cues a renderer's frame 0 cannot show: a change at the cut's first * instant, over before frame 1. Empty for a page settleFirstFrame has seen. * * @param {{ init?: object, cues?: Array<{ k: string, at: number, dur: number, from?: object, to: object }> }} data */ export function unseenFirstFrameCues(data, fps = 30) { const init = structuredClone(data.init ?? {}); const cues = (data.cues ?? []).filter((c) => JSON.stringify(c.from ?? null) !== JSON.stringify(c.to)); const settled = settleFirstFrame(init, cues, fps); return cues.filter((c) => !settled.includes(c)); }