// THE JOIN between two post items of one run (umtool/docs/report-video.md, "The join"; operator, 4 Nov, mid-6: // "I'd like a thread-like smooth transition here rather than a cut", "better transition needed between before and // here", "better thread handling here too"): a conversation run (post-conversation.mjs) -- or a manual thread -- // is ONE continuous scene even where its items' layouts differ (a card beside an attachment, a card alone, another // attachment, another width). The item's first `seconds` (0.6) are the join: // // the CARD its container morphs from the item before's box (as it ended) to its own -- position, // width and height eased; the words inside scaled with its width, never re-flowed // what it SHOWED the item before's card as it ended (the build: a shot of its page at its last frame -- its // clock, its parent's shot, a visit's pointer on it; Play: its page in a second frame) rides // the container on top and dissolves into this one's, whole under it first (joinOpacities) // the CLOCK carried on from the item before's when near, else that one dissolves and this comes in // (joinClockOf) -- never moved across the card's words // the COLUMN the post the item before ended on (carried, or the scene's) is drawn as that item drew it // -- its head, its words at their size -- and the column opens framed where it was // the SLOT a slot that appears fades in, one that goes fades out; another attachment cross-fades // over the one the item before ended on (its last frame); the same one is held (a media scene) // // render.posts.join = { on?: true, seconds?: 0.6 } the cut's // timeline[i].join = false | { seconds? } one item's (false: a cut) // // PURE and dependency-free (but its siblings): the plan carries `follow.join` (its seconds; the state at a second // carries the progress), the build and Play draw the same frame from joinFrameAt. const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); const r2 = (v) => Math.round(v * 100) / 100; export const JOIN_DEFAULTS = Object.freeze({ on: true, seconds: 0.6 }); export const JOIN_LIMITS = Object.freeze({ seconds: [0.2, 2] }); /** One join knob's errors (`where`: its path). */ export function joinKnobErrors(v, where, { item = false } = {}) { if (v === undefined || v === null || (item && v === false)) return []; if (!isObj(v)) return [`${where} must be ${item ? "false (a cut) or " : ""}{ ${item ? "" : "on?, "}seconds? }`]; const errors = []; for (const k of Object.keys(v)) if (!(item ? ["seconds"] : ["on", "seconds"]).includes(k)) errors.push(`${where}.${k} is not a join knob`); if (v.on !== undefined && typeof v.on !== "boolean") errors.push(`${where}.on must be true or false`); if (v.seconds !== undefined && !(fin(v.seconds) && v.seconds >= JOIN_LIMITS.seconds[0] && v.seconds <= JOIN_LIMITS.seconds[1])) errors.push(`${where}.seconds must be ${JOIN_LIMITS.seconds[0]}–${JOIN_LIMITS.seconds[1]}`); return errors; } /** A post item's `join` errors, and the cut's (validatePostItem / validatePostItems). */ export const validatePostJoin = (e) => joinKnobErrors(e?.join, `timeline ${e?.id ?? "?"}.join`, { item: true }); export const validatePostJoinRender = (render) => joinKnobErrors(render?.posts?.join, "render.posts.join"); /** One item's join knobs: its own, else the cut's, else the defaults. */ export function joinKnobs(entry, render = undefined) { const r = isObj(render?.posts?.join) ? render.posts.join : {}; const k = { ...JOIN_DEFAULTS, ...(typeof r.on === "boolean" ? { on: r.on } : {}), ...(fin(r.seconds) ? { seconds: r.seconds } : {}) }; if (entry?.join === false) return { ...k, on: false }; if (isObj(entry?.join) && fin(entry.join.seconds)) k.seconds = entry.join.seconds; return k; } /** The join's ease: in-out cubic. */ export const joinEase = (x) => { const v = Math.min(1, Math.max(0, x)); return v < 0.5 ? 4 * v ** 3 : 1 - (-2 * v + 2) ** 3 / 2; }; const lerp = (a, b, f) => a + (b - a) * f; const lerpRect = (a, b, f) => ({ x: lerp(a.x, b.x, f), y: lerp(a.y, b.y, f), width: lerp(a.width, b.width, f), height: lerp(a.height, b.height, f) }); const sameRect = (a, b) => !!a && !!b && ["x", "y", "width", "height"].every((k) => Math.abs(a[k] - b[k]) < 1.5); /** * THE DISSOLVE's strengths at eased progress `e`, pure: `{ own, from }` -- this card's (under) in by e 0.3 (from the * first frame when the content is the same), the item before's (over it) out from e 0.25 to 0.75. Both are whole * across the boundary (e 0: the item before as it ended) and the item before's is gone well before the join ends. */ export function joinOpacities(e, same = false) { const own = same ? 1 : Math.min(1, Math.max(0, e / 0.3)); const from = 1 - Math.min(1, Math.max(0, (e - 0.25) / 0.5)); return { own, from }; } /** * THE JOIN's FRAME at progress `p` (0: the item before as it ended, 1: this item's own), pure: `{ e, R, own, from, * slotFrom, slotTo }` -- `R` the container's box; `own` this card's transform (`tx`, `ty`, `s`: its top-left moved to * R's, scaled by R's width over its own; `clipH` the height of it that shows; `op`); `from` the item before's card * as it ended (`x`, `y`, `s` its scale -- R's width over its own -- and `op`); `slotFrom` / `slotTo` the strength of * the slot wells (the item before's, this one's). `g`: `{ A, B, slotA, slotB, same }` -- the two cards' boxes, their * slots, and whether the CONTENT is the same at both ends (a carried post drawn alike: no cross-fade). */ export function joinFrameAt(g, p) { const e = joinEase(p); const A = g.A; const B = g.B; const R = lerpRect(A, B, e); const s = R.width / Math.max(1, B.width); // The item before's card (on top) dissolves into this one's, which is in UNDER it before it starts to go -- so // what both draw (the post carried in) never dims, and what only the item before drew (its counts, its quote, its // clock) fades out; never a frame where it is all gone at once (operator, 4 Nov, mid-7: "if one post is visible // in the next scene, there should be a transition"). The same content (a carry alike) is this card's from the // first frame, under the item before's. const { own: opOwn, from: opFrom } = joinOpacities(e, !!g.same); const sameSlot = sameRect(g.slotA, g.slotB); return { e: r2(e), R: { x: r2(R.x), y: r2(R.y), width: r2(R.width), height: r2(R.height) }, own: { tx: r2(R.x - B.x), ty: r2(R.y - B.y), s: Math.round(s * 10000) / 10000, clipH: r2(R.height / Math.max(1e-6, s)), op: r2(opOwn) }, from: { x: r2(R.x), y: r2(R.y), s: Math.round((R.width / Math.max(1, A.width)) * 10000) / 10000, op: r2(opFrom) }, slotFrom: g.slotA && !sameSlot ? r2(1 - e) : g.slotA ? 1 : 0, slotTo: g.slotB && !sameSlot ? r2(e) : g.slotB ? 1 : 0, }; } /** How far (px at 1080) the clock may carry on from the item before's place by moving: never across the card's words. */ export const JOIN_CLOCK_NEAR = 24; /** * THE CLOCK OVER A JOIN, pure (post-clock.mjs): `{ bake, move, fadeIn }` -- `from` where the item before's clock sat * as it ended (or null: it showed none), `own` where this item's sits (or null), `k` the box's height over 1080. * Near (within JOIN_CLOCK_NEAR): this one opens on that one's place and moves the little way (`move` its offset * there), nothing faded. Apart: a move would cross the card's words -- that one is laid in the item before's shot * (`bake`) and dissolves with it, this one comes in over the join (`fadeIn`). None before: this one comes in; none * now: that one is laid in the shot. * @param {{ from: { x: number, y: number } | null, own: { x: number, y: number } | null, k?: number }} o */ export function joinClockOf({ from, own, k = 1 }) { if (!own) return { bake: !!from, move: null, fadeIn: false }; if (!from) return { bake: false, move: null, fadeIn: true }; const dx = from.x - own.x; const dy = from.y - own.y; if (Math.hypot(dx, dy) <= JOIN_CLOCK_NEAR * k) return { bake: false, move: { dx: r2(dx), dy: r2(dy) }, fadeIn: false }; return { bake: true, move: null, fadeIn: true }; } /** The join's progress at item second `s` (null: over, or none). */ export const joinProgressAt = (join, s) => (join && fin(join.seconds) && s < join.seconds - 1e-6 ? Math.max(0, s / join.seconds) : null); /** * WHAT THE ITEM BEFORE ENDED ON in its slot, pure: `[{ m, i, t, scope }]` -- the attachments up at its last frame * (`t` a video's or a GIF's source second there; a context post's on the panel over its own: `scope`). * `stepAt` / `mediaTimeAt` are post-item.mjs's (passed: this module imports none of it). * * @param {Record | null} plan * @param {{ stepAt: (plan: any, s: number) => any, mediaTimeAt: (step: any, s: number) => number | null, fps?: number }} o * @returns {Array<{ m: Record, i: number, t: number, scope?: string, step?: any }>} */ export function endMediaOf(plan, { stepAt, mediaTimeAt, fps = 30 }) { if (!plan) return []; const last = Math.max(0, plan.total - 1 / fps); const show = [...(plan.panel?.shows ?? [])].reverse().find((sh) => sh.t0 <= last && sh.t1 >= last - 1e-6); if (show) return show.media.map((m, i) => ({ m, i, t: 0, scope: show.scope })); const step = stepAt(plan, last) ?? plan.steps?.[0] ?? null; if (!step) return []; const t = mediaTimeAt(step, last); // A video held on its last frame: a little before its end (a seek to the very end finds no frame). return step.items.map((m) => ({ m, i: m.i, t: fin(t) ? Math.max(0, fin(m.seconds) ? Math.min(t, m.seconds - 0.12) : t) : 0, step })); } /** * IN THE PAGE (self-contained: the build passes it to page.evaluate, Play calls it on its frame's document): this * card at join progress `p` -- its box moved, scaled and clipped to the container's, its own background drawn by a * well at the container's box, the item before's card shot (`img[data-join-from]`, the build's) riding it, the slot * wells faded. The geometry is the body's `data-join` (`{ A, slotA, slotB, same }`); this card's own box is measured * on the first call (its style put back when `p` is null). */ export function applyJoinState(p, doc = document) { const body = doc.body; const card = doc.querySelector("[data-post-card]"); if (!body || !card) return; const raw = body.getAttribute("data-join"); let bg = doc.querySelector("[data-join-bg]"); // A page drawn without the join's layer (Play's: the card route's page): its container's well made here, as the card's. if (!bg && raw && p !== null && p !== undefined) { const cs = getComputedStyle(card); bg = doc.createElement("div"); bg.setAttribute("data-join-bg", ""); bg.setAttribute("style", `position:absolute;display:none;background:${cs.backgroundColor};border:${cs.borderTopWidth} solid ${cs.borderTopColor};border-radius:${cs.borderTopLeftRadius}`); card.parentNode.insertBefore(bg, card); } const from = doc.querySelector("[data-join-from]"); const slotFrom = doc.querySelector("[data-join-slot-from]"); const slot = doc.querySelector("[data-post-slot]"); if (p === null || p === undefined || !raw) { if (card.getAttribute("data-join-on")) { card.style.transform = card.getAttribute("data-join-t") || ""; card.style.clipPath = ""; card.style.opacity = ""; card.style.background = ""; card.style.borderColor = ""; card.style.top = card.getAttribute("data-join-top") || card.style.top; card.removeAttribute("data-join-on"); } if (bg) bg.style.display = "none"; if (from) from.style.display = "none"; if (slotFrom) slotFrom.style.display = "none"; if (slot) slot.style.opacity = ""; return; } const g = JSON.parse(raw); if (!card.getAttribute("data-join-on")) { // Its own box, as laid out (a card alone is centred by a translate: its top made explicit while it joins). card.setAttribute("data-join-t", card.style.transform || ""); card.setAttribute("data-join-top", card.style.top || ""); const b = card.getBoundingClientRect(); card.setAttribute("data-join-b", JSON.stringify({ x: b.left, y: b.top, width: b.width, height: b.height })); card.style.top = `${b.top}px`; card.setAttribute("data-join-on", "1"); } const B = JSON.parse(card.getAttribute("data-join-b")); const A = g.A; // joinFrameAt, inlined (this function reaches for nothing outside). const v = Math.min(1, Math.max(0, p)); const e = v < 0.5 ? 4 * v ** 3 : 1 - (-2 * v + 2) ** 3 / 2; const L = (a, b2) => a + (b2 - a) * e; const R = { x: L(A.x, B.x), y: L(A.y, B.y), width: L(A.width, B.width), height: L(A.height, B.height) }; const s = R.width / Math.max(1, B.width); // joinOpacities, inlined. const x = g.same ? 1 : Math.min(1, Math.max(0, e / 0.3)); const y = 1 - Math.min(1, Math.max(0, (e - 0.25) / 0.5)); const same = (a, b2) => !!a && !!b2 && ["x", "y", "width", "height"].every((k) => Math.abs(a[k] - b2[k]) < 1.5); const sameSlot = same(g.slotA, g.slotB); card.style.transformOrigin = "0 0"; card.style.transform = `translate(${R.x - B.x}px, ${R.y - B.y}px) scale(${s})`; card.style.clipPath = `inset(0 0 ${Math.max(0, B.height - R.height / s)}px 0)`; card.style.background = "transparent"; card.style.borderColor = "transparent"; card.style.opacity = String(x); if (bg) { bg.style.display = "block"; bg.style.left = `${R.x}px`; bg.style.top = `${R.y}px`; bg.style.width = `${R.width}px`; bg.style.height = `${R.height}px`; } if (from) { from.style.display = "block"; from.style.left = `${R.x}px`; from.style.top = `${R.y}px`; from.style.width = `${A.width}px`; from.style.transform = `scale(${R.width / Math.max(1, A.width)})`; from.style.clipPath = `inset(0 0 ${Math.max(0, A.height - R.height / (R.width / Math.max(1, A.width)))}px 0)`; // Over this card, dissolving into it (the same content drawn alike: identical where they overlap). from.style.opacity = String(y); } if (slotFrom) { slotFrom.style.display = g.slotA && !sameSlot ? "block" : "none"; slotFrom.style.opacity = String(1 - e); } if (slot) slot.style.opacity = g.slotB && !sameSlot ? String(e) : ""; } /** The page's join layer (postCardHtml): the container's well, the item before's shot, its slot's well -- hidden until a join state shows them. */ export function joinLayerHtml({ fromUrl = null, slotA = null, cardBg, line, radius }) { return `
${slotA ? `
` : ""}${fromUrl ? `` : ""}`; }