Open source CapCut alternative.
TypeScript
feat(effects-kit): slot and dive text animations [rebuild]
David Le08e1551ea98d
diff --git a/site/packages/effects-kit/src/anim.ts b/site/packages/effects-kit/src/anim.tsindex b6c3f716..c306888d 100644--- a/site/packages/effects-kit/src/anim.ts+++ b/site/packages/effects-kit/src/anim.ts@@ -36,6 +36,7 @@ export type OverlayAnimStyle =| "slidedown"| "typewriter" // text only; other kinds render it as a fade| "wipe"+ | "dive"// Per-glyph styles: on text each character runs the ramp on its own delay// (the preset carries a range selector); on every other kind the whole// element runs it once.@@ -49,7 +50,8 @@ export type OverlayAnimStyle =| "converge"| "streak"| "tumble"- | "scatter";+ | "scatter"+ | "slot";export type OverlayLoopStyle =| "pulse"@@ -83,7 +85,8 @@ export type GlyphAnimStyle =| "converge"| "streak"| "tumble"- | "scatter";+ | "scatter"+ | "slot";/*** A ramp at one end of the element.@@ -150,6 +153,22 @@ export const OVERLAY_LOOP_STYLE_IDS = LOOP_IDS as OverlayLoopStyle[];export const GLYPH_ANIM_STYLE_IDS = PER_UNIT_EDGE_IDS as GlyphAnimStyle[];export const GLYPH_LOOP_STYLE_IDS = PER_UNIT_LOOP_IDS as GlyphLoopStyle[];+/** The edges that only play on characters — typing them out, rolling each up+ * a reel. Every other kind is offered the rest. */+export const TEXT_ONLY_ANIM_STYLE_IDS = EDGE_IDS.filter(+ (id) => !!MOTION.edges[id].animate.typed || !!MOTION.edges[id].animate.roll+) as OverlayAnimStyle[];+/** The edges that fly the view into the element's ink until it fills the+ * frame. */+export const DIVE_ANIM_STYLE_IDS = EDGE_IDS.filter((id) => !!MOTION.edges[id].animate.dive) as OverlayAnimStyle[];++/** Each catalog edge that carries a note, as "id: note" lines — what tool+ * schemas and prompts teach the edges from. */+export const edgeNotes = (): string =>+ EDGE_IDS.filter((id) => MOTION.edges[id].note)+ .map((id) => `${id}: ${MOTION.edges[id].note}`)+ .join(" ");+const GLYPH_STYLES: Set<string> = new Set(GLYPH_ANIM_STYLE_IDS);const GLYPH_LOOPS: Set<string> = new Set(GLYPH_LOOP_STYLE_IDS);@@ -260,6 +279,10 @@ export interface OverlayAnimState {reveal?: number;glyphs?: GlyphPhase;glyphLoop?: GlyphLoopPhase;+ /** How far the view has flown into the element's deepest ink, 0..1 (dive).+ * Renderers turn it into a view with `diveView`, which needs the drawn+ * geometry this evaluator never sees. */+ dive?: number;}/** How one character sits at a moment: offsets in design px, a scale per axis@@ -271,6 +294,9 @@ export interface GlyphAnimState {sy: number;rotate: number;alpha: number;+ /** Characters its reel still rolls past before it lands (slot); absent or+ * 0 = the character alone. `slotReel` says what to draw. */+ roll?: number;}const IDLE: OverlayAnimState = { dx: 0, dy: 0, scale: 1, rotate: 0, alpha: 1 };@@ -292,6 +318,7 @@ function asGlyph(pose: MotionPose, index: number, count: number): GlyphAnimStatesy: pose.sy,rotate: pose.rotate,alpha: pose.alpha,+ ...(pose.roll ? { roll: pose.roll } : {}),};}@@ -324,6 +351,7 @@ export function glyphStateAt(if (!preset) return ramp;const n = Math.max(1, count);const loop = asGlyph(evalPreset(preset, state.glyphLoop.phase, index, n, false), index, n);+ const roll = (ramp.roll ?? 0) + (loop.roll ?? 0);return {dx: ramp.dx + loop.dx,dy: ramp.dy + loop.dy,@@ -331,6 +359,7 @@ export function glyphStateAt(sy: ramp.sy * loop.sy,rotate: ramp.rotate + loop.rotate,alpha: ramp.alpha * loop.alpha,+ ...(roll ? { roll } : {}),};}@@ -374,6 +403,7 @@ function edgeState(// element just holds — the same as it always did.if (pose.typed !== undefined) state.textProgress = pose.typed;if (pose.reveal !== undefined) state.reveal = pose.reveal;+ if (pose.dive !== undefined) state.dive = pose.dive;return state;}diff --git a/site/packages/effects-kit/src/dive.test.ts b/site/packages/effects-kit/src/dive.test.tsnew file mode 100644index 00000000..760f3373--- /dev/null+++ b/site/packages/effects-kit/src/dive.test.ts@@ -0,0 +1,73 @@+import { describe, expect, test } from "bun:test";+import { diveView, DIVE_MAX_SCALE, slotReel } from "./dive";+import { evalOverlayAnim } from "./anim";++const frame = { width: 1080, height: 1920, scale: 1 };+const rest = { x: 0.5, y: 0.5 };+const still = { x: 0.5, y: 0.5, dx: 0, dy: 0, scale: 1, rotation: 0 };++describe("slot reels", () => {+ test("every character in sight is a real character of the target's kind", () => {+ for (const ch of ["A", "z", "7"]) {+ for (let i = 0; i < 40; i++) {+ for (let roll = 0; roll <= 5; roll += 0.13) {+ for (const r of slotReel(ch, i, roll)) {+ expect(r.ch.length).toBe(1);+ if (ch === "7") expect(r.ch).toMatch(/[0-9]/);+ else if (ch === "A") expect(r.ch).toMatch(/[A-Z]/);+ else expect(r.ch).toMatch(/[a-z]/);+ }+ }+ }+ }+ });++ test("a landed reel shows the character alone, and a space never rolls", () => {+ expect(slotReel("Q", 3, 0)).toEqual([{ ch: "Q", y: 0, alpha: 1 }]);+ expect(slotReel(" ", 3, 2.5)).toEqual([{ ch: " ", y: 0, alpha: 1 }]);+ });+});++describe("dive", () => {+ const focus = { x: 120, y: 30, r: 24 };++ test("at rest the view is the identity", () => {+ const v = diveView(0, focus, still, rest, frame);+ expect(v.s).toBe(1);+ expect(v.tx).toBe(v.fx);+ expect(v.ty).toBe(v.fy);+ });++ test("landed, the ink around the focus reaches every corner from the frame's center", () => {+ const v = diveView(1, focus, still, rest, frame);+ expect(v.tx).toBeCloseTo(540, 6);+ expect(v.ty).toBeCloseTo(960, 6);+ expect(v.s * focus.r).toBeGreaterThanOrEqual(Math.hypot(540, 960));+ expect(v.s).toBeLessThanOrEqual(DIVE_MAX_SCALE);+ });++ test("a posed element lands its focus on the frame's center through the pose", () => {+ const pose = { x: 0.3, y: 0.7, dx: 10, dy: -20, scale: 1.5, rotation: 30 };+ const v = diveView(1, focus, pose, rest, frame);+ const cx = rest.x * frame.width;+ const cy = rest.y * frame.height;+ // P(t): the pose applied to the view's target.+ const th = (pose.rotation * Math.PI) / 180;+ const ux = v.tx - cx;+ const uy = v.ty - cy;+ const X = pose.x * frame.width + pose.dx + pose.scale * (Math.cos(th) * ux - Math.sin(th) * uy);+ const Y = pose.y * frame.height + pose.dy + pose.scale * (Math.sin(th) * ux + Math.cos(th) * uy);+ expect(X).toBeCloseTo(540, 6);+ expect(Y).toBeCloseTo(960, 6);+ });++ test("an exit dives in across its ramp and an entrance pulls back out", () => {+ const out = { out: { style: "dive" as const, seconds: 1 } };+ expect(evalOverlayAnim(out, 2.001, 3).dive).toBeCloseTo(0, 2);+ expect(evalOverlayAnim(out, 2.5, 3).dive).toBeCloseTo(0.5, 1);+ expect(evalOverlayAnim(out, 3, 3).dive).toBeCloseTo(1, 6);+ const inn = { in: { style: "dive" as const, seconds: 1 } };+ expect(evalOverlayAnim(inn, 0, 3).dive).toBeCloseTo(1, 6);+ expect(evalOverlayAnim(inn, 0.999, 3).dive).toBeLessThan(0.01);+ });+});diff --git a/site/packages/effects-kit/src/dive.ts b/site/packages/effects-kit/src/dive.tsnew file mode 100644index 00000000..501447e3--- /dev/null+++ b/site/packages/effects-kit/src/dive.ts@@ -0,0 +1,133 @@+/**+ * Two motions that need the drawn picture to play: a dive, which flies the+ * view into a letter until its ink fills the frame, and a slot, which rolls+ * each character up a reel of other characters until it lands.+ *+ * The evaluator says how far each one stands. What a dive flies into comes+ * from the drawn picture — the point of the last letter that sits deepest in+ * its ink, and how deep — which `measureDiveFocus` (render.ts) finds once per+ * look. `diveView` folds the two into the scale and offset every renderer+ * draws under, and `slotReel` says what a rolling character shows.+ */++/** The point a dive flies into, in design px (1080 short side) from the+ * element's center, and the radius of ink around it. */+export interface DiveFocus {+ x: number;+ y: number;+ r: number;+}++/** A dive's view in the painter's own coordinates — the element unposed, in+ * output px: a point p draws at s·(p − f) + t. */+export interface DiveView {+ s: number;+ fx: number;+ fy: number;+ tx: number;+ ty: number;+}++/** The deepest a dive zooms. A hairline face would otherwise ask for a scale+ * no renderer can draw. */+export const DIVE_MAX_SCALE = 600;++/** How far past the frame's corners the ink reaches when the dive lands, so+ * antialiased edges never show at the last frame. */+const DIVE_COVER = 1.05;++const clamp01 = (v: number) => (v < 0 ? 0 : v > 1 ? 1 : v);++/**+ * The view a dive draws under at `progress` (0 at rest, 1 landed).+ *+ * The focus travels from where the pose puts it to the frame's center while+ * the scale grows geometrically, so the flight reads at a constant speed and+ * lands with the ink disk around the focus reaching every corner. The view is+ * expressed in painter coordinates so it composes inside the pose: the posed+ * element draws P(V(p)), and P(V(f)) is the travelling focus.+ */+export function diveView(+ progress: number,+ focus: DiveFocus,+ pose: { x: number; y: number; dx: number; dy: number; scale: number; rotation: number },+ rest: { x: number; y: number },+ frame: { width: number; height: number; scale: number }+): DiveView {+ const { width: W, height: H, scale: k } = frame;+ const cx = rest.x * W;+ const cy = rest.y * H;+ const fx = cx + focus.x * k;+ const fy = cy + focus.y * k;+ const e = clamp01(progress);+ const sigma = pose.scale;+ if (e <= 0 || sigma <= 1e-6) return { s: 1, fx, fy, tx: fx, ty: fy };+ const ax = pose.x * W + pose.dx * k;+ const ay = pose.y * H + pose.dy * k;+ const th = (pose.rotation * Math.PI) / 180;+ const cos = Math.cos(th);+ const sin = Math.sin(th);+ // Where the pose puts the focus, and how wide its ink is on screen.+ const px = ax + sigma * (cos * (fx - cx) - sin * (fy - cy));+ const py = ay + sigma * (sin * (fx - cx) + cos * (fy - cy));+ const ink = Math.max(0.5, focus.r * k) * sigma;+ const reach = (Math.hypot(W, H) / 2) * DIVE_COVER;+ const most = Math.min(DIVE_MAX_SCALE, Math.max(1, reach / ink));+ const s = Math.pow(most, e);+ const Tx = px + (W / 2 - px) * e;+ const Ty = py + (H / 2 - py) * e;+ // Back through the pose: the painter-space point the pose sends to T.+ const ux = (Tx - ax) / sigma;+ const uy = (Ty - ay) / sigma;+ return {+ s,+ fx,+ fy,+ tx: cx + cos * ux + sin * uy,+ ty: cy - sin * ux + cos * uy,+ };+}++/** How far apart a reel's characters sit, in line heights. Tighter than a+ * line, so the band shows the letters above and below the one in the slot. */+export const SLOT_PITCH = 0.62;++/** A slot character's reel: what is in sight at `roll`, each at its vertical+ * offset in line heights (positive is below) and its own opacity. Letters roll+ * through letters of their own case and digits through digits; anything else+ * — a space, a mark — holds still. The fillers are a fixed draw per position,+ * so the same title always rolls through the same characters. */+export function slotReel(+ ch: string,+ index: number,+ roll: number+): { ch: string; y: number; alpha: number }[] {+ const pool = reelPool(ch);+ if (!pool || roll <= 0) return [{ ch, y: 0, alpha: 1 }];+ const lo = Math.floor(roll);+ const out: { ch: string; y: number; alpha: number }[] = [];+ for (let j = Math.max(0, lo - 1); j <= lo + 2; j++) {+ const y = (roll - j) * SLOT_PITCH;+ // Past the band's edge nothing of the character shows.+ if (Math.abs(y) >= 0.9) continue;+ out.push({ ch: j === 0 ? ch : filler(pool, ch, index, j), y, alpha: 1 - Math.min(1, Math.abs(y)) * 0.7 });+ }+ return out;+}++const UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";+const LOWER = "abcdefghijklmnopqrstuvwxyz";+const DIGITS = "0123456789";++function reelPool(ch: string): string | null {+ if (DIGITS.includes(ch)) return DIGITS;+ if (ch.toLowerCase() === ch.toUpperCase()) return null;+ return ch === ch.toUpperCase() ? UPPER : LOWER;+}++function filler(pool: string, ch: string, index: number, step: number): string {+ let h = (Math.imul(index + 1, 2654435761) ^ Math.imul(step, 40503)) >>> 0;+ h = Math.imul(h ^ (h >>> 15), 2246822519) >>> 0;+ const pick = pool[((h ^ (h >>> 13)) >>> 0) % pool.length];+ return pick === ch ? pool[(pool.indexOf(pick) + 1) % pool.length] : pick;+}diff --git a/site/packages/effects-kit/src/index.ts b/site/packages/effects-kit/src/index.tsindex 1bd5554b..282b8539 100644--- a/site/packages/effects-kit/src/index.ts+++ b/site/packages/effects-kit/src/index.ts@@ -1,5 +1,6 @@export * from "./types";export * from "./anim";+export * from "./dive";export * from "./words";export * from "./motion/types";export * from "./motion/evaluate";diff --git a/site/packages/effects-kit/src/keys.ts b/site/packages/effects-kit/src/keys.tsindex 521a3816..32d4e5b1 100644--- a/site/packages/effects-kit/src/keys.ts+++ b/site/packages/effects-kit/src/keys.ts@@ -192,6 +192,8 @@ export interface OverlayFrameState extends OverlayPose {glyphs?: GlyphPhase;/** Where a per-glyph loop stands, on the same terms. */glyphLoop?: GlyphLoopPhase;+ /** How far the view has flown into the element's deepest ink (dive). */+ dive?: number;}export function evalOverlayFrame(o: Posable, tLocal: number): OverlayFrameState {@@ -210,6 +212,7 @@ export function evalOverlayFrame(o: Posable, tLocal: number): OverlayFrameState...(ev.reveal !== undefined ? { reveal: ev.reveal } : {}),...(ev.glyphs ? { glyphs: ev.glyphs } : {}),...(ev.glyphLoop ? { glyphLoop: ev.glyphLoop } : {}),+ ...(ev.dive !== undefined ? { dive: ev.dive } : {}),};}diff --git a/site/packages/effects-kit/src/motion/edges.json b/site/packages/effects-kit/src/motion/edges.jsonindex 57ebaf56..d8948d9a 100644--- a/site/packages/effects-kit/src/motion/edges.json+++ b/site/packages/effects-kit/src/motion/edges.json@@ -1760,5 +1760,82 @@}]}+ },+ "slot": {+ "label": "Slot",+ "note": "Each letter rolls up through other characters like a slot reel and lands in turn.",+ "slots": [+ "in",+ "out"+ ],+ "selector": {+ "basedOn": "characters",+ "shape": "rampUp",+ "spread": 0.55+ },+ "animate": {+ "roll": [+ {+ "t": 0,+ "v": 5,+ "ease": [+ 0.2,+ 0.6,+ 0.3,+ 1+ ]+ },+ {+ "t": 1,+ "v": 0+ }+ ],+ "opacity": [+ {+ "t": 0,+ "v": 0,+ "ease": [+ 0.335,+ 0.335,+ 0.665,+ 0.665+ ]+ },+ {+ "t": 0.3,+ "v": 1+ },+ {+ "t": 1,+ "v": 1+ }+ ]+ }+ },+ "dive": {+ "label": "Dive",+ "note": "The camera flies into the last letter until its ink fills the frame. On In it pulls back out of the letter.",+ "slots": [+ "in",+ "out"+ ],+ "animate": {+ "dive": [+ {+ "t": 0,+ "v": 1,+ "ease": [+ 0.4,+ 0,+ 0.6,+ 1+ ]+ },+ {+ "t": 1,+ "v": 0+ }+ ]+ }}}diff --git a/site/packages/effects-kit/src/motion/evaluate.ts b/site/packages/effects-kit/src/motion/evaluate.tsindex 3e884786..461959c8 100644--- a/site/packages/effects-kit/src/motion/evaluate.ts+++ b/site/packages/effects-kit/src/motion/evaluate.ts@@ -90,6 +90,8 @@ export function sampleProperties(props: MotionProperties, q: number, dur = 0): Mtracking: capped(props.tracking, dur),reveal: capped(props.reveal, dur),typed: capped(props.typed, dur),+ roll: capped(props.roll, dur),+ dive: capped(props.dive, dur),};return samplePropertiesRaw(props, q);}@@ -107,6 +109,8 @@ function samplePropertiesRaw(props: MotionProperties, q: number): MotionPose {tracking: sampleTrack(props.tracking, q, 0),...(props.reveal ? { reveal: sampleTrack(props.reveal, q, 1) } : {}),...(props.typed ? { typed: sampleTrack(props.typed, q, 1) } : {}),+ ...(props.roll ? { roll: sampleTrack(props.roll, q, 0) } : {}),+ ...(props.dive ? { dive: sampleTrack(props.dive, q, 0) } : {}),};}diff --git a/site/packages/effects-kit/src/motion/types.ts b/site/packages/effects-kit/src/motion/types.tsindex cb5dd75f..21e4ccbd 100644--- a/site/packages/effects-kit/src/motion/types.ts+++ b/site/packages/effects-kit/src/motion/types.ts@@ -59,6 +59,12 @@ export interface MotionProperties {reveal?: PropertyKey<number>[];/** Share of the characters drawn, 0..1 (typewriter). */typed?: PropertyKey<number>[];+ /** Characters a unit's reel still has to roll past before it lands, 0 at+ * rest (slot). */+ roll?: PropertyKey<number>[];+ /** How far the view has flown into the element's deepest ink, 0..1: at 1+ * that ink covers the frame (dive). */+ dive?: PropertyKey<number>[];}/** What the selector counts as one unit. */@@ -140,6 +146,8 @@ export interface MotionPose {tracking: number;reveal?: number;typed?: number;+ roll?: number;+ dive?: number;}export const REST_POSE: MotionPose = {diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.tsindex 92b635b1..b9692a4e 100644--- a/site/packages/effects-kit/src/render.ts+++ b/site/packages/effects-kit/src/render.ts@@ -17,6 +17,7 @@ import {type GlyphPhase,type OverlayAnim,} from "./anim";+import { diveView, slotReel, type DiveFocus, type DiveView } from "./dive";import { presetExtent } from "./motion/evaluate";import { evalOverlayFrame, hasOverlayKeys, poseAt, poseExtent, sortedKeys } from "./keys";import { applyMaskToCanvas, isMaskAnimated } from "./mask";@@ -137,6 +138,8 @@ export interface PaintPhase {reveal?: number;glyphs?: GlyphPhase;glyphLoop?: GlyphLoopPhase;+ /** The view a dive draws the whole picture under (see `diveView`). */+ dive?: DiveView;}/**@@ -509,7 +512,20 @@ async function paintText(ctx.translate(x + w / 2 + g.dx * scale, y + g.dy * scale);if (g.rotate) ctx.rotate((g.rotate * Math.PI) / 180);ctx.scale(g.sx, g.sy);- drawText(ch, 0, 0);+ if (g.roll) {+ // A rolling reel shows through its own line band only; sideways+ // it stays open, so a wide filler is not cut to the slot.+ ctx.beginPath();+ ctx.rect(-w * 3, -lineH / 2, w * 6, lineH);+ ctx.clip();+ const a = ctx.globalAlpha;+ for (const r of slotReel(ch, gi - 1, g.roll)) {+ ctx.globalAlpha = a * r.alpha;+ drawText(r.ch, 0, r.y * lineH);+ }+ } else {+ drawText(ch, 0, 0);+ }ctx.restore();}x += w;@@ -653,6 +669,225 @@ export async function paintElement(}}+/** Draw everything after this under a dive's view. */+export function applyDiveView(+ ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,+ v: DiveView+): void {+ ctx.translate(v.tx, v.ty);+ ctx.scale(v.s, v.s);+ ctx.translate(-v.fx, -v.fy);+}++/** Whether either edge of an element dives, so a renderer knows to measure+ * the focus and give the flight the whole frame. */+export function divesAt(anim: OverlayAnim | undefined): boolean {+ return !!edgeMotion(anim?.in)?.animate.dive || !!edgeMotion(anim?.out)?.animate.dive;+}++/** Measured foci, by look, newest last. A focus is three numbers, so the+ * bound is a count. */+const diveFoci = new Map<string, Promise<DiveFocus>>();+const settledFoci = new Map<string, DiveFocus>();+const DIVE_FOCI_MAX = 64;+/** The longest side the focus raster is drawn at: enough to place the point+ * within a couple of design px, small enough to measure in a frame. */+const DIVE_RASTER = 480;++/** What decides where a dive lands: the look and the layout, never the+ * timing or the motion. */+function diveKey(o: Overlay, aspect: number): string {+ const look: Record<string, unknown> = { ...o };+ for (const k of ["id", "start", "end", "anim", "kf", "lane", "opacity", "rotation", "mask"]) delete look[k];+ return JSON.stringify([look, Math.round(aspect * 1000)]);+}++/**+ * Where a dive flies into: the point of the element's ink that sits deepest+ * inside it — on a title, inside its last letter — and how deep. Measured on+ * the same painter that draws the element, at a 1080 short side, and kept per+ * look, so every renderer and every frame reads one answer.+ */+export function measureDiveFocus(o: Overlay, aspect: number, env: RenderEnv): Promise<DiveFocus> {+ const key = diveKey(o, aspect);+ let hit = diveFoci.get(key);+ if (hit) {+ diveFoci.delete(key);+ } else {+ hit = findDiveFocus(o, aspect, env).then((f) => {+ settledFoci.set(key, f);+ return f;+ });+ }+ diveFoci.set(key, hit);+ while (diveFoci.size > DIVE_FOCI_MAX) {+ const old = diveFoci.keys().next().value!;+ diveFoci.delete(old);+ settledFoci.delete(old);+ }+ return hit;+}++/** The measured focus if it has landed; otherwise starts measuring and says+ * so with undefined. For renderers that draw synchronously. */+export function peekDiveFocus(o: Overlay, aspect: number, env: RenderEnv): DiveFocus | undefined {+ const hit = settledFoci.get(diveKey(o, aspect));+ if (!hit) void measureDiveFocus(o, aspect, env).catch(() => {});+ return hit;+}++async function findDiveFocus(o: Overlay, aspect: number, env: RenderEnv): Promise<DiveFocus> {+ const width = aspect >= 1 ? 1080 * aspect : 1080;+ const height = aspect >= 1 ? 1080 : 1080 / aspect;+ const frame: PaintFrame = { width, height, scale: 1 };+ const isText = (o.kind ?? "text") === "text";+ // The ink alone: a plate would pull the flight into the plate, a shadow+ // blurs the edge the depth is measured from.+ const el = {+ ...o,+ rotation: undefined,+ opacity: undefined,+ mask: undefined,+ anim: undefined,+ kf: undefined,+ ...(isText ? { plate: false, shadow: false } : {}),+ } as Overlay;+ const b = await measureElementBounds(el, frame, env);+ const q = Math.min(1, DIVE_RASTER / Math.max(b.w, b.h, 1));+ const pad = 2;+ const rw = Math.ceil(b.w * q) + pad * 2;+ const rh = Math.ceil(b.h * q) + pad * 2;+ const left = b.cx - b.w / 2;+ const top = b.cy - b.h / 2;+ const canvas = newCanvas(env, rw, rh);+ const ctx = canvas.getContext("2d")!;+ ctx.translate(pad, pad);+ ctx.scale(q, q);+ ctx.translate(-left, -top);+ await paintElement(ctx, el, frame, env);+ const pixels = ctx.getImageData(0, 0, rw, rh).data;+ const depth = inkDepth(pixels, rw, rh);+ // A title flies into its last letter; anything else into its deepest ink.+ const box = isText ? await lastGlyphBox(el as TextOverlay, frame, env) : null;+ const toRaster = (v: number, from: number) => (v - from) * q + pad;+ const x0 = box ? Math.max(0, Math.floor(toRaster(box.x0, left))) : 0;+ const x1 = box ? Math.min(rw, Math.ceil(toRaster(box.x1, left))) : rw;+ const y0 = box ? Math.max(0, Math.floor(toRaster(box.y0, top))) : 0;+ const y1 = box ? Math.min(rh, Math.ceil(toRaster(box.y1, top))) : rh;+ let best = -1;+ let bx = rw / 2;+ let by = rh / 2;+ const scan = (ax: number, bx1: number, ay: number, by1: number) => {+ for (let y = ay; y < by1; y++)+ for (let x = ax; x < bx1; x++) {+ const d = depth[y * rw + x];+ if (d > best) {+ best = d;+ bx = x;+ by = y;+ }+ }+ };+ scan(x0, x1, y0, y1);+ // A last letter with no ink of its own (a mark too thin to measure) falls+ // back to the deepest ink anywhere.+ if (best <= 0) scan(0, rw, 0, rh);+ return {+ x: (bx + 0.5 - pad) / q + left - b.cx,+ y: (by + 0.5 - pad) / q + top - b.cy,+ // Distances run center to center; the antialiased edge sits up to a+ // pixel short of the last ink center, so that pixel is not counted.+ r: Math.max(0.5, best - 1) / q,+ };+}++/** Each pixel's distance to the nearest pixel outside the ink, in raster px:+ * the exact Euclidean transform (Felzenszwalb–Huttenlocher), one pass down+ * the columns and one along the rows. A dive's landing scale is read off this+ * depth, so an estimate that runs long leaves a corner of the frame uncovered. */+function inkDepth(rgba: Uint8ClampedArray, w: number, h: number): Float32Array {+ const INF = 1e20;+ const n = Math.max(w, h);+ const f = new Float64Array(n);+ const d = new Float64Array(n);+ const v = new Int32Array(n);+ const z = new Float64Array(n + 1);+ // The squared distance along one line, in place.+ const line = (len: number) => {+ let k = 0;+ v[0] = 0;+ z[0] = -INF;+ z[1] = INF;+ for (let q = 1; q < len; q++) {+ let s = (f[q] + q * q - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);+ while (s <= z[k]) {+ k--;+ s = (f[q] + q * q - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);+ }+ k++;+ v[k] = q;+ z[k] = s;+ z[k + 1] = INF;+ }+ k = 0;+ for (let q = 0; q < len; q++) {+ while (z[k + 1] < q) k++;+ d[q] = (q - v[k]) * (q - v[k]) + f[v[k]];+ }+ };+ const sq = new Float64Array(w * h);+ for (let i = 0; i < w * h; i++) sq[i] = rgba[i * 4 + 3] >= 128 ? INF : 0;+ for (let x = 0; x < w; x++) {+ for (let y = 0; y < h; y++) f[y] = sq[y * w + x];+ line(h);+ for (let y = 0; y < h; y++) sq[y * w + x] = d[y];+ }+ const out = new Float32Array(w * h);+ for (let y = 0; y < h; y++) {+ for (let x = 0; x < w; x++) f[x] = sq[y * w + x];+ line(w);+ for (let x = 0; x < w; x++) out[y * w + x] = Math.sqrt(d[x]);+ }+ return out;+}++/** The box the last drawn character of a title takes, in frame px — laid out+ * the way `paintText` lays it out. */+async function lastGlyphBox(+ o: TextOverlay,+ frame: PaintFrame,+ env: RenderEnv+): Promise<{ x0: number; x1: number; y0: number; y1: number } | null> {+ const fpx = o.size * frame.scale;+ const cssFont = textCssFont(o, fpx, env);+ await ensureFontLoaded(cssFont, o.text);+ const ctx = newCanvas(env, 1, 1).getContext("2d")!;+ ctx.font = cssFont;+ if ("letterSpacing" in ctx) ctx.letterSpacing = `${(o.letterSpacing ?? 0) * fpx}px`;+ const measure = (line: string) => ctx.measureText(line).width;+ const lines = wrapTextToRoom(o.text, textWrapRoom(o, frame.width), measure).split("\n");+ const widths = lines.map(measure);+ const maxW = o.wrapWidth ? textWrapRoom(o, frame.width) : Math.max(...widths, 1);+ const lineH = fpx * (o.lineHeight ?? LINE_HEIGHT);+ const cx = o.x * frame.width;+ const cy = o.y * frame.height;+ const align = o.align ?? "center";+ let i = lines.length - 1;+ while (i >= 0 && !lines[i].trim()) i--;+ if (i < 0) return null;+ const chars = [...lines[i].trimEnd()];+ const center =+ align === "center" ? cx : align === "left" ? cx - maxW / 2 + widths[i] / 2 : cx + maxW / 2 - widths[i] / 2;+ const left = center - widths[i] / 2;+ const y0 = cy - (lines.length * lineH) / 2 + lineH * i;+ return {+ x0: left + measure(chars.slice(0, -1).join("")),+ x1: left + measure(chars.join("")),+ y0,+ y1: y0 + lineH,+ };+}+function pngBlob(canvas: HTMLCanvasElement, env: RenderEnv): Promise<Blob> {if (env.canvasToPngBlob) return env.canvasToPngBlob(canvas);return new Promise((resolve, reject) =>@@ -680,12 +915,41 @@ export async function renderElementPng(env: RenderEnv,phase?: PaintPhase): Promise<Blob> {+ return pngBlob(await renderElementCanvas(overlay, width, height, env, phase), env);+}++/** The same picture left on its canvas, for a caller that draws it straight+ * away — a picture redrawn every frame never pays a PNG round trip. */+export async function renderElementCanvas(+ overlay: Overlay,+ width: number,+ height: number,+ env: RenderEnv,+ phase?: PaintPhase+): Promise<HTMLCanvasElement> {+ const canvas = newCanvas(env, width, height);+ await paintElementInto(canvas, overlay, env, phase);+ return canvas;+}++/** Paint one element over the whole of a canvas the caller owns, cleared+ * first — the full-frame picture `renderElementPng` encodes, drawn into a+ * surface that is reused frame after frame. */+export async function paintElementInto(+ canvas: HTMLCanvasElement,+ overlay: Overlay,+ env: RenderEnv,+ phase?: PaintPhase+): Promise<void> {+ const { width, height } = canvas;// Element sizes are design pixels with a 1080 short side, so scaling by the// short side keeps them the same visual size in any aspect and resolution.const scale = Math.min(width, height) / 1080;- const canvas = newCanvas(env, width, height);const ctx = canvas.getContext("2d")!;const frame: PaintFrame = { width, height, scale, phase };+ ctx.setTransform(1, 0, 0, 1, 0, 0);+ ctx.globalAlpha = 1;+ ctx.clearRect(0, 0, width, height);ctx.globalAlpha = overlay.opacity ?? 1;if (overlay.rotation) {@@ -695,6 +959,7 @@ export async function renderElementPng(ctx.rotate((overlay.rotation * Math.PI) / 180);ctx.translate(-cx, -cy);}+ if (phase?.dive) applyDiveView(ctx, phase.dive);await paintElement(ctx, overlay, frame, env);if (overlay.mask) {@@ -711,7 +976,6 @@ export async function renderElementPng(ctx.getTransform());}- return pngBlob(canvas, env);}/** An element's resting box in output pixels (center + size), rotation left@@ -984,10 +1248,13 @@ export async function renderOverlayFrames(halfW = r;halfH = r;}- const x0 = Math.max(0, Math.floor(extent.x0 * width - halfW));- const y0 = Math.max(0, Math.floor(extent.y0 * height - halfH));- const x1 = Math.min(width, Math.ceil(extent.x1 * width + halfW));- const y1 = Math.min(height, Math.ceil(extent.y1 * height + halfH));+ // A dive flies into the element until it fills the frame, so its flight+ // takes the whole frame.+ const focus = divesAt(anim) ? await measureDiveFocus(overlay, width / height, env) : null;+ const x0 = focus ? 0 : Math.max(0, Math.floor(extent.x0 * width - halfW));+ const y0 = focus ? 0 : Math.max(0, Math.floor(extent.y0 * height - halfH));+ const x1 = focus ? width : Math.min(width, Math.ceil(extent.x1 * width + halfW));+ const y1 = focus ? height : Math.min(height, Math.ceil(extent.y1 * height + halfH));const rw = Math.max(2, x1 - x0);const rh = Math.max(2, y1 - y0);@@ -1009,6 +1276,9 @@ export async function renderOverlayFrames(// Back to the origin the painters draw around, so they stay unaware of// any of this.ctx.translate(-overlay.x * width, -overlay.y * height);+ // A dive's view goes inside the pose, so the type is drawn as vectors at+ // the size it is seen and stays sharp however far in the flight gets.+ if (focus && ev.dive) applyDiveView(ctx, diveView(ev.dive, focus, ev, overlay, frame));let el: Overlay = applyWordDraw({ ...overlay, rotation: undefined, opacity: undefined },tLocal,