// A POST ITEM's PROGRESS BAR (umtool/docs/report-video.md, "The progress bar"; operator, 4 Nov, a 6.5-minute post: // "we might want a per-clip progress bar in the engine specifically for clips like this"). // // A thin bar along the card's foot -- under its counts row, inside the card -- that fills as the item's read goes: // the READ's own seconds (the narration schedule: from the narrator's first word to the last), linear in real time, // so a part read faster (a `tempo` step, postReread.mjs) moves it faster and it never jumps. Subtle ticks mark where // each part of the read starts. An item with no read fills over its own length. // // render.posts.progress = "auto" | "on" | "off" // the cut's default (absent: off) // entry.progress = "auto" | "on" | "off" // one item's (absent: the cut's) // auto: on for a read of PROGRESS_AUTO_SECONDS (60 s) or more, as the item plays it (its rate counted) // // WHERE: drawn OVER the card, never in it (no data-w, no data-post-block): readInViewErrors never counts it. Its // place is judged over the read (measureProgressPlace): the card's foot, under the counts row; never over a word, // and short of the clock (post-clock.mjs) when that sits on the same line. Over a word anywhere, it moves just under // the card's edge. // // Pure but for the DOM functions (mountProgress, measureProgressPlace, setProgressState), SELF-CONTAINED: the // build passes them to a page to shoot its track and fill, Play and the bench call them on their frame. import { mixHex, rgbaOf } from "./post-item.mjs"; import { partStartOnItem, pickedTake } from "./post-narration.mjs"; export const PROGRESS_MODES = Object.freeze(["auto", "on", "off"]); /** auto: a read this long or longer (item seconds) draws the bar. */ export const PROGRESS_AUTO_SECONDS = 60; /** The bar fades in over this, ending as the read starts. */ export const PROGRESS_FADE = 0.35; /** Its drawing (bumped when its look changes: the segment key carries it). */ export const POST_PROGRESS_DRAW_V = 1; /** At most this many ticks (a read of more parts draws none). */ export const PROGRESS_TICKS_MAX = 120; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); const r4 = (v) => Math.round(v * 10000) / 10000; /** Every reason an item's `progress` is refused. */ export function progressErrors(v, where) { if (v === undefined) return []; return PROGRESS_MODES.includes(v) ? [] : [`${where} must be ${PROGRESS_MODES.join(", ")}`]; } /** Every reason `render.posts.progress` is refused (post-redact.mjs validatePostsRender calls it). */ export const validateProgressRender = (v) => progressErrors(v, "render.posts.progress"); /** The mode an item plays with: its own, else the cut's, else off. */ export function progressModeOf(entry, render) { if (PROGRESS_MODES.includes(entry?.progress)) return entry.progress; const c = render?.posts?.progress; return PROGRESS_MODES.includes(c) ? c : "off"; } /** * THE BAR's PLAN for post item `entry` (its plan, postItemPlan's): `{ from, to, ticks, until, mode }` -- it fills * from item second `from` to `to` (the read; an item with none: 0 to its length), `ticks` each later part's start * as a fraction of the bar, `until` when it goes (a stack card's media phase covers the card). Null: no bar. * * @param {Record} entry * @param {Record | undefined} render * @param {{ total: number, narration?: Record | null, steps?: Array>, reading?: number }} plan * @param {{ stack?: boolean }} [o] */ export function progressPlan(entry, render, plan, { stack = false } = {}) { if (entry?.type !== undefined && entry.type !== "post") return null; if (!plan || !fin(plan.total) || plan.total <= 0) return null; const mode = progressModeOf(entry, render); if (mode === "off") return null; const sch = isObj(plan.narration) ? plan.narration : null; const from = sch ? sch.at : 0; const to = sch ? sch.at + sch.seconds : plan.total; if (!(to > from)) return null; if (mode === "auto" && !(sch && sch.seconds >= PROGRESS_AUTO_SECONDS)) return null; const t = sch ? pickedTake(entry) : null; const parts = Array.isArray(t?.parts) ? t.parts.filter((p) => fin(p?.start)) : []; const ticks = parts.length > 1 && parts.length <= PROGRESS_TICKS_MAX ? parts.slice(1).map((p) => r4((partStartOnItem(sch, p) - from) / (to - from))).filter((x) => x > 0.002 && x < 0.998) : []; const until = stack && Array.isArray(plan.steps) && plan.steps.length ? plan.steps[0].at : plan.total; return { from: r4(from), to: r4(to), ticks, until: r4(until), mode }; } /** How full the bar is at item second `s`, 0–1: linear in the read's real seconds. */ export function progressAt(pp, s) { if (!pp) return 0; return Math.min(1, Math.max(0, (s - pp.from) / (pp.to - pp.from))); } /** Its opacity at item second `s`: faded in over PROGRESS_FADE ending at the read's start, gone at `until`. */ export function progressOpacityAt(pp, s) { if (!pp || s >= pp.until) return 0; const fade = Math.min(PROGRESS_FADE, pp.from); return fade > 0 ? Math.min(1, Math.max(0, (s - (pp.from - fade)) / fade)) : 1; } /** The fill's ffmpeg expression of `T` (seconds): the bar's width fraction, as progressAt. */ export const progressExpr = (pp) => `clip((T-${pp.from})/${r4(pp.to - pp.from)},0,1)`; // --------------------------------------------------------------------------- // THE LOOK, and the DOM (self-contained). // --------------------------------------------------------------------------- /** The bar's height in px for a page `height` px tall. */ export const progressHeight = (height = 1080) => Math.max(3, Math.round(5 * (height / 1080))); /** The bar's CSS for a page `height` px tall, in the card's palette. */ export function progressCss({ height = 1080, palette = {} } = {}) { const pal = { bg: "#12100c", fg: "#f6f1e6", muted: "#a2957f", accent: "#c8752a", ...palette }; const h = progressHeight(height); const tick = Math.max(1, Math.round(1.5 * (height / 1080))); return ` [data-post-progress-layer] { position: absolute; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none; z-index: 60; } .pprog { position: absolute; left: 0; top: 0; height: ${h}px; border-radius: ${h}px; overflow: hidden; } .pprog-track { position: absolute; inset: 0; background: ${rgbaOf(pal.fg, 0.13)}; } .pprog-fill { position: absolute; inset: 0; background: linear-gradient(90deg, ${mixHex(pal.accent, pal.bg, 0.25)}, ${pal.accent}); box-shadow: 0 0 ${h * 2}px ${rgbaOf(pal.accent, 0.35)}; } .pprog-tick { position: absolute; top: 0; bottom: 0; width: ${tick}px; margin-left: -${tick / 2}px; background: ${rgbaOf(pal.bg, 0.55)}; } [data-post-progress-layer][data-part="track"] .pprog-fill { display: none; } [data-post-progress-layer][data-part="fill"] .pprog-track { display: none; } `; } /** * Mount the bar over the card page (`{ css, ticks }`), hidden. `part`: "track" or "fill" alone (the build shoots * each), else both (Play). Returns the bar's element height. * @param {{ css: string, ticks?: number[], part?: "track" | "fill" | null } | null} arg * @param {Document} [doc] */ export function mountProgress(arg, doc = document) { doc.querySelectorAll("[data-post-progress-layer], style[data-post-progress-css]").forEach((el) => el.remove()); if (!arg) return null; const style = doc.createElement("style"); style.setAttribute("data-post-progress-css", ""); style.textContent = arg.css || ""; doc.head.appendChild(style); const layer = doc.createElement("div"); layer.setAttribute("data-post-progress-layer", ""); if (arg.part) layer.setAttribute("data-part", arg.part); const bar = doc.createElement("div"); bar.className = "pprog"; bar.setAttribute("data-post-progress", ""); bar.style.opacity = "0"; const ticks = (arg.ticks || []).map((f) => ``).join(""); bar.innerHTML = `${ticks}`; layer.appendChild(bar); doc.body.appendChild(layer); return Math.ceil(bar.getBoundingClientRect().height); } /** * Where the bar goes on the card as drawn: `{ x, y, width, height, place, covers, tries }` -- along the card's foot, * under its own post's counts row (a threaded card's: the post's tile), inset from the card's rounded corners; its * right end short of `avoid` boxes on its line (the clock). `fixed`: the places to judge (an earlier call's * `tries`), only counted here -- placed once, judged over the read; else, over a word, just under the card's edge. * @typedef {{ x: number, y: number, width: number, height: number, place: string }} ProgressPlace * @param {{ height: number, inset: number, avoid?: Array<{ x: number, y: number, width: number, height: number }>, fixed?: ProgressPlace[] | null }} arg * @param {Document} [doc] * @returns {(ProgressPlace & { covers: number, tries: ProgressPlace[], counts: number[] }) | null} */ export function measureProgressPlace(arg, doc = document) { const card = doc.querySelector("[data-post-card]"); if (!card || !arg) return null; const c = card.getBoundingClientRect(); const h = arg.height; // The words as SEEN: each clipped to its window (a following card's flow pans words past its window's edge, hidden). const wins = new Map(); const winOf = (el) => { const w = el.closest("[data-post-words]"); if (!w) return null; if (!wins.has(w)) wins.set(w, w.getBoundingClientRect()); return wins.get(w); }; const words = Array.from(doc.querySelectorAll("[data-w]")).flatMap((el) => { const w = winOf(el); return Array.from(el.getClientRects()).map((r) => (w ? { left: Math.max(r.left, w.left), right: Math.min(r.right, w.right), top: Math.max(r.top, w.top), bottom: Math.min(r.bottom, w.bottom) } : r)); }).filter((r) => r.right - r.left > 0 && r.bottom - r.top > 0); const hit = (r) => words.filter((w) => Math.min(r.x + r.width, w.right) - Math.max(r.x, w.left) > 0.5 && Math.min(r.y + r.height + 2, w.bottom) - Math.max(r.y - 2, w.top) > 0.5).length; /** @type {ProgressPlace[]} */ const tries = arg.fixed || (() => { const radius = parseFloat((doc.defaultView || window).getComputedStyle(card).borderTopLeftRadius) || 0; const side = Math.max(arg.inset, Math.round(radius * 0.9)); const x = Math.round(c.left + side); let right = Math.round(c.right - side); const all = Array.from(card.querySelectorAll(".counts")); const counts = card.querySelector('[data-thread-tile="post"] .counts') || all[all.length - 1] || null; const cb0 = counts ? counts.getBoundingClientRect() : null; // A counts row is the card's foot only when it sits at the foot (a threaded card's is a tile's, in its flow). const cb = cb0 && cb0.height > 0 && c.bottom - cb0.bottom >= 0 && c.bottom - cb0.bottom <= Math.max(60, 3 * arg.inset) ? cb0 : null; // Under the counts row: centred in the foot below it (else a little above the card's edge). const foot = cb ? Math.round((cb.bottom + c.bottom) / 2 - h / 2) : Math.round(c.bottom - h - Math.max(6, arg.inset * 0.5)); const y = Math.min(Math.round(c.bottom - h - 3), Math.max(foot, cb ? Math.round(cb.bottom + 2) : foot)); for (const a of arg.avoid || []) { if (a.y < y + h + 4 && a.y + a.height > y - 4 && a.x < right && a.x + a.width > x) right = Math.min(right, Math.round(a.x - arg.inset * 0.6)); } const width = Math.max(40, right - x); const below = Math.round(c.bottom + Math.max(4, h)); return [ { x, y, width, height: h, place: "foot" }, { x, y: below, width: Math.max(40, Math.round(c.right - side) - x), height: h, place: "under" }, ]; })(); const looked = tries.map((t) => ({ ...t, covers: hit(t) })); const best = looked.find((t) => !t.covers) || looked.slice().sort((a, b) => a.covers - b.covers)[0]; return { ...best, tries: looked.map(({ covers: _c, ...t }) => t), counts: looked.map((t) => t.covers) }; } /** * Set the bar: `{ x, y, width, frac, opacity }` (frac progressAt's), or null to hide. The fill is clipped at its * leading edge (as the build clips it), never squeezed. * @param {{ x: number, y: number, width: number, frac: number, opacity?: number } | null} arg * @param {Document} [doc] */ export function setProgressState(arg, doc = document) { const el = doc.querySelector("[data-post-progress]"); if (!el) return; if (!arg) { el.style.opacity = "0"; return; } el.style.transform = `translate(${Math.round(arg.x)}px, ${Math.round(arg.y)}px)`; el.style.width = `${Math.round(arg.width)}px`; el.style.opacity = String(arg.opacity === undefined ? 1 : arg.opacity); el.setAttribute("data-frac", String(Math.round(arg.frac * 1000) / 1000)); const fill = el.querySelector("[data-post-progress-fill]"); if (fill) fill.style.clipPath = `inset(0 ${((1 - Math.min(1, Math.max(0, arg.frac))) * 100).toFixed(3)}% 0 0)`; } /** * THE BAR's FILTERS for the build (post-render.mjs postSegmentArgs): the track and the fill (each a still the size of * the bar, inputs `ti` and `fi`, looped over the item) laid over `inLabel` at `x, y` -- the fill's alpha clipped at * its leading edge, frame by frame (geq on T: progressExpr), both faded in as the read starts and gone at `until`. * Returns `{ chains, out }`. * * @param {{ ti: number, fi: number, inLabel: string, outLabel: string, x: number, y: number, pp: ReturnType, T: number }} o */ export function progressFilters({ ti, fi, inLabel, outLabel, x, y, pp, T }) { const fade = Math.min(PROGRESS_FADE, pp.from); const fadeIn = fade > 0 ? `,fade=t=in:st=${r4(pp.from - fade)}:d=${r4(fade)}:alpha=1` : ""; const enable = pp.until < T - 1e-6 ? `:enable='lt(t,${pp.until})'` : ""; return { chains: [ `[${ti}:v]format=rgba,setsar=1${fadeIn}[pgt]`, `[${fi}:v]format=rgba,setsar=1,geq=r='r(X,Y)':g='g(X,Y)':b='b(X,Y)':a='alpha(X,Y)*clip(W*${progressExpr(pp)}-X,0,1)'${fadeIn}[pgf]`, `[${inLabel}][pgt]overlay=x=${x}:y=${y}${enable}[${outLabel}t]`, `[${outLabel}t][pgf]overlay=x=${x}:y=${y}${enable}[${outLabel}]`, ], out: outLabel, }; }