Open source CapCut alternative.
TypeScript
feat(site): add title, shape, effect and transition looks [rebuild]
David Lecc2bcc803578
diff --git a/site/packages/effects-kit/src/anim.ts b/site/packages/effects-kit/src/anim.tsindex 38804c81..c6d10aa0 100644--- a/site/packages/effects-kit/src/anim.ts+++ b/site/packages/effects-kit/src/anim.ts@@ -25,8 +25,10 @@ import {PER_UNIT_LOOP_IDS,} from "./motion/catalog";import { evalPreset, evalWhole, presetExtent, sampleProperties } from "./motion/evaluate";+import type { OverlayFontCycle } from "./fontCycle";import type { MotionPose, MotionPreset } from "./motion/types";import type { OverlayWords } from "./words";+import { splitScale } from "./stretch";export type OverlayAnimStyle =| "fade"@@ -37,8 +39,10 @@ export type OverlayAnimStyle =| "slideup"| "slidedown"| "typewriter" // text only; other kinds render it as a fade+ | "count" // text only, like typewriter| "wipe"| "dive"+ | "disintegrate"// 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.@@ -53,7 +57,10 @@ export type OverlayAnimStyle =| "streak"| "tumble"| "scatter"- | "slot";+ | "slot"+ | "blur"+ | "glitch"+ | "flicker";export type OverlayLoopStyle =| "pulse"@@ -88,7 +95,10 @@ export type GlyphAnimStyle =| "streak"| "tumble"| "scatter"- | "slot";+ | "slot"+ | "blur"+ | "glitch"+ | "flicker";/*** A ramp at one end of the element.@@ -106,6 +116,10 @@ export interface OverlayEdge {/** The typing bar, on an entrance that types (typewriter). Every other* entrance ignores it. */caret?: OverlayCaret;+ /** A per-letter style's hand-off, 0..1: the share of the ramp spent+ * passing the motion from the first letter to the last (0 = every letter+ * together, near 1 = one at a time). Absent = the style's own. */+ stagger?: number;}/**@@ -177,6 +191,9 @@ export interface OverlayAnim {* ignores it, the way typewriter is offered on titles alone. */words?: OverlayWords;hit?: OverlayHit;+ /** A stretch of typefaces the title cycles through (see fontCycle.ts).+ * Text only, like the word effects. */+ fonts?: OverlayFontCycle;}/** The motion a slot plays: the one it carries, or the catalog entry it@@ -216,9 +233,12 @@ export const OVERLAY_HIT_MAX_SECONDS = 1;export const OVERLAY_HIT_DEFAULT_SECONDS = 0.3;/** The edges that only play on characters — typing them out, rolling each up- * a reel. Every other kind is offered the rest. */+ * a reel, counting the numbers up. 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+ (id) =>+ !!MOTION.edges[id].animate.typed ||+ !!MOTION.edges[id].animate.roll ||+ !!MOTION.edges[id].animate.counted) as OverlayAnimStyle[];/** The edges that fly the view into the element's ink until it fills the* frame. */@@ -260,6 +280,24 @@ export const OVERLAY_ANIM_MIN_SECONDS = 0.1;export const OVERLAY_ANIM_MAX_SECONDS = 2;export const OVERLAY_ANIM_DEFAULT_SECONDS = 0.5;+/** The longest ramp a style may run: its own cap, else the shared one. */+export const edgeMaxSeconds = (style: string | undefined): number =>+ (style ? edgePreset(style)?.maxSeconds : undefined) ?? OVERLAY_ANIM_MAX_SECONDS;++/** The most of a ramp the letter hand-off may take: at 1 each letter would+ * get no time of its own to move. */+export const EDGE_STAGGER_MAX = 0.95;++/** The hand-off a per-letter edge plays with — its own, else its style's;+ * undefined for a style that moves the element as one piece. */+export const edgeStagger = (slot: OverlayEdge | undefined): number | undefined => {+ const sel = edgeMotion(slot)?.selector;+ if (!sel) {+ return undefined;+ }+ return slot?.stagger ?? sel.spread;+};+/*** Whether a style changes the element's pixels rather than moving them as one* picture. A cached bitmap under a transform can't express these, so both@@ -269,9 +307,11 @@ export function bakesPixels(slot: OverlayEdge | undefined, isText: boolean): booconst preset = edgeMotion(slot);if (!preset) return false;if (preset.animate.reveal) return true; // a wipe clips whatever it is over+ if (preset.animate.erode) return true; // and a disintegration eats itif (!isText) return false;- // A typewriter types characters; a selector moves them one at a time.- return !!preset.animate.typed || !!preset.selector;+ // A typewriter types characters, a count rewrites its numbers; a selector+ // moves them one at a time.+ return !!preset.animate.typed || !!preset.animate.counted || !!preset.selector;}/**@@ -314,6 +354,8 @@ export interface GlyphPhase {/** 0 at the far end of the ramp, 1 at rest. */p: number;exiting: boolean;+ /** The slot's own hand-off, over the style's (see `OverlayEdge.stagger`). */+ stagger?: number;}/** Where a per-glyph loop stands: one number, 0 to 1 around the cycle. Each@@ -327,6 +369,7 @@ export interface GlyphLoopPhase {/** The transform an animation contributes at one moment. dx/dy are design px* (1080 short side); rotate is degrees; alpha multiplies the element opacity;* textProgress (0..1, typewriter only) is the share of characters shown;+ * countProgress (0..1, count only) is the share of its value each number shows;* reveal (0..1, wipe) is the share of the box uncovered from its left edge;* glyphs and glyphLoop replace the whole-element transform with a* per-character one, and both apply at once (a title can fly its letters in@@ -334,11 +377,20 @@ export interface GlyphLoopPhase {export interface OverlayAnimState {dx: number;dy: number;+ /** The larger axis; `sx`/`sy` stretch each axis over it (see stretch.ts),+ * so a whole-element flip turns one axis through zero. */scale: number;+ sx?: number;+ sy?: number;rotate: number;alpha: number;textProgress?: number;+ countProgress?: number;reveal?: number;+ /** 0..1 share of the ink eaten away (disintegrate). */+ erode?: number;+ /** 0..1 strength of the electric arcs over the element (zap hit). */+ zap?: number;glyphs?: GlyphPhase;glyphLoop?: GlyphLoopPhase;/** How far the view has flown into the element's deepest ink, 0..1 (dive).@@ -350,6 +402,8 @@ export interface OverlayAnimState {caret?: boolean;/** Multiplies the element's colors (a hit that darkens); absent = 1. */brightness?: number;+ /** Defocus the ramp adds over the element's own, design px (blur). */+ blur?: number;}/** How one character sits at a moment: offsets in design px, a scale per axis@@ -364,6 +418,11 @@ export interface GlyphAnimState {/** Characters its reel still rolls past before it lands (slot); absent or* 0 = the character alone. `slotReel` says what to draw. */roll?: number;+ /** Defocus, design px; absent = sharp. */+ blur?: number;+ /** How far a red and a cyan copy pull apart either side, design px; absent+ * = one copy. */+ split?: number;}const IDLE: OverlayAnimState = { dx: 0, dy: 0, scale: 1, rotate: 0, alpha: 1 };@@ -386,6 +445,8 @@ function asGlyph(pose: MotionPose, index: number, count: number): GlyphAnimStaterotate: pose.rotate,alpha: pose.alpha,...(pose.roll ? { roll: pose.roll } : {}),+ ...(pose.blur ? { blur: pose.blur } : {}),+ ...(pose.split ? { split: pose.split } : {}),};}@@ -401,7 +462,7 @@ export function glyphAnimAt(phase: GlyphPhase, index: number, count: number): Gl// whole-element style leaves its glyphs where they are.if (!preset?.selector) return GLYPH_IDLE;const n = Math.max(1, count);- return asGlyph(evalPreset(preset, p, index, n, exiting), index, n);+ return asGlyph(evalPreset(preset, p, index, n, exiting, 0, phase.stagger), index, n);}/** Where character `index` of `count` sits with everything applied — the edge@@ -419,6 +480,8 @@ export function glyphStateAt(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);+ const blur = (ramp.blur ?? 0) + (loop.blur ?? 0);+ const split = (ramp.split ?? 0) + (loop.split ?? 0);return {dx: ramp.dx + loop.dx,dy: ramp.dy + loop.dy,@@ -427,6 +490,8 @@ export function glyphStateAt(rotate: ramp.rotate + loop.rotate,alpha: ramp.alpha * loop.alpha,...(roll ? { roll } : {}),+ ...(blur ? { blur } : {}),+ ...(split ? { split } : {}),};}@@ -454,7 +519,7 @@ function edgeState(if (preset.selector && isText)return {...IDLE,- glyphs: { style: slot.style, preset: slot.preset, p: clamped, exiting },+ glyphs: { style: slot.style, preset: slot.preset, p: clamped, exiting, ...(slot.stagger !== undefined ? { stagger: slot.stagger } : {}) },};// Anything with no characters to hand the motion to plays it once over the// whole box.@@ -462,15 +527,18 @@ function edgeState(const state: OverlayAnimState = {dx: pose.dx,dy: pose.dy,- scale: (pose.sx + pose.sy) / 2,+ ...splitScale(pose.sx, pose.sy),rotate: pose.rotate,alpha: pose.alpha,};// A typewriter on anything but text has no characters to reveal, so the// element just holds — the same as it always did.if (pose.typed !== undefined) state.textProgress = pose.typed;+ if (pose.counted !== undefined) state.countProgress = pose.counted;if (pose.reveal !== undefined) state.reveal = pose.reveal;+ if (pose.erode !== undefined) state.erode = pose.erode;if (pose.dive !== undefined) state.dive = pose.dive;+ if (pose.blur) state.blur = pose.blur;return state;}@@ -493,7 +561,7 @@ function applyLoop(state.dx += pose.dx;state.dy += pose.dy;state.rotate += pose.rotate;- state.scale *= (pose.sx + pose.sy) / 2;+ foldAxes(state, pose.sx, pose.sy);state.alpha *= pose.alpha;}@@ -533,20 +601,27 @@ export function evalOverlayAnim(return state;}+/** How long the hit plays: a stored hit without seconds plays the default,+ * the length the inspector shows for it. */+const hitSeconds = (slot: OverlayHit): number => (slot.seconds > 0 ? slot.seconds : OVERLAY_HIT_DEFAULT_SECONDS);+/** The hit's contribution at `tLocal`: nothing outside its window; inside,* the preset's pose folded onto what the other slots left, as one piece. */function applyHit(state: OverlayAnimState, slot: OverlayHit, tLocal: number): void {const preset = hitMotion(slot);- const q = (tLocal - slot.at) / slot.seconds;+ const q = (tLocal - slot.at) / hitSeconds(slot);if (!preset || !(q > 0 && q < 1)) return;const pose = evalWhole(preset, q, false);state.dx += pose.dx;state.dy += pose.dy;state.rotate += pose.rotate;- state.scale *= (pose.sx + pose.sy) / 2;+ foldAxes(state, pose.sx, pose.sy);state.alpha *= pose.alpha;if (slot.darken && pose.brightness !== undefined && pose.brightness !== 1)state.brightness = (state.brightness ?? 1) * pose.brightness;+ if (pose.zap !== undefined) {+ state.zap = pose.zap;+ }}/** Where the hit plays inside [0, dur], or null when there is none. The frame@@ -558,10 +633,14 @@ export function hitWindow(const slot = anim?.hit;if (!slot || !hitMotion(slot)) return null;const start = Math.max(0, Math.min(dur, slot.at));- const end = Math.max(start, Math.min(dur, slot.at + slot.seconds));+ const end = Math.max(start, Math.min(dur, slot.at + hitSeconds(slot)));return end - start > 1e-3 ? { start, end } : null;}+/** Whether the hit paints arcs (zap): pixels the stamped export bakes frame+ * by frame across the hit's window. */+export const hitZaps = (anim: OverlayAnim | undefined): boolean => !!hitMotion(anim?.hit)?.animate.zap;+/** The largest scale a hit reaches, so the frame sampler can pad its crop. */export function hitScale(anim: OverlayAnim | undefined): number {const preset = hitMotion(anim?.hit);@@ -640,10 +719,23 @@ function applyMove(state: OverlayAnimState, slot: OverlayMove, tLocal: number, dstate.dx += pose.dx * k;state.dy += pose.dy * k;state.rotate += pose.rotate * k;- state.scale *= 1 + ((pose.sx + pose.sy) / 2 - 1) * k;+ foldAxes(state, 1 + (pose.sx - 1) * k, 1 + (pose.sy - 1) * k);state.alpha *= 1 + (pose.alpha - 1) * k;}+/** Multiply a per-axis scale into the state, axis by axis, and split the+ * result back into its uniform scale and stretch. */+function foldAxes(state: OverlayAnimState, ax: number, ay: number): void {+ const next = splitScale(state.scale * (state.sx ?? 1) * ax, state.scale * (state.sy ?? 1) * ay);+ state.scale = next.scale;+ delete state.sx;+ delete state.sy;+ if (next.sx !== undefined) {+ state.sx = next.sx;+ state.sy = next.sy;+ }+}+/** The loop's exact cycle length in seconds (frame sequences render one cycle* and repeat it), or null when the element has no loop. */export function loopPeriod(anim: OverlayAnim | undefined): number | null {@@ -656,7 +748,8 @@ export function loopPeriod(anim: OverlayAnim | undefined): number | null {/** Whether any slot is set (an element with an empty anim object is static). */export function hasOverlayAnim(anim: OverlayAnim | undefined): boolean {return (- !!anim && (!!anim.in || !!anim.out || !!anim.loop || !!anim.move || !!anim.words || !!anim.hit)+ !!anim &&+ (!!anim.in || !!anim.out || !!anim.loop || !!anim.move || !!anim.words || !!anim.hit || !!anim.fonts));}@@ -702,11 +795,15 @@ export function holdMoveKeys(const keys = track.map((t) => {const p = pose(t);+ // Baked axes keep a hold's squash: the key's scale is the larger axis+ // and scaleX/scaleY stretch each one over it.+ const size = splitScale(1 + (p.sx - 1) * k, 1 + (p.sy - 1) * k);return {t: Math.round(t * dur * 1000) / 1000,x: rest.x + p.dx * px * k,y: rest.y + p.dy * px * k,- scale: 1 + ((p.sx + p.sy) / 2 - 1) * k,+ scale: size.scale,+ ...(size.sx !== undefined ? { scaleX: size.sx, scaleY: size.sy } : {}),rotation: rest.rotation + p.rotate * k,opacity: 1 + (p.alpha - 1) * k,};diff --git a/site/packages/effects-kit/src/burn.test.ts b/site/packages/effects-kit/src/burn.test.tsnew file mode 100644index 00000000..d8141012--- /dev/null+++ b/site/packages/effects-kit/src/burn.test.ts@@ -0,0 +1,88 @@+import { describe, expect, test } from "bun:test";+import { Canvas } from "skia-canvas";+import { applyEffectToCanvas, burnAt, burnGradient, EFFECT_IDS, effectFilterLines, effectPreviewState, effectRecipe } from "./effects";++type Surface = HTMLCanvasElement;+const W = 36;+const H = 64;+const noGrain = () => null;++function black(): Surface {+ const c = new Canvas(W, H) as unknown as Surface;+ const ctx = c.getContext("2d")!;+ ctx.fillStyle = "#000000";+ ctx.fillRect(0, 0, W, H);+ return c;+}++const pixel = (c: Surface, x: number, y: number) => Array.from(c.getContext("2d")!.getImageData(x, y, 1, 1).data);++describe("the light leak's burn course", () => {+ test("a light leak on its burn course draws the burn; drift and other effects keep their own", () => {+ expect(effectRecipe({ effect: "lightleak", leak: "burn" })).toBe("burn");+ expect(effectRecipe({ effect: "lightleak", leak: "drift" })).toBe("lightleak");+ expect(effectRecipe({ effect: "lightleak" })).toBe("lightleak");+ expect(effectRecipe({ effect: "flash", leak: "burn" })).toBe("flash");+ expect(EFFECT_IDS as string[]).not.toContain("burn");+ });+});++describe("the film burn's course", () => {+ test("it climbs from the bottom edge: the top is still clear while the bottom has caught", () => {+ const b = burnAt(0.1, 1, 1);+ const first = b.stops[0];+ const last = b.stops[b.stops.length - 1];+ expect(first.at).toBe(0);+ expect(last.at).toBe(1);+ expect(first.a).toBeGreaterThan(0.9);+ expect(last.a).toBe(0);+ });++ test("it covers the frame near white at the flare and hands the picture back by its end", () => {+ const flare = burnAt(0.45, 1, 1);+ expect(flare.stops.every((s) => s.a === 1)).toBe(true);+ expect(flare.stops[0].rgb[2]).toBeGreaterThan(150);+ expect(burnAt(1, 1, 1).alpha).toBe(0);+ });++ test("it plays across the element's own length", () => {+ expect(effectPreviewState("burn", 1, 0.9, undefined, undefined, 2).burn).toEqual(burnAt(0.9, 2, 1));+ });++ test("the CSS twin carries every stop the canvas draws", () => {+ const b = burnAt(0.15, 1, 1);+ const css = burnGradient(b);+ for (const s of b.stops) expect(css).toContain(`${(s.at * 100).toFixed(2)}%`);+ });+});++describe("the film burn on a frame", () => {+ test("mid-flare the whole frame is the burn's own color", () => {+ const c = black();+ applyEffectToCanvas(c, black(), "burn", 1, 0.45, noGrain, undefined, undefined, 1);+ for (const y of [1, H / 2, H - 2]) {+ const [r, g] = pixel(c, W / 2, y);+ expect(r).toBeGreaterThan(240);+ expect(g).toBeGreaterThan(200);+ }+ });++ test("early on the bottom has caught and the top still shows the picture", () => {+ const c = black();+ applyEffectToCanvas(c, black(), "burn", 1, 0.08, noGrain, undefined, undefined, 1);+ expect(pixel(c, W / 2, H - 2)[0]).toBeGreaterThan(200);+ expect(pixel(c, W / 2, 1)[0]).toBeLessThan(10);+ });+});++describe("the film burn in ffmpeg", () => {+ test("one geq of one size whatever the length, gated to the element", () => {+ const line = (d: number) => effectFilterLines("in", "out", "burn", 1, 2, 2 + d, 1080, 1920, "t")![0];+ expect(line(0.5).match(/geq=/g)!.length).toBe(1);+ expect(line(10).match(/geq=/g)!.length).toBe(1);+ expect(Math.abs(line(10).length - line(0.5).length)).toBeLessThan(40);+ expect(line(0.5)).toContain("enable='gte(t,2)*lt(t,2.5)'");+ expect(line(0.5).startsWith("[in]")).toBe(true);+ expect(line(0.5).endsWith("[out]")).toBe(true);+ });+});diff --git a/site/packages/effects-kit/src/caretHit.test.ts b/site/packages/effects-kit/src/caretHit.test.tsindex e5e2ea32..e8f31b82 100644--- a/site/packages/effects-kit/src/caretHit.test.ts+++ b/site/packages/effects-kit/src/caretHit.test.ts@@ -7,6 +7,7 @@ import {evalOverlayAnim,hasOverlayAnim,hitWindow,+ OVERLAY_HIT_DEFAULT_SECONDS,OVERLAY_HIT_STYLE_IDS,type OverlayAnim,} from "./anim";@@ -142,6 +143,12 @@ describe("the press", () => {expect(hitWindow({ hit: { style: "gone", at: 1, seconds: 0.3 } }, 4)).toBeNull();});+ test("a stored press without seconds plays for the default length", () => {+ const bare = { hit: { style: "press", at: 1 } } as unknown as OverlayAnim;+ expect(hitWindow(bare, 4)).toEqual({ start: 1, end: 1 + OVERLAY_HIT_DEFAULT_SECONDS });+ expect(evalOverlayAnim(bare, 1.1, 4)).not.toEqual(evalOverlayAnim(bare, 2, 4));+ });+test("darkenCanvas multiplies color and leaves alpha", () => {const c = new Canvas(4, 4);const ctx = c.getContext("2d") as unknown as CanvasRenderingContext2D;diff --git a/site/packages/effects-kit/src/clipEffects.test.ts b/site/packages/effects-kit/src/clipEffects.test.tsindex bc60b5a9..c154fd60 100644--- a/site/packages/effects-kit/src/clipEffects.test.ts+++ b/site/packages/effects-kit/src/clipEffects.test.ts@@ -1,6 +1,6 @@import { describe, expect, test } from "bun:test";import { Canvas } from "skia-canvas";-import { applyClipEffects, applyEffectToCanvas, type ClipEffect } from "./effects";+import { applyClipEffects, applyEffectToCanvas, glitchHitAt, type ClipEffect } from "./effects";type Surface = HTMLCanvasElement;const S = 64;@@ -41,3 +41,26 @@ describe("filter effects in a row", () => {expect(pixel(joined, 2, 2)[3]).toBe(0);});});++describe("the glitch on a full frame", () => {+ test("a split doubles the picture: the moved frame with the unmoved one lightened over it", () => {+ // A white bar down the middle of a black frame, drawn through a split.+ const step = Array.from({ length: 400 }, (_, i) => i).find((i) => glitchHitAt(i, 1)?.kind === "split")!;+ const shift = glitchHitAt(step, 1)!.shift;+ const c = new Canvas(S, S) as unknown as Surface;+ const ctx = c.getContext("2d")!;+ ctx.fillStyle = "#000000";+ ctx.fillRect(0, 0, S, S);+ ctx.fillStyle = "#FFFFFF";+ ctx.fillRect(S / 2 - 2, 0, 4, S);+ applyEffectToCanvas(c, scratch(), "glitch", 1, (step + 0.5) / 15, noGrain);+ // The bar shows twice: where it was, at the ghost's share, and where the+ // split moved it, at full strength.+ const moved = Math.round(S / 2 + shift * S);+ const at = (x: number) => pixel(c, x, S / 4)[1];+ expect(at(S / 2)).toBeGreaterThan(90);+ expect(at(moved)).toBeGreaterThan(at(S / 2));+ // Between the two bars the frame stays dark.+ expect(at(Math.round((S / 2 + moved) / 2))).toBeLessThan(at(S / 2));+ });+});diff --git a/site/packages/effects-kit/src/count.test.ts b/site/packages/effects-kit/src/count.test.tsnew file mode 100644index 00000000..df6ba551--- /dev/null+++ b/site/packages/effects-kit/src/count.test.ts@@ -0,0 +1,98 @@+import { describe, expect, test } from "bun:test";+import { bakesPixels, edgeMaxSeconds, evalOverlayAnim, OVERLAY_ANIM_MAX_SECONDS, TEXT_ONLY_ANIM_STYLE_IDS, type OverlayAnim } from "./anim";+import { countStep, countText } from "./count";+import { evalOverlayFrame } from "./keys";+import { elementAtMoment, planAnimatedLayers } from "./render";+import type { ShapeOverlay, TextOverlay } from "./types";++const title = (text: string, anim: OverlayAnim): TextOverlay => ({+ id: "t",+ text,+ start: 0,+ end: 5,+ x: 0.5,+ y: 0.5,+ size: 60,+ font: "sf",+ weight: 700,+ color: "#fff",+ plate: false,+ shadow: false,+ anim,+});++/** What the painter draws for `o` at `tLocal`. */+const drawnAt = (o: TextOverlay, tLocal: number): string =>+ (elementAtMoment(o, tLocal, evalOverlayFrame(o, tLocal)).el as TextOverlay).text;++describe("countText", () => {+ test("counts a percentage up from zero", () => {+ expect(countText("100 %", 0)).toBe("0 %");+ expect(countText("100 %", 0.5)).toBe("50 %");+ expect(countText("100 %", 1)).toBe("100 %");+ });++ test("keeps decimals, leading zeros and separators as written", () => {+ expect(countText("12.5", 0)).toBe("0.0");+ expect(countText("12.5", 0.5)).toBe("6.2");+ expect(countText("12.5", 1)).toBe("12.5");+ expect(countText("007", 0.5)).toBe("003");+ expect(countText("1,000 views", 0.5)).toBe("500 views");+ expect(countText("1,000 views", 1)).toBe("1,000 views");+ });++ test("leaves a title with no numbers unchanged", () => {+ expect(countText("Loading", 0)).toBe("Loading");+ expect(countText("Loading", 0.5)).toBe("Loading");+ });++ test("counts every number in the title at once", () => {+ expect(countText("3 of 10", 0.5)).toBe("1 of 5");+ expect(countText("3 of 10", 1)).toBe("3 of 10");+ });++ test("steps once per change in the shown text", () => {+ expect(countStep("100 %", 0)).toBe(0);+ expect(countStep("100 %", 0.5)).toBe(50);+ expect(countStep("3 of 10", 1)).toBe(13);+ expect(countStep("Loading", 1)).toBe(0);+ });+});++describe("the count entrance", () => {+ const anim: OverlayAnim = { in: { style: "count", seconds: 2 }, out: { style: "count", seconds: 1 } };++ test("is a text-only edge that bakes its pixels", () => {+ expect(TEXT_ONLY_ANIM_STYLE_IDS).toContain("count");+ expect(bakesPixels(anim.in, true)).toBe(true);+ expect(bakesPixels(anim.in, false)).toBe(false);+ expect(edgeMaxSeconds("count")).toBeGreaterThan(OVERLAY_ANIM_MAX_SECONDS);+ expect(edgeMaxSeconds("fade")).toBe(OVERLAY_ANIM_MAX_SECONDS);+ });++ test("counts up over the entrance, holds, and counts down on the exit", () => {+ const o = title("100 %", anim);+ expect(drawnAt(o, 0)).toBe("0 %");+ expect(Number(drawnAt(o, 1).split(" ")[0])).toBeGreaterThanOrEqual(49);+ expect(Number(drawnAt(o, 1).split(" ")[0])).toBeLessThanOrEqual(51);+ expect(drawnAt(o, 2.5)).toBe("100 %");+ expect(Number(drawnAt(o, 4.5).split(" ")[0])).toBeLessThan(60);+ expect(evalOverlayAnim(anim, 3, 5, true).countProgress).toBeUndefined();+ });++ test("leaves every other kind holding", () => {+ expect(evalOverlayAnim(anim, 1, 5, false).alpha).toBe(1);+ const shape = { ...title("", anim), kind: "shape" } as unknown as ShapeOverlay;+ expect(planAnimatedLayers(shape, 5).length).toBe(3);+ });++ test("the canvas plan draws one window per step, then the final value", () => {+ const o = title("10 %", { in: { style: "count", seconds: 2 } });+ const layers = planAnimatedLayers(o, 5);+ const texts = layers.map((l) => (l.overlay as TextOverlay).text);+ expect(texts.length).toBe(11);+ expect(texts[0]).toBe("0 %");+ expect(texts.at(-1)).toBe("10 %");+ expect(new Set(texts.slice(0, 10)).size).toBe(10);+ });+});diff --git a/site/packages/effects-kit/src/count.ts b/site/packages/effects-kit/src/count.tsnew file mode 100644index 00000000..19615e12--- /dev/null+++ b/site/packages/effects-kit/src/count.ts@@ -0,0 +1,80 @@+/**+ * The count entrance: every number written in a title counts from zero up to+ * its written value while the entrance plays, and back down on an exit.+ *+ * A number keeps the form it was written in at every step — "12.5" counts in+ * tenths, "007" stays three digits wide, "1,000" keeps its separators — so+ * the line reads the same at the end of the count as it does at rest. Text+ * with no number in it shows unchanged.+ */++/** A run of digits, with thousands separators or a decimal part. */+const NUMBER = /\d{1,3}(?:,\d{3})+(?:\.\d+)?|\d+(?:\.\d+)?/g;++/** Float slack when a count lands on a whole step ("0.57 × 100" is 56.999…). */+const STEP_EPSILON = 1e-7;++/** One written number: its value in its smallest written step, and the form+ * it is drawn back in. */+interface Written {+ units: number;+ decimals: number;+ width: number;+ grouped: boolean;+}++/** Read a matched number. "12.5" is 125 tenths; "007" is 7, three wide. */+function readNumber(match: string): Written {+ const [whole, frac = ""] = match.split(".");+ const digits = whole.replace(/,/g, "");+ return {+ units: Number(digits + frac),+ decimals: frac.length,+ width: digits.startsWith("0") ? digits.length : 1,+ grouped: whole.includes(","),+ };+}++/** How many steps of `w` show at `p` (0..1): the final value only at 1. */+function shownUnits(w: Written, p: number): number {+ if (p >= 1) {+ return w.units;+ }+ if (p <= 0) {+ return 0;+ }+ return Math.floor(w.units * p + STEP_EPSILON);+}++/** `units` steps drawn in the form `w` was written in. */+function formatUnits(units: number, w: Written): string {+ // Split off the decimals, e.g. 125 tenths → "12" + "5", 5 tenths → "0" + "5".+ const s = String(units).padStart(w.decimals + 1, "0");+ const whole = w.decimals > 0 ? s.slice(0, -w.decimals) : s;+ const frac = w.decimals > 0 ? `.${s.slice(-w.decimals)}` : "";++ // Leading zeros and separators as written.+ const padded = whole.padStart(w.width, "0");+ const grouped = w.grouped ? padded.replace(/\B(?=(\d{3})+(?!\d))/g, ",") : padded;+ return grouped + frac;+}++/** `text` with every number counted to `p` (0..1) of its written value:+ * "100 %" at 0.5 is "50 %". */+export function countText(text: string, p: number): string {+ return text.replace(NUMBER, (m) => {+ const w = readNumber(m);+ return formatUnits(shownUnits(w, p), w);+ });+}++/** How far the count in `text` stands at `p`, as one whole number that grows+ * with every step any number takes. Caches key a counted picture by it: two+ * moments with the same step show the same text. */+export function countStep(text: string, p: number): number {+ let step = 0;+ for (const m of text.match(NUMBER) ?? []) {+ step += shownUnits(readNumber(m), p);+ }+ return step;+}diff --git a/site/packages/effects-kit/src/doodle.test.ts b/site/packages/effects-kit/src/doodle.test.tsnew file mode 100644index 00000000..4a5f275a--- /dev/null+++ b/site/packages/effects-kit/src/doodle.test.ts@@ -0,0 +1,154 @@+import { describe, expect, test } from "bun:test";+import { Canvas } from "skia-canvas";+import { DOODLE_FPS, doodleBeat } from "./doodle";+import { evalOverlayFrame, isOverlayAnimated } from "./keys";+import { elementAtMoment, planAnimatedLayers, renderElementCanvas, renderOverlayFrames, type RenderEnv } from "./render";+import type { ShapeOverlay } from "./types";++const env: RenderEnv = {+ fontStack: () => "Arial",+ createCanvas: (w, h) => new Canvas(w, h) as unknown as HTMLCanvasElement,+ canvasToPngBlob: async (c) => new Blob([new Uint8Array(await (c as unknown as Canvas).toBuffer("png"))]),+};++// A doodle over the middle of a square frame for two seconds, in a red fill+// and a green ink.+const doodle: ShapeOverlay = {+ id: "d",+ kind: "shape",+ shape: "doodle",+ start: 1,+ end: 3,+ x: 0.5,+ y: 0.5,+ w: 0.6,+ h: 0.6,+ fill: "#ff0000",+ inks: ["#00ff00"],+};++const SIZE = 300;++/** The inked pixels of a picture: count, and how many fall outside the+ * doodle's box grown by 4%. */+function ink(c: HTMLCanvasElement): { n: number; outside: number; reds: number; greens: number } {+ const d = c.getContext("2d")!.getImageData(0, 0, c.width, c.height).data;+ const lo = (0.5 - 0.3 - 0.04) * SIZE;+ const hi = (0.5 + 0.3 + 0.04) * SIZE;+ let n = 0;+ let outside = 0;+ let reds = 0;+ let greens = 0;+ for (let i = 0; i < d.length; i += 4) {+ if (d[i + 3] < 40) {+ continue;+ }+ n++;+ const x = (i / 4) % c.width;+ const y = Math.floor(i / 4 / c.width);+ if (x < lo || x > hi || y < lo || y > hi) {+ outside++;+ }+ if (d[i] > d[i + 1] + 60) {+ reds++;+ }+ if (d[i + 1] > d[i] + 60) {+ greens++;+ }+ }+ return { n, outside, reds, greens };+}++/** A picture's bytes, to compare two deals. */+const bytes = (c: HTMLCanvasElement) => c.getContext("2d")!.getImageData(0, 0, c.width, c.height).data.join(",");++describe("graffiti doodles", () => {+ test("a doodle is drawn through the per-frame path", () => {+ expect(isOverlayAnimated(doodle)).toBe(true);+ expect(isOverlayAnimated({ ...doodle, shape: "rect" })).toBe(false);+ });++ test("each beat paints one mark inside the box, the same in every render", async () => {+ const seen = new Set<string>();+ let reds = 0;+ let greens = 0;+ for (let beat = 0; beat < 12; beat++) {+ const a = await renderElementCanvas(doodle, SIZE, SIZE, env, { doodle: beat });+ const b = await renderElementCanvas(doodle, SIZE, SIZE, env, { doodle: beat });+ expect(bytes(a)).toBe(bytes(b));+ const m = ink(a);+ expect(m.n).toBeGreaterThan(80);+ expect(m.outside).toBe(0);+ reds += m.reds;+ greens += m.greens;+ seen.add(bytes(a));+ }+ // Every beat deals its own mark, in the fill and the inks both.+ expect(seen.size).toBe(12);+ expect(reds).toBeGreaterThan(0);+ expect(greens).toBeGreaterThan(0);+ });++ test("a tall box keeps marks inside and lets the upright ones stand its height", async () => {+ // A full 9:16 frame: some beat paints from near the top to near the bottom.+ const tall: ShapeOverlay = { ...doodle, w: 1, h: 1 };+ const [W, H] = [180, 320];+ let reach = 0;+ for (let beat = 0; beat < 40; beat++) {+ const c = await renderElementCanvas(tall, W, H, env, { doodle: beat });+ const d = c.getContext("2d")!.getImageData(0, 0, W, H).data;+ let top = H;+ let bottom = 0;+ for (let i = 0; i < d.length; i += 4) {+ if (d[i + 3] >= 40) {+ const y = Math.floor(i / 4 / W);+ top = Math.min(top, y);+ bottom = Math.max(bottom, y);+ }+ }+ reach = Math.max(reach, (bottom - top) / H);+ }+ expect(reach).toBeGreaterThan(0.6);+ });++ test("the paint stays see-through", async () => {+ // Over the beats, most inked pixels let the footage show through.+ let inked = 0;+ let solid = 0;+ for (let beat = 0; beat < 12; beat++) {+ const d = (await renderElementCanvas(doodle, SIZE, SIZE, env, { doodle: beat })).getContext("2d")!.getImageData(0, 0, SIZE, SIZE).data;+ for (let i = 3; i < d.length; i += 4) {+ if (d[i] >= 40) {+ inked++;+ solid += d[i] > 240 ? 1 : 0;+ }+ }+ }+ expect(solid / inked).toBeLessThan(0.5);+ });++ test("two doodles deal apart", async () => {+ const a = await renderElementCanvas(doodle, SIZE, SIZE, env, { doodle: 3 });+ const b = await renderElementCanvas({ ...doodle, id: "e" }, SIZE, SIZE, env, { doodle: 3 });+ expect(bytes(a)).not.toBe(bytes(b));+ });++ test("the canvas export cuts a window per beat, each drawn on its beat", () => {+ const layers = planAnimatedLayers(doodle, doodle.end);+ expect(layers.length).toBe(2 * DOODLE_FPS);+ layers.forEach((l, i) => {+ expect(l.phase?.doodle).toBe(i);+ expect(l.end - l.start).toBeCloseTo(1 / DOODLE_FPS, 6);+ });+ });++ test("the frame sequences deal on the same beats", async () => {+ const at = 0.73;+ const { phase } = elementAtMoment(doodle, at, evalOverlayFrame(doodle, at, 1));+ expect(phase?.doodle).toBe(doodleBeat(at));+ const set = await renderOverlayFrames(doodle, SIZE, SIZE, 30, env);+ // Thirty frames a second for two seconds, three frames to a beat.+ expect(set.entries.length).toBe(60);+ expect(set.images.length).toBe(60);+ });+});diff --git a/site/packages/effects-kit/src/doodle.ts b/site/packages/effects-kit/src/doodle.tsnew file mode 100644index 00000000..7d47caf1--- /dev/null+++ b/site/packages/effects-kit/src/doodle.ts@@ -0,0 +1,376 @@+/**+ * Graffiti doodles: a shape that paints a hand-drawn mark and swaps it for a+ * new one on every beat — a dry-brush smear, a painted X, a burst, a tall+ * painted bar, a brushed ring, a pair of fat loops — often with a thin hand+ * line beside it in another ink. The paint is translucent, so the footage+ * shows through it the way sprayed and brushed paint lets it.+ *+ * The marks are pixels, so one painter draws them for every renderer: the+ * export's baked frames and the preview's doodle canvas alike. Each beat is+ * dealt from a seed, so the same moment always shows the same mark.+ */++/** How often a doodle deals a new mark, a second: every third frame at 30fps. */+export const DOODLE_FPS = 10;++/** The most extra inks a doodle takes beside its fill. */+export const DOODLE_INKS_MAX = 5;++/** A new doodle's paints: a teal fill with cream, red, orange and white+ * inks, the street-graffiti set. */+export const DOODLE_FILL = "#2FAFA2";+export const DOODLE_INKS = ["#F2E6C8", "#E0412F", "#F08A3C", "#FFFFFF"];++/** Keep the inks a call or a document holds that are colors, at most+ * `DOODLE_INKS_MAX` of them. */+export function cleanInks(inks: unknown): string[] {+ if (!Array.isArray(inks)) {+ return [];+ }+ return inks.filter((c): c is string => typeof c === "string" && c.trim().length > 0).slice(0, DOODLE_INKS_MAX);+}++/** The mark a doodle shows at `tLocal` seconds into its element. */+export const doodleBeat = (tLocal: number) => Math.floor(Math.max(0, tLocal) * DOODLE_FPS + 1e-6);++/** The brush marks a beat can deal. */+const MARKS = ["smear", "cross", "burst", "bar", "ring", "loops"] as const;+type Mark = (typeof MARKS)[number];++/** The thin hand lines that ride beside a mark. */+const LINES = ["oval", "strands", "squiggle"] as const;+type Line = (typeof LINES)[number];++/** How often a beat lays a thin line beside its mark. */+const LINE_ODDS = 0.45;++/** A thin line's width, px at 1080. */+const THIN = 7;++/** A bristle's paint strength, low to high: overlapping bristles build to+ * about two thirds cover, so the paint stays see-through and streaked. */+const BRISTLE_ALPHA = [0.22, 0.5] as const;++/** How a brush stroke's bristles end: `flat` all run its length, `round`+ * shorten toward its sides, so the stroke reads as a fat blob of paint. */+type Profile = "flat" | "round";++/** A small seeded generator, so a deal is the same in every renderer. */+function dealer(seed: number): () => number {+ let s = (Math.imul(seed + 1, 2654435761) ^ 0x85ebca6b) >>> 0;+ return () => {+ s = (Math.imul(s ^ (s >>> 15), 2246822519) + 0x6d2b79f5) >>> 0;+ s = (s ^ (s >>> 13)) >>> 0;+ return s / 4294967296;+ };+}++/** A stable number for an element id, so two doodles deal apart. */+export function doodleSeed(id: string): number {+ let h = 2166136261;+ for (let i = 0; i < id.length; i++) {+ h = Math.imul(h ^ id.charCodeAt(i), 16777619);+ }+ return h >>> 0;+}++/** Where a doodle paints: its box's center and size, output px, and the+ * output px per design px. */+export interface DoodleBox {+ cx: number;+ cy: number;+ w: number;+ h: number;+ scale: number;+}++/**+ * A painter working in square units of the box's short side, centered on+ * the box: a tall box is `tall` units high, so marks keep their shape in any+ * box and the upright ones can run its whole height.+ */+interface Brush {+ ctx: CanvasRenderingContext2D;+ rand: () => number;+ box: DoodleBox;+ /** The box's short side, px. */+ unit: number;+ /** The mark's center and turn, units. */+ ox: number;+ oy: number;+ turn: number;+}++/** A unit point as output px, through the mark's turn. */+function px(b: Brush, u: number, v: number): [number, number] {+ const c = Math.cos(b.turn);+ const s = Math.sin(b.turn);+ const ru = u * c - v * s;+ const rv = u * s + v * c;+ return [b.box.cx + (b.ox + ru) * b.unit, b.box.cy + (b.oy + rv) * b.unit];+}++/** Lay one bristle from (x0, y0) to (x1, y1), bowed `bow` px off its line. */+function bristle(b: Brush, x0: number, y0: number, x1: number, y1: number, nx: number, ny: number, bow: number): void {+ const ctx = b.ctx;+ ctx.globalAlpha = BRISTLE_ALPHA[0] + (BRISTLE_ALPHA[1] - BRISTLE_ALPHA[0]) * b.rand();+ ctx.beginPath();+ ctx.moveTo(x0, y0);+ ctx.quadraticCurveTo((x0 + x1) / 2 + nx * bow, (y0 + y1) / 2 + ny * bow, x1, y1);+ ctx.stroke();+}++/** How many bristles a bundle `span` px wide holds, and their width. */+function bristles(b: Brush, span: number): { n: number; width: number } {+ const n = Math.max(10, Math.min(40, Math.round(span / (3 * b.box.scale))));+ return { n, width: (span / n) * 2.2 };+}++/**+ * A dry-brush stroke from a to z: a bundle of bristle lines side by side,+ * each starting and stopping a little apart and laid at its own strength,+ * so the paint reads streaked and broken at the ends. `width` is the+ * bundle's width in units.+ */+function brushStroke(b: Brush, a: [number, number], z: [number, number], width: number, profile: Profile = "flat"): void {+ const [ax, ay] = px(b, a[0], a[1]);+ const [zx, zy] = px(b, z[0], z[1]);+ const len = Math.hypot(zx - ax, zy - ay) || 1;+ const nx = -(zy - ay) / len;+ const ny = (zx - ax) / len;+ const span = width * b.unit;+ const { n, width: lw } = bristles(b, span);+ const ctx = b.ctx;+ ctx.lineCap = "round";+ ctx.lineWidth = lw;+ for (let i = 0; i < n; i++) {+ // Each bristle sits at its place across the bundle, bowed a little. A+ // round stroke's bristles cover the length an ellipse has there.+ const across = i / (n - 1) - 0.5;+ const off = across * span;+ const cut = profile === "round" ? (1 - Math.sqrt(Math.max(0, 1 - 4 * across * across))) / 2 : 0;+ const t0 = cut + b.rand() * 0.14;+ const t1 = 1 - cut - b.rand() * 0.14;+ const bow = (b.rand() - 0.5) * span * 0.35;+ bristle(+ b,+ ax + (zx - ax) * t0 + nx * off,+ ay + (zy - ay) * t0 + ny * off,+ ax + (zx - ax) * t1 + nx * off,+ ay + (zy - ay) * t1 + ny * off,+ nx,+ ny,+ bow+ );+ }+}++/** A brushed ring: the bundle swept round `sweep` radians of a circle of+ * radius `r` units about the mark's center, `width` units thick. */+function brushRing(b: Brush, r: number, width: number, sweep: number): void {+ const ctx = b.ctx;+ const [cx, cy] = px(b, 0, 0);+ const span = width * b.unit;+ const { n, width: lw } = bristles(b, span);+ const start = b.turn;+ ctx.lineCap = "round";+ ctx.lineWidth = lw;+ for (let i = 0; i < n; i++) {+ // Each bristle runs its own arc, a little short or long at both ends.+ const rad = r * b.unit + (i / (n - 1) - 0.5) * span;+ const a0 = start + b.rand() * 0.3;+ const a1 = start + sweep - b.rand() * 0.3;+ ctx.globalAlpha = BRISTLE_ALPHA[0] + (BRISTLE_ALPHA[1] - BRISTLE_ALPHA[0]) * b.rand();+ ctx.beginPath();+ ctx.arc(cx, cy, Math.max(1, rad), a0, a1);+ ctx.stroke();+ }+}++/** A thin hand line through unit points, each knocked a little off. */+function thinLine(b: Brush, pts: [number, number][], width: number, closed: "open" | "closed"): void {+ const ctx = b.ctx;+ const wobble = 0.012;+ const out = pts.map(([u, v]) => px(b, u + (b.rand() - 0.5) * wobble, v + (b.rand() - 0.5) * wobble));+ ctx.globalAlpha = 0.9;+ ctx.lineCap = "round";+ ctx.lineJoin = "round";+ ctx.lineWidth = width * b.box.scale;+ ctx.beginPath();+ ctx.moveTo(out[0][0], out[0][1]);+ for (let i = 1; i < out.length; i++) {+ const [x, y] = out[i];+ const [px0, py0] = out[i - 1];+ ctx.quadraticCurveTo(px0, py0, (px0 + x) / 2, (py0 + y) / 2);+ }+ if (closed === "closed") {+ ctx.closePath();+ }+ ctx.stroke();+}++/** A ring of unit points around (u, v), radii ru × rv, its radius drifting+ * so it reads drawn by hand. */+function ringPoints(b: Brush, u: number, v: number, ru: number, rv: number, turns: number, steps: number): [number, number][] {+ const pts: [number, number][] = [];+ const start = b.rand() * Math.PI * 2;+ for (let i = 0; i <= steps; i++) {+ const a = start + (i / steps) * Math.PI * 2 * turns;+ const k = 1 + (b.rand() - 0.5) * 0.12;+ pts.push([u + Math.cos(a) * ru * k, v + Math.sin(a) * rv * k]);+ }+ return pts;+}++/** A mark's reach from its center, units: across and down when upright,+ * and whether it turns freely or stands upright. */+interface Reach {+ rx: number;+ ry: number;+ stance: "free" | "upright";+}++/** Place a mark of `reach` inside a box `wide` × `tall` units, at a random+ * spot and turn that keep it inside. A free mark may turn any way, so its+ * reach is a circle; an upright one leans only a little. */+function place(b: Brush, reach: Reach, wide: number, tall: number): void {+ const free = reach.stance === "free";+ const rx = free ? Math.hypot(reach.rx, reach.ry) : reach.rx + reach.ry * 0.1;+ const ry = free ? rx : reach.ry + reach.rx * 0.1;+ b.ox = (b.rand() * 2 - 1) * Math.max(0, wide / 2 - rx);+ b.oy = (b.rand() * 2 - 1) * Math.max(0, tall / 2 - ry);+ b.turn = free ? (b.rand() - 0.5) * Math.PI : (b.rand() - 0.5) * 0.2;+}++/** The share of a reach its strokes' own ends and bristle spill take, kept+ * free inside the box. */+const SPILL = 0.9;++/** Paint one brush mark of kind `mark`, `size` units across, in a box+ * `wide` × `tall` units. */+function paintMark(b: Brush, mark: Mark, size: number, wide: number, tall: number): void {+ const r = (size / 2) * SPILL;+ switch (mark) {+ case "smear": {+ // A fat smear, two passes laid slightly apart.+ place(b, { rx: r, ry: r * 0.45, stance: "free" }, wide, tall);+ brushStroke(b, [-r * 0.8, 0], [r * 0.8, -r * 0.15], 0.42 * size, "round");+ brushStroke(b, [-r * 0.65, r * 0.18], [r * 0.7, r * 0.05], 0.3 * size, "round");+ return;+ }+ case "cross": {+ place(b, { rx: r * 0.75, ry: r, stance: "free" }, wide, tall);+ brushStroke(b, [-r * 0.5, -r * 0.85], [r * 0.5, r * 0.85], 0.26 * size);+ brushStroke(b, [r * 0.5, -r * 0.85], [-r * 0.5, r * 0.85], 0.26 * size);+ return;+ }+ case "burst": {+ place(b, { rx: r, ry: r, stance: "free" }, wide, tall);+ for (let i = 0; i < 3; i++) {+ const a = (i * Math.PI) / 3 + (b.rand() - 0.5) * 0.4;+ const k = r * (0.6 + 0.25 * b.rand());+ brushStroke(b, [-Math.cos(a) * k, -Math.sin(a) * k], [Math.cos(a) * k, Math.sin(a) * k], 0.22 * size);+ }+ return;+ }+ case "bar": {+ // A tall painted bar standing most of the box's height.+ const half = (tall / 2) * (0.75 + 0.2 * b.rand()) * SPILL;+ place(b, { rx: 0.1 * size, ry: half, stance: "upright" }, wide, tall);+ brushStroke(b, [0, -half], [b.rand() * 0.04, half], 0.16 * size);+ return;+ }+ case "ring": {+ // A fat brushed ring, left a little open.+ place(b, { rx: r, ry: r, stance: "free" }, wide, tall);+ brushRing(b, r * 0.72, 0.2 * size, Math.PI * (1.5 + 0.4 * b.rand()));+ return;+ }+ case "loops": {+ // Two fat loops side by side, each trailing a short tail.+ place(b, { rx: r, ry: r * 0.85, stance: "free" }, wide, tall);+ for (const u of [-r * 0.45, r * 0.45]) {+ thinLine(b, ringPoints(b, u, -r * 0.15, r * 0.3, r * 0.34, 1, 18), THIN * 3.5, "closed");+ thinLine(b, [[u + r * 0.28, -r * 0.15], [u + r * 0.3, r * 0.6]], THIN * 3.5, "open");+ }+ return;+ }+ }+}++/** Paint one thin hand line of kind `line` in a box `wide` × `tall` units. */+function paintLine(b: Brush, line: Line, wide: number, tall: number): void {+ switch (line) {+ case "oval": {+ // A tall looping oval, most of the box's height.+ const ry = (tall / 2) * (0.7 + 0.2 * b.rand()) * SPILL;+ const rx = Math.min(ry * 0.5, (wide / 2) * SPILL);+ place(b, { rx, ry, stance: "upright" }, wide, tall);+ thinLine(b, ringPoints(b, 0, 0, rx, ry, 1.1, 36), THIN, "open");+ return;+ }+ case "strands": {+ // Two or three thin strands standing the box's height, close together.+ const half = (tall / 2) * (0.8 + 0.15 * b.rand()) * SPILL;+ place(b, { rx: 0.06, ry: half, stance: "upright" }, wide, tall);+ const n = 2 + Math.floor(b.rand() * 2);+ for (let i = 0; i < n; i++) {+ const u = (i - (n - 1) / 2) * 0.05;+ thinLine(b, [[u, -half], [u + 0.01, 0], [u, half]], THIN * (1 - i * 0.2), "open");+ }+ return;+ }+ case "squiggle": {+ // A small spiral scribble.+ const r = 0.18 * SPILL;+ place(b, { rx: r, ry: r, stance: "free" }, wide, tall);+ const pts: [number, number][] = [];+ for (let i = 0; i <= 40; i++) {+ const q = i / 40;+ const a = q * Math.PI * 5;+ pts.push([Math.cos(a) * r * (0.15 + 0.85 * q), Math.sin(a) * r * (0.15 + 0.85 * q)]);+ }+ thinLine(b, pts, THIN * 1.5, "open");+ return;+ }+ }+}++/**+ * Paint what a doodle deals on `beat` into its box: a brush mark in one of+ * `inks`, and on some beats a thin hand line beside it in another. The box+ * is drawn in the current transform; nothing outside it is touched but the+ * soft spill of the strokes' round ends.+ */+export function paintDoodle(+ ctx: CanvasRenderingContext2D,+ box: DoodleBox,+ inks: readonly string[],+ beat: number,+ seed: number+): void {+ if (inks.length === 0) {+ return;+ }+ const rand = dealer((seed ^ Math.imul(beat + 1, 0x27d4eb2d)) >>> 0);+ const mark = MARKS[Math.floor(rand() * MARKS.length)];+ const inkAt = Math.floor(rand() * inks.length);++ // The box in square units of its short side, so a mark keeps its shape.+ const unit = Math.max(1, Math.min(box.w, box.h));+ const wide = box.w / unit;+ const tall = box.h / unit;+ const brush: Brush = { ctx, rand, box, unit, ox: 0, oy: 0, turn: 0 };+ ctx.save();+ ctx.strokeStyle = inks[inkAt];+ ctx.fillStyle = inks[inkAt];+ paintMark(brush, mark, 0.6 + rand() * 0.4, wide, tall);++ // Some beats add a thin line, in the next ink along when there is one.+ if (rand() < LINE_ODDS) {+ const line = LINES[Math.floor(rand() * LINES.length)];+ ctx.strokeStyle = inks[(inkAt + 1 + Math.floor(rand() * Math.max(1, inks.length - 1))) % inks.length];+ paintLine(brush, line, wide, tall);+ }+ ctx.restore();+}diff --git a/site/packages/effects-kit/src/effects.test.ts b/site/packages/effects-kit/src/effects.test.tsindex d8b2bd8e..8d1c5d29 100644--- a/site/packages/effects-kit/src/effects.test.ts+++ b/site/packages/effects-kit/src/effects.test.ts@@ -1,5 +1,5 @@import { describe, expect, test } from "bun:test";-import { CLIP_EFFECT_IDS, codeScale, EFFECT_IDS, effectFilterLines, effectPreviewState } from "./effects";+import { CLIP_EFFECT_IDS, codeScale, EFFECT_IDS, effectFilterLines, effectPreviewState, glitchHitAt } from "./effects";// The bundled engine ffmpeg is LGPL: a recipe reaching for a GPL-only filter// renders on a dev machine (Homebrew ffmpeg) and fails in the shipped app.@@ -80,7 +80,7 @@ describe("effect recipes", () => {for (const id of EFFECT_IDS) {const lines = effectFilterLines("in", "out", id, 0.5, 2.5, 6, 1080, 1920, "t")!;const joined = lines.join("\n");- expect(joined.includes("gte(t,2.5)") || joined.includes("sin(t*")).toBe(true);+ expect(joined.includes("gte(t,2.500000)") || joined.includes("sin(t*")).toBe(true);}});@@ -184,6 +184,50 @@ describe("effect recipes", () => {}});+ test("a glitch breaks clean footage with runs of every kind, more at a higher amount", () => {+ const hits = (k: number) => Array.from({ length: 300 }, (_, i) => glitchHitAt(i, k));+ const count = (k: number) => hits(k).filter(Boolean).length;+ // 300 steps is 20 seconds: plenty of hits, with clean footage between.+ expect(count(0.1)).toBeGreaterThan(30);+ expect(count(1)).toBeGreaterThan(count(0.1));+ expect(count(1)).toBeLessThan(220);+ const kinds = new Set(hits(1).map((h) => h?.kind).filter(Boolean));+ expect([...kinds].sort()).toEqual(["blowout", "smear", "split", "tint"]);+ });++ test("a glitch's split holds its offset for the run while a smear moves its row", () => {+ const all = Array.from({ length: 600 }, (_, i) => glitchHitAt(i, 1));+ const held = all.findIndex((h, i) => h?.kind === "split" && all[i + 1]?.kind === "split");+ expect(held).toBeGreaterThanOrEqual(0);+ expect(all[held + 1]!.shift).toBe(all[held]!.shift);+ const smear = all.findIndex((h, i) => h?.kind === "smear" && all[i + 1]?.kind === "smear");+ expect(smear).toBeGreaterThanOrEqual(0);+ expect(all[smear + 1]!.row).not.toBe(all[smear]!.row);+ });++ test("a glitch's export hits the frames its preview does", () => {+ // Every frame of a 30fps window: the preview's state and the export's+ // gates must agree on which kind of hit plays, frame-edge times included.+ const start = 2.5;+ const lines = effectFilterLines("in", "out", "glitch", 0.6, start, 6.5, 1080, 1920, "t")!.join("\n");+ const kinds = new Set<string>();+ for (let n = 0; n < 120; n++) {+ const t = start + n / 30;+ const st = effectPreviewState("glitch", 0.6, t - start);+ const kind = st.stretch ? "smear" : st.ghost ? "split" : st.zoom ? "tint" : st.cssFilter ? "blowout" : null;+ const gate = (label: string) => {+ const m = new RegExp(`\\[${label}\\][^\\n]*?enable='([^']*)'`).exec(lines)!;+ return [...m[1].matchAll(/gte\(t,([\d.]+)\)\*lt\(t,([\d.]+)\)/g)]+ .slice(1)+ .some((w) => t >= Number(w[1]) && t < Number(w[2]));+ };+ const gated = gate("gsbt") ? "smear" : gate("gtbt") ? "tint" : gate("gpbt") ? "split" : gate("gpo2t") ? "blowout" : null;+ expect(gated).toBe(kind);+ if (kind) kinds.add(kind);+ }+ expect(kinds.size).toBeGreaterThanOrEqual(3);+ });+test("flash decays from its start and shake stays bounded", () => {const early = effectPreviewState("flash", 1, 0.05).flash!;const late = effectPreviewState("flash", 1, 0.5).flash!;diff --git a/site/packages/effects-kit/src/effects.ts b/site/packages/effects-kit/src/effects.tsindex 3e8153a6..dd201b4b 100644--- a/site/packages/effects-kit/src/effects.ts+++ b/site/packages/effects-kit/src/effects.ts@@ -11,6 +11,7 @@import type { OverlayBase } from "./types";import { AUDIO_EFFECT_IDS, AUDIO_EFFECT_LABELS, type AudioEffectId } from "./audioFx";import { LOOK_LABELS, lookCssFilter, lookFilterLines, lookPost, type LookStyle } from "./looks";+import { kitCanvas } from "./surface";/** The effects that treat the picture. */export type VisualEffectId =@@ -54,8 +55,107 @@ export interface EffectOverlay extends OverlayBase {ramp?: number;/** Reserved: a sub-frame region (fractions); absent = full frame. */region?: { w: number; h: number };+ /** A light leak's course; absent = drift. */+ leak?: LeakCourse;+ /** A glitch pinned to one kind, hitting with it on every step; absent =+ * the random schedule. */+ glitch?: GlitchKind;+ /** A flash's color; absent = white. */+ tone?: FlashTone;+ /** A flash's strobe rate in pulses a second; absent = one pop at the+ * element's start. */+ rate?: number;+ /** How a strobe's pulses land; absent = strobe. */+ rhythm?: FlashRhythm;}+/** A flash's color: `white` pops the picture bright, `black` dips it dark. */+export const FLASH_TONES = ["white", "black"] as const;+export type FlashTone = (typeof FLASH_TONES)[number];++/** The strobe rates a flash takes, pulses a second. Each pulse holds the tone+ * for the first half of its period and shows the picture for the second: at 6+ * a 30 fps cut strobes 2–3 frames dark, 2–3 frames lit. */+export const FLASH_RATE_MIN = 0.5;+export const FLASH_RATE_MAX = 15;++/** How a strobe's pulses land: `strobe` fires every pulse at full strength;+ * `flicker` deals each pulse its own strength and leaves the weak ones dark,+ * the uneven exposure pop of a failing light. */+export const FLASH_RHYTHMS = ["strobe", "flicker"] as const;+export type FlashRhythm = (typeof FLASH_RHYTHMS)[number];++/** What a flash plays beyond its amount: its tone, strobe rate and rhythm. */+export type FlashPulse = Pick<EffectOverlay, "tone" | "rate" | "rhythm">;++/** How far past a frame's time a pulse is read, in pulses: at the top rate+ * every 30 fps frame lands on a pulse's edge, and the nudge keeps rounding+ * in the frame time from tipping it into the wrong half. */+const PULSE_NUDGE = 1e-3;++/** A flicker's pulses dealt under this strength stay dark. */+const FLICKER_SKIP = 0.4;++/** The strength a flicker deals pulse `n`, 0..1: a sine hash ffmpeg's+ * expressions compute the same way (see the flash's geq). */+const flickerDeal = (n: number) => {+ const v = Math.sin(n * 12.9898 + 78.233) * 43758.5453;+ return v - Math.floor(v);+};++/** The strobe rate a flash plays at, clamped; 0 = a single pop. */+const flashRate = (p?: FlashPulse) =>+ p?.rate ? Math.max(FLASH_RATE_MIN, Math.min(FLASH_RATE_MAX, p.rate)) : 0;++/** The tone's strength while a strobe's pulse holds it (its first half), 0+ * while it shows the picture. A strobe pulse holds at 1; a flicker pulse at+ * its dealt strength, or 0 when the deal is weak. */+const flashOn = (tLocal: number, rate: number, rhythm?: FlashRhythm) => {+ const p = Math.max(0, tLocal) * rate + PULSE_NUDGE;+ if (p % 1 >= 0.5) {+ return 0;+ }+ if (rhythm !== "flicker") {+ return 1;+ }+ const deal = flickerDeal(Math.floor(p));+ return deal < FLICKER_SKIP ? 0 : deal;+};++/** How a light leak plays: `drift` glows and wanders for as long as it is+ * up; `burn` is the film burn, played once across the element's length;+ * `scorch` flashes the picture through hot yellow, orange and red tones,+ * a new one each frame. */+export const LEAK_COURSES = ["drift", "burn", "scorch"] as const;+export type LeakCourse = (typeof LEAK_COURSES)[number];++/** The kinds a glitch breaks the picture with. The random schedule deals the+ * first four; `rgb` pulls the color channels apart (red one way, blue the+ * other) and plays only when a glitch is pinned to it. */+export const GLITCH_KINDS = ["smear", "split", "tint", "blowout", "rgb"] as const;+export type GlitchKind = (typeof GLITCH_KINDS)[number];++/** The recipes the renderers draw: every picture effect, the burn a light+ * leak plays on its burn course, and a glitch pinned to one kind. */+type RecipeId = VisualEffectId | "burn" | "scorch" | `glitch:${GlitchKind}`;++/** The recipe an effect element renders with. Every renderer resolves the+ * element through here, so a light leak on its burn course draws the burn+ * recipe, and a pinned glitch its kind, in the preview, the in-tab export and+ * the ffmpeg graph alike. */+export const effectRecipe = (e: { effect: string; leak?: LeakCourse; glitch?: GlitchKind }): string =>+ e.effect === "lightleak" && (e.leak === "burn" || e.leak === "scorch")+ ? e.leak+ : e.effect === "glitch" && e.glitch && (GLITCH_KINDS as readonly string[]).includes(e.glitch)+ ? `glitch:${e.glitch}`+ : e.effect;++/** The kind a pinned-glitch recipe holds to, e.g. "glitch:rgb" → "rgb". */+const pinnedGlitch = (recipe: string): GlitchKind | undefined => {+ const kind = recipe.startsWith("glitch:") ? recipe.slice("glitch:".length) : "";+ return (GLITCH_KINDS as readonly string[]).includes(kind) ? (kind as GlitchKind) : undefined;+};+/** The picture treatments, in picker order. The audio ones are their own list* (`AUDIO_EFFECT_IDS`); `ALL_EFFECT_IDS` is both. */export const EFFECT_IDS: VisualEffectId[] = [@@ -131,7 +231,7 @@ export type ClipEffectId = (typeof CLIP_EFFECT_IDS)[number];export type EffectEdges = "opaque" | "keep";/** One effect a clip wears; `amount` 0..1, absent = 0.5. */-export interface ClipEffect {+export interface ClipEffect extends FlashPulse {effect: ClipEffectId;amount?: number;}@@ -223,6 +323,170 @@ export const streakGradient = (s: LeakStreak) =>`rgba(255,200,140,0.9) ${(s.p * 100).toFixed(1)}%, ` +`rgba(255,200,140,0) ${((s.p + 2 * s.w) * 100).toFixed(1)}%)`;+/**+ * The film burn, a light leak's burn course: the frame catches from its+ * bottom edge, the burn climbs to the top behind a hot orange front, flares+ * near white, settles to orange and lets the picture back in its last+ * moments — the burn-out at the end of a teaser. It plays across the+ * element's own length, so a short element burns fast and a long one slowly.+ */++/** Length the burn plays over when nothing gives it one. */+export const BURN_SECONDS = 1.2;++/** One stop of the burn's vertical profile: `at` is height from the bottom+ * edge as a frame fraction, `rgb` the color, `a` its opacity. */+export interface BurnStop {+ at: number;+ rgb: [number, number, number];+ a: number;+}++/** A diagonal band of darker flame sweeping through the flare. Same axis+ * convention as a leak streak: `angle` in CSS gradient degrees, `p` the band+ * center along that axis, `w` its half-width. */+export interface BurnStreak {+ angle: number;+ p: number;+ w: number;+ alpha: number;+}++/** The burn at one moment: its vertical profile, laid over the picture at+ * `alpha`, and the flame band over that. */+export interface BurnPaint {+ stops: BurnStop[];+ alpha: number;+ streak?: BurnStreak;+}++/** The burn's colors: the near-white flare, the yellow it cools through, the+ * orange it settles at, the front's hot edge and the flame band. */+const BURN_FLARE: [number, number, number] = [255, 244, 194];+const BURN_YELLOW: [number, number, number] = [255, 216, 102];+const BURN_ORANGE: [number, number, number] = [247, 168, 58];+const BURN_EDGE: [number, number, number] = [255, 106, 26];+export const BURN_FLAME: [number, number, number] = [232, 120, 42];++/** How far the burned body runs under the front, how wide the hot edge is,+ * and how far its glow reaches above it — frame fractions. */+const BURN_BODY = 0.08;+const BURN_EDGE_W = 0.02;+const BURN_GLOW = 0.1;+/** Share of the burn spent climbing, and where the picture starts to return. */+const BURN_RISE = 0.32;+const BURN_RELEASE = 0.9;+/** The flame band: it crosses while `u` runs from `from` over `span`, its+ * center sliding from `p0` by `travel` along a band tilted `angle` degrees,+ * `w` wide at peak opacity `alpha`. */+const BURN_STREAK = { from: 0.25, span: 0.35, angle: 30, p0: 1.1, travel: 1.2, w: 0.08, alpha: 0.7 };++const smooth = (p: number) => {+ const q = Math.max(0, Math.min(1, p));+ return q * q * (3 - 2 * q);+};+const mixRgb = (a: [number, number, number], b: [number, number, number], p: number): [number, number, number] => [+ a[0] + (b[0] - a[0]) * p,+ a[1] + (b[1] - a[1]) * p,+ a[2] + (b[2] - a[2]) * p,+];++/** The burn's body color `u` of the way through: flare, then yellow, then+ * orange. */+function burnCore(u: number): [number, number, number] {+ if (u < 0.4) return BURN_FLARE;+ if (u < 0.6) return mixRgb(BURN_FLARE, BURN_YELLOW, smooth((u - 0.4) / 0.2));+ return mixRgb(BURN_YELLOW, BURN_ORANGE, smooth((u - 0.6) / 0.25));+}++/**+ * The profile clipped to the frame, so every renderer reads stops inside+ * [0, 1] and draws the same piecewise-linear ramp. A stop past an edge is+ * replaced by the ramp's value at that edge.+ */+function clipStops(stops: BurnStop[]): BurnStop[] {+ const at = (x: number): BurnStop => {+ if (x <= stops[0].at) return { ...stops[0], at: x };+ for (let i = 1; i < stops.length; i++) {+ const a = stops[i - 1];+ const b = stops[i];+ if (x > b.at) continue;+ const p = b.at > a.at ? (x - a.at) / (b.at - a.at) : 1;+ return { at: x, rgb: mixRgb(a.rgb, b.rgb, p), a: a.a + (b.a - a.a) * p };+ }+ return { ...stops[stops.length - 1], at: x };+ };+ const inside = stops.filter((s) => s.at > 0 && s.at < 1);+ return [at(0), ...inside, at(1)];+}++/** The burn `tLocal` seconds into an element `dur` long, at amount `k`. */+export function burnAt(tLocal: number, dur: number, k: number): BurnPaint {+ const u = Math.max(0, Math.min(1, tLocal / Math.max(dur, 1e-3)));++ // The front climbs from the bottom edge until its glow has left the top.+ const h = (1 + BURN_GLOW + BURN_EDGE_W) * smooth(u / BURN_RISE);+ const core = burnCore(u);+ const stops = clipStops([+ { at: h - BURN_BODY, rgb: core, a: 1 },+ { at: h - BURN_EDGE_W, rgb: BURN_EDGE, a: 1 },+ { at: h + BURN_GLOW, rgb: BURN_EDGE, a: 0 },+ ]);++ // Opaque at full amount; the release hands the picture back.+ const peak = 0.7 + 0.3 * k;+ const alpha = peak * (1 - smooth((u - BURN_RELEASE) / (1 - BURN_RELEASE)));++ // The flame band crosses the frame while the burn flares.+ const B = BURN_STREAK;+ const f = (u - B.from) / B.span;+ const streak = f > 0 && f < 1 ? { angle: B.angle, p: B.p0 - B.travel * f, w: B.w, alpha: B.alpha * Math.sin(Math.PI * f) } : undefined;+ return streak ? { stops, alpha, streak } : { stops, alpha };+}++const rgba = (c: [number, number, number], a: number) =>+ `rgba(${Math.round(c[0])},${Math.round(c[1])},${Math.round(c[2])},${fmt(a)})`;++/** The CSS background of the burn's body — the DOM twin of the canvas pass's+ * vertical gradient; lay it over the picture at the burn's alpha. */+export const burnGradient = (b: BurnPaint) =>+ `linear-gradient(to top, ${b.stops.map((s) => `${rgba(s.rgb, s.a)} ${(s.at * 100).toFixed(2)}%`).join(", ")})`;++/** The CSS background of the flame band, at the band's own alpha. */+export const burnStreakGradient = (s: BurnStreak) =>+ `linear-gradient(${s.angle}deg, ${rgba(BURN_FLAME, 0)} ${((s.p - 2 * s.w) * 100).toFixed(1)}%, ` ++ `${rgba(BURN_FLAME, 1)} ${(s.p * 100).toFixed(1)}%, ` ++ `${rgba(BURN_FLAME, 0)} ${((s.p + 2 * s.w) * 100).toFixed(1)}%)`;++/** The ellipse a wash covers, in frame fractions: center (x, y), radii (rx,+ * ry). The wash holds its full strength out to `inner` of the way to the rim+ * and fades to nothing at it. */+export interface WashArea {+ x: number;+ y: number;+ rx: number;+ ry: number;+ inner: number;+}++/** A hex wash color with no alpha: a patch's ramp fades to it, so canvas+ * gradients, which blend unpremultiplied, never pass through black, e.g.+ * "#f4d628" → "rgba(244,214,40,0)". */+function clearOf(color: string): string {+ const n = parseInt(color.slice(1, 7), 16);+ return `rgba(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255},0)`;+}++/** The CSS background of a wash: its color over the frame, or a radial ramp+ * over its patch — the DOM twin of the canvas pass's gradient. */+export function washBackground(color: string, area?: WashArea): string {+ if (!area) {+ return color;+ }+ const pct = (v: number) => `${fmt(v * 100)}%`;+ return `radial-gradient(ellipse ${pct(area.rx)} ${pct(area.ry)} at ${pct(area.x)} ${pct(area.y)}, ${color} ${pct(area.inner)}, ${clearOf(color)} 100%)`;+}+/** What one effect asks the preview canvas to do at `tLocal` seconds in. */export interface EffectPreviewState {/** ctx.filter for a self-redraw of the frame; "" = none. */@@ -231,16 +495,21 @@ export interface EffectPreviewState {grain?: number;/** Radial corner darkening 0..1. */vignette?: number;- /** Flat washes over the picture. */- washes?: { color: string; alpha: number; mode: GlobalCompositeOperation }[];+ /** Flat washes over the picture, each over the whole frame or one patch. */+ washes?: { color: string; alpha: number; mode: GlobalCompositeOperation; area?: WashArea }[];/** A light leak's bloom: a warm radial glow centered at (x, y) in frame* fractions, screen-blended over the picture at `alpha`, with the streak* bands sweeping over it. */leak?: { x: number; y: number; alpha: number; streaks: LeakStreak[] };/** Chroma ghosting: tinted copies offset by this frame-width fraction. */ghostFrac?: number;- /** Whole-frame white flash alpha 0..1. */+ /** A channel split: red drawn moved by (dx, dy) design px, blue by the+ * opposite, green in place. */+ rgb?: { dx: number; dy: number };+ /** Whole-frame flash alpha 0..1. */flash?: number;+ /** The flash's color; absent = white. */+ flashTone?: FlashTone;/** Frame offset in design px (1080 short side). */dx?: number;dy?: number;@@ -249,9 +518,17 @@ export interface EffectPreviewState {zoom?: number;/** The point `zoom` scales about, in frame fractions; absent = the middle. */origin?: { x: number; y: number };+ /** A pixel stretch: the frame scaled `scale` times taller about the row at+ * `row` (a frame fraction), so one thin band smears into vertical streaks. */+ stretch?: { row: number; scale: number };+ /** The frame before it moved, laid back over the moved one at this alpha+ * with a lighten blend: a doubled picture. */+ ghost?: number;/** Self-copy glow: blur radius as a fraction of frame height, blended back* over the picture; `bright` isolates highlights first (halation). */glow?: { blurFrac: number; alpha: number; mode: "screen" | "lighten"; bright?: boolean };+ /** A film burn climbing over the picture (see `burnAt`). */+ burn?: BurnPaint;}/** A host-registered effect recipe: the same dual-renderer shape the@@ -331,6 +608,150 @@ export const zoomProgress = (tLocal: number, ramp: number | undefined, dur?: numreturn p * p * (3 - 2 * p);};+/**+ * The glitch: clean footage broken by runs of corrupted frames. Time runs in+ * steps of `1 / GLITCH_RATE` seconds; a step can start a run of one kind, and+ * the latest run that started still plays until it ends:+ *+ * - smear: the frame stretched tall about one row into dark streaks, blurred,+ * a new row each step (1–3 steps).+ * - split: the frame knocked sideways with the unmoved frame lightened back+ * over it, so the picture doubles, an orange burn glowing up from the+ * bottom and dust over it all (2–5 steps).+ * - tint: a blurred teal close-up of one corner (1–2 steps).+ * - blowout: one overexposed frame.+ *+ * The schedule is an integer hash of the step, so the preview, the in-tab+ * export and ffmpeg land the same hits on the same frames in every JS engine.+ */+const GLITCH_RATE = 15;+/** How many times taller a smear draws the frame about its row: 1/60th of+ * the picture runs over all of it. */+const GLITCH_STRETCH = 60;+/** How far a tint pushes into its corner. */+const GLITCH_TINT_ZOOM = 1.6;+/** The doubled frame's share in a split. */+const GLITCH_GHOST = 0.55;++/** Each kind's share of the runs and how many steps a run of it lasts. */+const GLITCH_RUNS: { kind: GlitchKind; weight: number; min: number; max: number }[] = [+ { kind: "smear", weight: 0.45, min: 1, max: 3 },+ { kind: "split", weight: 0.25, min: 2, max: 5 },+ { kind: "tint", weight: 0.15, min: 1, max: 2 },+ { kind: "blowout", weight: 0.15, min: 1, max: 1 },+];+const GLITCH_RUN_MAX = Math.max(...GLITCH_RUNS.map((r) => r.max));++export interface GlitchHit {+ kind: GlitchKind;+ /** The row a smear runs from, a frame fraction; new every step. */+ row: number;+ /** How far a split knocks the frame sideways, a signed short-side fraction;+ * held for the run. */+ shift: number;+ /** The corner a tint pushes into, frame fractions; held for the run. */+ focus: { x: number; y: number };+}++/** Slack on a step's edges, in seconds, well under a frame: a frame time+ * that lands on an edge belongs to the step it opens in every renderer, ffmpeg's+ * rounded clock included. */+const STEP_SLACK = 1e-4;++/** The step `tLocal` seconds into the effect falls in. */+const glitchStep = (tLocal: number) => Math.floor((tLocal + STEP_SLACK) * GLITCH_RATE);++/** A step's hash on one channel, 0..1. */+function stepHash(step: number, salt: number): number {+ let h = Math.imul(step ^ Math.imul(salt, 0x9e3779b1), 0x85ebca6b);+ h ^= h >>> 13;+ h = Math.imul(h, 0xc2b2ae35);+ h ^= h >>> 16;+ return (h >>> 0) / 4294967296;+}++/** The share of steps that start a run: about a third of the frames glitch+ * at the default amount, half at the top. */+const glitchOdds = (k: number) => 0.05 + 0.2 * k;++/** The run step `step` starts, if it starts one. */+function glitchRunAt(step: number, k: number): { kind: GlitchKind; len: number } | null {+ if (step < 0 || stepHash(step, 1) >= glitchOdds(k)) return null;+ let pick = stepHash(step, 2);+ const run = GLITCH_RUNS.find((r) => (pick -= r.weight) < 0) ?? GLITCH_RUNS[GLITCH_RUNS.length - 1];+ return { kind: run.kind, len: run.min + Math.floor(stepHash(step, 6) * (run.max - run.min + 1)) };+}++/** What the glitch does on step `step` at amount `k`; null = a clean step.+ * The latest run to start plays until it ends, then the picture is clean+ * until the next one. A pinned glitch hits with its kind on every step, the+ * row, shift and corner dealt fresh each step. */+export function glitchHitAt(step: number, k: number, pin?: GlitchKind): GlitchHit | null {+ if (pin) {+ return {+ kind: pin,+ row: 0.15 + 0.7 * stepHash(step, 3),+ shift: (stepHash(step, 4) < 0.5 ? -1 : 1) * (0.06 + 0.1 * stepHash(step, 5)),+ focus: { x: 0.15 + 0.7 * stepHash(step, 7), y: 0.15 + 0.7 * stepHash(step, 8) },+ };+ }+ for (let from = step; from > step - GLITCH_RUN_MAX; from--) {+ const run = glitchRunAt(from, k);+ if (!run) continue;+ if (from + run.len <= step) return null;+ const side = stepHash(from, 4) < 0.5 ? -1 : 1;+ return {+ kind: run.kind,+ row: 0.15 + 0.7 * stepHash(step, 3),+ shift: side * (0.06 + 0.1 * stepHash(from, 5)),+ focus: { x: 0.15 + 0.7 * stepHash(from, 7), y: 0.15 + 0.7 * stepHash(from, 8) },+ };+ }+ return null;+}++/** How far an rgb hit pulls the channels apart, as shares of its shift: red+ * moves by (x, y) of it and blue by the opposite, mostly up and down. A+ * shift of 0.1 moves red 8 px across and 27 px down at 1080. */+const GLITCH_RGB = { x: 0.075, y: 0.25 };++/** An rgb hit's channel offset in design px (1080 short side). */+const glitchRgb = (h: GlitchHit) => ({ dx: h.shift * GLITCH_RGB.x * 1080, dy: h.shift * GLITCH_RGB.y * 1080 });++/** The split's burn: a warm bloom low on the frame, the light-leak paint. */+const GLITCH_BURN = { x: 0.25, y: 1.15, alpha: 0.75 };++/** The canvas/DOM state of one glitch hit. */+function glitchState(hit: GlitchHit | null): EffectPreviewState {+ if (!hit) return { cssFilter: "" };+ switch (hit.kind) {+ case "smear":+ return {+ cssFilter: "brightness(0.6) contrast(1.25) blur(1.5px)",+ stretch: { row: hit.row, scale: GLITCH_STRETCH },+ grain: 0.3,+ };+ case "split":+ return {+ cssFilter: "contrast(1.1) saturate(1.25)",+ dx: hit.shift * 1080,+ ghost: GLITCH_GHOST,+ leak: { ...GLITCH_BURN, streaks: [] },+ grain: 0.35,+ };+ case "tint":+ return {+ cssFilter: "sepia(1) hue-rotate(125deg) saturate(1.6) brightness(0.7) blur(3px)",+ zoom: GLITCH_TINT_ZOOM,+ origin: hit.focus,+ };+ case "blowout":+ return { cssFilter: "brightness(2.2) contrast(1.5) saturate(0.35)" };+ case "rgb":+ return { cssFilter: "", rgb: glitchRgb(hit) };+ }+}+/** The focus a zoom holds on, defaulted to the middle of the frame. */const focusOf = (focus?: { x: number; y: number }) => ({x: Math.max(0, Math.min(1, focus?.x ?? 0.5)),@@ -346,7 +767,8 @@ export function effectPreviewState(focus?: { x: number; y: number },ramp?: number,/** The element's own length, so a zoom knows where to pull back out. */- dur?: number+ dur?: number,+ pulse?: FlashPulse): EffectPreviewState {const custom = customEffects.get(effect);if (custom) return custom.previewState(amount, tLocal);@@ -363,7 +785,12 @@ export function effectPreviewState(glow: post?.glow,};}- switch (effect as VisualEffectId) {+ // A pinned glitch plays its kind on every step.+ const pin = pinnedGlitch(effect);+ if (pin) {+ return glitchState(glitchHitAt(glitchStep(tLocal), k, pin));+ }+ switch (effect as RecipeId) {case "zoom":// A push in on one part of the picture: the frame renders larger and is// cropped back to size around the focus, which stays where it is. The@@ -381,13 +808,8 @@ export function effectPreviewState(grain: 0.25 * k,ghostFrac: 0.004 * k,};- case "glitch": {- // The shift jumps between quantized offsets a few times a second, so it- // reads as digital tearing rather than a steady fringe.- const step = Math.floor(tLocal * 9);- const jitter = ((step * 7919) % 5) - 2; // -2..2, deterministic- return { cssFilter: "", ghostFrac: (0.004 + 0.003 * jitter) * k, grain: 0.12 * k };- }+ case "glitch":+ return glitchState(glitchHitAt(glitchStep(tLocal), k));case "blur":return { cssFilter: `blur(${fmt(10 * k)}px)` };case "vignette":@@ -408,10 +830,17 @@ export function effectPreviewState(},};case "flash": {- // One bright pop at the element start, decaying over ~0.4s.- const gain = Math.exp(-9 * Math.max(0, tLocal));- return { cssFilter: "", flash: Math.min(1, 0.85 * k * gain) };+ // One pop at the element start, decaying over ~0.4s; on a strobe, the+ // tone held hard for the first half of every pulse.+ const tone = pulse?.tone === "black" ? "black" : undefined;+ const rate = flashRate(pulse);+ const gain = rate ? flashOn(tLocal, rate, pulse?.rhythm) : Math.exp(-9 * Math.max(0, tLocal));+ return { cssFilter: "", flash: Math.min(1, 0.85 * k * gain), ...(tone ? { flashTone: tone } : {}) };}+ case "burn":+ return { cssFilter: "", burn: burnAt(tLocal, dur ?? BURN_SECONDS, k) };+ case "scorch":+ return scorchState(tLocal, k);case "shake": {const amp = SHAKE_AMP * k; // design pxreturn {@@ -434,6 +863,24 @@ export function effectPreviewState(}}+/** The plane an rgb split builds its tinted copies on, one per scratch: it+ * lives as long as the caller's scratch and follows the frame's size, so a+ * run of split frames draws on one plane. */+const splitPlanes = new WeakMap<HTMLCanvasElement | OffscreenCanvas, HTMLCanvasElement>();+function splitPlane(scratch: HTMLCanvasElement | OffscreenCanvas, w: number, h: number): HTMLCanvasElement {+ const plane = splitPlanes.get(scratch);+ if (!plane) {+ const made = kitCanvas(w, h);+ splitPlanes.set(scratch, made);+ return made;+ }+ if (plane.width !== w || plane.height !== h) {+ plane.width = w;+ plane.height = h;+ }+ return plane;+}+/*** Apply one effect to the composited frame in place. `scratch` is a reusable* canvas the caller owns (same size as `canvas`); `grainTile` supplies the@@ -449,9 +896,10 @@ export function applyEffectToCanvas(focus?: { x: number; y: number },ramp?: number,dur?: number,- edges: EffectEdges = "opaque"+ edges: EffectEdges = "opaque",+ pulse?: FlashPulse): void {- const state = effectPreviewState(effect, amount, tLocal, focus, ramp, dur);+ const state = effectPreviewState(effect, amount, tLocal, focus, ramp, dur, pulse);const ctx = canvas.getContext("2d") as CanvasRenderingContext2D | null;const sctx = scratch.getContext("2d") as CanvasRenderingContext2D | null;if (!ctx || !sctx) return;@@ -483,12 +931,20 @@ export function applyEffectToCanvas(ctx.restore();};- const needsRedraw = state.cssFilter || state.dx || state.dy || (state.zoom && state.zoom !== 1);+ const needsRedraw = state.cssFilter || state.dx || state.dy || state.stretch || (state.zoom && state.zoom !== 1);if (needsRedraw) {sctx.clearRect(0, 0, W, H);sctx.drawImage(canvas, 0, 0);- // Over a clear edge the old picture would show through the redraw.+ // Over a clear edge the old picture would show through the redraw. A+ // moved frame uncovers an edge, which shows black as it does under the+ // preview's stage; a shake's overscale covers its own.if (edges === "keep") ctx.clearRect(0, 0, W, H);+ else if (state.dx || state.dy) {+ ctx.save();+ ctx.fillStyle = "#000000";+ ctx.fillRect(0, 0, W, H);+ ctx.restore();+ }ctx.save();if (state.cssFilter) ctx.filter = state.cssFilter;const zoom = state.zoom ?? 1;@@ -498,14 +954,72 @@ export function applyEffectToCanvas(// shake, the chosen point for a zoom.const ox = state.origin?.x ?? 0.5;const oy = state.origin?.y ?? 0.5;+ // A stretch draws only the thin band about its row, scaled to the full+ // height: the band that holds the row in place, e.g. row 0.5 at scale 60+ // is the 1/60th of the frame around its middle.+ const sy = state.stretch?.scale ?? 1;+ const band = H / sy;+ const bandTop = (state.stretch?.row ?? 0) * (H - band);ctx.drawImage(scratch,+ 0,+ bandTop,+ W,+ band,dx - W * (zoom - 1) * ox,dy - H * (zoom - 1) * oy,W * zoom,H * zoom);ctx.restore();+ // The unmoved frame lightened back over the moved one doubles the+ // picture, and fills the edge the move uncovered.+ if (state.ghost) {+ ctx.save();+ ctx.globalAlpha = state.ghost;+ ctx.globalCompositeOperation = "lighten";+ ctx.drawImage(scratch, 0, 0);+ ctx.restore();+ }+ }++ if (state.rgb) {+ // A channel split: the frame is copied off and rebuilt as three tinted+ // copies added back up — red moved by the offset, green in place, blue+ // moved the other way. Each copy keeps its own coverage, so a clear edge+ // stays clear. The copies build on the scratch's split plane.+ const ox = state.rgb.dx * scale;+ const oy = state.rgb.dy * scale;+ const plane = splitPlane(scratch, W, H);+ const pctx = plane.getContext("2d") as CanvasRenderingContext2D | null;+ if (pctx) {+ sctx.clearRect(0, 0, W, H);+ sctx.drawImage(canvas, 0, 0);+ ctx.save();+ ctx.clearRect(0, 0, W, H);+ if (edges === "opaque") {+ ctx.fillStyle = "#000000";+ ctx.fillRect(0, 0, W, H);+ }+ ctx.globalCompositeOperation = "lighter";+ const copies: [string, number, number][] = [+ ["#ff0000", ox, oy],+ ["#00ff00", 0, 0],+ ["#0000ff", -ox, -oy],+ ];+ for (const [tint, mx, my] of copies) {+ pctx.globalCompositeOperation = "source-over";+ pctx.clearRect(0, 0, W, H);+ pctx.drawImage(scratch, mx, my);+ pctx.globalCompositeOperation = "multiply";+ pctx.fillStyle = tint;+ pctx.fillRect(0, 0, W, H);+ pctx.globalCompositeOperation = "destination-in";+ pctx.drawImage(scratch, mx, my);+ ctx.drawImage(plane, 0, 0);+ }+ ctx.restore();+ }}if (state.glow) {@@ -540,8 +1054,22 @@ export function applyEffectToCanvas(for (const w of state.washes ?? []) {over(w.mode, w.alpha, (c) => {- c.fillStyle = w.color;- c.fillRect(0, 0, W, H);+ const a = w.area;+ if (!a) {+ c.fillStyle = w.color;+ c.fillRect(0, 0, W, H);+ return;+ }++ // A patch: a unit circle's ramp stretched onto the ellipse, filled+ // over the frame in the stretched space.+ const g = c.createRadialGradient(0, 0, 0, 0, 0, 1);+ g.addColorStop(a.inner, w.color);+ g.addColorStop(1, clearOf(w.color));+ c.translate(a.x * W, a.y * H);+ c.scale(a.rx * W, a.ry * H);+ c.fillStyle = g;+ c.fillRect(-a.x / a.rx, -a.y / a.ry, 1 / a.rx, 1 / a.ry);});}@@ -614,10 +1142,44 @@ export function applyEffectToCanvas(if (state.flash) {over("source-over", state.flash, (c) => {- c.fillStyle = "#FFFFFF";+ c.fillStyle = state.flashTone === "black" ? "#000000" : "#FFFFFF";c.fillRect(0, 0, W, H);});}++ if (state.burn) {+ // The burned body, bottom edge up, then the flame band over it.+ const b = state.burn;+ const body = ctx.createLinearGradient(0, H, 0, 0);+ for (const s of b.stops) body.addColorStop(s.at, rgba(s.rgb, s.a));+ over("source-over", b.alpha, (c) => {+ c.fillStyle = body;+ c.fillRect(0, 0, W, H);+ });+ if (b.streak) {+ // The band's stops may fall past the frame; the gradient line is+ // stretched to cover them, so the ramp is the one the CSS twin draws.+ const s = b.streak;+ const th = (s.angle * Math.PI) / 180;+ const ux = Math.sin(th);+ const uy = -Math.cos(th);+ const L = W * Math.abs(ux) + H * Math.abs(uy);+ const lo = Math.min(0, s.p - 2 * s.w);+ const hi = Math.max(1, s.p + 2 * s.w);+ const pt = (f: number) => [W / 2 + ux * L * (f - 0.5), H / 2 + uy * L * (f - 0.5)] as const;+ const [x0, y0] = pt(lo);+ const [x1, y1] = pt(hi);+ const band = ctx.createLinearGradient(x0, y0, x1, y1);+ const at = (f: number) => (f - lo) / (hi - lo);+ band.addColorStop(at(s.p - 2 * s.w), rgba(BURN_FLAME, 0));+ band.addColorStop(at(s.p), rgba(BURN_FLAME, 1));+ band.addColorStop(at(s.p + 2 * s.w), rgba(BURN_FLAME, 0));+ over("source-over", s.alpha * b.alpha, (c) => {+ c.fillStyle = band;+ c.fillRect(0, 0, W, H);+ });+ }+ }}/**@@ -640,13 +1202,13 @@ export function applyClipEffects(filters = [];};for (const e of effects) {- const { cssFilter, ...passes } = effectPreviewState(e.effect, e.amount, tLocal);+ const { cssFilter, ...passes } = effectPreviewState(e.effect, e.amount, tLocal, undefined, undefined, undefined, e);if (Object.values(passes).every((v) => v === undefined)) {if (cssFilter) filters.push(cssFilter);continue;}flush();- applyEffectToCanvas(canvas, scratch, e.effect, e.amount, tLocal, grainTileFor, undefined, undefined, undefined, "keep");+ applyEffectToCanvas(canvas, scratch, e.effect, e.amount, tLocal, grainTileFor, undefined, undefined, undefined, "keep", e);}flush();}@@ -667,9 +1229,13 @@ function redrawFiltered(canvas: HTMLCanvasElement | OffscreenCanvas, scratch: HT/* ---------------------------------------------------------------- ffmpeg */+/** A window edge in seconds, to the microsecond: a time floored to the+ * tenth of a millisecond still opens on the frame it sits just under. */+const edge = (t: number) => t.toFixed(6);+/** The timeline gate every fragment carries (the gte*lt spelling keeps commas* out of quoted expressions). */-const gate = (a: number, b: number) => `enable='gte(t,${fmt(a)})*lt(t,${fmt(b)})'`;+const gate = (a: number, b: number) => `enable='gte(t,${edge(a)})*lt(t,${edge(b)})'`;/** The pixel family a chain works in: the format its branches carry, the* option that keeps `overlay` blending in the same one, and the bits a plane@@ -692,6 +1258,121 @@ export const codeScale = (depth: 8 | 10 | undefined): number => ((1 << (depth ??/** An 8-bit code value spelled at the chain's depth. */const code = (v: number, depth: 8 | 10 | undefined): string => fmt(v * codeScale(depth));+/**+ * The glitch in ffmpeg: the run schedule is walked here and spelled as+ * per-frame expressions, one branch per kind, each gated to its own steps.+ *+ * - smear: a band cropped at the step's row (crop's y runs per frame), scaled+ * to full height, darkened and softened, laid over the frame.+ * - tint: the run's corner cropped and scaled up, cast teal, blurred.+ * - split: the frame blacked, a copy laid back at the run's offset, the+ * unmoved frame lightened over it, then the burn glowing up from the bottom.+ * - blowout: a lift and desaturate of the whole frame.+ *+ * Smear and split carry dust, the preview's grain.+ */+function glitchFilterLines(+ inLabel: string,+ outLabel: string,+ k: number,+ start: number,+ end: number,+ width: number,+ height: number,+ tag: string,+ chroma: ChainChroma,+ pin?: GlitchKind+): string[] {+ const hits: { a: number; b: number; hit: GlitchHit }[] = [];+ const steps = Math.ceil((end - start) * GLITCH_RATE);+ for (let step = 0; step < steps; step++) {+ const hit = glitchHitAt(step, k, pin);+ if (!hit) continue;+ hits.push({ a: start + step / GLITCH_RATE, b: Math.min(end, start + (step + 1) / GLITCH_RATE), hit });+ }++ // A step's window on `t` (or geq's `T`), shifted back by the same slack the+ // preview's step takes, and a sum over windows that is the hit's value+ // inside it, 0 outside.+ const at = (n: number) => (n - STEP_SLACK).toFixed(5);+ const win = (x: { a: number; b: number }, clock = "t") => `gte(${clock},${at(x.a)})*lt(${clock},${at(x.b)})`;+ const sum = (kinds: GlitchKind[], value: (h: GlitchHit) => number, clock = "t") => {+ const terms = hits.filter((x) => kinds.includes(x.hit.kind)).map((x) => `${fmt(value(x.hit))}*${win(x, clock)}`);+ return terms.length ? terms.join("+") : "0";+ };+ const gateOf = (...kinds: GlitchKind[]) =>+ `enable='gte(t,${edge(start)})*lt(t,${edge(end)})*(${sum(kinds, () => 1)})'`;+ const px = Math.min(width, height);+ const soft = (r: number) => `gblur=sigma=${fmt((r * px) / 1080)}`;+ const lines: string[] = [];++ // Smear: the band the canvas draws, `height / GLITCH_STRETCH` tall and+ // `row * (height - band)` down, scaled back over the whole frame.+ const band = 2 * Math.max(1, Math.round(height / GLITCH_STRETCH / 2));+ lines.push(+ `[${inLabel}]split[gsb${tag}][gss${tag}]`,+ `[gss${tag}]crop=${width}:${band}:0:'(ih-${band})*(${sum(["smear"], (h) => h.row)})',` ++ `scale=${width}:${height},lutyuv=y='(val-minval)*0.6+minval',${soft(1.5)}[gsc${tag}]`,+ `[gsb${tag}][gsc${tag}]overlay=0:0:${gateOf("smear")}:eof_action=pass${chroma.overlay}[gso${tag}]`+ );++ // Tint: the corner the canvas zooms into — the frame's 1/zoom share, as far+ // into the spare room as the focus sits — scaled back to size.+ const cw = 2 * Math.round(width / GLITCH_TINT_ZOOM / 2);+ const ch = 2 * Math.round(height / GLITCH_TINT_ZOOM / 2);+ lines.push(+ `[gso${tag}]split[gtb${tag}][gts${tag}]`,+ `[gts${tag}]crop=${cw}:${ch}:'(iw-${cw})*(${sum(["tint"], (h) => h.focus.x)})':'(ih-${ch})*(${sum(["tint"], (h) => h.focus.y)})',` ++ `scale=${width}:${height},` ++ // The preview's sepia → hue-rotate → saturate → brightness chain as+ // one matrix, applied in RGB.+ `format=${chroma.depth === 10 ? "gbrp10le" : "gbrp"},` ++ `colorchannelmixer=rr=0.147:rg=0.29:rb=0.071:gr=0.275:gg=0.54:gb=0.132:br=0.262:bg=0.517:bb=0.126,` ++ `format=${chroma.pixFmt},${soft(3)}[gtc${tag}]`,+ `[gtb${tag}][gtc${tag}]overlay=0:0:${gateOf("tint")}:eof_action=pass${chroma.overlay}[gto${tag}]`+ );++ // Split: black under the moved copy, the unmoved frame lightened back over+ // it, then the burn — the leak bloom the preview paints, as a radial ramp+ // in plane-relative coordinates so the chroma planes stay in register.+ const big = Math.max(width, height);+ const d = `hypot((X/W-${fmt(GLITCH_BURN.x)})*${fmt(width / big)},(Y/H-${fmt(GLITCH_BURN.y)})*${fmt(height / big)})/0.7`;+ const G = `${fmt(GLITCH_BURN.alpha)}*max(0,1-${d}/0.72)`;+ lines.push(+ `[gto${tag}]split=3[gpb${tag}][gps${tag}][gpo${tag}]`,+ `[gpb${tag}]lutyuv=y='minval':u='(minval+maxval)/2':v='(minval+maxval)/2':${gateOf("split")}[gpk${tag}]`,+ `[gpk${tag}][gps${tag}]overlay=x='${px}*(${sum(["split"], (h) => h.shift)})':y=0:eval=frame:` ++ `${gateOf("split")}:eof_action=pass${chroma.overlay}[gpm${tag}]`,+ `[gpm${tag}][gpo${tag}]blend=c0_mode=lighten:c1_mode=average:c2_mode=average:all_opacity=${fmt(GLITCH_GHOST)}:${gateOf("split")},` ++ `geq=lum='lum(X,Y)+(${code(235, chroma.depth)}-lum(X,Y))*${G}':cb='cb(X,Y)-${code(40, chroma.depth)}*${G}':` ++ `cr='cr(X,Y)+${code(30, chroma.depth)}*${G}':${gateOf("split")},` ++ `noise=alls=28:allf=t+u:${gateOf("smear", "split")}[gpo2${tag}]`+ );++ // Blowout: the preview's brightness and desaturate as one lookup.+ const mid = "(minval+maxval)/2";+ lines.push(+ `[gpo2${tag}]lutyuv=y='clip((val-minval)*2.4+minval,minval,maxval)':` ++ `u='(val-${mid})*0.35+${mid}':v='(val-${mid})*0.35+${mid}':${gateOf("blowout")}[${pin === "rgb" ? `gbo${tag}` : outLabel}]`+ );+ if (pin !== "rgb") {+ return lines;+ }++ // Rgb, pinned only: one channel shift per step, gated to it, since+ // rgbashift takes whole px and no expressions. A 2-frame burst is 1–2.+ const shifts = hits+ .filter((x) => x.hit.kind === "rgb")+ .map((x) => {+ const o = glitchRgb(x.hit);+ const rh = Math.round((o.dx * px) / 1080);+ const rv = Math.round((o.dy * px) / 1080);+ return `rgbashift=rh=${rh}:rv=${rv}:bh=${-rh}:bv=${-rv}:enable='${win(x)}'`;+ });+ lines.push(`[gbo${tag}]${shifts.join(",")}[${outLabel}]`);+ return lines;+}+/*** ffmpeg filter_complex lines rendering one effect from `[inLabel]` into* `[outLabel]`, active only inside [start, end). Every filter used is in the@@ -710,7 +1391,8 @@ export function effectFilterLines(tag: string,focus?: { x: number; y: number },ramp?: number,- chroma: ChainChroma = CHROMA_420+ chroma: ChainChroma = CHROMA_420,+ pulse?: FlashPulse): string[] | null {const custom = customEffects.get(effect);if (custom) return custom.filterLines(inLabel, outLabel, amount, start, end, width, height, tag);@@ -733,7 +1415,11 @@ export function effectFilterLines(`[lkfb${tag}][lkfo${tag}]overlay=0:0:${en}:eof_action=pass${chroma.overlay}[${outLabel}]`,];}- switch (effect as VisualEffectId) {+ const pin = pinnedGlitch(effect);+ if (pin) {+ return glitchFilterLines(inLabel, outLabel, k, start, end, width, height, tag, chroma, pin);+ }+ switch (effect as RecipeId) {case "zoom": {// The pushed-in copy renders on its own branch — scaled up, then laid// back over the frame so the focus point stays where it is — and@@ -780,10 +1466,7 @@ export function effectFilterLines(`noise=alls=${Math.round(14 * k)}:allf=t+u:${en}[${outLabel}]`,];case "glitch":- return [- `[${inLabel}]rgbashift=rh=${Math.round(8 * k)}:bh=${-Math.round(8 * k)}:rv=${Math.round(2 * k)}:${en},` +- `noise=alls=${Math.round(8 * k)}:allf=t+u:${en}[${outLabel}]`,- ];+ return glitchFilterLines(inLabel, outLabel, k, start, end, width, height, tag, chroma);case "blur":return [`[${inLabel}]gblur=sigma=${fmt((10 * k * h) / 1080)}:${en}[${outLabel}]`];case "vignette":@@ -817,14 +1500,38 @@ export function effectFilterLines(`geq=lum='lum(X,Y)+(${code(235, chroma.depth)}-lum(X,Y))*${G}':cb='cb(X,Y)-${code(40, chroma.depth)}*${G}':cr='cr(X,Y)+${code(26, chroma.depth)}*${G}':${en}[${outLabel}]`,];}- case "flash":- // A decaying brightness pop from the element start, added to luma per- // frame by geq — `T` is the frame's time, so the decay rides every- // frame of the window.+ case "flash": {+ // A decaying pop from the element start, or the strobe's square pulse,+ // per frame by geq — `T` is the frame's time, so the gain rides every+ // frame of the window. White adds to luma; black pulls every plane+ // toward black at the canvas pass's alpha.+ const rate = flashRate(pulse);+ const p = `((T-${fmt(start)})*${fmt(rate)}+${PULSE_NUDGE})`;+ // A flicker deals each pulse its strength with the same sine hash as+ // `flickerDeal`, held in register 0.+ const deal = `st(0,sin(floor(${p})*12.9898+78.233)*43758.5453);st(0,ld(0)-floor(ld(0)))`;+ const gain = !rate+ ? `exp(-9*(T-${fmt(start)}))`+ : pulse?.rhythm === "flicker"+ ? `(${deal};lt(mod(${p},1),0.5)*gte(ld(0),${FLICKER_SKIP})*ld(0))`+ : `lt(mod(${p},1),0.5)`;+ if (pulse?.tone === "black") {+ const keep = `(1-min(1,${fmt(0.85 * k)}*${gain}))`;+ const mid = code(128, chroma.depth);+ return [+ `[${inLabel}]geq=lum='${code(16, chroma.depth)}+(lum(X,Y)-${code(16, chroma.depth)})*${keep}':` ++ `cb='${mid}+(cb(X,Y)-${mid})*${keep}':cr='${mid}+(cr(X,Y)-${mid})*${keep}':${en}[${outLabel}]`,+ ];+ }return [- `[${inLabel}]geq=lum='min(${code(235, chroma.depth)},lum(X,Y)+${code(200 * k, chroma.depth)}*exp(-9*(T-${fmt(start)})))':` ++ `[${inLabel}]geq=lum='min(${code(235, chroma.depth)},lum(X,Y)+${code(200 * k, chroma.depth)}*${gain})':` +`cb='cb(X,Y)':cr='cr(X,Y)':${en}[${outLabel}]`,];+ }+ case "burn":+ return burnFilterLines(inLabel, outLabel, k, start, end, chroma);+ case "scorch":+ return scorchFilterLines(inLabel, outLabel, k, start, end, tag, chroma);case "shake": {// The shaken copy renders on its own branch (overscaled, then cropped// with time-jittered offsets) and replaces the frame only inside the@@ -859,3 +1566,248 @@ export function effectFilterLines(return null;}}++/** Steps a second the export's burn is drawn at: one per frame at 60 fps. */+const BURN_RATE = 60;++/** An 8-bit color as limited-range BT.709 Y, Cb, Cr at the chain's depth. */+function yuvCodes(c: [number, number, number], depth: 8 | 10 | undefined): [string, string, string] {+ const [r, g, b] = c.map((v) => v / 255);+ const y = 0.2126 * r + 0.7152 * g + 0.0722 * b;+ return [code(16 + 219 * y, depth), code(128 + (224 * (b - y)) / 1.8556, depth), code(128 + (224 * (r - y)) / 1.5748, depth)];+}++/**+ * The burn in ffmpeg: the element is cut into steps of `1 / BURN_RATE`+ * seconds, and one geq draws each step as the preview has it at the step's+ * start — the same stops down the frame, the same flame band across it.+ * Every term is a ratio of the plane's W and H, so the subsampled chroma+ * planes stay in register with luma.+ */+/**+ * The scorch, a light leak's third course: a film burn flashing through the+ * picture. Each frame is dealt one hot tone and a reach — the whole frame or+ * a soft-edged patch of it — and the picture under the reach is mapped onto+ * the tone by brightness: its darks lift to the tone's deep color, its+ * brights to its pale one. The map is a saturation, a screen and a multiply+ * wash, laid in that order by the preview, the in-tab export and an ffmpeg+ * geq alike, so every renderer lands on the same colors, rims included.+ * Amount is the washes' alpha.+ */+const SCORCH_RATE = 30;++/** One hot tone: how often it is dealt, and its deep and pale colors. */+interface ScorchTone {+ weight: number;+ dark: [number, number, number];+ light: [number, number, number];+}++/** Saturated yellow, orange, and red over pink: the narrow, hot ranges of+ * film burn, where the picture shows only as a shade. */+const SCORCH_TONES: ScorchTone[] = [+ { weight: 0.45, dark: [244, 212, 40], light: [248, 238, 130] },+ { weight: 0.35, dark: [240, 105, 25], light: [250, 180, 110] },+ { weight: 0.2, dark: [165, 32, 16], light: [235, 140, 120] },+];++/** The share of frames that burn the whole picture; the rest burn a patch. */+const SCORCH_FULL = 0.35;++/** How much of a patch's radius burns at full strength before its rim. */+const SCORCH_INNER = 0.85;+++/** The patch a frame step burns, none for the whole frame: a band wider+ * than the frame sitting high, e.g. centered at (0.5, 0.3) reaching 0.9+ * across and 0.45 down. */+function scorchArea(step: number): WashArea | undefined {+ if (scorchDeal(step, 12) < SCORCH_FULL) {+ return undefined;+ }+ return {+ x: 0.2 + 0.6 * scorchDeal(step, 13),+ y: 0.1 + 0.5 * scorchDeal(step, 14),+ rx: 0.6 + 0.5 * scorchDeal(step, 15),+ ry: 0.3 + 0.25 * scorchDeal(step, 16),+ inner: SCORCH_INNER,+ };+}++/** The luma a saturation blend keeps: CSS and canvas compositing weigh+ * red, green and blue this way. */+const BLEND_LUMA = [0.3, 0.59, 0.11];++/** A scorch step's deal on one channel, 0..1: a sine hash ffmpeg's+ * expressions spell the same way, so the export deals each step itself. */+const DEAL = { a: 12.9898, b: 78.233, m: 43758.5453 };+function scorchDeal(step: number, salt: number): number {+ const v = Math.sin(step * DEAL.a + salt * DEAL.b) * DEAL.m;+ return v - Math.floor(v);+}++/** The frame step `tLocal` seconds in falls in, and the tone it is dealt. */+const scorchStep = (tLocal: number) => Math.floor((tLocal + STEP_SLACK) * SCORCH_RATE);+function scorchTone(step: number): number {+ let pick = scorchDeal(step, 11);+ const i = SCORCH_TONES.findIndex((t) => (pick -= t.weight) < 0);+ return i < 0 ? SCORCH_TONES.length - 1 : i;+}++/** A tone's screen color: its deep color over its pale one, so black+ * screened to it and multiplied by the pale color lands on the deep one,+ * e.g. deep 160 and pale 245 → 167. Screening first keeps a patch's rim+ * from darkening: a wash order that multiplies first dims whites halfway+ * down the ramp. */+const scorchScreen = (t: ScorchTone): [number, number, number] =>+ t.dark.map((d, c) => Math.round((255 * d) / Math.max(1, t.light[c]))) as [number, number, number];++const hex = (rgb: [number, number, number]) =>+ `#${rgb.map((v) => Math.round(Math.max(0, Math.min(255, v))).toString(16).padStart(2, "0")).join("")}`;++/** The preview and canvas state of one scorched frame. */+function scorchState(tLocal: number, k: number): EffectPreviewState {+ const step = scorchStep(tLocal);+ const tone = SCORCH_TONES[scorchTone(step)];+ const area = scorchArea(step);+ return {+ cssFilter: "",+ washes: [+ { color: "#808080", alpha: k, mode: "saturation", area },+ { color: hex(scorchScreen(tone)), alpha: k, mode: "screen", area },+ { color: hex(tone.light), alpha: k, mode: "multiply", area },+ ],+ };+}++/** The scorch in ffmpeg: on its own branch in RGB, one geq lays each frame+ * step's three washes in the order the canvas does — the saturation to luma,+ * the screen, the multiply — each at the patch's ramp times the amount, so+ * rims blend as they do in the preview. Each slice deals its step from `T`+ * once a frame into registers the wash math reads, so the expression is the+ * same size however long the element runs. */+function scorchFilterLines(+ inLabel: string,+ outLabel: string,+ k: number,+ start: number,+ end: number,+ tag: string,+ chroma: ChainChroma+): string[] {+ const at = (n: number) => (n - STEP_SLACK).toFixed(5);+ const max = chroma.depth === 10 ? 1023 : 255;+ if (end <= start) {+ return [`[${inLabel}]null[${outLabel}]`];+ }++ // Register 9 holds the frame time the registers were dealt for, plus one,+ // so a fresh slice (0) deals. While dealing, register 1 holds the step and+ // a deal spells scorchDeal, scratching in register 0.+ const deal = (salt: number) => `(st(0,sin(ld(1)*${DEAL.a}+${salt * DEAL.b})*${DEAL.m});ld(0)-floor(ld(0)))`;++ // Registers 3–6: the patch's center and radii; a full-frame wash stores+ // radius 0. Registers 7–8 per channel: the tone's screen and multiply+ // colors, 0..1, picked as scorchTone does.+ const patch =+ `if(lt(${deal(12)},${SCORCH_FULL}),st(5,0),` ++ `st(3,0.2+0.6*${deal(13)});st(4,0.1+0.5*${deal(14)});st(5,0.6+0.5*${deal(15)});st(6,0.3+0.25*${deal(16)}))`;+ const pick = (value: (t: ScorchTone) => number) => {+ let left = "ld(0)";+ const branch = (i: number): string => {+ if (i === SCORCH_TONES.length - 1) {+ return fmt(value(SCORCH_TONES[i]));+ }+ left = `${left}-${SCORCH_TONES[i].weight}`;+ return `if(lt(${left},0),${fmt(value(SCORCH_TONES[i]))},${branch(i + 1)})`;+ };+ return branch(0);+ };+ const dealt = (c: number) =>+ `if(eq(ld(9),T+1),0,st(9,T+1);st(1,floor((T-${start}+${STEP_SLACK})*${SCORCH_RATE}));${patch};st(0,${deal(11)});` ++ `st(7,${pick((t) => scorchScreen(t)[c] / 255)});st(8,${pick((t) => t.light[c] / 255)}))`;++ // Register 0: the wash alpha, the amount — times, on a patch, its ramp: 1+ // inside `inner` of the radius, 0 past the rim, at pixel centers like the+ // canvas gradient. Register 1: the blend luma, 0..1.+ const q = `hypot(((X+0.5)/W-ld(3))/ld(5),((Y+0.5)/H-ld(4))/ld(6))`;+ const alpha = `if(ld(5),${fmt(k)}*clip((1-${q})/${fmt(1 - SCORCH_INNER)},0,1),${fmt(k)})`;+ const luma = BLEND_LUMA.map((w, c) => `${fmt(w)}*${"rgb"[c]}(X,Y)`).join("+");++ // Register 2 per channel: the saturation, then the screen, then the+ // multiply, each at the wash alpha.+ const plane = (c: number) =>+ `${dealt(c)};st(0,${alpha});st(1,(${luma})/${max});` ++ `st(2,(1-ld(0))*${"rgb"[c]}(X,Y)/${max}+ld(0)*ld(1));` ++ `st(2,ld(2)+ld(0)*ld(7)*(1-ld(2)));` ++ `${max}*ld(2)*(1-ld(0)+ld(0)*ld(8))`;+ const all = `enable='gte(t,${at(start)})*lt(t,${at(end)})'`;+ const rgb = chroma.depth === 10 ? "gbrp10le" : "gbrp";+ return [+ `[${inLabel}]split[scb${tag}][scs${tag}]`,+ `[scs${tag}]format=${rgb},geq=r='${plane(0)}':g='${plane(1)}':b='${plane(2)}':${all},format=${chroma.pixFmt}[scc${tag}]`,+ `[scb${tag}][scc${tag}]overlay=0:0:${all}:eof_action=pass${chroma.overlay}[${outLabel}]`,+ ];+}++function burnFilterLines(+ inLabel: string,+ outLabel: string,+ k: number,+ start: number,+ end: number,+ chroma: ChainChroma+): string[] {+ const dur = Math.max(end - start, 1e-3);+ const smooth = (x: string) => `(st(1,clip(${x},0,1));ld(1)*ld(1)*(3-2*ld(1)))`;+ const [flare, yellow, orange, edge, flame] = [BURN_FLARE, BURN_YELLOW, BURN_ORANGE, BURN_EDGE, BURN_FLAME].map((c) =>+ yuvCodes(c, chroma.depth).map(Number)+ );+ const S = BURN_STREAK;++ // A slice works out its step's burn from `T` once a frame, as burnAt does+ // at the step's start: register 4 the front's height, 5 the body color,+ // 6 the alpha, 7 the band's progress, 8 the band's opacity (0 off its+ // run), 9 the frame time plus one, so a fresh slice (0) deals.+ const now = `floor((T-${start}+${STEP_SLACK})*${BURN_RATE})`;+ const step = (p: 0 | 1 | 2) => {+ const core =+ `if(lt(ld(0),0.4),${fmt(flare[p])},if(lt(ld(0),0.6),` ++ `${fmt(flare[p])}+${fmt(yellow[p] - flare[p])}*${smooth("(ld(0)-0.4)/0.2")},` ++ `${fmt(yellow[p])}+${fmt(orange[p] - yellow[p])}*${smooth("(ld(0)-0.6)/0.25")}))`;+ return (+ `if(eq(ld(9),T+1),0,st(9,T+1);st(0,clip(${now}/${BURN_RATE}/${dur},0,1));` ++ `st(4,${1 + BURN_GLOW + BURN_EDGE_W}*${smooth(`ld(0)/${BURN_RISE}`)});st(5,${core});` ++ `st(6,${0.7 + 0.3 * k}*(1-${smooth(`(ld(0)-${BURN_RELEASE})/${1 - BURN_RELEASE}`)}));` ++ `st(7,(ld(0)-${S.from})/${S.span});st(8,if(gt(ld(7),0)*lt(ld(7),1),${S.alpha}*sin(PI*ld(7))*ld(6),0)))`+ );+ };++ // Body: the stops as one piecewise ramp in d (register 0), the height from+ // the bottom edge — the body color to the front's body depth, blending to+ // the hot edge, which fades out over its glow — stored as opacity+ // (register 1) and plane value (register 2).+ const plane = (p: 0 | 1 | 2) => {+ const src = ["lum", "cb", "cr"][p];+ const body = `ld(4)-${BURN_BODY}`;+ const rim = `ld(4)-${BURN_EDGE_W}`;+ let e =+ `st(0,1-Y/H);` ++ `st(1,ld(6)*if(lt(ld(0),${rim}),1,max(0,1-(ld(0)-(${rim}))/${BURN_GLOW + BURN_EDGE_W})));` ++ `st(2,if(lt(ld(0),${body}),ld(5),if(lt(ld(0),${rim}),ld(5)+(${fmt(edge[p])}-ld(5))*(ld(0)-(${body}))/${BURN_BODY - BURN_EDGE_W},${fmt(edge[p])})));` ++ `st(3,${src}(X,Y)+(ld(2)-${src}(X,Y))*ld(1))`;++ // The flame band: the canvas's three-stop ramp is a triangle along its+ // axis, peaking at the band's center.+ const th = (S.angle * Math.PI) / 180;+ const len = `(W*${fmt(Math.abs(Math.sin(th)))}+H*${fmt(Math.abs(Math.cos(th)))})`;+ const q = `((X-W/2)*${fmt(Math.sin(th))}+(Y-H/2)*${fmt(-Math.cos(th))})/${len}+0.5`;+ const tri = `max(0,1-abs(${q}-(${S.p0}-${S.travel}*ld(7)))/${fmt(2 * S.w)})`;+ e += `;if(ld(8),st(3,ld(3)+(${fmt(flame[p])}-ld(3))*ld(8)*${tri}),0)`;++ // A step whose burn has faded out leaves the picture as it is.+ return `${step(p)};if(lte(ld(6),0.001),${src}(X,Y),${e};ld(3))`;+ };+ const win = `enable='gte(t,${fmt(start)})*lt(t,${fmt(end)})'`;+ return [`[${inLabel}]geq=lum='${plane(0)}':cb='${plane(1)}':cr='${plane(2)}':${win}[${outLabel}]`];+}diff --git a/site/packages/effects-kit/src/elementFx.ts b/site/packages/effects-kit/src/elementFx.tsindex 2365d503..b7c55e2f 100644--- a/site/packages/effects-kit/src/elementFx.ts+++ b/site/packages/effects-kit/src/elementFx.ts@@ -17,6 +17,7 @@*/import { streakTaps } from "./camera";+import { TILT_PERSPECTIVE } from "./keys";type Ctx = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;type Surface = HTMLCanvasElement | OffscreenCanvas;@@ -32,6 +33,9 @@ export interface ElementLook {taps: number;/** What a hit leaves of the element's color, 0..1; 1 = untouched. */brightness: number;+ /** A 3D tilt, degrees (see OverlayKey.tiltX/tiltY), turned about (cx, cy)+ * in output px at `depth` px from the viewer; absent = flat. */+ tilt?: { x: number; y: number; cx: number; cy: number; depth: number };}/** A blur below this many output px is invisible and skipped. */@@ -40,22 +44,115 @@ const BLUR_FLOOR = 0.1;/** The look an evaluated frame asks for at `scale` output px per design px,* or null when the element draws sharp and at its own color. */export function elementLook(- ev: { blur?: number; streak?: { x: number; y: number }; brightness?: number },- scale: number+ ev: { blur?: number; streak?: { x: number; y: number }; brightness?: number; tiltX?: number; tiltY?: number },+ scale: number,+ /** The element's posed center on the target, output px; a tilt turns about+ * it. Absent = the element draws flat. */+ center?: { x: number; y: number }): ElementLook | null {const blur = (ev.blur ?? 0) * scale;const taps = ev.streak ? streakTaps(Math.hypot(ev.streak.x, ev.streak.y)) : 0;const brightness = ev.brightness ?? 1;- if (blur < BLUR_FLOOR && taps === 0 && !(brightness < 1)) return null;+ const tilted = !!center && !!(ev.tiltX || ev.tiltY);+ if (blur < BLUR_FLOOR && taps === 0 && !(brightness < 1) && !tilted) return null;return {blur: blur < BLUR_FLOOR ? 0 : blur,streakX: taps ? ev.streak!.x * scale : 0,streakY: taps ? ev.streak!.y * scale : 0,taps,brightness,+ ...(tilted+ ? {+ tilt: {+ x: ev.tiltX ?? 0,+ y: ev.tiltY ?? 0,+ cx: center!.x,+ cy: center!.y,+ depth: TILT_PERSPECTIVE * scale,+ },+ }+ : {}),};}+/** How far, in output px, the affine a tile is drawn with may stray from+ * the true projection at the tile's far corner. */+const TILT_TILE_ERROR = 0.4;++/**+ * Draw `src` onto a cleared `ctx` turned in perspective the way CSS draws+ * perspective(depth) rotateX(x) rotateY(y) about (cx, cy): turned about the+ * vertical axis (the right edge away for a positive y), then about the+ * horizontal one (the top edge away for a positive x), then divided by the+ * depth each point sits at. The picture is cut into tiles small enough that+ * the affine carrying three of a tile's corners to where they project lands+ * the fourth within a fraction of a pixel.+ */+export function drawTilted(+ ctx: Ctx,+ src: Surface,+ x: number,+ y: number,+ cx: number,+ cy: number,+ depth: number+): void {+ const a = (x * Math.PI) / 180;+ const b = (y * Math.PI) / 180;+ const sa = Math.sin(a);+ const ca = Math.cos(a);+ const sb = Math.sin(b);+ const cb = Math.cos(b);++ // A source pixel's depth, and where it lands on the target.+ const zOf = (u: number, v: number) => (v - cy) * sa - (u - cx) * ca * sb;+ const land = (u: number, v: number): [number, number] => {+ const du = u - cx;+ const dv = v - cy;+ const f = depth / Math.max(1, depth - zOf(u, v));+ return [cx + du * cb * f, cy + (dv * ca + du * sa * sb) * f];+ };++ // Square tiles: across a tile of side T depth changes by about slope·T,+ // which scales its far corner by that over the depth, a miss of about+ // slope·T² / depth px. Sized to keep the miss under the bound, e.g. 1600+ // deep with x tipped 60° gives 27 px tiles.+ const slope = Math.max(Math.abs(ca * sb), Math.abs(sa), 1e-6);+ const side = Math.max(4, Math.sqrt((TILT_TILE_ERROR * depth) / slope));+ const tw = Math.min(src.width, side);+ const th = Math.min(src.height, side);++ ctx.save();+ for (let v = 0; v < src.height; v += th) {+ for (let u = 0; u < src.width; u += tw) {+ const w = Math.min(tw, src.width - u);+ const h = Math.min(th, src.height - v);++ // A tile turned past the viewer never shows.+ if (depth - zOf(u + w / 2, v + h / 2) <= 1) {+ continue;+ }++ // The affine that carries the tile's corner and its two edges onto+ // where they land.+ const [x0, y0] = land(u, v);+ const [x1, y1] = land(u + w, v);+ const [x2, y2] = land(u, v + h);+ const m = [(x1 - x0) / w, (y1 - y0) / w, (x2 - x0) / h, (y2 - y0) / h];+ ctx.setTransform(m[0], m[1], m[2], m[3], x0 - m[0] * u - m[2] * v, y0 - m[1] * u - m[3] * v);++ // Each tile reaches half a pixel into its neighbors, so antialiased+ // seams between them never show.+ const u0 = Math.max(0, u - 0.5);+ const v0 = Math.max(0, v - 0.5);+ const u1 = Math.min(src.width, u + w + 0.5);+ const v1 = Math.min(src.height, v + h + 0.5);+ ctx.drawImage(src, u0, v0, u1 - u0, v1 - v0, u0, v0, u1 - u0, v1 - v0);+ }+ }+ ctx.restore();+}+/*** Multiply every color already on a surface by `brightness` (0..1), leaving* alpha alone: black laid source-atop at 1 - brightness. The surface has to@@ -157,6 +254,17 @@ export class ElementFx {}// The scratch holds the element alone, so a hit darkens its pixels only.darkenCanvas(src.getContext("2d") as Ctx, src.width, src.height, look.brightness);+ // A tilt turns the finished picture in perspective, read from one+ // scratch and drawn onto the other.+ const tilt = look.tilt;+ if (tilt) {+ const dst = src === this.picture ? (this.smear = this.fit(this.smear, src.width, src.height)) : this.picture!;+ const dc = dst.getContext("2d") as Ctx;+ reset(dc);+ dc.clearRect(0, 0, dst.width, dst.height);+ drawTilted(dc, src, tilt.x, tilt.y, tilt.cx, tilt.cy, tilt.depth);+ src = dst;+ }ctx.save();reset(ctx);if (look.blur > 0 && "filter" in ctx) ctx.filter = `blur(${look.blur.toFixed(2)}px)`;diff --git a/site/packages/effects-kit/src/flash.test.ts b/site/packages/effects-kit/src/flash.test.tsnew file mode 100644index 00000000..a2561e33--- /dev/null+++ b/site/packages/effects-kit/src/flash.test.ts@@ -0,0 +1,139 @@+import { describe, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { existsSync } from "node:fs";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { Canvas } from "skia-canvas";+import { applyClipEffects, applyEffectToCanvas, effectFilterLines, effectPreviewState } from "./effects";++// The black strobe: a flash held dark for the first half of every pulse.+const STROBE = { tone: "black", rate: 5 } as const;++describe("the flash strobe", () => {+ test("holds its tone for the first half of each pulse, then shows the picture", () => {+ // At 5 pulses a second a pulse is 0.2s: dark through 0.1s, lit to 0.2s.+ const on = effectPreviewState("flash", 1, 0.05, undefined, undefined, 2, STROBE);+ const off = effectPreviewState("flash", 1, 0.15, undefined, undefined, 2, STROBE);+ const again = effectPreviewState("flash", 1, 0.25, undefined, undefined, 2, STROBE);+ expect(on.flashTone).toBe("black");+ expect(on.flash).toBeCloseTo(0.85);+ expect(off.flash ?? 0).toBe(0);+ expect(again.flash).toBeCloseTo(0.85);+ });++ test("with no rate it is the single pop, and white carries no tone", () => {+ const pop = effectPreviewState("flash", 1, 0.5, undefined, undefined, 2, { tone: "black" });+ const early = effectPreviewState("flash", 1, 0.02, undefined, undefined, 2, { tone: "black" });+ expect(early.flash!).toBeGreaterThan(pop.flash!);+ expect(effectPreviewState("flash", 1, 0.02).flashTone).toBeUndefined();+ });++ test("the canvas pass paints black on a dark beat", () => {+ const W = 8;+ const grey = () => {+ const c = new Canvas(W, W) as unknown as HTMLCanvasElement;+ const ctx = c.getContext("2d")!;+ ctx.fillStyle = "#808080";+ ctx.fillRect(0, 0, W, W);+ return c;+ };+ const lum = (c: HTMLCanvasElement) => c.getContext("2d")!.getImageData(4, 4, 1, 1).data[0];+ const dark = grey();+ applyEffectToCanvas(dark, grey(), "flash", 1, 0.05, () => null, undefined, undefined, 2, "opaque", STROBE);+ const lit = grey();+ applyEffectToCanvas(lit, grey(), "flash", 1, 0.15, () => null, undefined, undefined, 2, "opaque", STROBE);+ expect(lum(dark)).toBeLessThan(30);+ expect(lum(lit)).toBe(128);+ });++ test("the ffmpeg recipe strobes the same frames in the bundled LGPL build", () => {+ const bin = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools/ffmpeg");+ expect(existsSync(bin)).toBe(true);+ const lines = effectFilterLines("in", "out", "flash", 1, 0, 1, 64, 64, "t", undefined, undefined, undefined, STROBE)!;+ const graph = `color=c=0x808080:s=64x64:r=30:d=0.4,format=yuv420p[in];${lines.join(";")}`;+ const run = spawnSync(bin, [+ "-v", "error", "-filter_complex", graph, "-map", "[out]", "-f", "rawvideo", "-pix_fmt", "gray", "-",+ ]);+ expect(run.status).toBe(0);+ // One luma sample per frame: frames 0–2 sit in the first dark half+ // (0..0.1s), frames 3–5 in the lit half.+ const frames = [...Array(12).keys()].map((i) => run.stdout[i * 64 * 64 + 32 * 64 + 32]);+ expect(frames[0]).toBeLessThan(40);+ expect(frames[2]).toBeLessThan(40);+ expect(frames[4]).toBeGreaterThan(100);+ expect(frames[7]).toBeLessThan(40);+ });+});++describe("the fastest strobe", () => {+ // At 15 pulses a second a 30 fps cut is one frame dark, one frame lit, so+ // every frame lands on a pulse's edge: the clip here starts mid-timeline,+ // where the frame times carry rounding.+ const FAST = { tone: "black", rate: 15 } as const;+ const START = 3.5;+ const N = 12;++ test("alternates dark and lit frame by frame in the preview", () => {+ const dark = [...Array(N).keys()].map((n) => (effectPreviewState("flash", 1, (105 + n) / 30 - START, undefined, undefined, 1, FAST).flash ?? 0) > 0.5);+ expect(dark).toEqual([...Array(N).keys()].map((n) => n % 2 === 0));+ });++ test("alternates on the same frames in the bundled LGPL build", () => {+ const bin = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools/ffmpeg");+ const lines = effectFilterLines("in", "out", "flash", 1, START, START + 1, 64, 64, "t", undefined, undefined, undefined, FAST)!;+ const graph = `color=c=0x808080:s=64x64:r=30:d=4.2,format=yuv420p[in];${lines.join(";")}`;+ const run = spawnSync(bin, ["-v", "error", "-filter_complex", graph, "-map", "[out]", "-f", "rawvideo", "-pix_fmt", "gray", "-"]);+ expect(run.status).toBe(0);+ const dark = [...Array(N).keys()].map((n) => run.stdout[(105 + n) * 64 * 64 + 32 * 64 + 32] < 60);+ expect(dark).toEqual([...Array(N).keys()].map((n) => n % 2 === 0));+ });+});++// The white flicker: one-frame pulses, each dealt its own strength.+const FLICKER = { rate: 15, rhythm: "flicker" } as const;+const FRAMES = 60;++describe("the flash flicker", () => {+ // The preview's strength on each frame's pulse.+ const preview = [...Array(FRAMES).keys()].map((i) => effectPreviewState("flash", 1, i / 30, undefined, undefined, 2, FLICKER).flash ?? 0);++ test("pops unevenly: some pulses dark, the rest at strengths of their own", () => {+ const lit = preview.filter((v) => v > 0);+ expect(lit.length).toBeGreaterThan(8);+ expect(lit.length).toBeLessThan(FRAMES / 2);+ expect(new Set(lit.map((v) => v.toFixed(2))).size).toBeGreaterThan(5);+ });++ test("a clip wearing it pops only its own opaque picture", () => {+ // A transparent canvas with a grey square: the clip inside its mask.+ const clip = () => {+ const c = new Canvas(8, 8) as unknown as HTMLCanvasElement;+ const ctx = c.getContext("2d")!;+ ctx.fillStyle = "#404040";+ ctx.fillRect(2, 2, 4, 4);+ return c;+ };+ const frame = preview.findIndex((v) => v > 0.5);+ const c = clip();+ applyClipEffects(c, clip(), [{ effect: "flash", amount: 1, ...FLICKER }], frame / 30, () => null);+ const px = (x: number, y: number) => c.getContext("2d")!.getImageData(x, y, 1, 1).data;+ expect(px(4, 4)[0]).toBeGreaterThan(0x40 + 60);+ expect(px(0, 0)[3]).toBe(0);+ });++ test("the ffmpeg recipe lights the same frames in the bundled LGPL build", () => {+ const bin = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools/ffmpeg");+ const lines = effectFilterLines("in", "out", "flash", 1, 0, 2, 64, 64, "t", undefined, undefined, undefined, FLICKER)!;+ const graph = `color=c=0x202020:s=64x64:r=30:d=${FRAMES / 30},format=yuv420p[in];${lines.join(";")}`;+ const run = spawnSync(bin, ["-v", "error", "-filter_complex", graph, "-map", "[out]", "-f", "rawvideo", "-pix_fmt", "gray", "-"]);+ expect(run.status).toBe(0);+ const base = run.stdout[32 * 64 + 32 + 64 * 64 * preview.findIndex((v) => v === 0)];+ const frames = [...Array(FRAMES).keys()].map((i) => run.stdout[i * 64 * 64 + 32 * 64 + 32] - base);+ // Lit exactly where the preview lights, brighter where it deals more.+ for (let i = 0; i < FRAMES; i++) {+ expect(frames[i] > 8).toBe(preview[i] > 0);+ }+ const order = (xs: number[]) => xs.map((_, i) => i).filter((i) => preview[i] > 0).sort((a, b) => xs[a] - xs[b]);+ expect(order(frames)[order(frames).length - 1]).toBe(order(preview)[order(preview).length - 1]);+ });+});diff --git a/site/packages/effects-kit/src/fontCycle.test.ts b/site/packages/effects-kit/src/fontCycle.test.tsnew file mode 100644index 00000000..2a01cc43--- /dev/null+++ b/site/packages/effects-kit/src/fontCycle.test.ts@@ -0,0 +1,70 @@+import { describe, expect, test } from "bun:test";+import { cutAtFaces, cycleFaceAt, cycleFaceTag, fontCycleCuts, fontCycleVariants, withFontCycle } from "./fontCycle";+import type { TextOverlay } from "./types";++const title = (fonts?: NonNullable<TextOverlay["anim"]>["fonts"]): TextOverlay => ({+ id: "t",+ kind: "text",+ text: "STAY TUNED",+ start: 2,+ end: 4,+ x: 0.5,+ y: 0.5,+ size: 50,+ font: "montserrat",+ weight: 700,+ color: "#ffffff",+ letterSpacing: 0.3,+ shadow: false,+ plate: false,+ ...(fonts ? { anim: { fonts } } : {}),+});++const CYCLE = {+ faces: [{ font: "caveat", weight: 400 as const, scale: 2 }, { font: "anton" }],+ at: 0.5,+ seconds: 1,+ rate: 10,+};++describe("the font cycle", () => {+ test("a new face every step, in order, wrapping round", () => {+ expect(cycleFaceAt(CYCLE, 0.5)?.font).toBe("caveat");+ expect(cycleFaceAt(CYCLE, 0.6)?.font).toBe("anton");+ expect(cycleFaceAt(CYCLE, 0.75)?.font).toBe("caveat");+ });++ test("the title keeps its own face outside the run", () => {+ expect(cycleFaceAt(CYCLE, 0.4)).toBeNull();+ expect(cycleFaceAt(CYCLE, 1.5)).toBeNull();+ const o = title(CYCLE);+ expect(withFontCycle(o, 0.2)).toBe(o);+ });++ test("a face sets the font, weight, size and keeps what it leaves out", () => {+ const set = withFontCycle(title(CYCLE), 0.52);+ expect(set.font).toBe("caveat");+ expect(set.weight).toBe(400);+ expect(set.size).toBe(100);+ expect(set.letterSpacing).toBe(0.3);+ // The face carries no cycle, so resolving it again changes nothing.+ expect(withFontCycle(set, 0.62)).toBe(set);+ expect(withFontCycle(title(CYCLE), 0.62).weight).toBe(700);+ });++ test("samplers cut at every face change and the run's end", () => {+ expect(fontCycleCuts(title(CYCLE), 2)).toEqual([0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5]);+ expect(cutAtFaces([{ start: 0, end: 2 }], [0.5, 1.5])).toEqual([+ { start: 0, end: 0.5 },+ { start: 0.5, end: 1.5 },+ { start: 1.5, end: 2 },+ ]);+ });++ test("a sampler sees every face, tagged apart from the title's own", () => {+ expect(fontCycleVariants(title(CYCLE)).map((v) => v.font)).toEqual(["caveat", "anton"]);+ expect(cycleFaceTag(title(CYCLE), 0.65)).toBe("|face1");+ expect(cycleFaceTag(title(CYCLE), 0.1)).toBe("");+ expect(fontCycleCuts(title(), 2)).toEqual([]);+ });+});diff --git a/site/packages/effects-kit/src/fontCycle.ts b/site/packages/effects-kit/src/fontCycle.tsnew file mode 100644index 00000000..7530be8d--- /dev/null+++ b/site/packages/effects-kit/src/fontCycle.ts@@ -0,0 +1,137 @@+/**+ * The font flicker: a title that cycles through typefaces for a stretch of+ * its life, a new face every step, then settles back into its own. Each face+ * can carry its own weight, size and tracking, because a face shown for a+ * frame or two has to fill the same room the title's own face does.+ *+ * It is a slot on the element's animation and changes the type itself, so+ * every painter reads the element through `withFontCycle` at the moment it+ * draws, and every sampler cuts its pictures at `fontCycleCuts`.+ */++import type { Overlay, TextOverlay } from "./types";++/** One face in the cycle. Absent fields keep the title's own. */+export interface CycleFace {+ font: string;+ weight?: TextOverlay["weight"];+ /** Type-size multiplier on the title's own size. */+ scale?: number;+ /** Letter spacing, em; absent = the title's own. */+ tracking?: number;+}++/** The cycle: `faces` in order, `rate` changes a second, running from `at`+ * seconds into the element for `seconds`. */+export interface OverlayFontCycle {+ faces: CycleFace[];+ at: number;+ seconds: number;+ rate: number;+}++/** Face changes a second the cycle runs at unless it names its own: one every+ * other frame at 30 fps. */+export const FONT_CYCLE_RATE = 15;+export const FONT_CYCLE_RATE_MIN = 2;+export const FONT_CYCLE_RATE_MAX = 30;+/** The most faces one cycle walks. */+export const FONT_CYCLE_FACES_MAX = 12;+/** Face scale bounds. */+export const CYCLE_SCALE_MIN = 0.5;+export const CYCLE_SCALE_MAX = 4;++/** Slack on a step's edges, well under a frame: a frame time on an edge+ * belongs to the step it opens in every renderer. */+const STEP_SLACK = 1e-4;++const rateOf = (c: OverlayFontCycle) =>+ Math.max(FONT_CYCLE_RATE_MIN, Math.min(FONT_CYCLE_RATE_MAX, c.rate > 0 ? c.rate : FONT_CYCLE_RATE));++/** The cycle a text element carries, or nothing — other kinds have no face. */+export function overlayFontCycle(o: Overlay): OverlayFontCycle | undefined {+ if ((o.kind ?? "text") !== "text") return undefined;+ const c = o.anim?.fonts;+ return c && c.faces.length > 0 && c.seconds > 0 ? c : undefined;+}++/** The face showing `tLocal` seconds into the element, or null outside the+ * cycle's run. */+export function cycleFaceAt(c: OverlayFontCycle, tLocal: number): CycleFace | null {+ const u = tLocal - c.at;+ if (u < -STEP_SLACK || u >= c.seconds - STEP_SLACK) return null;+ const step = Math.max(0, Math.floor((u + STEP_SLACK) * rateOf(c)));+ return c.faces[step % c.faces.length];+}++/** The element as it is set `tLocal` seconds in: the cycle's face while it+ * runs, its own outside it. Hands back the same object when nothing changes,+ * so a caller can memoize on identity. The face it hands back carries no+ * cycle, so resolving it again changes nothing. */+export function withFontCycle<T extends Overlay>(o: T, tLocal: number): T {+ const c = overlayFontCycle(o);+ if (!c) return o;+ const face = cycleFaceAt(c, tLocal);+ if (!face) return o;+ const t = o as TextOverlay;+ const scale = Math.max(CYCLE_SCALE_MIN, Math.min(CYCLE_SCALE_MAX, face.scale ?? 1));+ return {+ ...o,+ font: face.font,+ weight: face.weight ?? t.weight,+ size: t.size * scale,+ letterSpacing: face.tracking ?? t.letterSpacing,+ anim: { ...o.anim, fonts: undefined },+ } as T;+}++/** Seconds from the element's start where its face changes, inside (0, dur):+ * the edges of every step, and the cycle's own start and end. A sampler cuts+ * its pictures here. */+export function fontCycleCuts(o: Overlay, dur: number): number[] {+ const c = overlayFontCycle(o);+ if (!c) return [];+ const rate = rateOf(c);+ const end = Math.min(dur, c.at + c.seconds);+ const cuts: number[] = [];+ for (let i = 0; ; i++) {+ const t = c.at + i / rate;+ if (t >= end - 1e-3) break;+ if (t > 1e-3) cuts.push(Math.round(t * 1e4) / 1e4);+ }+ if (end > 1e-3 && end < dur - 1e-3) cuts.push(Math.round(end * 1e4) / 1e4);+ return cuts;+}++/** `spans` (a partition of [0, dur]) cut again at every face change, each+ * piece the span it came from. */+export function cutAtFaces<S extends { start: number; end: number }>(spans: S[], cuts: number[]): S[] {+ if (cuts.length === 0) return spans;+ const out: S[] = [];+ for (const s of spans) {+ let a = s.start;+ for (const c of cuts) {+ if (c <= a + 1e-3 || c >= s.end - 1e-3) continue;+ out.push({ ...s, start: a, end: c });+ a = c;+ }+ out.push({ ...s, start: a, end: s.end });+ }+ return out;+}++/** The element once in each face of its cycle — what a sampler measures so+ * its crop holds the widest face as well as the title's own. */+export function fontCycleVariants<T extends Overlay>(o: T): T[] {+ const c = overlayFontCycle(o);+ if (!c) return [];+ return c.faces.map((_, i) => withFontCycle(o, c.at + (i + 0.5) / rateOf(c)));+}++/** A tag naming the face showing at `tLocal`, for a sampler that caches+ * pictures by what they show; "" in the title's own face. */+export function cycleFaceTag(o: Overlay, tLocal: number): string {+ const c = overlayFontCycle(o);+ const face = c ? cycleFaceAt(c, tLocal) : null;+ return face ? `|face${c!.faces.indexOf(face)}` : "";+}diff --git a/site/packages/effects-kit/src/glitchRgb.test.ts b/site/packages/effects-kit/src/glitchRgb.test.tsnew file mode 100644index 00000000..4eb80511--- /dev/null+++ b/site/packages/effects-kit/src/glitchRgb.test.ts@@ -0,0 +1,115 @@+import { afterAll, beforeAll, describe, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { existsSync } from "node:fs";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { Canvas } from "skia-canvas";+import { applyEffectToCanvas, effectFilterLines, effectPreviewState, effectRecipe, glitchHitAt } from "./effects";++/**+ * A glitch pinned to one kind hits with it on every frame, and its rgb kind+ * pulls a white bar apart: the channel moved up shows alone over the bar, the+ * one moved down alone under it, in the canvas pass and the ffmpeg graph.+ */++const S = 270;++// The split draws its channels on a kit-made plane; this file lends the kit+// skia's canvas as its OffscreenCanvas and hands the global back after.+const lent = globalThis.OffscreenCanvas;+beforeAll(() => {+ (globalThis as { OffscreenCanvas?: unknown }).OffscreenCanvas = Canvas;+});+afterAll(() => {+ (globalThis as { OffscreenCanvas?: unknown }).OffscreenCanvas = lent;+});+const BAR_TOP = 120;+const BAR_BOTTOM = 150;++/** Black with a full-width white bar across rows [120, 150). */+function barred(): HTMLCanvasElement {+ const c = new Canvas(S, S) as unknown as HTMLCanvasElement;+ const ctx = c.getContext("2d")!;+ ctx.fillStyle = "#000000";+ ctx.fillRect(0, 0, S, S);+ ctx.fillStyle = "#ffffff";+ ctx.fillRect(0, BAR_TOP, S, BAR_BOTTOM - BAR_TOP);+ return c;+}++/** The channel moved up and the one moved down, by the first step's offset. */+function sides(): { up: number; down: number; d: number } {+ const rgb = effectPreviewState("glitch:rgb", 1, 0).rgb!;+ const d = Math.round((rgb.dy * S) / 1080);+ // Red moves by the offset, blue by its opposite: channel 0 is red, 2 blue.+ return d > 0 ? { up: 2, down: 0, d } : { up: 0, down: 2, d: -d };+}++describe("a pinned glitch", () => {+ test("a glitch with a kind renders that kind's recipe; other effects ignore it", () => {+ expect(effectRecipe({ effect: "glitch", glitch: "rgb" })).toBe("glitch:rgb");+ expect(effectRecipe({ effect: "glitch" })).toBe("glitch");+ expect(effectRecipe({ effect: "flash", glitch: "rgb" })).toBe("flash");+ });++ test("it hits on every step, where the random mix leaves clean ones", () => {+ const steps = [...Array(60).keys()];+ expect(steps.every((n) => glitchHitAt(n, 1, "rgb")?.kind === "rgb")).toBe(true);+ expect(steps.some((n) => glitchHitAt(n, 1) === null)).toBe(true);+ expect([...Array(3000).keys()].some((n) => glitchHitAt(n, 1)?.kind === "rgb")).toBe(false);+ });++ test("the canvas pass splits the bar: one channel alone over it, the other under it", () => {+ const { up, down, d } = sides();+ expect(d).toBeGreaterThanOrEqual(3);+ const c = barred();+ applyEffectToCanvas(c, barred(), "glitch:rgb", 1, 0, () => null);+ const px = (y: number) => Array.from(c.getContext("2d")!.getImageData(S / 2, y, 1, 1).data);+ const over = px(BAR_TOP - 2);+ const under = px(BAR_BOTTOM + 1);+ expect(over[up]).toBeGreaterThan(200);+ expect(over[down]).toBeLessThan(40);+ expect(under[down]).toBeGreaterThan(200);+ expect(under[up]).toBeLessThan(40);+ });++ test("split frames on one scratch draw on one plane", () => {+ // Count the planes the kit makes across a run of split frames.+ let made = 0;+ class Counted extends Canvas {+ constructor(w: number, h: number) {+ super(w, h);+ made++;+ }+ }+ (globalThis as { OffscreenCanvas?: unknown }).OffscreenCanvas = Counted;+ try {+ const scratch = barred();+ for (let i = 0; i < 3; i++) {+ applyEffectToCanvas(barred(), scratch, "glitch:rgb", 1, i / 30, () => null);+ }+ expect(made).toBe(1);+ } finally {+ (globalThis as { OffscreenCanvas?: unknown }).OffscreenCanvas = Canvas;+ }+ });++ test("the ffmpeg recipe splits the same way in the bundled LGPL build", () => {+ const bin = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools/ffmpeg");+ expect(existsSync(bin)).toBe(true);+ const { up, down } = sides();+ const lines = effectFilterLines("in", "out", "glitch:rgb", 1, 0, 0.2, S, S, "t")!;+ const graph =+ `color=c=black:s=${S}x${S}:r=30:d=0.1,format=yuv444p,` ++ `drawbox=x=0:y=${BAR_TOP}:w=${S}:h=${BAR_BOTTOM - BAR_TOP}:color=white:t=fill[in];${lines.join(";")}`;+ const run = spawnSync(bin, ["-v", "error", "-filter_complex", graph, "-map", "[out]", "-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "rgb24", "-"]);+ expect(run.status).toBe(0);+ const px = (y: number) => Array.from(run.stdout.subarray((y * S + S / 2) * 3, (y * S + S / 2) * 3 + 3));+ const over = px(BAR_TOP - 2);+ const under = px(BAR_BOTTOM + 1);+ expect(over[up]).toBeGreaterThan(200);+ expect(over[down]).toBeLessThan(40);+ expect(under[down]).toBeGreaterThan(200);+ expect(under[up]).toBeLessThan(40);+ });+});diff --git a/site/packages/effects-kit/src/glyphLook.test.ts b/site/packages/effects-kit/src/glyphLook.test.tsnew file mode 100644index 00000000..f4957714--- /dev/null+++ b/site/packages/effects-kit/src/glyphLook.test.ts@@ -0,0 +1,101 @@+import { describe, expect, test } from "bun:test";+import { evalOverlayAnim, glyphStateAt } from "./anim";+import { planAnimatedLayers } from "./render";++// A five-letter title ("TUNED") on screen for 4s.+const LETTERS = 5;+const DUR = 4;++/** Letter `i` of a title whose `slot` edge runs over `seconds`, at `t`. */+function letter(slot: "in" | "out", style: string, seconds: number, t: number, i: number) {+ const anim = { [slot]: { style, seconds } } as Parameters<typeof evalOverlayAnim>[0];+ return glyphStateAt(evalOverlayAnim(anim, t, DUR, true), i, LETTERS);+}++describe("letters that sharpen out of a blur", () => {+ test("each letter starts defocused and lands sharp", () => {+ const early = [...Array(LETTERS).keys()].map((i) => letter("in", "blur", 0.8, 0.02, i));+ expect(Math.max(...early.map((g) => g.blur ?? 0))).toBeGreaterThan(8);+ for (let i = 0; i < LETTERS; i++) {+ expect(letter("in", "blur", 0.8, 0.8, i).blur ?? 0).toBe(0);+ }+ });++ test("the letters land in a shuffled order", () => {+ // The first letter to show is not simply the leftmost one.+ const firstSeen = (i: number) => {+ for (let t = 0; t <= 0.8; t += 1 / 120) {+ if (letter("in", "blur", 0.8, t, i).alpha > 0.05) {+ return t;+ }+ }+ return Infinity;+ };+ const order = [...Array(LETTERS).keys()].sort((a, b) => firstSeen(a) - firstSeen(b));+ expect(order).not.toEqual([0, 1, 2, 3, 4]);+ });+});++describe("letters that glitch in", () => {+ test("a letter arrives split into red and cyan and settles whole", () => {+ const mid = [...Array(LETTERS).keys()].map((i) => letter("in", "glitch", 0.5, 0.15, i));+ expect(Math.max(...mid.map((g) => g.split ?? 0))).toBeGreaterThan(4);+ for (let i = 0; i < LETTERS; i++) {+ expect(letter("in", "glitch", 0.5, 0.5, i).split ?? 0).toBe(0);+ }+ });+});++describe("letters that flicker out", () => {+ test("each letter blinks on its own pattern and the line ends dark", () => {+ // Sampled across the exit, the letters go dark at different moments.+ const seconds = 0.75;+ const start = DUR - seconds;+ const darkAt = (i: number) =>+ [...Array(20).keys()].map((k) => letter("out", "flicker", seconds, start + (k + 0.5) * (seconds / 20), i).alpha < 0.5);+ const patterns = [...Array(LETTERS).keys()].map((i) => darkAt(i).join(""));+ expect(new Set(patterns).size).toBeGreaterThan(2);+ // Every letter blinks at least once and comes back at least once.+ for (const p of [...Array(LETTERS).keys()].map(darkAt)) {+ expect(p.some(Boolean)).toBe(true);+ expect(p.some((d) => !d)).toBe(true);+ }+ // The flicker takes hold early: through the exit's first half, well over+ // a third of the letter samples are out.+ const firstHalf = [...Array(LETTERS).keys()].flatMap((i) => darkAt(i).slice(0, 10));+ expect(firstHalf.filter(Boolean).length / firstHalf.length).toBeGreaterThan(0.35);+ // Before the exit starts the line is steady.+ for (let i = 0; i < LETTERS; i++) {+ expect(letter("out", "flicker", seconds, start - 0.1, i).alpha).toBe(1);+ }+ });+});++describe("the canvas export's ramp pictures", () => {+ test("each frame of a glyph ramp shows the moment the preview shows", () => {+ // A title on a frame boundary with a 0.6s glitch in and a 0.75s flicker+ // out: at every 30 fps frame the window covering it carries the ramp+ // position the live evaluator gives for that frame's time.+ const o = {+ id: "t",+ kind: "text",+ text: "TUNED",+ start: 2,+ end: 2 + DUR,+ x: 0.5,+ y: 0.5,+ anim: { in: { style: "glitch", seconds: 0.6 }, out: { style: "flicker", seconds: 0.75 } },+ } as Parameters<typeof planAnimatedLayers>[0];+ const layers = planAnimatedLayers(o, o.end);+ for (let k = 0; k < DUR * 30; k++) {+ const t = k / 30;+ const live = evalOverlayAnim(o.anim!, t, DUR, true).glyphs;+ if (!live) {+ continue;+ }+ const at = o.start + t + 1e-9;+ const layer = layers.find((l) => l.start <= at && at < l.end)!;+ expect(layer.phase?.glyphs?.p).toBeCloseTo(live.p, 6);+ }+ });+});diff --git a/site/packages/effects-kit/src/glyphLook.ts b/site/packages/effects-kit/src/glyphLook.tsnew file mode 100644index 00000000..d2068646--- /dev/null+++ b/site/packages/effects-kit/src/glyphLook.ts@@ -0,0 +1,24 @@+/**+ * How a letter looks mid-glitch, shared by the canvas painter and the DOM+ * preview so both tear a title into the same two copies.+ */++/** The channel copies a split letter shows either side of itself: red pulled+ * left, cyan pulled right, the letter itself on top. */+export const SPLIT_RED = "#FF2A55";+export const SPLIT_CYAN = "#00E5FF";++/** Whether a text color lays down no ink — an outline-only title's fill.+ * Its tinted copies keep the fill clear too, so a hollow title tears into+ * hollow copies. "transparent", "#RGB0", "#RRGGBB00" and a zero-alpha+ * rgba()/hsla() all count. */+export function isInkless(color: string): boolean {+ const c = color.trim().toLowerCase();+ if (c === "transparent") {+ return true;+ }+ if (/^#([0-9a-f]{3}0|[0-9a-f]{6}00)$/.test(c)) {+ return true;+ }+ return /^(rgba|hsla)\(.*[,/]\s*0(\.0*)?\s*\)$/.test(c);+}diff --git a/site/packages/effects-kit/src/index.ts b/site/packages/effects-kit/src/index.tsindex c8401276..7a14b98c 100644--- a/site/packages/effects-kit/src/index.ts+++ b/site/packages/effects-kit/src/index.ts@@ -1,16 +1,22 @@export * from "./types";export * from "./anim";+export * from "./fontCycle";+export * from "./doodle";+export * from "./glyphLook";+export * from "./count";export * from "./dive";export * from "./words";export * from "./motion/types";export * from "./motion/evaluate";export * from "./motion/catalog";export * from "./keys";+export * from "./stretch";export * from "./ease";export * from "./camera";export * from "./elementFx";export * from "./mask";export * from "./shapePath";+export * from "./pattern";export * from "./render";export * from "./registry";export * from "./cutout";@@ -31,5 +37,7 @@ export * from "./gradeLut";export * from "./gradePresets";export * from "./gradeMatch";export * from "./effects";+export * from "./texture";+export * from "./zap";export * from "./audioFx";export * from "./lutFile";diff --git a/site/packages/effects-kit/src/keys.ts b/site/packages/effects-kit/src/keys.tsindex fa02b3d4..a28ae5e5 100644--- a/site/packages/effects-kit/src/keys.ts+++ b/site/packages/effects-kit/src/keys.ts@@ -34,6 +34,8 @@ import {type GroupCamera,} from "./camera";import { easeAt, type EaseId } from "./ease";+import { splitScale } from "./stretch";+import { dealsMarks, type ShapeKind } from "./types";/** A pose captured at `t` seconds into the element. Values are absolute, in* the same units as the element's own fields — `scale` multiplies the@@ -43,7 +45,16 @@ export interface OverlayKey {x: number; // center, fraction of frame widthy: number; // center, fraction of frame heightscale: number;+ /** Width and height over `scale`; absent = 1. A screen switching off+ * squashes scaleY toward 0. */+ scaleX?: number;+ scaleY?: number;rotation: number; // degrees clockwise+ /** 3D tilt in degrees, seen in perspective (TILT_PERSPECTIVE): tiltX turns+ * the element about its horizontal axis, top edge away from the viewer;+ * tiltY about its vertical axis, right edge away. Absent = flat. */+ tiltX?: number;+ tiltY?: number;opacity: number; // 0..1/** Gaussian blur, px at the 1080 short side; absent = the element's own. */blur?: number;@@ -51,12 +62,31 @@ export interface OverlayKey {ease?: EaseId;}+/** How far the viewer sits from a tilted element, px at the 1080 design+ * short side: the CSS perspective() the DOM draws with and the depth the+ * canvas painters project through. */+export const TILT_PERSPECTIVE = 1600;++/** The steepest tilt a key holds, degrees either way: at 90 the element is+ * edge-on and gone. */+export const TILT_MAX = 85;++/** The most a tilt can grow an element's nearer edge, for the frame sampler's+ * crop: an edge 540 design px off center tipped fully toward the viewer. */+export const TILT_GROW = TILT_PERSPECTIVE / (TILT_PERSPECTIVE - 540);+/** What an element looks like at one moment, before presets compose over it. */export interface OverlayPose {x: number;y: number;+ /** The larger axis; `sx`/`sy` stretch each axis over it (see stretch.ts). */scale: number;+ sx?: number;+ sy?: number;rotation: number;+ /** Absent = flat (see OverlayKey). */+ tiltX?: number;+ tiltY?: number;opacity: number;/** Absent = sharp. */blur?: number;@@ -73,6 +103,8 @@ interface Posable {/** Absent means text — the union's own default. Only the per-glyph styles* read it, to decide whether there are glyphs to move. */kind?: string;+ /** A shape's kind; a doodle paints anew on every beat. */+ shape?: ShapeKind;rotation?: number;opacity?: number;anim?: OverlayAnim;@@ -105,10 +137,12 @@ export function hasOverlayKeys(o: Posable): boolean {}/** Whether the element is drawn through the per-frame evaluator: something- * moves it (a preset, pose keys, its group's camera, a keyframed mask). Any- * other element renders as one still picture, its own blur baked in. */+ * moves it (a preset, pose keys, its group's camera, a keyframed mask) or it+ * paints anew on every beat (a doodle). Any other element renders as one+ * still picture, its own blur baked in. */export function isOverlayAnimated(o: Posable): boolean {return (+ (o.kind === "shape" && !!o.shape && dealsMarks(o.shape)) ||hasOverlayAnim(o.anim) ||hasOverlayKeys(o) ||hasCameraKeys(o.camera) ||@@ -164,26 +198,48 @@ export function lerpKeys<K extends { t: number; ease?: EaseId }>(/** The pose at `tLocal` seconds into the element: interpolated between the* surrounding keys, held flat outside them, resting when there are no keys. */export function poseAt(o: Posable, tLocal: number): OverlayPose {- const keys = o.kf;- if (!keys || keys.length === 0) return restingPose(o);+ if (!hasOverlayKeys(o)) return restingPose(o);+ return poseOf(trackAt(o.kf!, tLocal, o.blur ?? 0), o.blur ?? 0);+}++/** The pose at `tLocal` in the keys' own terms (a uniform scale with its+ * per-axis stretch apart), the way an editor shows and writes it. */+export function keyPoseAt(o: Posable, tLocal: number): Omit<OverlayKey, "t" | "ease"> {+ if (!hasOverlayKeys(o)) return restingPose(o);+ const k: Partial<OverlayKey> = { ...trackAt(o.kf!, tLocal, o.blur ?? 0) };+ delete k.t;+ delete k.ease;+ return k as Omit<OverlayKey, "t" | "ease">;+}++/** The key track interpolated at `tLocal`. */+function trackAt(keys: OverlayKey[], tLocal: number, rest: number): OverlayKey {// A key with no blur of its own takes the element's.- const rest = o.blur ?? 0;- const k = lerpKeys(keys, tLocal, (a, b, p) => {+ return lerpKeys(keys, tLocal, (a, b, p) => {const mix = (u: number, v: number) => u + (v - u) * p;const blur = mix(a.blur ?? rest, b.blur ?? rest);+ // Axes and tilts a key leaves out sit at rest, so a squash or a tilt on+ // one key eases in from the key before it.+ const scaleX = mix(a.scaleX ?? 1, b.scaleX ?? 1);+ const scaleY = mix(a.scaleY ?? 1, b.scaleY ?? 1);+ const tiltX = mix(a.tiltX ?? 0, b.tiltX ?? 0);+ const tiltY = mix(a.tiltY ?? 0, b.tiltY ?? 0);return {t: mix(a.t, b.t),x: mix(a.x, b.x),y: mix(a.y, b.y),scale: mix(a.scale, b.scale),+ ...(scaleX !== 1 ? { scaleX } : {}),+ ...(scaleY !== 1 ? { scaleY } : {}),// Rotation takes the short way around, so a key at 350° into one at 10°// turns 20° forward instead of 340° back.rotation: a.rotation + shortestTurn(a.rotation, b.rotation) * p,+ ...(tiltX !== 0 ? { tiltX } : {}),+ ...(tiltY !== 0 ? { tiltY } : {}),opacity: mix(a.opacity, b.opacity),...(blur > 0 ? { blur } : {}),};});- return poseOf(k, rest);}/** The signed short-way-around turn from one angle to another, degrees. */@@ -199,8 +255,10 @@ function poseOf(k: OverlayKey, restBlur = 0): OverlayPose {return {x: k.x,y: k.y,- scale: k.scale,+ ...splitScale(k.scale * (k.scaleX ?? 1), k.scale * (k.scaleY ?? 1)),rotation: k.rotation,+ ...(k.tiltX ? { tiltX: k.tiltX } : {}),+ ...(k.tiltY ? { tiltY: k.tiltY } : {}),opacity: k.opacity,...(blur > 0 ? { blur } : {}),};@@ -210,7 +268,7 @@ function poseOf(k: OverlayKey, restBlur = 0): OverlayPose {* live pose is what makes the first two keys a no-op: the element sits where* it already sat until one of them is moved. */export function keyAt(o: Posable, t: number): OverlayKey {- return { t, ...poseAt(o, t) };+ return { t, ...keyPoseAt(o, t) };}/** The camera's pose at `tLocal` seconds into the element: eased between the@@ -260,8 +318,15 @@ export interface OverlayFrameState extends OverlayPose {dy: number;/** 0..1 share of the text shown (typewriter); absent when not typing. */textProgress?: number;+ /** 0..1 share of its value each number shows (count); absent when not+ * counting. */+ countProgress?: number;/** 0..1 share of the box uncovered from its left edge (wipe). */reveal?: number;+ /** 0..1 share of the ink eaten away (disintegrate). */+ erode?: number;+ /** 0..1 strength of the electric arcs over the element (zap hit). */+ zap?: number;/** Where a per-glyph ramp stands; the element's own transform stays neutral* and each character carries the motion. */glyphs?: GlyphPhase;@@ -325,20 +390,31 @@ function ownFrame(o: Posable, tLocal: number): OverlayFrameState {const dur = Math.max(0.1, o.end - o.start);const pose = poseAt(o, tLocal);const ev = evalOverlayAnim(o.anim, tLocal, dur, (o.kind ?? "text") === "text");+ // The pose's axes and the preset's multiply axis by axis, so a key squash+ // and a preset flip compose.+ const size = splitScale(+ pose.scale * (pose.sx ?? 1) * ev.scale * (ev.sx ?? 1),+ pose.scale * (pose.sy ?? 1) * ev.scale * (ev.sy ?? 1)+ );return {x: pose.x,y: pose.y,dx: ev.dx,dy: ev.dy,- scale: pose.scale * ev.scale,+ ...size,rotation: pose.rotation + ev.rotate,+ ...(pose.tiltX ? { tiltX: pose.tiltX } : {}),+ ...(pose.tiltY ? { tiltY: pose.tiltY } : {}),opacity: pose.opacity * ev.alpha,...(ev.textProgress !== undefined ? { textProgress: ev.textProgress } : {}),+ ...(ev.countProgress !== undefined ? { countProgress: ev.countProgress } : {}),...(ev.reveal !== undefined ? { reveal: ev.reveal } : {}),+ ...(ev.erode !== undefined ? { erode: ev.erode } : {}),+ ...(ev.zap ? { zap: ev.zap } : {}),...(ev.glyphs ? { glyphs: ev.glyphs } : {}),...(ev.glyphLoop ? { glyphLoop: ev.glyphLoop } : {}),...(ev.dive !== undefined ? { dive: ev.dive } : {}),- ...(pose.blur ? { blur: pose.blur } : {}),+ ...(pose.blur || ev.blur ? { blur: (pose.blur ?? 0) + (ev.blur ?? 0) } : {}),...(ev.caret !== undefined ? { caret: ev.caret } : {}),...(ev.brightness !== undefined ? { brightness: ev.brightness } : {}),};@@ -355,11 +431,13 @@ export function poseExtent(o: Posable): {scale: number;} {const poses = hasOverlayKeys(o) ? sortedKeys(o.kf!).map((k) => poseOf(k)) : [restingPose(o)];+ // A tilted edge swings toward the viewer and draws larger than its scale.+ const tilted = poses.some((p) => p.tiltX || p.tiltY);return {x0: Math.min(...poses.map((p) => p.x)),y0: Math.min(...poses.map((p) => p.y)),x1: Math.max(...poses.map((p) => p.x)),y1: Math.max(...poses.map((p) => p.y)),- scale: Math.max(...poses.map((p) => p.scale), 1),+ scale: Math.max(...poses.map((p) => p.scale), 1) * (tilted ? TILT_GROW : 1),};}diff --git a/site/packages/effects-kit/src/mask.ts b/site/packages/effects-kit/src/mask.tsindex ff3beaf4..3f45e11f 100644--- a/site/packages/effects-kit/src/mask.ts+++ b/site/packages/effects-kit/src/mask.ts@@ -56,7 +56,20 @@ export const MASK_KINDS: MaskKind[] = MASK_SHAPES.map((s) => s.id);export const MASK_SHAPE_KINDS: MaskKind[] = MASK_KINDS.filter((k) => k !== "subject");/** Feather ceiling, design px at the 1080 short side. */-export const MASK_FEATHER_MAX = 200;+export const MASK_FEATHER_MAX = 600;+/** A straight edge's feather, from coverage `a` to `b` along the gradient.+ * The ramp eases at both ends (a smoothstep), the falloff a blurred edge has,+ * so a wide feather reads as soft light. A linear ramp shows its two corners+ * as bands. */+function featherStops(g: CanvasGradient, a: number, b: number) {+ for (let i = 0; i <= FEATHER_STEPS; i++) {+ const u = i / FEATHER_STEPS;+ const v = a + (b - a) * u * u * (3 - 2 * u);+ g.addColorStop(u, `rgba(255,255,255,${v.toFixed(3)})`);+ }+}+const FEATHER_STEPS = 8;+/** Corner radius ceiling for rect and square masks, design px. */export const MASK_RADIUS_MAX = 400;@@ -406,8 +419,7 @@ export function paintMaskCoverage(// the feather, centered on the edge line.if (feather > 0) {const g = ctx.createLinearGradient(0, -feather / 2, 0, feather / 2);- g.addColorStop(0, "rgba(255,255,255,1)");- g.addColorStop(1, "rgba(255,255,255,0)");+ featherStops(g, 1, 0);ctx.fillStyle = g;ctx.fillRect(-big, -feather / 2, big * 2, feather);ctx.fillStyle = "#ffffff";@@ -421,13 +433,11 @@ export function paintMaskCoverage(const fe = Math.min(feather, h);if (fe > 0) {const top = ctx.createLinearGradient(0, -h / 2 - fe / 2, 0, -h / 2 + fe / 2);- top.addColorStop(0, "rgba(255,255,255,0)");- top.addColorStop(1, "rgba(255,255,255,1)");+ featherStops(top, 0, 1);ctx.fillStyle = top;ctx.fillRect(-big, -h / 2 - fe / 2, big * 2, fe);const bottom = ctx.createLinearGradient(0, h / 2 - fe / 2, 0, h / 2 + fe / 2);- bottom.addColorStop(0, "rgba(255,255,255,1)");- bottom.addColorStop(1, "rgba(255,255,255,0)");+ featherStops(bottom, 1, 0);ctx.fillStyle = bottom;ctx.fillRect(-big, h / 2 - fe / 2, big * 2, fe);ctx.fillStyle = "#ffffff";diff --git a/site/packages/effects-kit/src/motion/edges.json b/site/packages/effects-kit/src/motion/edges.jsonindex d8948d9a..ce718bfa 100644--- a/site/packages/effects-kit/src/motion/edges.json+++ b/site/packages/effects-kit/src/motion/edges.json@@ -1837,5 +1837,244 @@}]}+ },+ "count": {+ "label": "Count",+ "note": "Every number in a title counts up from zero to its written value, one step at a time: a loading percentage, a stat, a price. Decimals and leading zeros keep their written form; on Out the numbers count back down to zero.",+ "maxSeconds": 10,+ "slots": [+ "in",+ "out"+ ],+ "animate": {+ "counted": [+ {+ "t": 0,+ "v": 0,+ "ease": [+ 0.335,+ 0.335,+ 0.665,+ 0.665+ ]+ },+ {+ "t": 1,+ "v": 1+ }+ ]+ }+ },+ "disintegrate": {+ "label": "Disintegrate",+ "note": "The element crumbles away in grain: blotches of its ink are eaten until nothing is left, the gritty exit of a teaser title; on In it assembles out of the grain.",+ "slots": [+ "in",+ "out"+ ],+ "animate": {+ "erode": [+ {+ "t": 0,+ "v": 1,+ "ease": [+ 0.42,+ 0,+ 0.58,+ 1+ ]+ },+ {+ "t": 1,+ "v": 0+ }+ ]+ }+ },+ "blur": {+ "label": "Blur",+ "note": "Letters sharpen out of a soft blur one at a time, in a shuffled order, each drifting into its line: the dreamy title reveal. On Out they melt back into the blur.",+ "slots": [+ "in",+ "out"+ ],+ "selector": {+ "basedOn": "characters",+ "shape": "rampUp",+ "spread": 0.55,+ "randomize": true,+ "seed": 3+ },+ "wiggly": {+ "position": [+ 0,+ 70+ ],+ "seed": 5+ },+ "animate": {+ "opacity": [+ {+ "t": 0,+ "v": 0,+ "ease": [+ 0.25,+ 0.6,+ 0.4,+ 1+ ]+ },+ {+ "t": 0.6,+ "v": 1+ },+ {+ "t": 1,+ "v": 1+ }+ ],+ "blur": [+ {+ "t": 0,+ "v": 22,+ "ease": [+ 0.2,+ 0.7,+ 0.35,+ 1+ ]+ },+ {+ "t": 1,+ "v": 0+ }+ ]+ }+ },+ "glitch": {+ "label": "Glitch",+ "note": "Letters snap in one after another, torn into red and cyan copies that jitter sideways and lock together: the digital glitch title. On Out they tear apart and drop out.",+ "slots": [+ "in",+ "out"+ ],+ "selector": {+ "basedOn": "characters",+ "shape": "rampUp",+ "spread": 0.7+ },+ "animate": {+ "opacity": [+ {+ "t": 0,+ "v": 0,+ "hold": true+ },+ {+ "t": 0.04,+ "v": 1+ }+ ],+ "split": [+ {+ "t": 0,+ "v": 30,+ "ease": [+ 0.25,+ 0.6,+ 0.45,+ 1+ ]+ },+ {+ "t": 1,+ "v": 0+ }+ ],+ "position": [+ {+ "t": 0,+ "v": [+ -26,+ 0+ ],+ "hold": true+ },+ {+ "t": 0.18,+ "v": [+ 18,+ 0+ ],+ "hold": true+ },+ {+ "t": 0.36,+ "v": [+ -10,+ 0+ ],+ "hold": true+ },+ {+ "t": 0.54,+ "v": [+ 6,+ 0+ ],+ "hold": true+ },+ {+ "t": 0.72,+ "v": [+ 0,+ 0+ ]+ }+ ],+ "flicker": [+ {+ "t": 0,+ "v": 0.5+ },+ {+ "t": 0.6,+ "v": 0+ }+ ]+ }+ },+ "flicker": {+ "label": "Flicker",+ "note": "Letters blink on and off at random, each on its own beat, a few out at any moment, until the line locks on: the failing-neon title. On Out the line breaks into the same flicker within a few frames and keeps failing to the end.",+ "slots": [+ "in",+ "out"+ ],+ "selector": {+ "basedOn": "characters",+ "shape": "rampUp",+ "spread": 0+ },+ "animate": {+ "flicker": [+ {+ "t": 0,+ "v": 1+ },+ {+ "t": 0.12,+ "v": 0.4+ },+ {+ "t": 0.88,+ "v": 0.4+ },+ {+ "t": 1,+ "v": 0+ }+ ]+ }}}diff --git a/site/packages/effects-kit/src/motion/evaluate.ts b/site/packages/effects-kit/src/motion/evaluate.tsindex ad7e81b6..4f574cd0 100644--- a/site/packages/effects-kit/src/motion/evaluate.ts+++ b/site/packages/effects-kit/src/motion/evaluate.ts@@ -89,10 +89,16 @@ export function sampleProperties(props: MotionProperties, q: number, dur = 0): Mopacity: capped(props.opacity, dur),tracking: capped(props.tracking, dur),reveal: capped(props.reveal, dur),+ erode: capped(props.erode, dur),+ zap: capped(props.zap, dur),typed: capped(props.typed, dur),+ counted: capped(props.counted, dur),roll: capped(props.roll, dur),dive: capped(props.dive, dur),brightness: capped(props.brightness, dur),+ blur: capped(props.blur, dur),+ split: capped(props.split, dur),+ flicker: capped(props.flicker, dur),};return samplePropertiesRaw(props, q);}@@ -109,10 +115,16 @@ function samplePropertiesRaw(props: MotionProperties, q: number): MotionPose {alpha: sampleTrack(props.opacity, q, 1),tracking: sampleTrack(props.tracking, q, 0),...(props.reveal ? { reveal: sampleTrack(props.reveal, q, 1) } : {}),+ ...(props.erode ? { erode: sampleTrack(props.erode, q, 0) } : {}),+ ...(props.zap ? { zap: sampleTrack(props.zap, q, 0) } : {}),...(props.typed ? { typed: sampleTrack(props.typed, q, 1) } : {}),+ ...(props.counted ? { counted: sampleTrack(props.counted, q, 1) } : {}),...(props.roll ? { roll: sampleTrack(props.roll, q, 0) } : {}),...(props.dive ? { dive: sampleTrack(props.dive, q, 0) } : {}),...(props.brightness ? { brightness: sampleTrack(props.brightness, q, 1) } : {}),+ ...(props.blur ? { blur: sampleTrack(props.blur, q, 0) } : {}),+ ...(props.split ? { split: sampleTrack(props.split, q, 0) } : {}),+ ...(props.flicker ? { flicker: sampleTrack(props.flicker, q, 0) } : {}),};}@@ -124,6 +136,28 @@ function draw(seed: number, i: number, salt = 0): number {return ((h ^ (h >>> 16)) >>> 0) / 4294967296;}+/** How many times a flickering unit may change state across its window. A+ * 0.75s exit changes about every frame and a half, the pace of a failing+ * tube. */+const FLICKER_STEPS = 16;+/** How bright a flickering unit shows when it dips without going out. */+const FLICKER_DIM = 0.35;+/** Seeds every flicker pattern, so the same line always blinks the same way. */+const FLICKER_SEED = 29;++/** The pose with its flicker spent on its opacity. Each unit draws its own+ * pattern from its index, so the letters of a line blink independently: at+ * flicker 0.5 a letter is dark for about half the steps, and at 1 for all of+ * them. A dark step is out half the time and dim the rest. */+function flickered(pose: MotionPose, q: number, index: number): MotionPose {+ const chance = pose.flicker ?? 0;+ if (chance <= 0) return pose;+ const step = Math.min(FLICKER_STEPS - 1, Math.floor(clamp01(q) * FLICKER_STEPS));+ if (chance < 1 && draw(FLICKER_SEED, index, 100 + step) >= chance) return pose;+ const level = draw(FLICKER_SEED, index, 500 + step) < 0.5 ? 0 : FLICKER_DIM;+ return { ...pose, alpha: pose.alpha * level };+}+/** Where unit `i` sits in the hand-off order, 0 at the front. */function orderOf(sel: RangeSelector, i: number, n: number): number {if (n <= 1) return 0;@@ -149,10 +183,10 @@ function orderOf(sel: RangeSelector, i: number, n: number): number {/** The share of the window unit `i` starts at. `square` gives every unit its* own slot with no overlap; the ramps and curves overlap by `spread`. */-function startShare(sel: RangeSelector, i: number, n: number): number {+function startShare(sel: RangeSelector, i: number, n: number, share = sel.spread): number {if (n <= 1) return 0;const o = orderOf(sel, i, n);- const spread = clamp01(sel.spread);+ const spread = clamp01(share);if (sel.shape === "square") return o / n;const u = o / (n - 1);switch (sel.shape) {@@ -176,13 +210,15 @@ export function selectorProgress(p: number,index: number,count: number,- exiting = false+ exiting = false,+ /** The hand-off share over the selector's own `spread`. */+ spread = sel.spread): number {const n = Math.max(1, count);// An exit sweeps out the way it came in, so the order reverses.const i = exiting ? n - 1 - index : index;- const from = startShare(sel, i, n);- const window = sel.shape === "square" ? 1 / n : Math.max(1e-6, 1 - clamp01(sel.spread));+ const from = startShare(sel, i, n, spread);+ const window = sel.shape === "square" ? 1 / n : Math.max(1e-6, 1 - clamp01(spread));let q = clamp01((clamp01(p) - from) / window);if (sel.easeHigh) q = lerp(q, bezierEase([0.42, 0, 1, 1], q), clamp01(sel.easeHigh));if (sel.easeLow) q = lerp(q, bezierEase([0, 0, 0.58, 1], q), clamp01(sel.easeLow));@@ -240,15 +276,17 @@ export function evalPreset(index = 0,count = 1,exiting = false,- dur = 0+ dur = 0,+ /** An edge's own hand-off over its selector's (`OverlayEdge.stagger`). */+ spread?: number): MotionPose {const props = exiting && preset.animateOut ? preset.animateOut : preset.animate;const q = !preset.selector? clamp01(p): preset.period !== undefined? loopProgress(preset.selector, p, index, count)- : selectorProgress(preset.selector, p, index, count, exiting);- const pose = sampleProperties(props, q, dur);+ : selectorProgress(preset.selector, p, index, count, exiting, spread);+ const pose = flickered(sampleProperties(props, q, dur), q, index);return preset.wiggly ? applyWiggly(pose, preset.wiggly, index, 1 - q) : pose;}@@ -266,7 +304,7 @@ export function evalWhole(): MotionPose {const props = exiting && preset.animateOut ? preset.animateOut : preset.animate;const q = clamp01(p);- const pose = sampleProperties(props, q, dur);+ const pose = flickered(sampleProperties(props, q, dur), q, 0);// A wiggly preset keeps its stray here too — it is the whole motion of a// scatter, and dropping it would leave the element with only its fade.return preset.wiggly ? applyWiggly(pose, preset.wiggly, 0, 1 - q) : pose;@@ -287,7 +325,12 @@ export function presetExtent(preset: MotionPreset): { travel: number; rotates: bfor (const k of props.position ?? [])offset = Math.max(offset, Math.abs(k.v[0]), Math.abs(k.v[1]));for (const k of props.tracking ?? []) spread = Math.max(spread, Math.abs(k.v));- travel = Math.max(travel, offset + spread);+ // A split letter's copies and a blurred letter's halo reach past it too:+ // a defocus spreads about twice its radius.+ let halo = 0;+ for (const k of props.split ?? []) halo = Math.max(halo, Math.abs(k.v));+ for (const k of props.blur ?? []) halo = Math.max(halo, Math.abs(k.v) * 2);+ travel = Math.max(travel, offset + spread + halo);if ((props.rotation ?? []).some((k) => k.v !== 0)) rotates = true;}if (preset.wiggly?.position)diff --git a/site/packages/effects-kit/src/motion/hits.json b/site/packages/effects-kit/src/motion/hits.jsonindex 77ed5e05..ad84e64e 100644--- a/site/packages/effects-kit/src/motion/hits.json+++ b/site/packages/effects-kit/src/motion/hits.json@@ -68,5 +68,44 @@}]}+ },+ "zap": {+ "label": "Zap",+ "note": "electric arcs crackle over the element: blue-white bolts with a flare where each lands, re-dealt a dozen times a second, sparking up fast and dying out at the end \u2014 the charged beat of a teaser title",+ "slots": [+ "hit"+ ],+ "animate": {+ "zap": [+ {+ "t": 0,+ "v": 0,+ "ease": [+ 0.2,+ 0,+ 0.4,+ 1+ ]+ },+ {+ "t": 0.1,+ "v": 1+ },+ {+ "t": 0.75,+ "v": 1,+ "ease": [+ 0.4,+ 0,+ 0.6,+ 1+ ]+ },+ {+ "t": 1,+ "v": 0+ }+ ]+ }}}diff --git a/site/packages/effects-kit/src/motion/types.ts b/site/packages/effects-kit/src/motion/types.tsindex a313b3f7..0574b5a3 100644--- a/site/packages/effects-kit/src/motion/types.ts+++ b/site/packages/effects-kit/src/motion/types.ts@@ -57,8 +57,15 @@ export interface MotionProperties {tracking?: PropertyKey<number>[];/** Share of the box uncovered from its leading edge, 0..1 (wipes). */reveal?: PropertyKey<number>[];+ /** Share of the ink eaten away by grain, 0..1 (disintegrate). */+ erode?: PropertyKey<number>[];+ /** Strength of the electric arcs crawling over the element, 0..1 (zap). */+ zap?: PropertyKey<number>[];/** Share of the characters drawn, 0..1 (typewriter). */typed?: PropertyKey<number>[];+ /** Share of its written value every number in the text shows, 0..1+ * (count). */+ counted?: PropertyKey<number>[];/** Characters a unit's reel still has to roll past before it lands, 0 at* rest (slot). */roll?: PropertyKey<number>[];@@ -68,6 +75,14 @@ export interface MotionProperties {/** Multiplies the element's colors, 1 = as drawn; below 1 it darkens* (a pressed button). */brightness?: PropertyKey<number>[];+ /** Defocus, design px: the unit sharpens out of a blur as it falls to 0. */+ blur?: PropertyKey<number>[];+ /** How far a red and a cyan copy of the unit pull apart either side of it,+ * design px (a glitch's channel split). */+ split?: PropertyKey<number>[];+ /** The chance, 0..1, that the unit is dark at a given moment. Each unit+ * blinks on its own seeded pattern, so a line flickers letter by letter. */+ flicker?: PropertyKey<number>[];}/** What the selector counts as one unit. */@@ -120,6 +135,9 @@ export interface MotionPreset {slots: MotionSlot[];/** Loops only: one cycle in seconds at speed 1. */period?: number;+ /** Edges only: the longest the ramp may run, seconds, over the shared cap+ * (a count can run for as long as a loading bar does). */+ maxSeconds?: number;selector?: RangeSelector;wiggly?: WigglySelector;animate: MotionProperties;@@ -151,10 +169,16 @@ export interface MotionPose {alpha: number;tracking: number;reveal?: number;+ erode?: number;+ zap?: number;typed?: number;+ counted?: number;roll?: number;dive?: number;brightness?: number;+ blur?: number;+ split?: number;+ flicker?: number;}export const REST_POSE: MotionPose = {diff --git a/site/packages/effects-kit/src/pattern.test.ts b/site/packages/effects-kit/src/pattern.test.tsnew file mode 100644index 00000000..5031b967--- /dev/null+++ b/site/packages/effects-kit/src/pattern.test.ts@@ -0,0 +1,122 @@+import { describe, expect, test } from "bun:test";+import { Canvas } from "skia-canvas";+import { paintPattern, stripeGeometry, stripesSolid } from "./pattern";+import { measureElementBounds, renderElementCanvas, type RenderEnv } from "./render";+import type { ShapeOverlay } from "./types";++const env: RenderEnv = {+ fontStack: () => "Arial",+ createCanvas: (w, h) => new Canvas(w, h) as unknown as HTMLCanvasElement,+ canvasToPngBlob: async (c) => new Blob([new Uint8Array(await (c as unknown as Canvas).toBuffer("png"))]),+};++const pixel = (c: HTMLCanvasElement | Canvas, x: number, y: number) =>+ (c.getContext("2d") as unknown as CanvasRenderingContext2D).getImageData(x, y, 1, 1).data;++// Lit columns along row y, as a string of 1s and 0s.+const litRow = (c: Canvas, y: number) =>+ Array.from({ length: c.width }, (_, x) => (pixel(c, x, y)[3] > 127 ? "1" : "0")).join("");++const bar = (extra: Partial<ShapeOverlay>): ShapeOverlay => ({+ id: "s",+ kind: "shape",+ shape: "rect",+ start: 0,+ end: 4,+ x: 0.5,+ y: 0.5,+ w: 0.4,+ h: 0.4,+ fill: "#ffffff",+ ...extra,+});++describe("stripes", () => {+ test("lay 2 px lines with 1 px gaps from the box's left edge", () => {+ const c = new Canvas(30, 4);+ const ctx = c.getContext("2d") as unknown as CanvasRenderingContext2D;+ paintPattern(ctx, { kind: "stripes", width: 2, gap: 1 }, 30, 4, "#ffffff", 1);+ expect(litRow(c, 2)).toBe("110".repeat(10));+ });++ test("scale with the frame: a half-size frame halves line and gap", () => {+ const g = stripeGeometry({ kind: "stripes", width: 4, gap: 2 }, 0.5);+ expect(g).toEqual({ line: 2, period: 3, angle: 0 });+ });++ test("turn about the box center: 90 degrees lays them across", () => {+ const c = new Canvas(12, 12);+ const ctx = c.getContext("2d") as unknown as CanvasRenderingContext2D;+ paintPattern(ctx, { kind: "stripes", width: 2, gap: 1, angle: 90 }, 12, 12, "#ffffff", 1);+ const column = Array.from({ length: 12 }, (_, y) => (pixel(c, 6, y)[3] > 127 ? "1" : "0")).join("");+ expect(column).toBe("110".repeat(4));+ expect(litRow(c, 0)).toBe("1".repeat(12));+ });++ test("one line every period across a loading bar 0.79 of a 1080 frame", () => {+ // 2 px lines + 1 px gaps over 853 px: one line every 3 px.+ const c = new Canvas(1080, 4);+ const ctx = c.getContext("2d") as unknown as CanvasRenderingContext2D;+ paintPattern(ctx, { kind: "stripes", width: 2, gap: 1 }, Math.round(0.79 * 1080), 4, "#ffffff", 1);+ const lines = litRow(c, 2).match(/1+/g) ?? [];+ expect(lines.length).toBe(Math.ceil(853 / 3));+ });++ test("fill solid with no gap", () => {+ expect(stripesSolid(stripeGeometry({ kind: "stripes", width: 2, gap: 0 }, 1))).toBe(true);+ expect(stripesSolid(stripeGeometry({ kind: "stripes", width: 2, gap: 1 }, 1))).toBe(false);+ });+});++describe("a shape's pattern fill", () => {+ test("clips the stripes to the outline", async () => {+ // 216 px frame, scale 0.2: 10/5 design px land as 2 px lines, 1 px gaps+ // over the box 54..162.+ const o = bar({ w: 0.5, h: 0.5, pattern: { kind: "stripes", width: 10, gap: 5 } });+ const c = await renderElementCanvas(o, 216, 216, env);+ expect(pixel(c, 54, 108)[3]).toBe(255);+ expect(pixel(c, 55, 108)[3]).toBe(255);+ expect(pixel(c, 56, 108)[3]).toBe(0);+ expect(pixel(c, 57, 108)[3]).toBe(255);+ expect(pixel(c, 50, 108)[3]).toBe(0);+ expect(pixel(c, 108, 50)[3]).toBe(0);+ });+});++describe("a shape's shadow", () => {+ const glow = { color: "#00ffff", blur: 20, offsetY: 0, opacity: 1 };++ test("glows past the outline in its color and leaves the body as painted", async () => {+ const c = await renderElementCanvas(bar({ shadow: glow }), 200, 200, env);+ const spill = pixel(c, 55, 100);+ expect(spill[3]).toBeGreaterThan(0);+ expect(spill[0]).toBeLessThan(spill[2]);+ expect(Array.from(pixel(c, 100, 100))).toEqual([255, 255, 255, 255]);+ expect(pixel(c, 2, 2)[3]).toBe(0);+ });++ test("follows a turned shape", async () => {+ const c = await renderElementCanvas(bar({ shadow: glow, rotation: 45 }), 200, 200, env);+ // The turned square's top corner sits at y ≈ 43; just above it is glow.+ expect(pixel(c, 100, 40)[3]).toBeGreaterThan(0);+ expect(pixel(c, 100, 100)[3]).toBe(255);+ expect(pixel(c, 2, 2)[3]).toBe(0);+ });++ test("glows around a striped fill and through its gaps", async () => {+ const o = bar({ w: 0.5, h: 0.5, shadow: glow, pattern: { kind: "stripes", width: 10, gap: 5 } });+ const c = await renderElementCanvas(o, 216, 216, env);+ expect(pixel(c, 50, 108)[3]).toBeGreaterThan(0);+ expect(pixel(c, 56, 108)[3]).toBeGreaterThan(0);+ });++ test("pads the crop by its blur and offset", async () => {+ const frame = { width: 1080, height: 1080, scale: 1 };+ const plain = await measureElementBounds(bar({}), frame, env);+ const lit = await measureElementBounds(bar({ shadow: { blur: 20, offsetY: 4 } }), frame, env);+ expect(lit.w - plain.w).toBe(48);+ expect(lit.h - plain.h).toBe(48);+ const tight = await measureElementBounds(bar({ shadow: { blur: 20, offsetY: 4 } }), frame, env, { pad: false });+ expect(tight.w).toBe(plain.w);+ });+});diff --git a/site/packages/effects-kit/src/pattern.ts b/site/packages/effects-kit/src/pattern.tsnew file mode 100644index 00000000..a397336a--- /dev/null+++ b/site/packages/effects-kit/src/pattern.ts@@ -0,0 +1,120 @@+/**+ * Pattern fills: a fill color laid down as a repeating figure.+ *+ * A pattern is measured in design px (1080 short side) and anchored at the+ * top-left of the box it fills, turning about the box center. The canvas+ * painter here and the preview's SVG <pattern> read the same geometry, so the+ * lines land on the same pixels in the preview and every export.+ */++/** The figures a fill can repeat. */+export const PATTERN_KINDS = ["stripes"] as const;+export type PatternKind = (typeof PATTERN_KINDS)[number];++/** Parallel lines in the fill color with clear gaps between them. */+export interface StripesPattern {+ kind: "stripes";+ /** Line direction in degrees, clockwise; absent or 0 = vertical lines. */+ angle?: number;+ /** Line thickness, px at 1080. */+ width: number;+ /** Clear space between lines, px at 1080. */+ gap: number;+}++/** A pattern fill. Each kind is one member, keyed by `kind`. */+export type PatternSpec = StripesPattern;++/** Slider and schema bounds for stripe line and gap, px at 1080. */+export const STRIPE_LINE_MIN = 0.5;+export const STRIPE_LINE_MAX = 40;+export const STRIPE_GAP_MAX = 40;++/** The default stripes: 2 px lines with 1 px gaps, a loading-bar fill. */+export const STRIPES_DEFAULT: StripesPattern = { kind: "stripes", width: 2, gap: 1 };++/** Stripes in output px: line thickness, line-to-line period and angle. */+export interface StripeGeometry {+ line: number;+ period: number;+ angle: number;+}++/** Resolve stripes to output px at `scale` (output px per design px). */+export function stripeGeometry(spec: StripesPattern, scale: number): StripeGeometry {+ const line = Math.max(STRIPE_LINE_MIN, spec.width) * scale;+ const gap = Math.max(0, spec.gap) * scale;+ return { line, period: line + gap, angle: spec.angle ?? 0 };+}++/** Below this period (output px) the lines blur into one coat. */+const STRIPE_PERIOD_MIN = 0.25;++/** Stripes that draw as a flat coat: no gap, or lines finer than a pixel,+ * where a loop of thousands of rects buys a picture no one can tell apart. */+export function stripesSolid(g: StripeGeometry): boolean {+ return g.period < STRIPE_PERIOD_MIN || g.line >= g.period;+}++/**+ * Paint a pattern over the box [0, 0, w, h] of the current transform in+ * `color`. Figures run a little past the box edge, so the caller clips to the+ * outline it fills (a shape's path, a glyph run's ink).+ */+export function paintPattern(+ ctx: CanvasRenderingContext2D,+ spec: PatternSpec,+ w: number,+ h: number,+ color: string,+ scale: number+): void {+ paintStripes(ctx, spec, w, h, color, scale);+}++function paintStripes(+ ctx: CanvasRenderingContext2D,+ spec: StripesPattern,+ w: number,+ h: number,+ color: string,+ scale: number+): void {+ const g = stripeGeometry(spec, scale);+ ctx.save();+ ctx.fillStyle = color;++ // A flat coat needs one rect.+ if (stripesSolid(g)) {+ ctx.fillRect(0, 0, w, h);+ ctx.restore();+ return;+ }++ // Turn about the box center; a turned box needs lines across its whole+ // diagonal to stay covered.+ let x0 = 0;+ let x1 = w;+ let y0 = 0;+ let y1 = h;+ if (g.angle) {+ const r = Math.hypot(w, h) / 2;+ ctx.translate(w / 2, h / 2);+ ctx.rotate((g.angle * Math.PI) / 180);+ ctx.translate(-w / 2, -h / 2);+ x0 = w / 2 - r;+ x1 = w / 2 + r;+ y0 = h / 2 - r;+ y1 = h / 2 + r;+ }++ // One line per period from the box origin, one fill for the lot.+ // Example: 2 px lines, 1 px gaps → lines at 0, 3, 6, … each 2 px wide.+ ctx.beginPath();+ const kEnd = Math.ceil(x1 / g.period);+ for (let k = Math.floor(x0 / g.period); k < kEnd; k++) {+ ctx.rect(k * g.period, y0, g.line, y1 - y0);+ }+ ctx.fill();+ ctx.restore();+}diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.tsindex 05bf718d..c2a5b256 100644--- a/site/packages/effects-kit/src/render.ts+++ b/site/packages/effects-kit/src/render.ts@@ -13,10 +13,12 @@ import {caretOn,caretToggles,edgeMotion,+ evalOverlayAnim,glyphStateAt,hasGlyphMotion,hitScale,hitWindow,+ hitZaps,loopExtent,loopPeriod,moveExtent,@@ -26,22 +28,30 @@ import {type OverlayAnim,} from "./anim";import { diveView, slotReel, slotSeed, type DiveFocus, type DiveView } from "./dive";-import { presetExtent } from "./motion/evaluate";+import { cutAtFaces, cycleFaceTag, fontCycleCuts, fontCycleVariants, withFontCycle } from "./fontCycle";+import { evalWhole, presetExtent } from "./motion/evaluate";+import { countStep, countText } from "./count";import { evalOverlayFrame, hasOverlayKeys, poseAt, poseExtent, sortedKeys, type OverlayFrameState } from "./keys";import { ELEMENT_BLUR_MAX, hasCameraKeys, STREAK_MAX } from "./camera";import { ElementFx, elementLook } from "./elementFx";import { applyMaskToCanvas, isMaskAnimated } from "./mask";import { tracePolyShape } from "./shapePath";+import { paintPattern } from "./pattern";import type { LottieHandle } from "./lottie";import { elementPlugin } from "./registry";import { kitCanvas } from "./surface";+import { cutTexture, erodeTile } from "./texture";+import { isInkless, SPLIT_CYAN, SPLIT_RED } from "./glyphLook";+import { paintZap, zapSeed, type ZapPhase } from "./zap";+import { DOODLE_FPS, doodleBeat, doodleSeed, paintDoodle } from "./doodle";import {+ dealsMarks,lineLikeShape,type Overlay,type ShapeOverlay,type StickerOverlay,type TextOverlay,- type TextShadowSpec,+ type ShadowSpec,} from "./types";import {applyWordDraw,@@ -83,10 +93,10 @@ export function plateFill(o: { plateColor?: string; plateOpacity?: number }): st/** Resolve a text element's shadow to concrete design-px values, or null when* it is off. `true` and `{}` both mean the legacy default look. */export function resolveShadow(- shadow: boolean | TextShadowSpec+ shadow: boolean | ShadowSpec): { color: string; blur: number; offsetY: number } | null {if (shadow === false) return null;- const spec: TextShadowSpec = shadow === true ? {} : shadow;+ const spec: ShadowSpec = shadow === true ? {} : shadow;return {color: rgba(spec.color ?? "#000000", spec.opacity ?? 0.65),blur: spec.blur ?? SHADOW.blur,@@ -146,15 +156,25 @@ export interface PaintFrame {* glyph motion, and the pose and view a dive paints inside the frame. */export interface PaintPhase {reveal?: number;+ /** Share of the ink eaten away (see `erodeTile`). */+ erode?: number;+ /** Electric arcs over the element (see `paintZap`). */+ zap?: ZapPhase;+ /** The beat a doodle deals its mark on (see `doodleBeat`). */+ doodle?: number;glyphs?: GlyphPhase;glyphLoop?: GlyphLoopPhase;/** The view a dive draws the whole picture under (see `diveView`). */dive?: DiveView;/** The typing bar is lit in this picture (see `caretOn`). */caret?: boolean;+ /** How many characters a typewriter has typed. They stand where the whole+ * text puts them and the rest hold their room, so letters never slide as+ * the line grows. A line break takes a turn, as the glyph index counts. */+ typed?: number;/** A dive's pose is painted before frame clipping, so a half-size title* still fills the output frame when its flight lands. */- pose?: Pick<OverlayFrameState, "x" | "y" | "dx" | "dy" | "scale" | "rotation">;+ pose?: Pick<OverlayFrameState, "x" | "y" | "dx" | "dy" | "scale" | "sx" | "sy" | "rotation">;}/**@@ -183,21 +203,107 @@ export function shapeMetrics(o: ShapeOverlay, frame: PaintFrame) {};}+type ShapeMetrics = ReturnType<typeof shapeMetrics>;++/** How a shape body draws: as painted, or as the solid silhouette that casts+ * its shadow (a pattern fill's gaps still cast). */+type ShapeCoat = "painted" | "silhouette";+function paintShape(ctx: CanvasRenderingContext2D, o: ShapeOverlay, frame: PaintFrame) {const m = shapeMetrics(o, frame);+ const shadow = resolveShadow(o.shadow ?? false);+ const beat = frame.phase?.doodle ?? 0;+ if (shadow) {+ castShapeShadow(ctx, o, m, shadow, frame.scale, beat);+ }+ drawShapeBody(ctx, o, m, frame.scale, "painted", beat);+}++/**+ * Cast a shape's shadow with no body: the silhouette draws far off to the+ * left of the surface and its shadow is thrown back by the same distance, so+ * only the shadow lands. Nothing is clipped while it draws, so a pattern fill+ * keeps its lines inside the outline and its glow spreads past it.+ */+function castShapeShadow(+ ctx: CanvasRenderingContext2D,+ o: ShapeOverlay,+ m: ShapeMetrics,+ shadow: NonNullable<ReturnType<typeof resolveShadow>>,+ scale: number,+ beat: number+): void {+ const t = ctx.getTransform();+ const det = t.a * t.d - t.b * t.c;+ if (!det) {+ return;+ }++ // Far enough on screen that the moved body clears the surface: past the+ // body's own screen x by its whole reach.+ const reach = Math.hypot(m.w, Math.max(m.h, m.headHalf * 2)) + 2 * (m.strokeWidth + m.thickness);+ const zoom = Math.max(Math.hypot(t.a, t.b), Math.hypot(t.c, t.d));+ const away = Math.ceil(Math.abs(t.a * m.cx + t.c * m.cy + t.e) + reach * zoom + 1);+ ctx.save();+ ctx.shadowColor = shadow.color;+ ctx.shadowBlur = shadow.blur * scale;+ ctx.shadowOffsetX = away;+ ctx.shadowOffsetY = shadow.offsetY * scale;++ // Shadow offsets are screen px; the move is the same screen vector+ // (-away, 0) carried back through the current transform.+ ctx.translate((-away * t.d) / det, (away * t.b) / det);+ drawShapeBody(ctx, o, m, scale, "silhouette", beat);+ ctx.restore();+}++/** Trace a filled shape's outline as the current path. */+function traceShapeOutline(ctx: CanvasRenderingContext2D, o: ShapeOverlay, m: ShapeMetrics): void {+ ctx.beginPath();+ if (o.shape === "rect") {+ ctx.roundRect(m.cx - m.w / 2, m.cy - m.h / 2, m.w, m.h, m.radius);+ } else if (o.shape === "ellipse") {+ ctx.ellipse(m.cx, m.cy, m.w / 2, m.h / 2, 0, 0, Math.PI * 2);+ } else {+ tracePolyShape(ctx, o.shape, m.w, m.h, m.cx - m.w / 2, m.cy - m.h / 2);+ }+}++function drawShapeBody(+ ctx: CanvasRenderingContext2D,+ o: ShapeOverlay,+ m: ShapeMetrics,+ scale: number,+ coat: ShapeCoat,+ beat: number+): void {const alpha = ctx.globalAlpha;++ // A doodle paints the mark dealt on this beat inside its box, in the fill+ // or one of its inks.+ if (dealsMarks(o.shape)) {+ ctx.globalAlpha = alpha * (o.fillOpacity ?? 1);+ paintDoodle(ctx, { cx: m.cx, cy: m.cy, w: m.w, h: m.h, scale }, [o.fill, ...(o.inks ?? [])], beat, doodleSeed(o.id));+ ctx.globalAlpha = alpha;+ return;+ }ctx.fillStyle = o.fill;if (!lineLikeShape(o.shape)) {- ctx.beginPath();- if (o.shape === "rect") {- ctx.roundRect(m.cx - m.w / 2, m.cy - m.h / 2, m.w, m.h, m.radius);- } else if (o.shape === "ellipse") {- ctx.ellipse(m.cx, m.cy, m.w / 2, m.h / 2, 0, 0, Math.PI * 2);+ traceShapeOutline(ctx, o, m);+ ctx.globalAlpha = alpha * (o.fillOpacity ?? 1);++ // A pattern paints over the box clipped to the outline, anchored at the+ // box's top-left; the outline is traced again for the stroke.+ if (o.pattern && coat === "painted") {+ ctx.save();+ ctx.clip();+ ctx.translate(m.cx - m.w / 2, m.cy - m.h / 2);+ paintPattern(ctx, o.pattern, m.w, m.h, o.fill, scale);+ ctx.restore();+ traceShapeOutline(ctx, o, m);} else {- tracePolyShape(ctx, o.shape, m.w, m.h, m.cx - m.w / 2, m.cy - m.h / 2);+ ctx.fill();}- ctx.globalAlpha = alpha * (o.fillOpacity ?? 1);- ctx.fill();ctx.globalAlpha = alpha;if (o.stroke && m.strokeWidth > 0) {ctx.strokeStyle = o.stroke.color;@@ -488,16 +594,32 @@ async function paintText(const widthsOf = runs ? runs.map((r) => r.lineW) : lines.map((l) => ctx.measureText(l).width);const maxW = overlay.wrapWidth ? textWrapRoom(overlay, width) : Math.max(...widthsOf, 1);- if (overlay.plate) {+ // A textured title cuts its grain out of the type, so its plate goes in+ // behind the finished letters instead of under them.+ const texture = overlay.texture;+ const surface = { owner: env, make: (w: number, h: number) => newCanvas(env, w, h) };+ const paintPlate = () => {+ if (!overlay.plate) {+ return;+ }const padX = PLATE_PAD_X * fpx;const padY = PLATE_PAD_Y * fpx;const r = (overlay.plateRadius ?? PLATE_RADIUS) * fpx;const w = maxW + padX * 2;const h = totalH + padY * 2;+ ctx.save();+ if (texture) {+ ctx.globalCompositeOperation = "destination-over";+ ctx.shadowColor = "transparent";+ }ctx.fillStyle = plateFill(overlay);ctx.beginPath();ctx.roundRect(cx - w / 2, cy - h / 2, w, h, r);ctx.fill();+ ctx.restore();+ };+ if (!texture) {+ paintPlate();}const shadow = resolveShadow(overlay.shadow);@@ -522,6 +644,20 @@ async function paintText(ctx.fillStyle = fill;ctx.fillText(text, x, y);};+ // A glitching letter's channel copies: the same outline and fill in one+ // tint, so a hollow title tears into hollow copies.+ const drawTinted = (text: string, x: number, y: number, tint: string) => {+ if (overlay.stroke && strokeW > 0) {+ ctx.lineJoin = "round";+ ctx.lineWidth = (strokeW / fpx) * drawSize * 2;+ ctx.strokeStyle = tint;+ ctx.strokeText(text, x, y);+ }+ if (!isInkless(overlay.color)) {+ ctx.fillStyle = tint;+ ctx.fillText(text, x, y);+ }+ };// Line alignment inside the block; the block stays centered on (x, y).const align = overlay.align ?? "center";@@ -533,7 +669,23 @@ async function paintText(: cx + maxW / 2 - widthsOf[i] / 2;const phase = frame.phase;- // The typing bar: just past the last character drawn, on the last line, as++ // Typing draws the typed characters where the whole text lays them out.+ // Each line's first character index counts a break as a turn, so a typed+ // count reads the same here as in the glyph loop. "AB\nCD" typed 4 shows+ // "AB" and "C".+ const typed = phase?.typed;+ const lineStart: number[] = [];+ lines.reduce((at, l) => {+ lineStart.push(at);+ return at + [...l].length + 1;+ }, 0);+ const shownIn = (i: number) => (typed === undefined ? Infinity : Math.max(0, typed - lineStart[i]));+ // The line the typing stands on: the last one it has reached.+ const typingLine =+ typed === undefined ? lines.length - 1 : Math.max(0, lineStart.filter((s) => s <= typed).length - 1);++ // The typing bar: just past the last character drawn, on its line, as// tall as the type, centered where the DOM's vertical-align: middle puts// it — half an x-height over the baseline. No shadow, so it reads the same// as the preview's bar.@@ -544,9 +696,9 @@ async function paintText(ctx.textBaseline = baseline;return ctx.measureText("Hg").actualBoundingBoxAscent;};- const drawCaret = (right: number) => {+ const drawCaret = (right: number, line = lines.length - 1) => {if (!caretColor) return;- const y = cy - totalH / 2 + lineH * (lines.length - 0.5);+ const y = cy - totalH / 2 + lineH * (line + 0.5);ctx.save();ctx.font = cssFont;const base = ascentAt("alphabetic");@@ -561,14 +713,15 @@ async function paintText(ctx.fillRect(right + CARET_GAP_EM * fpx, y + off - h / 2, Math.max(2 * scale, CARET_WIDTH_EM * fpx), h);ctx.restore();};- if (phase && hasGlyphMotion(phase)) {+ if ((phase && hasGlyphMotion(phase)) || (texture && !draws)) {// A per-glyph ramp or loop draws character by character, each at its own// place in the motion. The index counts through the whole element, line- // breaks included, which is exactly what the DOM preview counts.+ // breaks included, which is exactly what the DOM preview counts. A+ // texture draws the same way, so each letter wears its own grain.const total = Math.max(1, [...overlay.text].length);const alpha0 = ctx.globalAlpha;let gi = 0;- let lastRight = cx;+ let caretRight = cx;lines.forEach((line, i) => {const y = cy - totalH / 2 + lineH * (i + 0.5);// Characters drawn one by one lose the kerning between them, so the run@@ -577,17 +730,34 @@ async function paintText(const chars = [...line];const widths = chars.map((ch) => ctx.measureText(ch).width);let x = lineX(i) - widths.reduce((a, b) => a + b, 0) / 2;- lastRight = lineX(i) + widths.reduce((a, b) => a + b, 0) / 2;+ // The bar waits at the head of the line the typing has reached.+ if (i === typingLine) {+ caretRight = x;+ }chars.forEach((ch, ci) => {const w = widths[ci];- const g = glyphStateAt(phase, gi, total);+ const shown = typed === undefined || gi < typed;+ const g = glyphStateAt(phase ?? {}, gi, total);gi++;- if (ch !== " " && g.alpha > 0.001) {+ if (shown && i === typingLine) {+ caretRight = x + w;+ }+ if (shown && ch !== " " && g.alpha > 0.001) {ctx.save();ctx.globalAlpha = alpha0 * Math.min(1, Math.max(0, g.alpha));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);+ if (g.blur && "filter" in ctx) {+ // A letter's own defocus stacks on whatever the element wears.+ const own = ctx.filter && ctx.filter !== "none" ? `${ctx.filter} ` : "";+ ctx.filter = `${own}blur(${(g.blur * scale).toFixed(2)}px)`;+ }+ if (g.split) {+ const off = g.split * scale;+ drawTinted(ch, -off, 0, SPLIT_RED);+ drawTinted(ch, off, 0, SPLIT_CYAN);+ }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.@@ -602,13 +772,17 @@ async function paintText(} else {drawText(ch, 0, 0);}+ if (texture) {+ cutTexture(ctx, texture, surface, -w / 2, -lineH / 2, w, lineH);+ }ctx.restore();}x += w;});gi++; // the line break takes a turn too});- drawCaret(lastRight);+ paintPlate();+ drawCaret(caretRight, typingLine);return;}@@ -617,8 +791,31 @@ async function paintText(// words sit in the layout measured above, so one drawn bigger has already// pushed its neighbours aside and they close back up as it shrinks.if (!runs || !draws) {- lines.forEach((line, i) => drawText(line, lineX(i), cy - totalH / 2 + lineH * (i + 0.5)));- drawCaret(lineX(lines.length - 1) + widthsOf[lines.length - 1] / 2);+ let caretRight = lineX(typingLine) + widthsOf[typingLine] / 2;+ lines.forEach((line, i) => {+ const y = cy - totalH / 2 + lineH * (i + 0.5);+ const chars = [...line];+ const n = shownIn(i);+ if (n >= chars.length) {+ drawText(line, lineX(i), y);+ return;+ }++ // A line typing in draws its typed head from where the whole line+ // starts, so each letter lands where it will stay.+ const left = lineX(i) - widthsOf[i] / 2;+ const head = chars.slice(0, n).join("");+ if (i === typingLine) {+ caretRight = left + ctx.measureText(head).width;+ }+ if (!head) {+ return;+ }+ ctx.textAlign = "left";+ drawText(head, left, y);+ ctx.textAlign = "center";+ });+ drawCaret(caretRight, typingLine);return;}// A word drawn at another size shares the baseline with the words beside it,@@ -654,26 +851,44 @@ async function paintText(ctx.textBaseline = "middle";const alpha0 = ctx.globalAlpha;let k = 0;+ let caretRight = cx - (runs[typingLine]?.lineW ?? 0) / 2;ctx.textAlign = "left";runs.forEach((run, i) => {const y = cy - totalH / 2 + lineH * (i + 0.5);let x = cx - run.lineW / 2;+ // Where each word starts in its line, in characters, for the typing count.+ let from = 0;+ const wordAt = run.words.map((w) => {+ const s = lines[i].indexOf(w, from);+ from = s + w.length;+ return [...lines[i].slice(0, s)].length;+ });run.words.forEach((w, wi) => {const d = draws[k] ?? { ...REST_WORD, color: overlay.color };const mult = sizeKey(d.scale);const width = run.widths[wi];const alpha = alpha0 * Math.min(1, Math.max(0, d.opacity));++ // Typing shows a word's typed head from the word's own left edge.+ const letters = [...w];+ const typedIn = Math.min(letters.length, shownIn(i) - wordAt[wi]);+ const shown = typedIn >= letters.length ? w : letters.slice(0, Math.max(0, typedIn)).join("");+ // Centered in the room the layout left it.+ const drawX = x + (width - run.drawn[wi]) / 2;+ if (i === typingLine && shown) {+ setSize(mult, d);+ caretRight = drawX + (shown === w ? run.drawn[wi] : ctx.measureText(shown).width);+ setSize(1);+ }// A word its effect has not brought in yet has no pixels; it still holds// its place, so the words beside it stay where they will be.- if (alpha > 0.001) {+ if (alpha > 0.001 && shown) {// Every word draws from its own middle; a resized one has that middle// lifted or dropped by exactly as much as sharing the line's baseline// asks for.const size = fpx * mult;const mid = y + drop - dropOf(d) * mult;const drawY = mid;- // Centered in the room the layout left it.- const drawX = x + (width - run.drawn[wi]) / 2;// Everything below rides one saved state, so nothing set for this word// reaches the next.ctx.save();@@ -686,6 +901,7 @@ async function paintText(ctx.translate(-wx, -mid);}setSize(mult, d);+ const inkW = shown === w ? run.drawn[wi] : ctx.measureText(shown).width;const boxed = d.mark === "box" && !!d.markAlpha;if (boxed) {// Accent box behind the word, and the word on top of it — both drawn@@ -698,18 +914,22 @@ async function paintText(ctx.roundRect(drawX - pad,mid - size * 0.5 - pad,- run.drawn[wi] + pad * 2,+ inkW + pad * 2,size + pad * 2,0.18 * size);ctx.fill();ctx.globalAlpha = alpha;}- drawText(w, drawX, drawY, d.color);+ drawText(shown, drawX, drawY, d.color);+ // A textured line in a word effect wears its grain word by word.+ if (texture) {+ cutTexture(ctx, texture, surface, drawX, mid - lineH / 2, inkW, lineH);+ }if (d.mark === "underline" && d.markAlpha) {ctx.globalAlpha = alpha * d.markAlpha;ctx.fillStyle = d.color;- ctx.fillRect(drawX, mid + size * 0.42, run.drawn[wi], Math.max(2 * scale, size * 0.07));+ ctx.fillRect(drawX, mid + size * 0.42, inkW, Math.max(2 * scale, size * 0.07));}ctx.restore();// `restore` puts the canvas back at the line's own size; the size this@@ -722,7 +942,8 @@ async function paintText(});ctx.globalAlpha = alpha0;ctx.textAlign = "center";- drawCaret(cx + (runs[runs.length - 1]?.lineW ?? 0) / 2);+ paintPlate();+ drawCaret(caretRight, typingLine);}/** Paint one element in frame coordinates with no element-level transform —@@ -750,6 +971,28 @@ export async function paintElement(ctx.restore();return;}+ const erode = frame.phase?.erode;+ if (erode !== undefined && erode > 0) {+ // A disintegration paints the element inside its box, then cuts the+ // eaten grain out of it — the box the DOM masks with the same tile.+ if (erode >= 1) {+ return;+ }+ const b = await measureElementBounds(overlay, frame, env, { pad: false });+ const x = b.cx - b.w / 2;+ const y = b.cy - b.h / 2;+ ctx.save();+ ctx.beginPath();+ ctx.rect(x, y, b.w, b.h);+ ctx.clip();+ await paintElement(ctx, overlay, { ...frame, phase: { ...frame.phase, erode: undefined } }, env);+ ctx.globalCompositeOperation = "destination-out";+ ctx.globalAlpha = 1;+ ctx.shadowColor = "transparent";+ ctx.drawImage(erodeTile(erode, "holes", { owner: env, make: (w, h) => newCanvas(env, w, h) }), x, y, b.w, b.h);+ ctx.restore();+ return;+ }const plugin = elementPlugin(kind);if (plugin) {await plugin.paint(ctx, overlay, frame, env);@@ -760,6 +1003,35 @@ export async function paintElement(} else {await paintText(ctx, overlay as TextOverlay, frame, env);}+ // A zap crackles over the finished element, inside its own box.+ const zap = frame.phase?.zap;+ if (zap && zap.amount > 0) {+ paintZap(ctx, await measureElementBounds(overlay, frame, env, { pad: false }), zap, frame.scale);+ }+}++/**+ * Paint only an element's arcs over the whole of a canvas the caller owns,+ * cleared first, under the element's pose — the preview's arc layer, laid+ * over the element its DOM box draws.+ */+export async function paintZapInto(+ canvas: HTMLCanvasElement,+ overlay: Overlay,+ env: RenderEnv,+ phase: PaintPhase & { zap: ZapPhase }+): Promise<void> {+ const { width, height } = canvas;+ const scale = Math.min(width, height) / 1080;+ 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);+ if (phase.pose) {+ applyPaintPose(ctx, phase.pose, overlay, frame);+ }+ paintZap(ctx, await measureElementBounds(overlay, frame, env, { pad: false }), phase.zap, scale);}/** Draw everything after this under a dive's view. */@@ -781,7 +1053,7 @@ function applyPaintPose(): void {ctx.translate(pose.x * frame.width + pose.dx * frame.scale, pose.y * frame.height + pose.dy * frame.scale);ctx.rotate((pose.rotation * Math.PI) / 180);- ctx.scale(pose.scale, pose.scale);+ ctx.scale(pose.scale * (pose.sx ?? 1), pose.scale * (pose.sy ?? 1));ctx.translate(-rest.x * frame.width, -rest.y * frame.height);}@@ -1130,12 +1402,16 @@ export async function measureElementBounds(const s = overlay as ShapeOverlay;const m = shapeMetrics(s, frame);const lineLike = lineLikeShape(s.shape);- return {- cx,- cy,- w: m.w + m.strokeWidth * 2 + (lineLike ? m.thickness : 0),- h: (lineLike ? Math.max(m.thickness, m.headHalf * 2) : m.h) + m.strokeWidth * 2,- };+ const w = m.w + m.strokeWidth * 2 + (lineLike ? m.thickness : 0);+ const h = (lineLike ? Math.max(m.thickness, m.headHalf * 2) : m.h) + m.strokeWidth * 2;+ if (opts?.pad === false) {+ return { cx, cy, w, h };+ }++ // A shadow or glow spills past the box by the same reach text pads for.+ const shadow = resolveShadow(s.shadow ?? false);+ const shadowPad = shadow ? (shadow.blur + Math.abs(shadow.offsetY)) * frame.scale : 0;+ return { cx, cy, w: w + shadowPad * 2, h: h + shadowPad * 2 };}if (kind === "sticker") {const s = overlay as StickerOverlay;@@ -1203,6 +1479,21 @@ export interface AnimatedLayer {* sequences sample at. */const TYPE_SLICE_FPS = 30;+/** `a`, every output frame time strictly between `a` and `b` (timeline+ * seconds) at `fps`, and `b`: the cuts that give each frame of a stretch its+ * own piece. */+function frameCuts(a: number, b: number, fps = TYPE_SLICE_FPS): number[] {+ const slack = 1e-6;+ const cuts = [a];+ for (let k = Math.floor(a * fps) + 1; k / fps < b - slack; k++) {+ if (k / fps > a + slack) {+ cuts.push(k / fps);+ }+ }+ cuts.push(b);+ return cuts;+}+/*** Split an animated element into the windows a canvas renderer draws.*@@ -1244,13 +1535,18 @@ export function planAnimatedLayers(o: Overlay, end: number): AnimatedLayer[] {anim: animPart,phase,});- if (words) {- wordSampleWindows((o as TextOverlay).text, words, dur).forEach((w) => {+ // A font cycle changes the type at every step, so the windows are cut+ // again at each face change and each piece drawn in its face.+ const faces = fontCycleCuts(o, dur);+ if (words || faces.length > 0) {+ const spans = words ? wordSampleWindows((o as TextOverlay).text, words, dur) : [{ start: 0, end: dur }];+ cutAtFaces(spans, faces).forEach((w) => {const a = Math.max(start, o.start + w.start);const b = Math.min(stop, o.start + w.end);// Drawn at the middle of the span it stands for, which is where the// picture it caches actually sits.- if (b - a >= 1e-3) add(applyWordDraw(overlay, (w.start + w.end) / 2, dur), a, b);+ const mid = (w.start + w.end) / 2;+ if (b - a >= 1e-3) add(withFontCycle(words ? applyWordDraw(overlay, mid, dur) : overlay, mid), a, b);});return;}@@ -1268,31 +1564,53 @@ export function planAnimatedLayers(o: Overlay, end: number): AnimatedLayer[] {for (let i = 0; i + 1 < cuts.length; i++)push(o, cuts[i], cuts[i + 1], animPart, lit(undefined, cuts[i], cuts[i + 1]));};- /** One window per slice, each drawn at the ramp's position mid-window. The- * slot leaves the window's anim: its motion is in the picture already. */+ /** One window per slice. A typing or counting slice is one step, drawn at+ * its middle; every other slice is one frame, drawn at its start, the+ * moment the preview shows on that frame. The slot leaves the window's+ * anim: its motion is in the picture already. */const slices = (from: number, secs: number, slot: "in" | "out") => {const edge = anim[slot]!;// Which of the three bakes this is comes from the preset's own channels,// so a composed animation is sliced by what it does.const preset = edgeMotion(edge);const types = !!preset?.animate.typed;+ const counts = !!preset?.animate.counted;const perGlyph = !!preset?.selector;const text = isText ? (o as TextOverlay).text : "";- const cap = types ? Math.max(1, text.length) : Infinity;+ // A count never needs more slices than it has steps ("100 %" has 100).+ const cap = types ? Math.max(1, text.length) : counts ? Math.max(1, countStep(text, 1)) : Infinity;const n = Math.max(1, Math.min(cap, Math.ceil(secs * TYPE_SLICE_FPS)));const rest: OverlayAnim = { ...anim, [slot]: undefined };- for (let i = 0; i < n; i++) {- const mid = (i + 0.5) / n;- const p = slot === "out" ? 1 - mid : mid;- const at = from + (i / n) * secs;- const to = from + ((i + 1) / n) * secs;+ // A typing or counting ramp is cut into its steps, each drawn at its+ // middle; any other ramp is cut where the output frames fall, each piece+ // drawn at its own start, so a frame shows what the preview shows then.+ const stepped = types || counts;+ const cuts = stepped ? [...Array(n + 1).keys()].map((i) => from + (i / n) * secs) : frameCuts(from, from + secs);+ for (let i = 0; i + 1 < cuts.length; i++) {+ const at = cuts[i];+ const to = cuts[i + 1];+ const q = stepped ? (i + 0.5) / n : (at - from) / secs;+ const p = slot === "out" ? 1 - q : q;if (types) {- const chars = Math.max(1, Math.ceil(p * text.length));- push({ ...o, text: text.slice(0, chars) } as Overlay, at, to, rest, lit(undefined, at, to));+ push(o, at, to, rest, { ...lit(undefined, at, to), typed: Math.max(1, typedChars(text, p)) });+ } else if (counts) {+ // The count follows the preset's own curve, the way the evaluator+ // reads it for the live preview.+ const shown = evalWhole(preset!, p, slot === "out").counted ?? p;+ push({ ...o, text: countText(text, shown) } as Overlay, at, to, rest, lit(undefined, at, to));} else if (perGlyph) {push(o, at, to, rest, lit({- glyphs: { style: edge.style, preset: edge.preset, p, exiting: slot === "out" },+ glyphs: {+ style: edge.style,+ preset: edge.preset,+ p,+ exiting: slot === "out",+ ...(edge.stagger !== undefined ? { stagger: edge.stagger } : {}),+ },}, at, to));+ } else if (preset?.animate.erode) {+ // A disintegration eats the ink along the preset's own curve.+ push(o, at, to, rest, lit({ erode: evalWhole(preset, p, slot === "out").erode ?? 0 }, at, to));} else {push(o, at, to, rest, lit({ reveal: p }, at, to));}@@ -1308,7 +1626,65 @@ export function planAnimatedLayers(o: Overlay, end: number): AnimatedLayer[] {if (baked("out")) slices(o.start + dur - outS, outS, "out");else pushLit(o.start + dur - outS, o.start + dur, anim);}- return out;+ return doodleWindows(o, zapWindows(o, out, anim, dur, isText));+}++/** A doodle deals a new mark on every beat, so every window is cut at the+ * beats inside it and each piece drawn with its beat's mark. */+function doodleWindows(o: Overlay, layers: AnimatedLayer[]): AnimatedLayer[] {+ if (o.kind !== "shape" || !dealsMarks(o.shape)) {+ return layers;+ }+ return layers.flatMap((l) => {+ const pieces: AnimatedLayer[] = [];+ let at = l.start;+ while (l.end - at > 1e-4) {+ const beat = doodleBeat(at - o.start);+ const next = Math.min(l.end, o.start + (beat + 1) / DOODLE_FPS);+ pieces.push({ ...l, start: at, end: next, phase: { ...l.phase, doodle: beat } });+ at = next;+ }+ return pieces;+ });+}++/** A zap paints arcs inside the hit's window over whatever plays there — a+ * ramp's slice, a fade, the hold — so every window crossing it is cut per+ * frame inside it, each piece keeping its picture and adding its own deal of+ * the arcs. */+function zapWindows(o: Overlay, layers: AnimatedLayer[], anim: OverlayAnim, dur: number, isText: boolean): AnimatedLayer[] {+ const hit = hitZaps(anim) ? hitWindow(anim, dur) : null;+ if (!hit) {+ return layers;+ }+ const from = o.start + hit.start;+ const to = o.start + hit.end;+ return layers.flatMap((l) => {+ const a = Math.max(l.start, from);+ const b = Math.min(l.end, to);+ if (b - a < 1e-3) {+ return [l];+ }+ const pieces: AnimatedLayer[] = a > l.start ? [{ ...l, end: a }] : [];+ const n = Math.max(1, Math.round((b - a) * TYPE_SLICE_FPS));+ for (let i = 0; i < n; i++) {+ const at = a + (i / n) * (b - a);+ const next = a + ((i + 1) / n) * (b - a);+ const tLocal = (at + next) / 2 - o.start;+ const amount = evalOverlayAnim(anim, tLocal, dur, isText).zap ?? 0;+ pieces.push({ ...l, start: at, end: next, ...(amount > 0 ? { phase: { ...l.phase, zap: { amount, seed: zapSeed(tLocal) } } } : {}) });+ }+ if (b < l.end) {+ pieces.push({ ...l, start: b });+ }+ return pieces;+ });+}++/** How many characters a typewriter at `progress` (0..1) has typed. */+export function typedChars(text: string, progress: number): number {+ const n = [...text].length;+ return Math.max(0, Math.min(n, Math.ceil(progress * n)));}/** The element as it paints at one moment, neutral — rotation and opacity@@ -1321,20 +1697,28 @@ export function elementAtMoment(ev: OverlayFrameState): { el: Overlay; phase?: PaintPhase; typed: boolean } {const dur = Math.max(0.1, overlay.end - overlay.start);- let el: Overlay = applyWordDraw({ ...overlay, rotation: undefined, opacity: undefined }, tLocal, dur);+ let el: Overlay = withFontCycle(+ applyWordDraw({ ...overlay, rotation: undefined, opacity: undefined }, tLocal, dur),+ tLocal+ );const typed = ev.textProgress !== undefined && (overlay.kind ?? "text") === "text";- if (typed) {- const text = (overlay as TextOverlay).text;- const chars = Math.max(0, Math.min(text.length, Math.ceil(ev.textProgress! * text.length)));- el = { ...el, text: text.slice(0, chars) } as Overlay;+ const typedCount = typed ? typedChars((overlay as TextOverlay).text, ev.textProgress!) : undefined;+ // A count draws every number in the text where its count stands.+ if (ev.countProgress !== undefined && (overlay.kind ?? "text") === "text") {+ el = { ...el, text: countText((el as TextOverlay).text, ev.countProgress) } as Overlay;}+ const marks = overlay.kind === "shape" && dealsMarks(overlay.shape);const phase: PaintPhase | undefined =- ev.glyphs || ev.glyphLoop || ev.reveal !== undefined || ev.caret+ ev.glyphs || ev.glyphLoop || ev.reveal !== undefined || ev.erode !== undefined || ev.zap || ev.caret || typed || marks? {+ ...(marks ? { doodle: doodleBeat(tLocal) } : {}),...(ev.glyphs ? { glyphs: ev.glyphs } : {}),...(ev.glyphLoop ? { glyphLoop: ev.glyphLoop } : {}),...(ev.reveal !== undefined ? { reveal: ev.reveal } : {}),+ ...(ev.erode !== undefined ? { erode: ev.erode } : {}),+ ...(ev.zap ? { zap: { amount: ev.zap, seed: zapSeed(tLocal) } } : {}),...(ev.caret ? { caret: true } : {}),+ ...(typedCount !== undefined ? { typed: typedCount } : {}),}: undefined;return { el, phase, typed };@@ -1429,7 +1813,13 @@ export async function renderOverlayFrames(// The crop region: the resting box, grown for travel, scale overshoot,// shadow spill, and — when anything rotates — the circumscribed square.- const base = await measureElementBounds(overlay, frame, env);+ // A font cycle sets the title in other faces for a while, so the region+ // holds the widest and tallest of them too.+ let base = await measureElementBounds(overlay, frame, env);+ for (const face of fontCycleVariants(overlay)) {+ const b = await measureElementBounds(face, frame, env);+ base = { ...base, w: Math.max(base.w, b.w), h: Math.max(base.h, b.h) };+ }// Read from the presets themselves, so a composed animation pads its crop// by what it actually travels rather than by what its name suggests.const edges = [anim?.in, anim?.out]@@ -1455,11 +1845,13 @@ export async function renderOverlayFrames(// A swollen word grows the line past the resting box, so the region carries// it — width from the swell, and the type's own extra height with it.const maxScale = 1.15 * extent.scale * move.scale * wordSwell(accent) * hitScale(anim);+ // A key tilted about both axes swings the box's ends like a turn, never+ // past its half diagonal.const rotates =!!overlay.rotation ||loop.rotates ||move.rotates ||- (keyed && sortedKeys(overlay.kf!).some((k) => !!k.rotation));+ (keyed && sortedKeys(overlay.kf!).some((k) => !!k.rotation || (!!k.tiltX && !!k.tiltY)));let halfW = (base.w * maxScale) / 2 + travel + 4;let halfH = (base.h * maxScale) / 2 + travel + 4;if (rotates) {@@ -1503,7 +1895,9 @@ export async function renderOverlayFrames(const ev = evalOverlayFrame(overlay, tLocal, width / height);if (bar) ev.caret = bar === "lit";if (ev.opacity <= 0.001) return pngBlob(canvas, env); // fully transparent frame- const ctx = fx.begin(target, rw, rh, elementLook(ev, scale)) as CanvasRenderingContext2D;+ // A tilt turns about the posed center, here in the crop's own px.+ const center = { x: ev.x * width + ev.dx * scale - x0, y: ev.y * height + ev.dy * scale - y0 };+ const ctx = fx.begin(target, rw, rh, elementLook(ev, scale, center)) as CanvasRenderingContext2D;ctx.globalAlpha = ev.opacity;ctx.translate(-x0, -y0);// The pose places the element; the preset's travel rides on top of it.@@ -1540,30 +1934,33 @@ export async function renderOverlayFrames(entries.push({ image: images.length - 1, duration });};- const step = 1 / fps;+ // A stretch of the element [a, b), element seconds, cut where the output+ // frames fall on the timeline, each piece drawn at its own start: the+ // moment the preview shows on that frame.+ const perFrame = async (a: number, b: number) => {+ const cuts = frameCuts(overlay.start + a, overlay.start + b, fps);+ for (let i = 0; i + 1 < cuts.length; i++) {+ await push(cuts[i] - overlay.start, cuts[i + 1] - cuts[i]);+ }+ };const period = loopPeriod(anim) ?? lottieDur;const hit = hitWindow(anim, dur);const toggles = (overlay.kind ?? "text") === "text" ? caretToggles(anim, dur) : [];// A keyframed pose, a camera, a move, or a keyframed mask changes on its// own schedule across the whole element, so there is no still middle and no cycle to// repeat: sample the element frame by frame.- if (keyed || filmed || anim?.move || isMaskAnimated(overlay.mask) || (accent && anim?.loop)) {- const n = Math.max(1, Math.round(dur * fps));- for (let i = 0; i < n; i++) {- await push(i * step, i === n - 1 ? dur - (n - 1) * step : step);- }+ // A doodle deals a new mark every beat, the same.+ const marks = overlay.kind === "shape" && dealsMarks(overlay.shape);+ if (keyed || filmed || marks || anim?.move || isMaskAnimated(overlay.mask) || (accent && anim?.loop)) {+ await perFrame(0, dur);target.setTransform(1, 0, 0, 1, 0, 0);target.clearRect(0, 0, rw, rh);fx.dispose();return { x: x0, y: y0, w: rw, h: rh, images, blank: await pngBlob(canvas, env), entries };}- // Head ramp, frame by frame; the last frame absorbs the rounding so the- // segment lengths sum exactly.+ // Head ramp, frame by frame.if (inS > 1e-3) {- const n = Math.max(1, Math.round(inS * fps));- for (let i = 0; i < n; i++) {- await push(i * step, i === n - 1 ? inS - (n - 1) * step : step);- }+ await perFrame(0, inS);}const a0 = inS;const b0 = dur - outS;@@ -1598,14 +1995,15 @@ export async function renderOverlayFrames(.map((w, i) => ({ i, a: Math.max(a, w.start), b: Math.min(b, w.end) })).filter((w) => w.b - w.a > 1e-3);if (spans.length === 0) {- entries.push({ image: await keep(`still:${bar}`, (a + b) / 2, bar), duration: b - a });+ entries.push({ image: await keep(`still:${bar}${cycleFaceTag(overlay, (a + b) / 2)}`, (a + b) / 2, bar), duration: b - a });return;}// The last one absorbs the rounding, so the pieces sum to the stretch.spans[0].a = a;spans[spans.length - 1].b = b;for (const w of spans) {- entries.push({ image: await keep(`word${w.i}:${bar}`, (w.a + w.b) / 2, bar), duration: w.b - w.a });+ const mid = (w.a + w.b) / 2;+ entries.push({ image: await keep(`word${w.i}:${bar}${cycleFaceTag(overlay, mid)}`, mid, bar), duration: w.b - w.a });}return;}@@ -1618,20 +2016,20 @@ export async function renderOverlayFrames(while (b - t > 1e-4) {const j = Math.floor((t - a0) / cycleStep + 1e-6);const next = Math.min(b, a0 + (j + 1) * cycleStep);- const image = await keep(`cycle${j % n}:${bar}`, calm(a0 + (j % n) * cycleStep), bar);+ const image = await keep(`cycle${j % n}:${bar}${cycleFaceTag(overlay, t)}`, calm(a0 + (j % n) * cycleStep), bar);entries.push({ image, duration: next - t });t = next;}return;}- entries.push({ image: await keep(`still:${bar}`, (a + b) / 2, bar), duration: b - a });+ entries.push({ image: await keep(`still:${bar}${cycleFaceTag(overlay, (a + b) / 2)}`, (a + b) / 2, bar), duration: b - a });};// A hit and a blinking bar change the middle at their own moments: it is// cut there, the hit plays frame by frame, and every stretch between fills// as an uninterrupted middle would.if (b0 - a0 > 1e-3) {- const cuts = [a0, b0, ...toggles];+ const cuts = [a0, b0, ...toggles, ...fontCycleCuts(overlay, dur)];if (hit) cuts.push(hit.start, hit.end);const marks = [...new Set(cuts.filter((t) => t >= a0 && t <= b0))].sort((x, y) => x - y);for (let i = 0; i + 1 < marks.length; i++) {@@ -1639,8 +2037,7 @@ export async function renderOverlayFrames(const b = marks[i + 1];if (b - a < 1e-4) continue;if (hit && a >= hit.start - 1e-6 && b <= hit.end + 1e-6) {- const n = Math.max(1, Math.round((b - a) * fps));- for (let j = 0; j < n; j++) await push(a + j * step, j === n - 1 ? b - a - (n - 1) * step : step);+ await perFrame(a, b);continue;}const on = caretOn(anim, (a + b) / 2, dur);@@ -1648,11 +2045,7 @@ export async function renderOverlayFrames(}}if (outS > 1e-3) {- const from = dur - outS;- const n = Math.max(1, Math.round(outS * fps));- for (let i = 0; i < n; i++) {- await push(from + i * step, i === n - 1 ? outS - (n - 1) * step : step);- }+ await perFrame(dur - outS, dur);}target.setTransform(1, 0, 0, 1, 0, 0);diff --git a/site/packages/effects-kit/src/scorch.test.ts b/site/packages/effects-kit/src/scorch.test.tsnew file mode 100644index 00000000..b4c90806--- /dev/null+++ b/site/packages/effects-kit/src/scorch.test.ts@@ -0,0 +1,172 @@+import { describe, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { existsSync } from "node:fs";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { Canvas } from "skia-canvas";+import { applyEffectToCanvas, effectFilterLines, effectPreviewState, effectRecipe, type WashArea } from "./effects";++/**+ * The scorch maps each frame onto a hot tone by brightness: black lands on+ * the tone's deep color, white on its pale one. Some frames burn the whole+ * picture, others a patch of it, and the canvas pass and the ffmpeg graph+ * deal the same tone and the same patch to the same frame.+ */++const S = 64;+const FRAMES = 12;++/** Left half black, right half white. */+function halves(): HTMLCanvasElement {+ const c = new Canvas(S, S) as unknown as HTMLCanvasElement;+ const ctx = c.getContext("2d")!;+ ctx.fillStyle = "#000000";+ ctx.fillRect(0, 0, S, S);+ ctx.fillStyle = "#ffffff";+ ctx.fillRect(S / 2, 0, S / 2, S);+ return c;+}++/** The patch frame `i` burns; none when it burns the whole picture. */+const areaAt = (i: number): WashArea | undefined => effectPreviewState("scorch", 1, i / 30).washes?.[0]?.area;++/** How far into the burn a pixel center sits: 1 inside the patch, 0 past+ * its rim, a ramp between. */+function cover(a: WashArea | undefined, x: number, y: number): number {+ if (!a) {+ return 1;+ }+ const q = Math.hypot(((x + 0.5) / S - a.x) / a.rx, ((y + 0.5) / S - a.y) / a.ry);+ return Math.max(0, Math.min(1, (1 - q) / (1 - a.inner)));+}++/** Every frame through the canvas pass, as RGBA rows. */+function canvasFrames(): Uint8ClampedArray[] {+ return [...Array(FRAMES).keys()].map((i) => {+ const c = halves();+ applyEffectToCanvas(c, halves(), "scorch", 1, i / 30, () => null);+ return c.getContext("2d")!.getImageData(0, 0, S, S).data;+ });+}++const rgbAt = (d: Uint8ClampedArray, x: number, y: number) => Array.from(d.subarray((y * S + x) * 4, (y * S + x) * 4 + 3));++describe("the scorch", () => {+ test("a light leak on its scorch course renders the scorch", () => {+ expect(effectRecipe({ effect: "lightleak", leak: "scorch" })).toBe("scorch");+ });++ test("burned pixels land on a saturated hot tone, a new tone some frames", () => {+ const frames = canvasFrames();+ const tones = new Set<string>();+ frames.forEach((d, i) => {+ const a = areaAt(i);+ for (let y = 0; y < S; y += 3) {+ for (let x = 0; x < S; x += 3) {+ if (cover(a, x, y) < 1) {+ continue;+ }+ const [r, g, b] = rgbAt(d, x, y);+ expect(r).toBeGreaterThan(140);+ expect(r - b).toBeGreaterThan(80);+ if (x < S / 2) {+ tones.add(`${r},${g},${b}`);+ }+ }+ }+ });+ expect(tones.size).toBeGreaterThan(1);+ });++ test("some frames burn only a patch, leaving the picture past its rim", () => {+ const frames = canvasFrames();+ const patched = frames.map((d, i) => ({ d, a: areaAt(i) })).filter((f) => f.a);+ expect(patched.length).toBeGreaterThan(0);+ expect(patched.length).toBeLessThan(FRAMES);+ let untouched = 0;+ for (const { d, a } of patched) {+ for (let y = 0; y < S; y += 2) {+ for (let x = 0; x < S; x += 2) {+ if (cover(a, x, y) > 0) {+ continue;+ }+ const want = x < S / 2 ? 0 : 255;+ rgbAt(d, x, y).forEach((v) => expect(Math.abs(v - want)).toBeLessThanOrEqual(1));+ untouched++;+ }+ }+ }+ expect(untouched).toBeGreaterThan(0);+ });++ test("a patch's rim fades white to the pale tone without dipping darker", () => {+ const frames = canvasFrames();+ let rims = 0;+ frames.forEach((d, i) => {+ const a = areaAt(i);+ if (!a) {+ return;+ }++ // The pale tone is any white pixel deep inside the patch.+ let pale: number[] | undefined;+ for (let y = 0; y < S && !pale; y++) {+ for (let x = S / 2; x < S && !pale; x++) {+ if (cover(a, x, y) === 1) {+ pale = rgbAt(d, x, y);+ }+ }+ }+ if (!pale) {+ return;+ }+ for (let y = 0; y < S; y++) {+ for (let x = S / 2; x < S; x++) {+ const k = cover(a, x, y);+ if (k <= 0 || k >= 1) {+ continue;+ }+ rgbAt(d, x, y).forEach((v, c) => expect(v).toBeGreaterThanOrEqual(pale![c] - 3));+ rims++;+ }+ }+ });+ expect(rims).toBeGreaterThan(0);+ });++ test("the ffmpeg recipe is one geq of one size whatever the length", () => {+ const graph = (d: number) => effectFilterLines("in", "out", "scorch", 1, 0, d, S, S, "t")!.join(";");+ expect(graph(1).match(/geq=/g)!.length).toBe(1);+ expect(graph(10).match(/geq=/g)!.length).toBe(1);+ expect(Math.abs(graph(10).length - graph(1).length)).toBeLessThan(40);+ });++ test("the ffmpeg recipe deals the same tones and patches in the bundled LGPL build", () => {+ const bin = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools/ffmpeg");+ expect(existsSync(bin)).toBe(true);+ // A 4:4:4 chain, so the rims keep their color per pixel.+ const chroma = { pixFmt: "yuv444p", overlay: ":format=yuv444" };+ const lines = effectFilterLines("in", "out", "scorch", 1, 0, FRAMES / 30, S, S, "t", undefined, undefined, chroma)!;+ const graph =+ `color=c=black:s=${S}x${S}:r=30:d=${FRAMES / 30},format=yuv444p,` ++ `drawbox=x=${S / 2}:y=0:w=${S / 2}:h=${S}:color=white:t=fill[in];${lines.join(";")}`;+ const run = spawnSync(bin, ["-v", "error", "-filter_complex", graph, "-map", "[out]", "-pix_fmt", "rgb24", "-f", "rawvideo", "-"]);+ expect(run.status).toBe(0);+ const frames = canvasFrames();+ let checked = 0;+ for (let i = 0; i < FRAMES; i++) {+ const a = areaAt(i);+ const base = i * S * S * 3;+ for (let y = 1; y < S; y += 4) {+ for (let x = 1; x < S; x += 4) {+ // yuv444 round trips cost a few codes; the rims blend alike.+ const at = base + (y * S + x) * 3;+ const ours = Array.from(run.stdout.subarray(at, at + 3));+ rgbAt(frames[i], x, y).forEach((v, c) => expect(Math.abs(ours[c] - v)).toBeLessThanOrEqual(12));+ checked++;+ }+ }+ }+ expect(checked).toBeGreaterThan(FRAMES * 100);+ });+});diff --git a/site/packages/effects-kit/src/stagger.test.ts b/site/packages/effects-kit/src/stagger.test.tsnew file mode 100644index 00000000..30215c9d--- /dev/null+++ b/site/packages/effects-kit/src/stagger.test.ts@@ -0,0 +1,34 @@+import { describe, expect, test } from "bun:test";+import { edgeStagger, evalOverlayAnim, glyphStateAt } from "./anim";++// A six-letter title dropping in over 1.6s.+const LETTERS = 6;++/** The moment letter `i` first moves off its start pose, in seconds. */+function firstMove(stagger: number | undefined, i: number): number {+ const anim = { in: { style: "drop" as const, seconds: 1.6, ...(stagger !== undefined ? { stagger } : {}) } };+ for (let t = 0; t <= 1.6; t += 1 / 120) {+ const g = glyphStateAt(evalOverlayAnim(anim, t, 4, true), i, LETTERS);+ if (g.alpha > 0.001) {+ return t;+ }+ }+ return Infinity;+}++describe("an edge's letter hand-off", () => {+ test("the slot's stagger spaces the letters evenly across its share of the ramp", () => {+ // At 0.8 the hand-off takes 1.28s: a letter every 0.256s.+ const starts = [...Array(LETTERS).keys()].map((i) => firstMove(0.8, i));+ for (let i = 1; i < LETTERS; i++) {+ expect(starts[i] - starts[i - 1]).toBeCloseTo(0.256, 1);+ }+ });++ test("absent, the style's own spread plays", () => {+ expect(edgeStagger({ style: "drop", seconds: 1 })).toBe(0.6);+ expect(edgeStagger({ style: "drop", seconds: 1, stagger: 0.2 })).toBe(0.2);+ expect(edgeStagger({ style: "fade", seconds: 1 })).toBeUndefined();+ expect(firstMove(undefined, 5)).toBeCloseTo(0.96, 1);+ });+});diff --git a/site/packages/effects-kit/src/stretch.ts b/site/packages/effects-kit/src/stretch.tsnew file mode 100644index 00000000..8d9db52e--- /dev/null+++ b/site/packages/effects-kit/src/stretch.ts@@ -0,0 +1,37 @@+/**+ * Per-axis scale, split into the two numbers every renderer reads: a uniform+ * `scale` and a stretch on each axis over it. Readers that only know uniform+ * size (extents, selection chrome) take `scale`, the larger axis; the+ * painters multiply the stretch in. A TV switching off is x 1, y 0.02: scale+ * 1, sy 0.02. A flip through zero is x 1, y -1: scale 1, sy -1.+ */+export interface Stretched {+ scale: number;+ /** Width over `scale`; absent = 1. */+ sx?: number;+ /** Height over `scale`; absent = 1. */+ sy?: number;+}++/** Two axes differing by less than this are the same size. */+const SAME_AXIS = 1e-6;++/** The uniform scale and stretch for a picture `x` wide and `y` tall+ * (multiples of its drawn size). */+export function splitScale(x: number, y: number): Stretched {+ if (Math.abs(x - y) < SAME_AXIS) {+ return { scale: x };+ }+ const m = Math.max(Math.abs(x), Math.abs(y));+ return { scale: m, sx: x / m, sy: y / m };+}++/** The picture's width multiple: uniform scale times its x stretch. */+export function scaleXOf(s: Stretched): number {+ return s.scale * (s.sx ?? 1);+}++/** The picture's height multiple: uniform scale times its y stretch. */+export function scaleYOf(s: Stretched): number {+ return s.scale * (s.sy ?? 1);+}diff --git a/site/packages/effects-kit/src/texture.test.ts b/site/packages/effects-kit/src/texture.test.tsnew file mode 100644index 00000000..73958487--- /dev/null+++ b/site/packages/effects-kit/src/texture.test.ts@@ -0,0 +1,139 @@+import { describe, expect, test } from "bun:test";+import { Canvas } from "skia-canvas";+import { planAnimatedLayers, renderElementCanvas, type RenderEnv } from "./render";+import { evalOverlayFrame } from "./keys";+import { stippleKeeps } from "./texture";+import type { TextOverlay } from "./types";++const env: RenderEnv = {+ fontStack: () => "Arial",+ createCanvas: (w, h) => new Canvas(w, h) as unknown as HTMLCanvasElement,+ canvasToPngBlob: async (c) => new Blob([new Uint8Array(await (c as unknown as Canvas).toBuffer("png"))]),+};++// One heavy block letter filling the middle of the frame.+const title = (over: Partial<TextOverlay> = {}): TextOverlay => ({+ id: "t",+ text: "M",+ start: 0,+ end: 2,+ x: 0.5,+ y: 0.5,+ size: 700,+ font: "sf",+ weight: 900,+ color: "#ffffff",+ shadow: false,+ plate: false,+ ...over,+});++/** The share of opaque pixels in a band of columns across the letter's ink. */+async function inkShare(o: TextOverlay, from: number, to: number): Promise<number> {+ const c = await renderElementCanvas(o, 1080, 1080, env);+ const data = c.getContext("2d")!.getImageData(0, 0, 1080, 1080).data;+ // The letter's own extent, read off the solid render.+ let left = 1080;+ let right = 0;+ const solid = (await renderElementCanvas({ ...o, texture: undefined }, 1080, 1080, env)).getContext("2d")!.getImageData(0, 0, 1080, 1080).data;+ for (let x = 0; x < 1080; x++) {+ if (solid[(540 * 1080 + x) * 4 + 3] > 128) {+ left = Math.min(left, x);+ right = Math.max(right, x);+ }+ }+ const x0 = Math.round(left + (right - left) * from);+ const x1 = Math.round(left + (right - left) * to);+ let ink = 0;+ let all = 0;+ for (let y = 380; y < 700; y++) {+ for (let x = x0; x < x1; x++) {+ if (solid[(y * 1080 + x) * 4 + 3] < 128) {+ continue;+ }+ all++;+ if (data[(y * 1080 + x) * 4 + 3] > 128) {+ ink++;+ }+ }+ }+ return ink / Math.max(1, all);+}++describe("the stipple texture", () => {+ test("the tile sprays thin at the left edge and runs nearly solid past the middle", () => {+ const share = (x0: number, x1: number) => {+ let on = 0;+ for (let y = 0; y < 80; y++) {+ for (let x = x0; x < x1; x++) {+ on += stippleKeeps(x, y) ? 1 : 0;+ }+ }+ return on / ((x1 - x0) * 80);+ };+ expect(share(0, 6)).toBeLessThan(0.35);+ expect(share(48, 64)).toBeGreaterThan(0.9);+ });++ test("the canvas painter cuts it out of each letter: specks at the left, solid at the right", async () => {+ const o = title({ texture: "stipple" });+ expect(await inkShare(o, 0, 0.2)).toBeLessThan(0.6);+ expect(await inkShare(o, 0.75, 1)).toBeGreaterThan(0.85);+ });++ test("a plate stays whole behind the textured letters", async () => {+ const c = await renderElementCanvas(title({ texture: "stipple", plate: true, plateColor: "#ff0000", plateOpacity: 1 }), 1080, 1080, env);+ const solid = await renderElementCanvas(title({ plate: true, plateColor: "#ff0000", plateOpacity: 1 }), 1080, 1080, env);+ const ctx = c.getContext("2d")!;+ // Every pixel the plate covers stays opaque: the grain shows the plate+ // through the letter, never a hole.+ const a = ctx.getImageData(0, 0, 1080, 1080).data;+ const b = solid.getContext("2d")!.getImageData(0, 0, 1080, 1080).data;+ let holes = 0;+ for (let i = 3; i < a.length; i += 4) {+ if (b[i] > 250 && a[i] < 250) {+ holes++;+ }+ }+ expect(holes).toBe(0);+ });+});++/** Opaque pixels in a rendered element. */+async function inked(o: TextOverlay, phase?: { erode: number }): Promise<number> {+ const c = await renderElementCanvas(o, 540, 540, env, phase);+ const data = c.getContext("2d")!.getImageData(0, 0, 540, 540).data;+ let n = 0;+ for (let i = 3; i < data.length; i += 4) {+ if (data[i] > 128) {+ n++;+ }+ }+ return n;+}++describe("the disintegrate exit", () => {+ const o = title({ text: "MW", size: 300, anim: { out: { style: "disintegrate", seconds: 0.5 } } });++ test("the evaluator eats more of the ink as the exit runs", () => {+ const early = evalOverlayFrame(o, 1.55).erode!;+ const late = evalOverlayFrame(o, 1.9).erode!;+ expect(early).toBeGreaterThanOrEqual(0);+ expect(late).toBeGreaterThan(early);+ expect(evalOverlayFrame(o, 1).erode).toBeUndefined();+ });++ test("the export bakes one picture per frame along the same curve", () => {+ const slices = planAnimatedLayers(o, 10).filter((l) => l.phase?.erode !== undefined);+ expect(slices.length).toBe(15);+ expect(slices[0].phase!.erode!).toBeLessThan(slices[14].phase!.erode!);+ });++ test("the canvas cuts the eaten share out of the ink", async () => {+ const whole = await inked(o);+ const half = await inked(o, { erode: 0.5 });+ expect(half / whole).toBeGreaterThan(0.3);+ expect(half / whole).toBeLessThan(0.7);+ expect(await inked(o, { erode: 1 })).toBe(0);+ });+});diff --git a/site/packages/effects-kit/src/texture.ts b/site/packages/effects-kit/src/texture.tsnew file mode 100644index 00000000..ba694f9b--- /dev/null+++ b/site/packages/effects-kit/src/texture.ts@@ -0,0 +1,205 @@+/**+ * Text textures: grain laid into the type itself, letter by letter.+ *+ * A texture is one small tile stretched over every glyph's box — the advance+ * width by the line height — so each letter wears the same grain whatever its+ * size. The DOM preview hands the tile to each glyph span as its mask-image;+ * the canvas painter draws the tile's holes out of each glyph with+ * `destination-out`. Both read the one tile, so the speckle lands on the same+ * pixels in the preview and every export.+ */++/** The textures a title can wear. `stipple` is spray-paint grain: each letter+ * breaks into scattered specks along its left edge and runs solid by its+ * middle. */+export const TEXT_TEXTURES = ["stipple"] as const;+export type TextTexture = (typeof TEXT_TEXTURES)[number];++/** Which half of the tile a renderer wants: the ink a mask keeps, or the holes+ * a painter cuts out. */+export type TexturePart = "keep" | "holes";++/** Tile size in texels. Stretched over a 1080-frame letter, a texel lands at+ * about 2–3 px, the grain the template prints. */+const TILE_W = 64;+const TILE_H = 80;++/** A deterministic 0..1 draw per texel, so every render speckles alike. */+function hash(x: number, y: number, seed: number): number {+ let h = (Math.imul(x, 374761393) + Math.imul(y, 668265263) + Math.imul(seed, 2246822519)) | 0;+ h = Math.imul(h ^ (h >>> 13), 1274126177);+ return ((h ^ (h >>> 16)) >>> 0) / 4294967296;+}++/** Value noise over 8-texel cells: the clumps the specks gather in. */+function clump(x: number, y: number): number {+ const cx = Math.floor(x / 8);+ const cy = Math.floor(y / 8);+ const fx = (x % 8) / 8;+ const fy = (y % 8) / 8;+ const sx = fx * fx * (3 - 2 * fx);+ const sy = fy * fy * (3 - 2 * fy);+ const a = hash(cx, cy, 7) + (hash(cx + 1, cy, 7) - hash(cx, cy, 7)) * sx;+ const b = hash(cx, cy + 1, 7) + (hash(cx + 1, cy + 1, 7) - hash(cx, cy + 1, 7)) * sx;+ return a + (b - a) * sy;+}++/** How much ink survives at `u` across the letter, 0 at its left edge: a+ * sparse spray that fills in to nearly solid by the middle. */+function stippleDensity(u: number): number {+ const s = Math.min(1, Math.max(0, u / 0.55));+ return Math.min(0.97, 0.16 + 0.82 * s * s * (3 - 2 * s));+}++/** Whether texel (x, y) of the stipple keeps its ink. */+export function stippleKeeps(x: number, y: number): boolean {+ const grain = 0.7 * hash(x, y, 1) + 0.3 * clump(x, y);+ return grain < stippleDensity((x + 0.5) / TILE_W);+}++/** Where tiles are painted: the canvas maker, and the object that owns it (a+ * render env), which the cache is kept on. */+export interface TextureSurface {+ owner: object;+ make: (w: number, h: number) => HTMLCanvasElement;+}++/** Painted tiles, one set per owner: a tab and a render worker each draw on+ * their own surface, and a picture from one cannot be drawn on the other. Two+ * small tiles per owner and one per erosion step, so the cache needs no+ * bound. */+const tiles = new WeakMap<object, Map<string, HTMLCanvasElement>>();++/** The texture's tile, white where the part is. */+export function textureTile(texture: TextTexture, part: TexturePart, surface: TextureSurface): HTMLCanvasElement {+ let byPart = tiles.get(surface.owner);+ if (!byPart) {+ byPart = new Map();+ tiles.set(surface.owner, byPart);+ }+ const key = `${texture}:${part}`;+ const cached = byPart.get(key);+ if (cached) {+ return cached;+ }++ // One texel per pixel: opaque white where the part is, clear elsewhere.+ const canvas = surface.make(TILE_W, TILE_H);+ const ctx = canvas.getContext("2d")!;+ const img = ctx.createImageData(TILE_W, TILE_H);+ for (let y = 0; y < TILE_H; y++) {+ for (let x = 0; x < TILE_W; x++) {+ const on = stippleKeeps(x, y) === (part === "keep");+ const i = (y * TILE_W + x) * 4;+ img.data[i] = img.data[i + 1] = img.data[i + 2] = 255;+ img.data[i + 3] = on ? 255 : 0;+ }+ }+ ctx.putImageData(img, 0, 0);+ byPart.set(key, canvas);+ return canvas;+}++/** Cut a texture out of the glyph just drawn: the tile's holes, stretched over+ * the glyph's box (`x, y, w, h` in the current transform) and clipped to it,+ * so the letters beside it keep their ink. */+export function cutTexture(+ ctx: CanvasRenderingContext2D,+ texture: TextTexture,+ surface: TextureSurface,+ x: number,+ y: number,+ w: number,+ h: number+): void {+ ctx.save();+ ctx.beginPath();+ ctx.rect(x, y, w, h);+ ctx.clip();+ ctx.globalCompositeOperation = "destination-out";+ ctx.globalAlpha = 1;+ ctx.shadowColor = "transparent";+ ctx.drawImage(textureTile(texture, "holes", surface), x, y, w, h);+ ctx.restore();+}++/** The erosion tile's size in texels; stretched over the element's box. */+const ERODE_W = 160;+const ERODE_H = 80;+/** Erosion steps a renderer draws: a frame's share is rounded to one of+ * these, so each step's tile is painted once and reused. */+const ERODE_LEVELS = 24;+/** The soft band at the edge of the eaten grain, in field units. */+const ERODE_EDGE = 0.05;++/** Value noise over `cell`-texel cells, seeded per octave. */+function cellNoise(x: number, y: number, cell: number, seed: number): number {+ const cx = Math.floor(x / cell);+ const cy = Math.floor(y / cell);+ const fx = (x % cell) / cell;+ const fy = (y % cell) / cell;+ const sx = fx * fx * (3 - 2 * fx);+ const sy = fy * fy * (3 - 2 * fy);+ const a = hash(cx, cy, seed) + (hash(cx + 1, cy, seed) - hash(cx, cy, seed)) * sx;+ const b = hash(cx, cy + 1, seed) + (hash(cx + 1, cy + 1, seed) - hash(cx, cy + 1, seed)) * sx;+ return a + (b - a) * sy;+}++/** The order the ink goes in: blotches at three scales over a fine grain,+ * flattened to ranks so a share of `e` eats exactly that share of the box. */+let erodeField: Float32Array | null = null;+function erosionField(): Float32Array {+ if (erodeField) {+ return erodeField;+ }+ const n = ERODE_W * ERODE_H;+ const raw = new Float32Array(n);+ for (let y = 0; y < ERODE_H; y++) {+ for (let x = 0; x < ERODE_W; x++) {+ const blot = 0.5 * cellNoise(x, y, 20, 11) + 0.3 * cellNoise(x, y, 8, 12) + 0.2 * cellNoise(x, y, 3, 13);+ raw[y * ERODE_W + x] = 0.75 * blot + 0.25 * hash(x, y, 14);+ }+ }+ const order = [...raw.keys()].sort((a, b) => raw[a] - raw[b]);+ const field = new Float32Array(n);+ order.forEach((i, rank) => {+ field[i] = rank / (n - 1);+ });+ erodeField = field;+ return field;+}++/** The erosion step a share `erode` of the ink eaten draws at, 0 (whole)+ * through `erodeLevel(1)` (gone): pictures of one step look the same. */+export const erodeLevel = (erode: number) => Math.round(Math.min(1, Math.max(0, erode)) * ERODE_LEVELS);++/** The erosion tile for a share `erode` of the ink eaten, white where the+ * part is: `keep` for a DOM mask, `holes` for a canvas cut. */+export function erodeTile(erode: number, part: TexturePart, surface: TextureSurface): HTMLCanvasElement {+ const level = erodeLevel(erode);+ let byPart = tiles.get(surface.owner);+ if (!byPart) {+ byPart = new Map();+ tiles.set(surface.owner, byPart);+ }+ const key = `erode:${level}:${part}`;+ const cached = byPart.get(key);+ if (cached) {+ return cached;+ }++ // Ink keeps where its rank is past the eaten share, through a soft band.+ const e = level / ERODE_LEVELS;+ const field = erosionField();+ const canvas = surface.make(ERODE_W, ERODE_H);+ const ctx = canvas.getContext("2d")!;+ const img = ctx.createImageData(ERODE_W, ERODE_H);+ for (let i = 0; i < field.length; i++) {+ const keep = Math.min(1, Math.max(0, (field[i] - e) / ERODE_EDGE + 0.5));+ img.data[i * 4] = img.data[i * 4 + 1] = img.data[i * 4 + 2] = 255;+ img.data[i * 4 + 3] = Math.round(255 * (part === "keep" ? keep : 1 - keep));+ }+ ctx.putImageData(img, 0, 0);+ byPart.set(key, canvas);+ return canvas;+}diff --git a/site/packages/effects-kit/src/types.ts b/site/packages/effects-kit/src/types.tsindex 19797f85..7fd12582 100644--- a/site/packages/effects-kit/src/types.ts+++ b/site/packages/effects-kit/src/types.ts@@ -10,6 +10,8 @@ import type { GroupCamera } from "./camera";import type { EffectOverlay } from "./effects";import type { OverlayKey } from "./keys";import type { Mask } from "./mask";+import type { PatternSpec } from "./pattern";+import type { TextTexture } from "./texture";import type { WordDraw } from "./words/types";/** Fields every overlay element carries, whatever its kind. */@@ -54,17 +56,32 @@ export interface OverlayBase {camera?: GroupCamera;}-/** A custom drop shadow. Every field is optional — absent ones take the- * legacy defaults, so `shadow: true` and `shadow: {}` render identically. */-export interface TextShadowSpec {+/** A custom drop shadow, for text and shapes alike. Every field is optional —+ * absent ones take the legacy defaults, so `shadow: true` and `shadow: {}`+ * render identically. A colored shadow with offsetY 0 reads as a glow. */+export interface ShadowSpec {color?: string; // hex; opacity folds in separatelyblur?: number; // px at the 1080 design short sideoffsetY?: number; // px at the 1080 design short sideopacity?: number; // 0..1}+/** Shadow blur and offset bounds, px at the 1080 design short side. */+export const SHADOW_BLUR_MAX = 60;+export const SHADOW_OFFSET_MAX = 40;+export type TextAlign = "left" | "center" | "right";+/** The weights a title can be set in: regular, bold, extra bold and black. A+ * face without a weight draws in its nearest one. */+export const TEXT_WEIGHTS = [400, 700, 800, 900] as const;++/** Tracking bounds, em: display type set tight down to the floor, spaced caps+ * up to the ceiling. */+export const LETTER_SPACING_MIN = -0.15;+export const LETTER_SPACING_MAX = 0.5;+export type TextWeight = (typeof TEXT_WEIGHTS)[number];+/** A text element. `kind` may be absent — documents written before the union* existed stored bare title objects, and absence still means text. `font` is a* host font id, resolved to a CSS stack through the render env. */@@ -73,7 +90,7 @@ export interface TextOverlay extends OverlayBase {text: string;size: number; // px at a 1080-wide design framefont: string;- weight: 400 | 700;+ weight: TextWeight;italic?: boolean;color: string;/** Outline drawn behind the fill (stroke-before-fill; the DOM pair is@@ -91,11 +108,13 @@ export interface TextOverlay extends OverlayBase {* Width changes wrap lines; height follows the content. */wrapWidth?: number;/** Drop shadow: `true` = the legacy default look, an object customizes it. */- shadow: boolean | TextShadowSpec;+ shadow: boolean | ShadowSpec;plate: boolean; // rounded plate behind the textplateRadius?: number; // plate corner radius in emplateColor?: string; // plate fill colorplateOpacity?: number; // plate fill opacity 0..1+ /** Grain laid into the letters themselves; absent = solid type. */+ texture?: TextTexture;/** Word burn-in: one entry per display word (whitespace-split across all* lines), each resolved to the color, opacity and pose it draws at. Stamped* by the word engine at paint time; absent = the line draws as one piece. */@@ -111,7 +130,8 @@ export type ShapeKind =| "heart"| "hexagon"| "line"- | "arrow";+ | "arrow"+ | "doodle";/** Shapes drawn as a stroke across the element box: `h` is the stroke* thickness and rotation gives them their direction. Every other kind is a@@ -129,6 +149,19 @@ export interface ShapeOverlay extends OverlayBase {fillOpacity?: number; // 0..1, absent = 1 (composes with `opacity`)radius?: number; // rect corner radius, px at 1080 short sidestroke?: { color: string; width: number }; // outline, width px at 1080 short side+ /** The fill color laid down as a pattern clipped to the outline; absent =+ * a solid fill. Line and arrow shapes always draw solid. */+ pattern?: PatternSpec;+ /** Drop shadow cast by the shape's silhouette; absent or false = none. */+ shadow?: boolean | ShadowSpec;+ /** A doodle's other paints: each mark it deals takes the fill or one of+ * these. Other kinds ignore it. */+ inks?: string[];+}++/** Shapes that paint a new hand-drawn mark on every beat (see `doodle.ts`). */+export function dealsMarks(k: ShapeKind): boolean {+ return k === "doodle";}/** An image sticker. Height follows the source's own aspect until a side@@ -239,6 +272,7 @@ export const TEXT_STYLE_FIELDS = ["plateColor","plateOpacity","plateRadius",+ "texture",] as const;/** A saved look, and what one title copies from another. */diff --git a/site/packages/effects-kit/src/words/types.ts b/site/packages/effects-kit/src/words/types.tsindex c224558b..e8bab15f 100644--- a/site/packages/effects-kit/src/words/types.ts+++ b/site/packages/effects-kit/src/words/types.ts@@ -12,6 +12,7 @@* this file. Adding an effect means adding an entry; no code lists them.*/+import type { TextWeight } from "../types";import type { Easing } from "../motion/types";/** One word's look at one moment. Offsets are em of the word's own type size,@@ -109,7 +110,7 @@ export interface WordDraw {* word in another family, slant or weight. Absent = the line's. */font?: string;italic?: boolean;- weight?: 400 | 700;+ weight?: TextWeight;}/** How one word is set apart from the rest of its line: its own fill, face@@ -120,7 +121,7 @@ export interface WordFace {color?: string;font?: string;italic?: boolean;- weight?: 400 | 700;+ weight?: TextWeight;/** Type-size multiplier, on top of whatever the effect does to the word. */scale?: number;}diff --git a/site/packages/effects-kit/src/zap.test.ts b/site/packages/effects-kit/src/zap.test.tsnew file mode 100644index 00000000..b55c201e--- /dev/null+++ b/site/packages/effects-kit/src/zap.test.ts@@ -0,0 +1,118 @@+import { describe, expect, test } from "bun:test";+import { Canvas } from "skia-canvas";+import { evalOverlayFrame } from "./keys";+import { planAnimatedLayers, renderElementCanvas, type RenderEnv } from "./render";+import type { TextOverlay } from "./types";+import { zapSeed } from "./zap";++const env: RenderEnv = {+ fontStack: () => "Arial",+ createCanvas: (w, h) => new Canvas(w, h) as unknown as HTMLCanvasElement,+ canvasToPngBlob: async (c) => new Blob([new Uint8Array(await (c as unknown as Canvas).toBuffer("png"))]),+};++// A title that crackles from 0.3s for a second.+const title: TextOverlay = {+ id: "t",+ text: "SOON",+ start: 2,+ end: 4.5,+ x: 0.5,+ y: 0.5,+ size: 200,+ font: "sf",+ weight: 700,+ color: "#ffffff",+ shadow: false,+ plate: false,+ anim: { hit: { style: "zap", at: 0.3, seconds: 1 } },+};++/** Pixels bluer than they are red: the arcs' glow, never the white type. */+function blue(c: HTMLCanvasElement): number {+ const d = c.getContext("2d")!.getImageData(0, 0, c.width, c.height).data;+ let n = 0;+ for (let i = 0; i < d.length; i += 4) {+ if (d[i + 3] > 40 && d[i + 2] > d[i] + 30) {+ n++;+ }+ }+ return n;+}++describe("the zap hit", () => {+ test("it crackles only inside the hit's window", () => {+ expect(evalOverlayFrame(title, 0.1).zap).toBeUndefined();+ expect(evalOverlayFrame(title, 0.8).zap).toBeCloseTo(1);+ expect(evalOverlayFrame(title, 1.5).zap).toBeUndefined();+ });++ test("the export bakes the hit frame by frame, each a fresh deal of the arcs", () => {+ const zaps = planAnimatedLayers(title, 10).filter((l) => l.phase?.zap);+ expect(zaps.length).toBeGreaterThanOrEqual(28);+ expect(new Set(zaps.map((l) => l.phase!.zap!.seed)).size).toBeGreaterThan(8);+ expect(zaps[0].start).toBeCloseTo(2.3);+ });++ test("the painter lays blue arcs over the element, and the same deal twice alike", async () => {+ const calm = await renderElementCanvas(title, 540, 540, env);+ const zap = { amount: 1, seed: zapSeed(0.8) };+ const a = await renderElementCanvas(title, 540, 540, env, { zap });+ const b = await renderElementCanvas(title, 540, 540, env, { zap });+ expect(blue(calm)).toBe(0);+ expect(blue(a)).toBeGreaterThan(200);+ expect(blue(b)).toBe(blue(a));+ });+});++/** The centroid of the arcs' blue glow, in frame fractions. */+function blueCenter(c: HTMLCanvasElement): { x: number; y: number } {+ const d = c.getContext("2d")!.getImageData(0, 0, c.width, c.height).data;+ let sx = 0;+ let sy = 0;+ let n = 0;+ for (let i = 0; i < d.length; i += 4) {+ if (d[i + 3] > 40 && d[i + 2] > d[i] + 30) {+ const p = i / 4;+ sx += p % c.width;+ sy += Math.floor(p / c.width);+ n++;+ }+ }+ return { x: sx / n / c.width, y: sy / n / c.height };+}++describe("the zap over a textured title", () => {+ test("the arcs crackle over the letters wherever the title sits", async () => {+ const o: TextOverlay = { ...title, texture: "stipple", x: 0.5, y: 0.55 };+ // Averaged over deals, the arcs center on the letters: a dealer that+ // leans one way drags every deal off to that side.+ let x = 0;+ let y = 0;+ const seeds = [1, 3, 5, 8, 11, 19, 27, 40];+ for (const seed of seeds) {+ const at = blueCenter(await renderElementCanvas(o, 1080, 1920, env, { zap: { amount: 1, seed } }));+ x += at.x / seeds.length;+ y += at.y / seeds.length;+ }+ expect(Math.abs(x - 0.5)).toBeLessThan(0.06);+ expect(Math.abs(y - 0.55)).toBeLessThan(0.02);+ });+});++describe("a zap over the entrance", () => {+ test("the export bakes arcs from the hit's start, inside the ramp too", () => {+ // The hit lands 0.17s in, while a 0.45s fade is still bringing it up.+ const o: TextOverlay = { ...title, anim: { in: { style: "fade", seconds: 0.45 }, hit: { style: "zap", at: 0.17, seconds: 0.75 } } };+ const layers = planAnimatedLayers(o, 10);+ const zaps = layers.filter((l) => l.phase?.zap);+ expect(zaps[0].start).toBeCloseTo(2.17 + 1 / 30, 1);+ // The ramp's own motion still rides the pieces: they keep the fade.+ expect(zaps.some((l) => l.start < 2.45 && l.anim.in)).toBe(true);+ // The windows still tile the element without gaps or overlaps.+ const sorted = [...layers].sort((a, b) => a.start - b.start);+ for (let i = 1; i < sorted.length; i++) {+ expect(sorted[i].start).toBeCloseTo(sorted[i - 1].end, 6);+ }+ });+});diff --git a/site/packages/effects-kit/src/zap.ts b/site/packages/effects-kit/src/zap.tsnew file mode 100644index 00000000..f19bec05--- /dev/null+++ b/site/packages/effects-kit/src/zap.ts@@ -0,0 +1,136 @@+/**+ * Electric arcs crawling over an element: the zap hit.+ *+ * The arcs are pixels, so one painter draws them for every renderer — the+ * export's baked frames and the preview's arc canvas alike. Each arc is a+ * jagged bolt between two points of the element's box, re-dealt `ZAP_FPS`+ * times a second from a seed, so the same moment always crackles the same+ * way. A bolt is a wide blue glow under a thin white core, with a flare where+ * it lands.+ */++/** How often the arcs re-deal, a second. */+export const ZAP_FPS = 12;++/** The arcs at one moment: how strong (0..1) and which deal. */+export interface ZapPhase {+ amount: number;+ seed: number;+}++/** The deal a zap shows at `tLocal` seconds into its element. */+export const zapSeed = (tLocal: number) => Math.floor(Math.max(0, tLocal) * ZAP_FPS);++/** The bloom, glow and core colors of a bolt. */+const BLOOM = "rgba(120,160,255,0.22)";+const GLOW = "rgba(140,175,255,0.8)";+const HALO = "rgba(90,140,255,0.95)";+const CORE = "rgba(240,246,255,1)";++/** A small seeded generator, so a deal is the same in every renderer. */+function dealer(seed: number): () => number {+ let s = (Math.imul(seed + 1, 2654435761) ^ 0x9e3779b9) >>> 0;+ return () => {+ s = (Math.imul(s ^ (s >>> 15), 2246822519) + 0x6d2b79f5) >>> 0;+ // XOR yields a signed int; `>>> 0` keeps the draw in 0..1.+ s = (s ^ (s >>> 13)) >>> 0;+ return s / 4294967296;+ };+}++/** A bolt from a to b: the line split in halves, each midpoint knocked+ * sideways by a share of its span that shrinks as the halves do. */+function bolt(ax: number, ay: number, bx: number, by: number, rand: () => number): [number, number][] {+ let pts: [number, number][] = [[ax, ay], [bx, by]];+ let jag = 0.24;+ for (let level = 0; level < 6; level++) {+ const next: [number, number][] = [pts[0]];+ for (let i = 0; i + 1 < pts.length; i++) {+ const [x0, y0] = pts[i];+ const [x1, y1] = pts[i + 1];+ const len = Math.hypot(x1 - x0, y1 - y0);+ const off = (rand() - 0.5) * 2 * jag * len;+ // Sideways is the span's normal.+ const nx = len ? -(y1 - y0) / len : 0;+ const ny = len ? (x1 - x0) / len : 0;+ next.push([(x0 + x1) / 2 + nx * off, (y0 + y1) / 2 + ny * off], pts[i + 1]);+ }+ pts = next;+ jag *= 0.72;+ }+ return pts;+}++/**+ * Draw the arcs over a box (`cx, cy, w, h` in the current transform). `scale`+ * is output px per design px. The bolts stay inside the box's own height+ * band and a little past its ends, so a crop padded for the element holds+ * them.+ */+export function paintZap(+ ctx: CanvasRenderingContext2D,+ box: { cx: number; cy: number; w: number; h: number },+ zap: ZapPhase,+ scale: number+): void {+ const k = Math.min(1, Math.max(0, zap.amount));+ if (k <= 0) {+ return;+ }+ const rand = dealer(zap.seed);+ const count = 2 + Math.round(3 * k);+ const pick = () => [box.cx + (rand() - 0.5) * box.w * 1.05, box.cy + (rand() - 0.5) * box.h * 0.9] as const;++ ctx.save();+ ctx.lineJoin = "round";+ ctx.lineCap = "round";+ for (let i = 0; i < count; i++) {+ // Each bolt reaches a sixth to a half of the box across.+ const [ax, ay] = pick();+ const reach = box.w * (0.16 + 0.34 * rand());+ const turn = rand() * Math.PI * 2;+ const bx = Math.min(box.cx + box.w * 0.55, Math.max(box.cx - box.w * 0.55, ax + Math.cos(turn) * reach));+ const by = Math.min(box.cy + box.h * 0.45, Math.max(box.cy - box.h * 0.45, ay + Math.sin(turn) * reach * 0.5));+ const pts = bolt(ax, ay, bx, by, rand);+ const trace = () => {+ ctx.beginPath();+ ctx.moveTo(pts[0][0], pts[0][1]);+ for (let j = 1; j < pts.length; j++) {+ ctx.lineTo(pts[j][0], pts[j][1]);+ }+ };++ // A wide bloom, the blue glow, then the white-hot core over it.+ ctx.globalAlpha = k;+ ctx.shadowColor = HALO;+ ctx.shadowBlur = 30 * scale;+ ctx.strokeStyle = BLOOM;+ ctx.lineWidth = 16 * scale;+ trace();+ ctx.stroke();+ ctx.shadowBlur = 12 * scale;+ ctx.strokeStyle = GLOW;+ ctx.lineWidth = 6 * scale;+ trace();+ ctx.stroke();+ ctx.shadowColor = CORE;+ ctx.shadowBlur = 4 * scale;+ ctx.strokeStyle = CORE;+ ctx.lineWidth = 2.5 * scale;+ trace();+ ctx.stroke();++ // A flare where the bolt lands.+ const r = (16 + 22 * rand()) * scale * k;+ const flare = ctx.createRadialGradient(bx, by, 0, bx, by, r);+ flare.addColorStop(0, "rgba(255,255,255,1)");+ flare.addColorStop(0.35, "rgba(150,185,255,0.8)");+ flare.addColorStop(1, "rgba(90,140,255,0)");+ ctx.shadowColor = "transparent";+ ctx.fillStyle = flare;+ ctx.beginPath();+ ctx.arc(bx, by, r, 0, Math.PI * 2);+ ctx.fill();+ }+ ctx.restore();+}diff --git a/site/scripts/eval-cut-export.ts b/site/scripts/eval-cut-export.tsindex 19793911..7c5bc02b 100644--- a/site/scripts/eval-cut-export.ts+++ b/site/scripts/eval-cut-export.ts@@ -59,6 +59,11 @@ const HERMETIC_PROJECT = "cut-export-eval";const CLIP_S = 3;/** How far the probed file may drift from the doc's own duration. */const DURATION_TOLERANCE_S = 0.5;+/** Where the click sits in its file, and where the doc puts it on the timeline. */+const CLICK_IN_FILE_S = 0.25;+const CLICK_AT_S = 4.85;+/** Sound against picture: under a fifth of a frame at 24 fps. */+const SYNC_TOLERANCE_S = 0.008;function run(cmd: string, cmdArgs: string[]): Promise<string> {return new Promise((resolve, reject) => {@@ -110,6 +115,17 @@ async function buildFixtures(): Promise<void> {music,]);}+ // A one-sample-wide click 0.25 s into an MP3. The encoder's tag says how+ // much priming opens the file; the click is where the sync check looks.+ const click = path.join(OUT, "click.mp3");+ if (!existsSync(click)) {+ await run("ffmpeg", [+ "-y", "-loglevel", "error",+ "-f", "lavfi", "-i", `aevalsrc='if(between(t,${CLICK_IN_FILE_S},${CLICK_IN_FILE_S}+0.0005),0.9,0)':s=44100:d=0.6`,+ "-c:a", "libmp3lame", "-b:a", "128k",+ click,+ ]);+ }}/** A browser with the fixture media served from disk and the session answered. */@@ -293,6 +309,7 @@ function fixtureDoc(url: (file: string) => string, clipS = CLIP_S) {asset("v0", "clip-0.mp4", "video", clipS),asset("v1", "clip-1.mp4", "video", clipS),asset("m0", "music.m4a", "audio", 8),+ asset("k0", "click.mp3", "audio", 0.6),],clips: [{@@ -324,6 +341,10 @@ function fixtureDoc(url: (file: string) => string, clipS = CLIP_S) {id: "a0", assetId: "m0", start: 0, in: 0, out: 5,volume: 0.5, fadeIn: 0.3, fadeOut: 0.5, lane: 0,},+ {+ id: "k0", assetId: "k0", start: CLICK_AT_S - CLICK_IN_FILE_S, in: 0, out: 0.5,+ volume: 1, lane: 1,+ },],overlays: [{@@ -565,6 +586,21 @@ async function main(): Promise<void> {if (!(Math.abs(dB) <= 0.5)) failures.push(`level at 3.5–4.5s is ${dB.toFixed(2)} dB off the sources`);console.log(`[level] ${dB >= 0 ? "+" : ""}${dB.toFixed(2)} dB against the sources; ${frames.length} frames, ${flashes.length} bare`);+ // Sync: the click lands where the doc puts it. Encoder priming written+ // without an edit list, or an MP3's own priming read as sound, plays every+ // cut's audio late against its picture. The tones under it sit below the+ // high-pass, so the loudest sample left is the click.+ const near = CLICK_AT_S - 0.2;+ const hp = await run("ffmpeg", ["-loglevel", "error", "-ss", String(near), "-t", "0.4", "-i", outPath, "-vn", "-af", "highpass=f=4000,pan=mono|c0=c0", "-f", "f32le", "-ar", "48000", "-"]);+ const hx = new Float32Array(Buffer.from(hp, "binary").buffer);+ let peak = 0;+ for (let i = 1; i < hx.length; i++) if (Math.abs(hx[i]) > Math.abs(hx[peak])) peak = i;+ const clickAt = near + peak / 48000;+ if (!(Math.abs(clickAt - CLICK_AT_S) <= SYNC_TOLERANCE_S)) {+ failures.push(`the click plays at ${clickAt.toFixed(4)}s, the doc puts it at ${CLICK_AT_S}s`);+ }+ console.log(`[sync] click ${((clickAt - CLICK_AT_S) * 1000).toFixed(1)} ms from where the doc puts it`);+if (failures.length) {console.error(`[FAIL] ${failures.join("; ")}`);process.exit(1);diff --git a/site/src/cut/components/AnimationTiles.tools.ts b/site/src/cut/components/AnimationTiles.tools.tsindex 7a224c39..49aa762b 100644--- a/site/src/cut/components/AnimationTiles.tools.ts+++ b/site/src/cut/components/AnimationTiles.tools.ts@@ -8,8 +8,15 @@* until every side catches up.*/-import { CAMERA_WORLD_MAX, CAMERA_WORLD_MIN, EASE_IDS, ELEMENT_BLUR_MAX } from "@donkeycut/effects-kit";+import { CAMERA_WORLD_MAX, CAMERA_WORLD_MIN, EASE_IDS, EDGE_STAGGER_MAX, ELEMENT_BLUR_MAX, TILT_MAX } from "@donkeycut/effects-kit";import {+ CYCLE_SCALE_MAX,+ CYCLE_SCALE_MIN,+ FONT_CYCLE_FACES_MAX,+ FONT_CYCLE_RATE,+ FONT_CYCLE_RATE_MAX,+ FONT_CYCLE_RATE_MIN,+ TEXT_WEIGHTS,CARET_BLINK_SECONDS,CARET_BLINKS_MAX,hitNotes,@@ -19,6 +26,7 @@ import {OVERLAY_HIT_STYLE_IDS,OVERLAY_ANIM_DEFAULT_SECONDS,OVERLAY_ANIM_MAX_SECONDS,+ edgeMaxSeconds,OVERLAY_ANIM_MIN_SECONDS,GLYPH_ANIM_STYLE_IDS,GLYPH_LOOP_STYLE_IDS,@@ -41,13 +49,17 @@ import {TEXT_MOVE_NOTES,} from "@/cut/lib/textMotion";-const RAMP_SECONDS = `${OVERLAY_ANIM_MIN_SECONDS}..${OVERLAY_ANIM_MAX_SECONDS} (default ${OVERLAY_ANIM_DEFAULT_SECONDS})`;+// Styles that may run past the shared cap say how far, e.g. "; count up to 10".+const LONG_RAMPS = OVERLAY_ANIM_STYLE_IDS.filter((id) => edgeMaxSeconds(id) !== OVERLAY_ANIM_MAX_SECONDS)+ .map((id) => `; ${id} up to ${edgeMaxSeconds(id)}`)+ .join("");+const RAMP_SECONDS = `${OVERLAY_ANIM_MIN_SECONDS}..${OVERLAY_ANIM_MAX_SECONDS} (default ${OVERLAY_ANIM_DEFAULT_SECONDS}${LONG_RAMPS})`;export const OVERLAY_ANIMATION_TOOLS = [{name: "set_overlay_animation",description:- `Animate an overlay element (title, shape, or sticker): preset In/Out ramps, a Loop that runs its whole duration, and a Move that says what the element does WHILE it holds. Omitted slots keep their setting; pass "none" to clear one. In/Out styles: ${OVERLAY_ANIM_STYLE_IDS.join(", ")} — slide names are the motion direction; ${TEXT_ONLY_ANIM_STYLE_IDS.join(", ")} animate titles only; ${GLYPH_ANIM_STYLE_IDS.join(", ")} move a title's letters one at a time, and move any other kind as one piece. ${edgeNotes()} ${DIVE_ANIM_STYLE_IDS.join(", ")} as the exit is the zoom-through-a-letter transition: the shot after the element shows once the ink has filled the frame, so it needs no freeze frames, image stills or per-letter keyframes. Loop styles: ${OVERLAY_LOOP_STYLE_IDS.join(", ")} — ${GLYPH_LOOP_STYLE_IDS.join(", ")} carry a title's letters on their own delays, and carry any other kind as one piece. A move is a slot like the others and never touches the element's keyframe track; set_overlay_keyframes is the precise way to move an element and the default reach, so name a move when it is exactly what you mean — the two compose together. Word effects — words_style, titles only — play the line word by word, timed against the cut's transcript when there is one and spread across the element's own span when there is not: an emphasis travels along a line that is fully up, and a build assembles the line as it is spoken. It is the slot for "make each word pop as I say it" and for "have the words appear one by one". A typewriter entrance on a title can carry a caret: a thin bar at the end of the typed text, solid while it types, then blinking every ${CARET_BLINK_SECONDS}s — the typed comment or search box of a call-to-action card. A hit plays one motion once at a moment inside the element (hit_at seconds from its start), between its entrance and exit: ${hitNotes()}. Use press for a button being pressed, a tap on a UI element, a like or post button; pair it with a click from stock_search kind "sound" placed at the same moment when the tap should be heard.`,+ `Animate an overlay element (title, shape, or sticker): preset In/Out ramps, a Loop that runs its whole duration, and a Move that says what the element does WHILE it holds. Omitted slots keep their setting; pass "none" to clear one. In/Out styles: ${OVERLAY_ANIM_STYLE_IDS.join(", ")} — slide names are the motion direction; ${TEXT_ONLY_ANIM_STYLE_IDS.join(", ")} animate titles only; ${GLYPH_ANIM_STYLE_IDS.join(", ")} move a title's letters one at a time, and move any other kind as one piece; in_stagger/out_stagger set how much of their ramp the hand-off from the first letter to the last takes, so a 1.6s drop at stagger 0.8 lands a letter every ~0.27s, each moving for ~0.3s. ${edgeNotes()} ${DIVE_ANIM_STYLE_IDS.join(", ")} as the exit is the zoom-through-a-letter transition: the shot after the element shows once the ink has filled the frame, so it needs no freeze frames, image stills or per-letter keyframes. Loop styles: ${OVERLAY_LOOP_STYLE_IDS.join(", ")} — ${GLYPH_LOOP_STYLE_IDS.join(", ")} carry a title's letters on their own delays, and carry any other kind as one piece. A move is a slot like the others and never touches the element's keyframe track; set_overlay_keyframes is the precise way to move an element and the default reach, so name a move when it is exactly what you mean — the two compose together. Word effects — words_style, titles only — play the line word by word, timed against the cut's transcript when there is one and spread across the element's own span when there is not: an emphasis travels along a line that is fully up, and a build assembles the line as it is spoken. It is the slot for "make each word pop as I say it" and for "have the words appear one by one". A typewriter entrance on a title can carry a caret: a thin bar at the end of the typed text, solid while it types, then blinking every ${CARET_BLINK_SECONDS}s — the typed comment or search box of a call-to-action card. A hit plays one motion once at a moment inside the element (hit_at seconds from its start), between its entrance and exit: ${hitNotes()}. Use press for a button being pressed, a tap on a UI element, a like or post button; pair it with a click from stock_search kind "sound" placed at the same moment when the tap should be heard. A font cycle flickers a title through typefaces — a new face font_cycle_rate times a second, from font_cycle_at for font_cycle_seconds — and then the title settles back into its own face: the typeface-shuffle beat of a teaser title. Give each face a scale (and tracking) so it fills about the room the title's own face does; a wide tracked title cycling through condensed or hand-drawn faces wants tracking 0 and a scale above 1 on them.`,inputSchema: obj({id: str("Overlay element id"),move: {@@ -70,6 +82,8 @@ export const OVERLAY_ANIMATION_TOOLS = [description: 'Exit style, or "none" to clear',},out_seconds: num(`Exit ramp seconds ${RAMP_SECONDS}`),+ in_stagger: num(`Per-letter entrance: share of the ramp spent handing the motion from the first letter to the last, 0..${EDGE_STAGGER_MAX} (0 = every letter together, higher = one after another, each moving for less of the ramp); a negative restores the style's own`),+ out_stagger: num(`Per-letter exit: the same hand-off share for the exit ramp, 0..${EDGE_STAGGER_MAX}; a negative restores the style's own`),loop_style: {type: "string",enum: [...OVERLAY_LOOP_STYLE_IDS, "none"],@@ -105,12 +119,30 @@ export const OVERLAY_ANIMATION_TOOLS = [`Hit length seconds ${OVERLAY_HIT_MIN_SECONDS}..${OVERLAY_HIT_MAX_SECONDS} (default ${OVERLAY_HIT_DEFAULT_SECONDS})`),hit_darken: bool("Darken the element while the hit holds it down (default true for a new hit)"),+ font_cycle: {+ type: "array",+ description: `Titles only: the faces the title cycles through, in order (up to ${FONT_CYCLE_FACES_MAX}); [] removes the cycle`,+ items: obj(+ {+ font: str("Font id (see the graphics skill)"),+ weight: { type: "number", enum: [...TEXT_WEIGHTS], description: "Weight of this face; default the title's own" },+ scale: num(`Size multiplier on the title's own size, ${CYCLE_SCALE_MIN}..${CYCLE_SCALE_MAX} (default 1)`),+ tracking: num("Letter spacing in em for this face; default the title's own"),+ },+ ["font"]+ ),+ },+ font_cycle_at: num("Seconds from the element's start where the cycle begins (default 0)"),+ font_cycle_seconds: num("How long the cycle runs, seconds (default: to the element's end)"),+ font_cycle_rate: num(+ `Face changes a second, ${FONT_CYCLE_RATE_MIN}..${FONT_CYCLE_RATE_MAX} (default ${FONT_CYCLE_RATE}: a new face every other frame at 30 fps)`+ ),}, ["id"]),},{name: "set_overlay_keyframes",description:- `Give an overlay element a keyframed pose track — the default way to animate one, because a key names the exact pose at the exact second where a preset ramp approximates it. Each key is a whole pose at a time measured in seconds from the element's own start; the pose moves between keys along the key's ease (${EASE_IDS.join(", ")}; default linear) and holds outside them. Omitted fields on a key take the element's current value. blur softens the element (px at the 1080 short side, 0..${ELEMENT_BLUR_MAX}): keyed with opacity it is depth of field — the card being read sharp, its neighbours blurred and dimmed while a group camera (set_group_camera) pushes in. Pass an empty list to clear the track and return the element to its resting pose. Preset In/Out/Loop animation still composes on top, so a keyframed title can also fade in.`,+ `Give an overlay element a keyframed pose track — the default way to animate one, because a key names the exact pose at the exact second where a preset ramp approximates it. Each key is a whole pose at a time measured in seconds from the element's own start; the pose moves between keys along the key's ease (${EASE_IDS.join(", ")}; default linear) and holds outside them. Omitted fields on a key take the element's current value. scale_x/scale_y stretch one side over scale (a screen switching off: scale_y to 0.02, then scale_x to 0.02); tilt_x/tilt_y turn the element in 3D perspective about its horizontal/vertical axis (tilt_x positive tips the top edge away, tilt_y the right edge; ±${TILT_MAX}°): a bar falling back, a card swinging open. blur softens the element (px at the 1080 short side, 0..${ELEMENT_BLUR_MAX}): keyed with opacity it is depth of field — the card being read sharp, its neighbours blurred and dimmed while a group camera (set_group_camera) pushes in. Pass an empty list to clear the track and return the element to its resting pose. Preset In/Out/Loop animation still composes on top, so a keyframed title can also fade in.`,inputSchema: obj({id: str("Overlay element id"),@@ -123,7 +155,11 @@ export const OVERLAY_ANIMATION_TOOLS = [x: num(`Center x, fraction of frame width 0..1 (${CAMERA_WORLD_MIN}..${CAMERA_WORLD_MAX} for a grouped element)`),y: num(`Center y, fraction of frame height 0..1 (${CAMERA_WORLD_MIN}..${CAMERA_WORLD_MAX} for a grouped element)`),scale: num("Size multiplier, 1 = the element's own size (0.1..4)"),+ scale_x: num("Width over scale, 0..4 (default 1)"),+ scale_y: num("Height over scale, 0..4 (default 1)"),rotation: num("Degrees clockwise, -180..180"),+ tilt_x: num(`3D tilt about the horizontal axis, degrees, -${TILT_MAX}..${TILT_MAX}; positive tips the top edge away (default 0)`),+ tilt_y: num(`3D tilt about the vertical axis, degrees, -${TILT_MAX}..${TILT_MAX}; positive tips the right edge away (default 0)`),opacity: num("0..1"),blur: num(`Blur, px at the 1080 short side, 0..${ELEMENT_BLUR_MAX}`),ease: { type: "string", enum: [...EASE_IDS], description: "Curve into the next key" },diff --git a/site/src/cut/components/AnimationTiles.tsx b/site/src/cut/components/AnimationTiles.tsxindex e628b672..8b20bdd5 100644--- a/site/src/cut/components/AnimationTiles.tsx+++ b/site/src/cut/components/AnimationTiles.tsx@@ -1,7 +1,9 @@"use client";+import { erodeMask } from "./textureCss";import { useEffect, useRef, useState } from "react";import {+ countText,evalOverlayAnim,glyphStateAt,hasGlyphMotion,@@ -28,6 +30,7 @@ import {type OverlayAnimStyle,type OverlayLoopStyle,type OverlayWords,+ zapSeed,} from "@donkeycut/effects-kit";import { Tile } from "@/cut/components/PanelTile";import { SectionTitle } from "@/cut/components/SectionTitle";@@ -180,6 +183,14 @@ const wordStyle = () => ({ fontFamily: fontStack("montserrat"), fontWeight: 700,const typesItsName = (slot: Slot, style: string) =>slot === "in" || slot === "out" ? !!MOTION.edges[style]?.animate.typed : false;+/** Whether the demo counts a number up (count). Its element comes from React+ * empty, for the reason a typed demo's does. */+const countsItsName = (slot: Slot, style: string) =>+ slot === "in" || slot === "out" ? !!MOTION.edges[style]?.animate.counted : false;++/** The number a count demo counts to, after the style's name: "Count 100". */+const COUNT_DEMO_TO = 100;+/** Whether the demo rolls each letter up a reel (slot). The reel's three* windows come from React empty, for the reason a typed demo's do. */const rollsItsName = (slot: Slot, style: string) =>@@ -235,6 +246,7 @@ function LiveName({const ref = useRef<HTMLSpanElement>(null);const label = labelOf(slot, style);const typed = typesItsName(slot, style);+ const counts = countsItsName(slot, style);const rolls = isText && rollsItsName(slot, style);const dives = divesIntoName(slot, style);// Golden-ratio steps around the cycle: neighbouring tiles land far apart in@@ -265,6 +277,10 @@ function LiveName({el.style.opacity = "";el.style.clipPath = "";el.style.filter = "";+ el.style.textShadow = "";+ for (const prop of ["mask-image", "-webkit-mask-image"]) {+ el.style.removeProperty(prop);+ }letters().forEach((kid, i) => {kid.style.transform = "";kid.style.opacity = "";@@ -316,11 +332,39 @@ function LiveName({}el.style.clipPath =st.reveal !== undefined ? `inset(0 ${(1 - st.reveal) * 100}% 0 0)` : "";+ // A disintegration eats the name through the kit's erosion tile.+ const eroded = st.erode !== undefined && st.erode > 0 ? erodeMask(st.erode, null) : null;+ for (const prop of ["mask-image", "-webkit-mask-image"]) {+ el.style.setProperty(prop, eroded ? String(eroded.maskImage) : "");+ }+ for (const prop of ["mask-size", "-webkit-mask-size"]) {+ el.style.setProperty(prop, eroded ? "100% 100%" : "");+ }+ for (const prop of ["mask-repeat", "-webkit-mask-repeat"]) {+ el.style.setProperty(prop, eroded ? "no-repeat" : "");+ }+ // A zap's arcs read on a tile this small as the name crackling blue,+ // its glow re-dealt at the arcs' own rate.+ if (st.zap) {+ const flick = 0.6 + 0.4 * ((zapSeed((now - start) / 1000) * 7919) % 5) / 4;+ el.style.textShadow = `0 0 ${(3 * st.zap * flick).toFixed(1)}px #dbe6ff, 0 0 ${(9 * st.zap * flick).toFixed(1)}px #4f7bff`;+ } else {+ el.style.textShadow = "";+ }el.style.filter = st.brightness !== undefined ? `brightness(${st.brightness})` : "";if (st.textProgress !== undefined) {const word = el.dataset.word ?? "";el.textContent = word.slice(0, Math.ceil(st.textProgress * word.length));}+ // A count demo shows its number where the count stands, and the whole+ // number once the ramp is over.+ if (counts) {+ const word = el.dataset.word ?? "";+ const shown = st.countProgress !== undefined ? countText(word, st.countProgress) : word;+ if (el.textContent !== shown) {+ el.textContent = shown;+ }+ }raf = requestAnimationFrame(tick);};raf = requestAnimationFrame(tick);@@ -329,16 +373,16 @@ function LiveName({cancelAnimationFrame(raf);rest();};- }, [slot, style, isText, seconds, speed, phase, label, rolls, dives]);+ }, [slot, style, isText, seconds, speed, phase, label, rolls, dives, counts]);return (<spanref={ref}- data-word={typed ? label : undefined}+ data-word={typed ? label : counts ? `${label} ${COUNT_DEMO_TO}` : undefined}className={`${WORD} text-foreground`}style={{ willChange: "transform", ...wordStyle() }}>- {typed+ {typed || counts? null: rolls? [...label].map((ch, i) => (diff --git a/site/src/cut/components/ClipEffectsSection.tsx b/site/src/cut/components/ClipEffectsSection.tsxindex 29fbbf60..13f22e38 100644--- a/site/src/cut/components/ClipEffectsSection.tsx+++ b/site/src/cut/components/ClipEffectsSection.tsx@@ -1,6 +1,7 @@"use client";import { X } from "lucide-react";+import { Fragment } from "react";import {AMOUNTLESS_EFFECTS,CLIP_EFFECT_IDS,@@ -10,6 +11,7 @@ import {} from "@donkeycut/effects-kit";import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";+import { FlashPulseRows, type PulseWrite } from "@/cut/components/FlashPulseRows";import { Row, Section, useSliderCheckpoint } from "@/cut/components/panelBits";import { parsePercentInput } from "@/cut/components/ScrubValue";import { ValueSlider } from "@/cut/components/ValueSlider";@@ -37,6 +39,17 @@ export function ClipEffectsSection({ clip }: { clip: VideoClip }) {st().updateClipTransient(clip.id, { effects: list.map((e, j) => (j === i ? { ...e, amount } : e)) });};+ // A flash's pulse rows write into its entry: drafts ride the transient+ // path under the rows' own checkpoint, clicks land as one step.+ const setPulse = (i: number, patch: Partial<ClipEffect>, how: PulseWrite) => {+ const effects = list.map((e, j) => (j === i ? { ...e, ...patch } : e));+ if (how === "draft") {+ st().updateClipTransient(clip.id, { effects });+ return;+ }+ write(effects);+ };+return (<Sectiontitle="Effects"@@ -65,37 +78,40 @@ export function ClipEffectsSection({ clip }: { clip: VideoClip }) {}>{list.map((e, i) => (- <Row key={e.effect} label={EFFECT_LABELS[e.effect]}>- {!AMOUNTLESS_EFFECTS.includes(e.effect) && (- <ValueSlider- label={`${EFFECT_LABELS[e.effect]} amount`}- sliderClassName="data-horizontal:w-24"- valueClassName="w-9 text-muted-foreground"- value={e.amount ?? 0.5}- min={0.05}- max={1}- step={0.01}- snap={[0.5]}- format={(v) => String(Math.round(v * 100))}- parse={parsePercentInput}- onDraft={(v) => setAmount(i, v)}- onCommit={(v) => {- setAmount(i, v);- ck.end();- }}- />- )}- <Tooltip>- <TooltipTrigger- aria-label={`Remove ${EFFECT_LABELS[e.effect]}`}- className="grid size-6 place-items-center rounded text-muted-foreground transition-colors hover:text-foreground"- onClick={() => write(list.filter((_, j) => j !== i))}- >- <X className="size-3.5" />- </TooltipTrigger>- <TooltipContent>Remove</TooltipContent>- </Tooltip>- </Row>+ <Fragment key={e.effect}>+ <Row label={EFFECT_LABELS[e.effect]}>+ {!AMOUNTLESS_EFFECTS.includes(e.effect) && (+ <ValueSlider+ label={`${EFFECT_LABELS[e.effect]} amount`}+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={e.amount ?? 0.5}+ min={0.05}+ max={1}+ step={0.01}+ snap={[0.5]}+ format={(v) => String(Math.round(v * 100))}+ parse={parsePercentInput}+ onDraft={(v) => setAmount(i, v)}+ onCommit={(v) => {+ setAmount(i, v);+ ck.end();+ }}+ />+ )}+ <Tooltip>+ <TooltipTrigger+ aria-label={`Remove ${EFFECT_LABELS[e.effect]}`}+ className="grid size-6 place-items-center rounded text-muted-foreground transition-colors hover:text-foreground"+ onClick={() => write(list.filter((_, j) => j !== i))}+ >+ <X className="size-3.5" />+ </TooltipTrigger>+ <TooltipContent>Remove</TooltipContent>+ </Tooltip>+ </Row>+ {e.effect === "flash" && <FlashPulseRows pulse={e} write={(patch, how) => setPulse(i, patch, how)} />}+ </Fragment>))}</Section>);diff --git a/site/src/cut/components/EffectsPanel.tools.ts b/site/src/cut/components/EffectsPanel.tools.tsindex 36a14b40..20acfd99 100644--- a/site/src/cut/components/EffectsPanel.tools.ts+++ b/site/src/cut/components/EffectsPanel.tools.ts@@ -6,7 +6,7 @@* removed or renamed tool breaks the build until every side catches up.*/-import { ALL_EFFECT_IDS, AUDIO_EFFECT_IDS, EFFECT_IDS, LOOK_EFFECTS } from "@donkeycut/effects-kit";+import { ALL_EFFECT_IDS, AUDIO_EFFECT_IDS, EFFECT_IDS, FLASH_RHYTHMS, FLASH_RATE_MAX, FLASH_RATE_MIN, FLASH_TONES, GLITCH_KINDS, LEAK_COURSES, LOOK_EFFECTS } from "@donkeycut/effects-kit";import { num, obj, type AiToolDef } from "@/cut/lib/aiToolDef";/** The effects with a treatment recipe of their own; the rest of the list is@@ -19,7 +19,7 @@ export const EFFECTS_TOOLS = [{name: "add_effect",description:- `Add a time-ranged effect element over the cut: a zoom into part of the frame, a picture treatment (${EFFECT_TREATMENTS.join(", ")}), a graded look (${LOOK_EFFECTS.join(", ")}), or an audio treatment (${AUDIO_EFFECT_IDS.join(", ")}) over the sound. A picture effect filters everything visible under its window (start..end); an audio effect treats everything audible under it — clip sound, upper tracks and soundtrack together. They stack. Tasteful default: one effect at a time, amount under ~0.5.`,+ `Add a time-ranged effect element over the cut: a zoom into part of the frame, a picture treatment (${EFFECT_TREATMENTS.join(", ")}), a graded look (${LOOK_EFFECTS.join(", ")}), or an audio treatment (${AUDIO_EFFECT_IDS.join(", ")}) over the sound. A picture effect filters everything visible under its window (start..end); an audio effect treats everything audible under it — clip sound, upper tracks and soundtrack together. They stack. glitch is clean footage broken by runs of corrupted frames — dark pixel-stretch smears, the picture doubled sideways over an orange burn, teal close-ups, blown-out flashes — the glitch ending of a teaser, where amount is how often it hits. A glitch pinned to one kind (glitch "smear", "split", "tint", "blowout" or "rgb") hits with it on every frame; "rgb" pulls the color channels apart, red down and blue up — a 2–3 frame rgb glitch is the RGB-split hit of a collapsing title or HUD. lightleak drifts a warm bloom and light streaks over the picture for as long as it is up; with leak "burn" it is a film burn played once across the element's own length: it climbs from the bottom edge behind a hot orange front, flares near white, settles orange and hands the picture back in its last tenth — the burn-out of a teaser, a 0.4–0.6s element over the end of the last shot, amount 1 for a fully opaque burn. With leak "scorch" it flashes film burn through the picture: every frame maps the picture onto a saturated hot tone by brightness — yellow, orange, or red over pink — across the whole frame or a soft-edged patch of it, a new tone and patch each frame; short 2–12 frame scorches over the last shots, amount 1, are the burn-through flicker of a teaser's ending. flash pops the picture white at the element's start and decays in about 0.4s; tone "black" dips it dark instead, and a strobe rate holds the tone on and off that many times a second for the whole element — a black strobe at 5–6 with amount 0.9–1 is the dark beat-flicker over a teaser's last shot. rhythm "flicker" deals each pulse its own strength and leaves some dark: an uneven exposure flicker, a white flicker at rate 12–15 and amount 0.15–0.25. To flicker one shot inside its mask, give the clip the flash through set_clip_effects. Tasteful default: one effect at a time, amount under ~0.5.`,inputSchema: obj({effect: { type: "string", enum: [...ALL_EFFECT_IDS], description: "Effect id" },start: num("Start time s (default: playhead)"),@@ -27,6 +27,27 @@ export const EFFECTS_TOOLS = [amount: num("Strength 0.05..1 (default 0.5). For zoom it is the depth: 0.25 shallow, 0.5 moderate, 1 deep; for an audio effect it is how far the treatment goes"),focus_x: num("Zoom only: the x of the point to zoom into, 0..1 (default 0.5)"),focus_y: num("Zoom only: the y of the point to zoom into, 0..1 (default 0.5)"),+ leak: {+ type: "string",+ enum: [...LEAK_COURSES],+ description: 'lightleak only: "drift" glows and wanders for the whole element (default), "burn" is the film burn played once across it, "scorch" flashes hot burn tones through the picture frame by frame',+ },+ glitch: {+ type: "string",+ enum: [...GLITCH_KINDS],+ description: 'glitch only: pin every frame to one kind; "rgb" is the channel split. Omit for the random mix',+ },+ tone: {+ type: "string",+ enum: [...FLASH_TONES],+ description: 'flash only: "white" pops the picture bright (default), "black" dips it dark',+ },+ rate: num(`flash only: strobe pulses a second, ${FLASH_RATE_MIN}..${FLASH_RATE_MAX}; each pulse holds the tone for its first half. Omit for one pop at the start`),+ rhythm: {+ type: "string",+ enum: [...FLASH_RHYTHMS],+ description: 'flash with a rate only: "strobe" fires every pulse at full strength (default), "flicker" deals each pulse its own strength and skips some',+ },lane: num("Element row (0 = the front row, drawn over every higher row). Elements on one row never overlap — a title over a shape needs a lower row than the shape."),}, ["effect"]),},diff --git a/site/src/cut/components/EffectsPanel.tsx b/site/src/cut/components/EffectsPanel.tsxindex 861afd24..141b10e7 100644--- a/site/src/cut/components/EffectsPanel.tsx+++ b/site/src/cut/components/EffectsPanel.tsx@@ -923,25 +923,16 @@ export function EffectSwatch({const shake =st.dx || st.dy || st.zoom? `translate(${pct(st.dx ?? 0).toFixed(2)}%, ${pct(st.dy ?? 0).toFixed(2)}%) scale(${st.zoom ?? 1})`+ : st.stretch+ ? `scaleY(${st.stretch.scale})`+ : undefined;+ // A zoom scales about the point it holds, a stretch about its row;+ // everything else about the middle.+ const origin = st.origin+ ? `${st.origin.x * 100}% ${st.origin.y * 100}%`+ : st.stretch+ ? `50% ${st.stretch.row * 100}%`: undefined;- // A zoom scales about the point it holds; everything else about the middle.- const origin = st.origin ? `${st.origin.x * 100}% ${st.origin.y * 100}%` : undefined;- // The glitch's chroma ghost is a few pixels of screen blend, and bright- // footage swallows it at tile size. The swatch tears the picture the way- // the full-size channel shift reads: strips of the frame knocked sideways,- // jumping on the preview state's quantized clock.- const tears =- id === "glitch"- ? [0, 1].map((i) => {- const step = Math.floor(t * 9) + 3 * i;- const j = ((step * 7919) % 5) - 2; // -2..2, deterministic- return {- top: i ? 58 : 22,- h: i ? 8 : 12,- dx: (j || 1) * (i ? -2.6 : 3.2),- };- })- : [];return (<spandata-drag-object@@ -961,18 +952,12 @@ export function EffectSwatch({><SwatchScene frame={frame} /></span>- {tears.map((tear) => (- <span- key={tear.top}- className="absolute inset-0"- style={{- clipPath: `inset(${tear.top}% 0 ${100 - tear.top - tear.h}% 0)`,- transform: `translateX(${tear.dx.toFixed(2)}%)`,- }}- >+ {!!st.ghost && (+ // A split's unmoved frame, lightened back over the moved one.+ <span className="absolute inset-0 mix-blend-lighten" style={{ opacity: st.ghost }}><SwatchScene frame={frame} /></span>- ))}+ )}{!!st.grain && grainUrl && (<spanclassName="cut-grain absolute inset-0"diff --git a/site/src/cut/components/ElementsPanel.tools.ts b/site/src/cut/components/ElementsPanel.tools.tsindex ea9f6b5a..fca06af6 100644--- a/site/src/cut/components/ElementsPanel.tools.ts+++ b/site/src/cut/components/ElementsPanel.tools.ts@@ -7,7 +7,8 @@* side catches up.*/-import { num, obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";+import { DOODLE_FPS, DOODLE_INKS_MAX, PATTERN_KINDS, SHADOW_BLUR_MAX, SHADOW_OFFSET_MAX, STRIPE_GAP_MAX, STRIPE_LINE_MAX, STRIPE_LINE_MIN } from "@donkeycut/effects-kit";+import { bool, num, obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";import { SHAPE_LABELS, type ShapeKind } from "@/cut/lib/types";/** Every shape the panel's grid shows, in `SHAPE_LABELS` order. */@@ -17,7 +18,7 @@ export const ELEMENTS_TOOLS = [{name: "add_shape",description:- `Add a vector shape overlay (${SHAPE_KINDS.join(", ")}). Position is the shape center as frame fractions; w/h are frame fractions too (a line/arrow's h is its stroke thickness). Rotation gives lines and arrows their direction.`,+ `Add a vector shape overlay (${SHAPE_KINDS.join(", ")}). Position is the shape center as frame fractions; w/h are frame fractions too (a line/arrow's h is its stroke thickness). Rotation gives lines and arrows their direction. A doodle is animated graffiti in translucent paint: inside its box (default: the whole frame) it paints one hand-drawn mark — a dry-brush smear, a painted X, a burst, a tall painted bar, a brushed ring, a pair of fat loops — often with a thin hand line beside it (a tall oval, standing strands, a scribble), and deals new ones ${DOODLE_FPS} times a second, each in the fill or one of its inks (default: the teal, cream, red, orange and white graffiti set). Put it on a row under the titles to paint behind them, or between titles to sit over one line and under another.`,inputSchema: obj({shape: { type: "string", enum: [...SHAPE_KINDS], description: "Shape kind" },start: num("Start time s (default: playhead)"),@@ -26,11 +27,26 @@ export const ELEMENTS_TOOLS = [y: num("Center y 0..1 (default 0.5)"),w: num("Width, fraction of frame width"),h: num("Height, fraction of frame height (line/arrow: thickness)"),- fill: str("CSS color (default #FFFFFF)"),+ fill: str("CSS color (default #FFFFFF; a doodle's default is the graffiti set's teal)"),+ inks: {+ type: "array",+ items: { type: "string" },+ maxItems: DOODLE_INKS_MAX,+ description: `Doodle: up to ${DOODLE_INKS_MAX} more paints beside the fill; each mark takes one of them or the fill`,+ },fill_opacity: num("Fill opacity 0..1 (rect/ellipse)"),radius: num("Rect corner radius, px at 1080 short side"),stroke_color: str("Outline color (rect/ellipse)"),stroke_width: num("Outline width px at 1080 short side (0 removes it)"),+ pattern: { type: "string", enum: [...PATTERN_KINDS, "none"], description: 'Not line/arrow: the fill color laid as a pattern clipped to the outline — "stripes" are parallel lines with clear gaps (a loading bar); absent or "none" = solid' },+ pattern_width: num(`Stripe line thickness, px at 1080, ${STRIPE_LINE_MIN}..${STRIPE_LINE_MAX} (default 2)`),+ pattern_gap: num(`Clear space between stripes, px at 1080, 0..${STRIPE_GAP_MAX} (default 1)`),+ pattern_angle: num("Stripe direction in degrees clockwise, -90..90; 0 = vertical lines"),+ shadow: bool("Drop shadow on or off (default off); the shadow_* fields switch it on"),+ shadow_color: str("Shadow hex color, e.g. #00E5FF"),+ shadow_blur: num(`Shadow blur, px at 1080, 0..${SHADOW_BLUR_MAX}`),+ shadow_opacity: num("Shadow opacity 0..1"),+ shadow_y: num(`Shadow drop, px at 1080, -${SHADOW_OFFSET_MAX}..${SHADOW_OFFSET_MAX}; 0 with a bright color makes a glow`),rotation: num("Degrees clockwise, -180..180"),opacity: num("Whole-element opacity 0..1"),lane: num("Element row (0 = the front row, drawn over every higher row). Elements on one row never overlap — a title over a shape needs a lower row than the shape."),diff --git a/site/src/cut/components/ElementsPanel.tsx b/site/src/cut/components/ElementsPanel.tsxindex a1b552a1..470e4832 100644--- a/site/src/cut/components/ElementsPanel.tsx+++ b/site/src/cut/components/ElementsPanel.tsx@@ -1,6 +1,6 @@"use client";-import { lineLikeShape, shapePathD } from "@donkeycut/effects-kit";+import { dealsMarks, lineLikeShape, shapePathD } from "@donkeycut/effects-kit";import { SubTabs } from "@/cut/components/SubTabs";import { clearElementDrag, setElementDragData, setObjectDragImage } from "@/cut/lib/assetDrag";import { PICKED_RING, pickGridNav, useAssetPick } from "@/cut/lib/assetPick";@@ -138,7 +138,14 @@ function ShapeSwatch({ shape, className }: { shape: ShapeKind; className?: strin<polygon points="22,12 14,7.5 14,16.5" /></g>)}- {!lineLikeShape(shape) && shape !== "rect" && shape !== "ellipse" && (+ {shape === "doodle" && (+ <g fill="none" stroke="currentColor" strokeLinecap="round">+ <path d="M5 16 C8 6, 12 18, 15 9 S20 8, 19 14" strokeWidth="3" />+ <path d="M6 5 L10 10 M10 5 L6 10" strokeWidth="1.5" />+ <circle cx="17" cy="18" r="2.5" strokeWidth="1.5" />+ </g>+ )}+ {!lineLikeShape(shape) && !dealsMarks(shape) && shape !== "rect" && shape !== "ellipse" && (<path d={shapePathD(shape, 18, 18)} transform="translate(3 3)" />)}</svg>diff --git a/site/src/cut/components/FlashPulseRows.tsx b/site/src/cut/components/FlashPulseRows.tsxnew file mode 100644index 00000000..32cd8bdf--- /dev/null+++ b/site/src/cut/components/FlashPulseRows.tsx@@ -0,0 +1,88 @@+"use client";++import { FLASH_RHYTHMS, FLASH_RATE_MAX, FLASH_TONES, type FlashRhythm, type FlashPulse, type FlashTone } from "@donkeycut/effects-kit";+import { Row, useSliderCheckpoint } from "@/cut/components/panelBits";+import { parseNumberInput } from "@/cut/components/ScrubValue";+import { ValueSlider } from "@/cut/components/ValueSlider";+import { cn } from "@/lib/utils";++const FLASH_TONE_LABELS: Record<FlashTone, string> = { white: "White", black: "Black" };+const FLASH_RHYTHM_LABELS: Record<FlashRhythm, string> = { strobe: "Strobe", flicker: "Flicker" };++/** How a write lands: `draft` while a slider drags (one undo step, closed by+ * the checkpoint), `commit` for a single click. */+export type PulseWrite = "draft" | "commit";++/** A two-way segmented choice in the flash rows. */+function Segmented<T extends string>({ items, value, labels, onPick }: { items: readonly T[]; value: T; labels: Record<T, string>; onPick: (v: T) => void }) {+ return (+ <div className="flex w-36 rounded-lg border border-input p-0.5 text-[11.5px] font-medium">+ {items.map((c) => (+ <button+ key={c}+ type="button"+ aria-pressed={value === c}+ className={cn(+ "flex-1 rounded-md px-1 py-1 whitespace-nowrap transition-colors",+ value === c ? "bg-neutral-900 text-white" : "text-muted-foreground hover:text-foreground"+ )}+ onClick={() => onPick(c)}+ >+ {labels[c]}+ </button>+ ))}+ </div>+ );+}++/** A flash's tone and strobe: one pop, or a pulse held on and off at a rate+ * (0 = the single pop), each pulse at full strength or dealt its own. The+ * effect element and a clip's own flash share these rows; defaults write as+ * absence. */+export function FlashPulseRows({ pulse, write }: { pulse: FlashPulse; write: (patch: FlashPulse, how: PulseWrite) => void }) {+ const rateCk = useSliderCheckpoint();+ return (+ <>+ <Row label="Tone">+ <Segmented+ items={FLASH_TONES}+ value={pulse.tone ?? "white"}+ labels={FLASH_TONE_LABELS}+ onPick={(c) => write({ tone: c === "white" ? undefined : c }, "commit")}+ />+ </Row>+ <Row label="Strobe">+ <ValueSlider+ label="Strobe rate"+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={pulse.rate ?? 0}+ min={0}+ max={FLASH_RATE_MAX}+ step={0.5}+ format={(v) => (v ? `${v}/s` : "Off")}+ parse={(raw) => (raw.trim().toLowerCase() === "off" ? 0 : parseNumberInput(raw.replace(/\/s$/, "")))}+ onDraft={(v) => {+ rateCk.begin();+ write({ rate: v || undefined }, "draft");+ }}+ onCommit={(v) => {+ rateCk.begin();+ write({ rate: v || undefined }, "draft");+ rateCk.end();+ }}+ />+ </Row>+ {!!pulse.rate && (+ <Row label="Rhythm">+ <Segmented+ items={FLASH_RHYTHMS}+ value={pulse.rhythm ?? "strobe"}+ labels={FLASH_RHYTHM_LABELS}+ onPick={(c) => write({ rhythm: c === "strobe" ? undefined : c }, "commit")}+ />+ </Row>+ )}+ </>+ );+}diff --git a/site/src/cut/components/GroupPanel.tsx b/site/src/cut/components/GroupPanel.tsxindex cb380384..b5c02c32 100644--- a/site/src/cut/components/GroupPanel.tsx+++ b/site/src/cut/components/GroupPanel.tsx@@ -2,7 +2,7 @@import { useMemo, useState } from "react";import { AlignCenter, AlignHorizontalSpaceAround, AlignLeft, AlignRight, AlignVerticalSpaceAround, Bold, FlipHorizontal2, FlipVertical2, Italic, StretchHorizontal, Type } from "lucide-react";-import { hasOverlayKeys, hasSpeedCurve, lineLikeShape, poseAt, retimeOf, ZOOM_LEVELS, ZOOM_RAMP_MAX, zoomRampOf } from "@donkeycut/effects-kit";+import { hasOverlayKeys, hasSpeedCurve, LETTER_SPACING_MIN, lineLikeShape, poseAt, retimeOf, ZOOM_LEVELS, ZOOM_RAMP_MAX, zoomRampOf } from "@donkeycut/effects-kit";import { ColorField } from "@/cut/components/ColorField";import { FontPicker } from "@/cut/components/FontPicker";import { LETTER_SPACINGS, LINE_HEIGHTS, SoundQualityPanel, SoundQualityRow, TEXT_WIDTHS, StylePresetsRow } from "@/cut/components/Inspector";@@ -353,7 +353,7 @@ function TextRows({ texts }: { texts: TextOverlay[] }) {const write = useGroupWrite();const font = sharedValue(texts.map((t) => t.font));const size = sharedNumber(texts.map((t) => t.size));- const bold = sharedValue(texts.map((t) => t.weight === 700));+ const bold = sharedValue(texts.map((t) => t.weight >= 700));const italic = sharedValue(texts.map((t) => !!t.italic));const align = sharedValue(texts.map((t) => t.align ?? "center"));const lineHeight = sharedNumber(texts.map((t) => t.lineHeight ?? 1.25));@@ -465,7 +465,7 @@ function TextRows({ texts }: { texts: TextOverlay[] }) {icon={<AlignHorizontalSpaceAround />}value={letterSpacing.value}mixed={letterSpacing.mixed}- min={-5}+ min={LETTER_SPACING_MIN * 100}max={30}step={1}snap={[0]}diff --git a/site/src/cut/components/Inspector.tools.ts b/site/src/cut/components/Inspector.tools.tsindex 67bf64a3..7c007b9a 100644--- a/site/src/cut/components/Inspector.tools.ts+++ b/site/src/cut/components/Inspector.tools.ts@@ -34,6 +34,21 @@ import {MASK_RADIUS_MAX,PEN_MIN_POINTS,CLIP_EFFECT_IDS,+ FLASH_RHYTHMS,+ FLASH_RATE_MAX,+ FLASH_RATE_MIN,+ FLASH_TONES,+ TEXT_WEIGHTS,+ TEXT_TEXTURES,+ DOODLE_INKS_MAX,+ LETTER_SPACING_MAX,+ LETTER_SPACING_MIN,+ PATTERN_KINDS,+ SHADOW_BLUR_MAX,+ SHADOW_OFFSET_MAX,+ STRIPE_GAP_MAX,+ STRIPE_LINE_MAX,+ STRIPE_LINE_MIN,} from "@donkeycut/effects-kit";import { bool, ids, num, obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";import { builtinLutCatalogText } from "@/cut/lib/builtinLuts";@@ -105,7 +120,7 @@ export const INSPECTOR_TOOLS = [{name: "update_overlay",description:- "Update any overlay element — title, shape, or sticker — by id (from the selection or state). Titles take text/size/font/weight/color/shadow/plate; shapes take w/h/fill/fill_opacity/radius/stroke; stickers take w/h. Every kind takes name, timing, position, rotation, opacity, blur, motion blur, hidden. This is the tool for 'make this text better' requests too. Pass ids to land one change on several elements at once (a group, every title in a run): fields a kind lacks are skipped on that element, and the whole write is one undo step.",+ "Update any overlay element — title, shape, or sticker — by id (from the selection or state). Titles take text/size/font/weight/color/shadow/plate; shapes take w/h/fill/inks/fill_opacity/radius/stroke/pattern/shadow; stickers take w/h. A shadow on a title or shape takes color, blur, opacity and offset; a colored shadow at shadow_y 0 is a glow. Every kind takes name, timing, position, rotation, opacity, blur, motion blur, hidden. This is the tool for 'make this text better' requests too. Pass ids to land one change on several elements at once (a group, every title in a run): fields a kind lacks are skipped on that element, and the whole write is one undo step.",inputSchema: obj({id: str("Overlay element id"),ids: { type: "array", items: { type: "string" }, description: "Several element ids to change together (instead of id)" },@@ -118,19 +133,38 @@ export const INSPECTOR_TOOLS = [size: num("Font size px at 1080w (titles)"),color: str("CSS text color (titles)"),font: str("Font id (titles; see the graphics skill)"),- weight: { type: "number", enum: [400, 700], description: "Font weight (titles)" },+ weight: { type: "number", enum: [...TEXT_WEIGHTS], description: "Font weight (titles): 400 regular, 700 bold, 800 extra bold, 900 black" },italic: bool("Italic (titles)"),align: { type: "string", enum: ["left", "center", "right"], description: "Multi-line alignment (titles)" },- letter_spacing: num("Tracking in em (titles; 0 = normal)"),+ letter_spacing: num(`Tracking in em, ${LETTER_SPACING_MIN}..${LETTER_SPACING_MAX} (titles; 0 = normal, negative sets display type tight)`),line_height: num("Line height multiplier (titles; default 1.25)"),wrap_width: num("Text box width as a frame-width fraction, 0.01..2; 0 restores auto width. Text wraps and height follows content."),- shadow: bool("Drop shadow (titles)"),+ shadow: bool("Drop shadow on or off (titles and shapes); the shadow_* fields switch it on"),+ shadow_color: str("Shadow hex color, e.g. #00E5FF (titles and shapes)"),+ shadow_blur: num(`Shadow blur, px at 1080, 0..${SHADOW_BLUR_MAX} (titles and shapes)`),+ shadow_opacity: num("Shadow opacity 0..1 (titles and shapes)"),+ shadow_y: num(`Shadow drop, px at 1080, -${SHADOW_OFFSET_MAX}..${SHADOW_OFFSET_MAX}; 0 with a bright color makes a glow (titles and shapes)`),plate: bool("Backdrop plate (titles)"),+ texture: {+ type: "string",+ enum: [...TEXT_TEXTURES, "none"],+ description: 'Titles: grain inside the letters: "stipple" breaks each letter into spray-paint specks along its left edge, solid by its middle (the gritty teaser title); "none" makes the type solid again',+ },w: num("Width, fraction of frame width (shapes/stickers)"),h: num("Height, fraction of frame height (shapes/stickers; a sticker's 0 returns it to the source's aspect)"),fill: str("Fill color (shapes)"),+ inks: {+ type: "array",+ items: { type: "string" },+ maxItems: DOODLE_INKS_MAX,+ description: `Doodle shapes: up to ${DOODLE_INKS_MAX} more paints beside the fill; each mark takes one of them or the fill. The list replaces the old one; [] leaves the fill alone`,+ },fill_opacity: num("Fill opacity 0..1 (rect/ellipse)"),radius: num("Rect corner radius, px at 1080 short side"),+ pattern: { type: "string", enum: [...PATTERN_KINDS, "none"], description: 'Shapes (not line/arrow): the fill color laid as a pattern clipped to the outline — "stripes" are parallel lines with clear gaps (a loading bar); "none" makes the fill solid' },+ pattern_width: num(`Stripe line thickness, px at 1080, ${STRIPE_LINE_MIN}..${STRIPE_LINE_MAX} (default 2)`),+ pattern_gap: num(`Clear space between stripes, px at 1080, 0..${STRIPE_GAP_MAX} (default 1)`),+ pattern_angle: num("Stripe direction in degrees clockwise, -90..90; 0 = vertical lines"),stroke_color: str("Outline color — text or shape"),stroke_width: num("Outline width: em for titles (0..0.15), px at 1080 for shapes; 0 removes it"),rotation: num("Degrees clockwise, -180..180 (0 clears)"),@@ -205,13 +239,13 @@ export const INSPECTOR_TOOLS = [{name: "set_clip_keyframes",description:- "Give a video clip (any track) a keyframed pose track: each key is a whole pose at a time measured in seconds from the clip's own start — center position in frame fractions, scale multiplier on its fitted size, rotation, opacity — moving linearly between keys and holding outside them. Omitted fields on a key take the clip's pose at that moment. Pass an empty list to clear the track and return the clip to its region. Use for pans, push-ins, picture-in-picture flights, spins, and fades on video itself.",+ `Give a video clip (any track) a keyframed pose track: each key is a whole pose at a time measured in seconds from the clip's own start — center position in frame fractions, scale multiplier on its fitted size, rotation, opacity, blur — moving linearly between keys and holding outside them. Omitted fields on a key take the clip's pose at that moment. Pass an empty list to clear the track and return the clip to its region. Use for pans, push-ins, picture-in-picture flights, spins, and fades on video itself. blur softens the picture (px at the 1080 short side, 0..${ELEMENT_BLUR_MAX}); motion_blur streaks the picture along its keyed movement, so a panel that flies in with blur keyed from high to 0 and a shutter on lands as a smear that settles sharp.`,inputSchema: obj({clipId: str("Video clip id"),keys: {type: "array",- description: "Keys in any order; two at the same time collapse to one",+ description: "Keys in any order; two at the same time collapse to one; omit to keep the track and change only the motion blur",items: obj({t: num("Seconds from the clip's start"),@@ -220,12 +254,15 @@ export const INSPECTOR_TOOLS = [scale: num("Size multiplier, 1 = the clip's fitted size (0.1..4)"),rotation: num("Degrees clockwise, -180..180"),opacity: num("0..1"),+ blur: num(`Blur, px at the 1080 short side, 0..${ELEMENT_BLUR_MAX}`),},["t"]),},+ motion_blur: bool("Streak the clip along its keyed movement while it moves (true starts at the default shutter); false switches it off; omitted keeps it"),+ shutter: num("Motion blur shutter, 0.05..1 of a 30fps frame (0.5 is the usual half-open shutter); setting it switches motion blur on"),},- ["clipId", "keys"]+ ["clipId"]),},{@@ -365,7 +402,7 @@ export const INSPECTOR_TOOLS = [{name: "set_clip_effects",description:- `Set the effects a video clip wears over its own picture (${CLIP_EFFECT_IDS.join(", ")}). They treat that clip alone — inside its mask, under its pose — for its whole length, on the clip's own clock: a masked copy over the shot wearing negative and huecycle shows a color-cycling negative window while the shot around it plays untouched. add_effect places an element that treats the whole frame under its window. Pass the full list in order; [] removes them all. amount 0.05..1 (default 0.5) is the strength; for huecycle it is how fast the hue turns (0.5 is a turn every 1.25s); negative has none.`,+ `Set the effects a video clip wears over its own picture (${CLIP_EFFECT_IDS.join(", ")}). They treat that clip alone — inside its mask, under its pose — for its whole length, on the clip's own clock: a masked copy over the shot wearing negative and huecycle shows a color-cycling negative window while the shot around it plays untouched. add_effect places an element that treats the whole frame under its window. Pass the full list in order; [] removes them all. amount 0.05..1 (default 0.5) is the strength; for huecycle it is how fast the hue turns (0.5 is a turn every 1.25s); negative has none. A flash takes add_effect's tone, rate and rhythm on the clip's own clock: a white flicker at rate 12–15, amount 0.15–0.25, pops a masked shot's exposure unevenly while the black around the mask stays black.`,inputSchema: obj({clipId: str("Video clip id"),effects: {@@ -374,6 +411,9 @@ export const INSPECTOR_TOOLS = [items: obj({effect: { type: "string", enum: [...CLIP_EFFECT_IDS], description: "Effect id" },amount: num("Strength 0.05..1 (default 0.5)"),+ tone: { type: "string", enum: [...FLASH_TONES], description: 'flash only: "white" (default) or "black"' },+ rate: num(`flash only: strobe pulses a second, ${FLASH_RATE_MIN}..${FLASH_RATE_MAX}; omit for one pop at the clip's start`),+ rhythm: { type: "string", enum: [...FLASH_RHYTHMS], description: 'flash with a rate only: "strobe" (default) or "flicker", each pulse its own strength, some skipped' },}, ["effect"]),},}, ["clipId", "effects"]),diff --git a/site/src/cut/components/Inspector.tsx b/site/src/cut/components/Inspector.tsxindex 9410ced6..b126c04c 100644--- a/site/src/cut/components/Inspector.tsx+++ b/site/src/cut/components/Inspector.tsx@@ -2,7 +2,7 @@import { useQueryClient } from "@tanstack/react-query";import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";-import { AlignCenter, AlignHorizontalSpaceAround, AlignLeft, AlignRight, AlignVerticalSpaceAround, Bold, ChevronLeft, ChevronRight, Diamond, FlipHorizontal2, FlipVertical2, Frame, House, Italic, Link2, Link2Off, Loader2, type LucideIcon, Palette, PanelRightClose, PanelRightOpen, PanelTopClose, PenTool, Scissors, Smile, Sparkles, StretchHorizontal, Trash2, Type, User, Volume2 } from "lucide-react";+import { AlignCenter, AlignHorizontalSpaceAround, AlignLeft, AlignRight, AlignVerticalSpaceAround, ChevronLeft, ChevronRight, Diamond, FlipHorizontal2, FlipVertical2, Frame, House, Italic, Link2, Link2Off, Loader2, type LucideIcon, Palette, PanelRightClose, PanelRightOpen, PanelTopClose, PenTool, Plus, Scissors, Smile, Sparkles, StretchHorizontal, Trash2, Type, User, Volume2, X } from "lucide-react";import { Button } from "@/components/ui/button";import { EmojiPicker } from "@/cut/components/EmojiPicker";import { FontPicker } from "@/cut/components/FontPicker";@@ -33,18 +33,26 @@ import {hasOverlayKeys,KEY_EPSILON,keyIndexAt,+ dealsMarks,+ DOODLE_INKS,+ DOODLE_INKS_MAX,+ LETTER_SPACING_MIN,lineLikeShape,maskFrameAt,poseAt,+ keyPoseAt,+ TILT_MAX,type Mask,type MaskKey,type MaskKind,type OverlayKey,OVERLAY_ANIM_DEFAULT_SECONDS,- OVERLAY_ANIM_MAX_SECONDS,+ edgeMaxSeconds,OVERLAY_ANIM_MIN_SECONDS,CARET_BLINKS_MAX,edgeMotion,+ edgeStagger,+ EDGE_STAGGER_MAX,keepCaret,OVERLAY_HIT_DEFAULT_SECONDS,OVERLAY_HIT_MAX_SECONDS,@@ -89,15 +97,33 @@ import {restingMaskFrame,AMOUNTLESS_EFFECTS,displayWords,+ CYCLE_SCALE_MAX,+ CYCLE_SCALE_MIN,+ FONT_CYCLE_FACES_MAX,+ FONT_CYCLE_RATE,+ FONT_CYCLE_RATE_MAX,+ FONT_CYCLE_RATE_MIN,+ TEXT_WEIGHTS,+ LEAK_COURSES,+ type LeakCourse,+ GLITCH_KINDS,+ type GlitchKind,+ TEXT_TEXTURES,+ type TextTexture,+ type CycleFace,+ type OverlayFontCycle,+ type TextWeight,} from "@donkeycut/effects-kit";+import { SHADOW, SHADOW_BLUR_MAX, SHADOW_OFFSET_MAX, STRIPE_GAP_MAX, STRIPE_LINE_MAX, STRIPE_LINE_MIN, STRIPES_DEFAULT, type ShadowSpec, type StripesPattern } from "@donkeycut/effects-kit";import { BLOCK_COLOR } from "@/cut/lib/blockSource";import { bindRecording, unbindRecording } from "@/cut/lib/soundBind";import { SPLIT_EDIT_MAX_S } from "@/cut/lib/soundSource";import { clipLen, clipWindow, maxClipFade, useEditor, type EditorState } from "@/cut/lib/store";import { PANEL_GLOBAL, usePanelState, useRememberedScroll } from "@/cut/lib/panelState";import { playheadAt, usePreviewTime } from "@/cut/lib/playhead";-import { CLIP_MAX_ZOOM, clipCovers, clipKeyed, clipPoseAt, clipZoom, contentRect } from "@/cut/lib/types";+import { allFonts, CLIP_MAX_ZOOM, clipCovers, clipKeyed, clipPoseAt, clipZoom, contentRect } from "@/cut/lib/types";import { AnimationTiles } from "@/cut/components/AnimationTiles";+import { FlashPulseRows } from "@/cut/components/FlashPulseRows";import { ColorField } from "@/cut/components/ColorField";import { wordTimesFor } from "@/cut/lib/textWords";import { NumberField } from "@/cut/components/NumberField";@@ -178,7 +204,7 @@ import { ScrollArea } from "@/components/ui/scroll-area";import { Field, ResetButton, Row, Section, SegGroup, SegToggle, Tip, useSliderCheckpoint, Value } from "@/cut/components/panelBits";import { ColorPanel } from "@/cut/components/ColorPanel";import { GroupPanel } from "@/cut/components/GroupPanel";-import { ElementMotionRows } from "@/cut/components/MotionControls";+import { ClipMotionRows, ElementMotionRows } from "@/cut/components/MotionControls";import { RemovalPanel } from "@/cut/components/RemovalPanel";import { CameraCardSection } from "@/cut/components/CameraCardSection";import { useMatteBakes } from "@/cut/lib/removal/bakeJobs";@@ -2084,7 +2110,6 @@ function TextPanel({ overlay: o }: { overlay: TextOverlay }) {const lineHeightCk = useSliderCheckpoint();const widthCk = useSliderCheckpoint();const strokeCk = useSliderCheckpoint();- const shadowCk = useSliderCheckpoint();const taRef = useRef<HTMLTextAreaElement>(null);// The saved-style previews below are set in whatever font each style names,// so they re-render when a family finishes registering.@@ -2157,19 +2182,16 @@ function TextPanel({ overlay: o }: { overlay: TextOverlay }) {/></div><StylePresetsRow overlay={o} />- <FontPicker- value={o.font}- onChange={(v) => update(o.id, { font: v as TextOverlay["font"] })}- />+ <div className="flex items-center gap-2">+ <FontPicker+ className="min-w-0 flex-1"+ value={o.font}+ onChange={(v) => update(o.id, { font: v as TextOverlay["font"] })}+ />+ <WeightSelect value={o.weight} onChange={(weight) => update(o.id, { weight })} />+ </div><div className="mt-1 flex items-center gap-2"><SegGroup>- <SegToggle- label="Bold"- active={o.weight === 700}- onClick={() => update(o.id, { weight: o.weight === 700 ? 400 : 700 })}- >- <Bold />- </SegToggle><SegTogglelabel="Italic"active={!!o.italic}@@ -2251,7 +2273,7 @@ function TextPanel({ overlay: o }: { overlay: TextOverlay }) {label="Letter spacing"icon={<AlignHorizontalSpaceAround />}value={(o.letterSpacing ?? 0) * 100}- min={-5}+ min={LETTER_SPACING_MIN * 100}max={30}step={1}snap={[0]}@@ -2283,6 +2305,7 @@ function TextPanel({ overlay: o }: { overlay: TextOverlay }) {onCommit={(v) => { widthCk.begin(); useEditor.getState().updateOverlayTransient(o.id, { wrapWidth: v > 0 ? v / 100 : undefined }); widthCk.end(); }}/></Field>+ <TextTextureRow overlay={o} /><Row label="Color"><ColorFieldvalue={o.color}@@ -2345,75 +2368,7 @@ function TextPanel({ overlay: o }: { overlay: TextOverlay }) {</>)}</Section>- <Section- title="Shadow"- enabled={!!o.shadow}- onEnabledChange={(v) => update(o.id, { shadow: v })}- >- <>- <Row label="Color">- <ColorField- value={(typeof o.shadow === "object" ? o.shadow.color : undefined) ?? "#000000"}- label="Shadow color"- onBegin={() => useEditor.getState().pushHistory()}- onLive={(c) =>- useEditor.getState().updateOverlayTransient(o.id, {- shadow: { ...(typeof o.shadow === "object" ? o.shadow : {}), color: c },- })- }- onCommit={(c) =>- update(o.id, {- shadow: { ...(typeof o.shadow === "object" ? o.shadow : {}), color: c },- })- }- opacity={{- label: "Shadow opacity",- value: (typeof o.shadow === "object" ? o.shadow.opacity : undefined) ?? 0.65,- onDraft: (v) => {- shadowCk.begin();- useEditor.getState().updateOverlayTransient(o.id, {- shadow: { ...(typeof o.shadow === "object" ? o.shadow : {}), opacity: v },- });- },- onCommit: (v) => {- shadowCk.begin();- useEditor.getState().updateOverlayTransient(o.id, {- shadow: { ...(typeof o.shadow === "object" ? o.shadow : {}), opacity: v },- });- shadowCk.end();- },- }}- />- </Row>- <Row label="Blur">- <ValueSlider- label="Shadow blur"- sliderClassName="data-horizontal:w-24"- valueClassName="w-9 text-muted-foreground"- value={(typeof o.shadow === "object" ? o.shadow.blur : undefined) ?? 14}- min={0}- max={60}- step={1}- snap={[14]}- format={(v) => String(Math.round(v))}- parse={parseNumberInput}- onDraft={(v) => {- shadowCk.begin();- useEditor.getState().updateOverlayTransient(o.id, {- shadow: { ...(typeof o.shadow === "object" ? o.shadow : {}), blur: v },- });- }}- onCommit={(v) => {- shadowCk.begin();- useEditor.getState().updateOverlayTransient(o.id, {- shadow: { ...(typeof o.shadow === "object" ? o.shadow : {}), blur: v },- });- shadowCk.end();- }}- />- </Row>- </>- </Section>+ <ShadowSection id={o.id} shadow={o.shadow} onToggle={(v) => update(o.id, { shadow: v })} /><Sectiontitle="Backdrop"enabled={o.plate}@@ -2603,6 +2558,7 @@ function writeOverlayAnim(o: Overlay, anim: OverlayAnim, patch: Partial<OverlayAif (!next.move) delete next.move;if (!next.words) delete next.words;if (!next.hit) delete next.hit;+ if (!next.fonts) delete next.fonts;const value = hasOverlayAnim(next) ? next : undefined;const st = useEditor.getState();// A multi-selection names its targets; a lone grouped element stamps its group.@@ -2920,8 +2876,204 @@ function HitSettings({ overlay: o, peers }: { overlay: Overlay; peers?: readonly);}+/** What each text weight is called in the weight menu. */+const WEIGHT_LABELS: Record<TextWeight, string> = {+ 400: "Regular",+ 700: "Bold",+ 800: "Extra bold",+ 900: "Black",+};++/** A title's weight, from regular to black. */+function WeightSelect({ value, onChange }: { value: TextWeight; onChange: (w: TextWeight) => void }) {+ return (+ <Select+ value={String(value)}+ items={Object.fromEntries(TEXT_WEIGHTS.map((w) => [String(w), WEIGHT_LABELS[w]]))}+ onValueChange={(v) => onChange(Number(v) as TextWeight)}+ >+ <SelectTrigger aria-label="Weight" size="sm" className="w-24 text-[12px]">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ {TEXT_WEIGHTS.map((w) => (+ <SelectItem key={w} value={String(w)} className="text-[12px]">+ {WEIGHT_LABELS[w]}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ );+}++/** A face the cycle does not show yet, for the Add face button: the next+ * font in the menu after the last face, so each add brings a new look. */+function nextCycleFont(o: TextOverlay, faces: CycleFace[]): string {+ const ids = allFonts().map((f) => f.id);+ const used = new Set([o.font, ...faces.map((f) => f.font)]);+ const from = ids.indexOf(faces.at(-1)?.font ?? o.font);+ for (let i = 1; i <= ids.length; i++) {+ const id = ids[(from + i) % ids.length];+ if (!used.has(id)) return id;+ }+ return o.font;+}++/** The font cycle's faces, in the order the title walks them: each one's+ * font, weight, size and tracking, and a button to add another. The first+ * face added starts a cycle across the title's whole length. */+function FontCycleFaces({ overlay: o, peers }: { overlay: TextOverlay; peers?: readonly Overlay[] }) {+ const ck = useSliderCheckpoint();+ const anim = o.anim ?? {};+ const fontCycle = anim.fonts;+ const faces = fontCycle?.faces ?? [];+ const dur = Math.max(0.2, Math.min(...(peers ?? [o]).map((el) => el.end - el.start)));++ // Every edit writes the whole face list; none left clears the cycle.+ const write = (next: CycleFace[]) => {+ const fonts: OverlayFontCycle | undefined = next.length+ ? (fontCycle ? { ...fontCycle, faces: next } : { faces: next, at: 0, seconds: dur, rate: FONT_CYCLE_RATE })+ : undefined;+ writeOverlayAnim(o, anim, { fonts }, peers);+ };+ const commit = (next: CycleFace[]) => {+ useEditor.getState().pushHistory();+ write(next);+ };+ const setFace = (i: number, patch: Partial<CycleFace>) => faces.map((f, j) => (j === i ? { ...f, ...patch } : f));++ return (+ <div className="flex flex-col gap-2">+ {faces.map((face, i) => (+ <div key={i} className={cn("flex flex-col gap-1.5", i > 0 && "border-t border-border pt-2")}>+ <div className="flex items-center gap-1.5">+ <FontPicker className="min-w-0 flex-1" value={face.font} onChange={(font) => commit(setFace(i, { font }))} />+ <Button+ variant="ghost"+ size="icon-sm"+ aria-label="Remove face"+ onClick={() => commit(faces.filter((_, j) => j !== i))}+ >+ <X />+ </Button>+ </div>+ <Row label="Weight">+ <WeightSelect value={face.weight ?? o.weight} onChange={(weight) => commit(setFace(i, { weight }))} />+ </Row>+ <Row label="Size">+ <ValueSlider+ label="Size"+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={face.scale ?? 1}+ min={CYCLE_SCALE_MIN}+ max={CYCLE_SCALE_MAX}+ step={0.05}+ snap={[1]}+ format={(v) => `${v.toFixed(2)}×`}+ parse={parseNumberInput}+ onDraft={(scale) => {+ ck.begin();+ write(setFace(i, { scale }));+ }}+ onCommit={(scale) => {+ write(setFace(i, { scale }));+ ck.end();+ }}+ />+ </Row>+ <Row label="Tracking">+ <ValueSlider+ label="Tracking"+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={(face.tracking ?? o.letterSpacing ?? 0) * 100}+ min={-5}+ max={50}+ step={1}+ format={(v) => `${Math.round(v)}%`}+ parse={(raw) => parseNumberInput(raw.replace(/%$/, ""))}+ onDraft={(v) => {+ ck.begin();+ write(setFace(i, { tracking: v / 100 }));+ }}+ onCommit={(v) => {+ write(setFace(i, { tracking: v / 100 }));+ ck.end();+ }}+ />+ </Row>+ </div>+ ))}+ <Button+ variant="secondary"+ size="sm"+ disabled={faces.length >= FONT_CYCLE_FACES_MAX}+ onClick={() => commit([...faces, { font: nextCycleFont(o, faces) }])}+ >+ <Plus />+ Add face+ </Button>+ </div>+ );+}++/** Where the font cycle starts inside the title, how long it runs, and how+ * many times a second the face changes. */+function FontCycleSettings({ overlay: o, peers }: { overlay: Overlay; peers?: readonly Overlay[] }) {+ const ck = useSliderCheckpoint();+ const anim = o.anim ?? {};+ const cycle = anim.fonts;+ const dur = Math.max(0.2, Math.min(...(peers ?? [o]).map((el) => el.end - el.start)));+ const fontCycleAt = cycle?.at ?? 0;+ const fontCycleSeconds = cycle?.seconds ?? dur;+ const fontCycleRate = cycle?.rate ?? FONT_CYCLE_RATE;+ const write = (patch: Partial<OverlayFontCycle>) => {+ if (!cycle) return;+ ck.begin();+ writeOverlayAnim(o, anim, { fonts: { ...cycle, ...patch } }, peers);+ };+ const slider = (+ label: string,+ value: number,+ min: number,+ max: number,+ step: number,+ format: (v: number) => string,+ key: keyof OverlayFontCycle+ ) => (+ <Row label={label}>+ <ValueSlider+ label={label}+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={value}+ min={min}+ max={max}+ step={step}+ format={format}+ parse={parseNumberInput}+ disabled={!cycle}+ onDraft={(v) => write({ [key]: v })}+ onCommit={(v) => {+ write({ [key]: v });+ ck.end();+ }}+ />+ </Row>+ );+ const secs = (v: number) => `${v.toFixed(2)}s`;+ return (+ <>+ {slider("At", fontCycleAt, 0, Math.max(0, dur - 0.05), 0.05, secs, "at")}+ {slider("Duration", fontCycleSeconds, 0.05, Math.max(0.05, dur - fontCycleAt), 0.05, secs, "seconds")}+ {slider("Rate", fontCycleRate, FONT_CYCLE_RATE_MIN, FONT_CYCLE_RATE_MAX, 1, (v) => `${Math.round(v)}/s`, "rate")}+ </>+ );+}+/** The slots the picker fills, in tab order. */-type AnimSlot = "in" | "out" | "loop" | "move" | "words" | "hit";+type AnimSlot = "in" | "out" | "loop" | "move" | "words" | "fonts" | "hit";/** The pill of slot tabs over an animation grid. A slot that is already set* reads darker, so switching tabs is not the only way to see what an item is@@ -2944,7 +3096,7 @@ function SlotTabs<T extends string>({key={id}type="button"className={cn(- "flex-1 rounded-md px-1.5 py-1 capitalize transition-colors",+ "flex-1 rounded-md px-1 py-1 capitalize transition-colors",slot === id ? "bg-neutral-900 text-white" : "text-muted-foreground hover:text-foreground",isSet(id) && slot !== id && "text-foreground")}@@ -3055,11 +3207,12 @@ export function AnimationPanel({ overlay: o, peers }: { overlay: Overlay; peers?// selecting one hands the picker back to the entrance.// Word emphasis needs words on every target.const tabs: AnimSlot[] = (peers ?? [o]).every(isTextOverlay)- ? ["in", "out", "loop", "move", "words", "hit"]+ ? ["in", "out", "loop", "move", "words", "fonts", "hit"]: ["in", "out", "loop", "move", "hit"];const slot = tabs.includes(picked) ? picked : "in";const tilesScroll = useRememberedScroll(o.id, `anim:${slot}`);- const active = slot === "move" || slot === "words" || slot === "hit" ? undefined : anim[slot];+ const active =+ slot === "move" || slot === "words" || slot === "fonts" || slot === "hit" ? undefined : anim[slot];const seconds =slot === "in" || slot === "out"? anim[slot]?.seconds@@ -3069,6 +3222,8 @@ export function AnimationPanel({ overlay: o, peers }: { overlay: Overlay; peers?const activeMove = anim.move;const hitStyle = anim.hit?.style;const pick = (style: string | null) => {+ // The fonts tab edits its faces in place; it has no tiles to pick.+ if (slot === "fonts") return;if (slot === "hit") {if (!style && !anim.hit) return;useEditor.getState().pushHistory();@@ -3131,7 +3286,8 @@ export function AnimationPanel({ overlay: o, peers }: { overlay: Overlay; peers?: {[slot]: {style: style as OverlayAnimStyle,- seconds: seconds ?? OVERLAY_ANIM_DEFAULT_SECONDS,+ // A long count's length shrinks to the new style's cap.+ seconds: Math.min(edgeMaxSeconds(style), seconds ?? OVERLAY_ANIM_DEFAULT_SECONDS),...(caret ? { caret } : {}),},};@@ -3169,6 +3325,9 @@ export function AnimationPanel({ overlay: o, peers }: { overlay: Overlay; peers?viewportClassName="overscroll-contain"contentClassName="flex flex-col gap-1 px-3.5 pt-1 pb-2">+ {slot === "fonts" && isTextOverlay(o) ? (+ <FontCycleFaces overlay={o} peers={peers} />+ ) : slot === "fonts" ? null : (<AnimationTilesslot={slot}// A slot carrying its own preset is playing something no tile here@@ -3195,6 +3354,7 @@ export function AnimationPanel({ overlay: o, peers }: { overlay: Overlay; peers?accentDim={anim.words?.dim}onPick={pick}/>+ )}</ScrollArea><AnimationToolbar overlay={o} slot={slot} peers={peers} /></div>@@ -3224,6 +3384,8 @@ function AnimationToolbar({ overlay: o, slot, peers }: { overlay: Overlay; slot:if (slot === "hit") return bar(<HitSettings overlay={o} peers={peers} />);+ if (slot === "fonts") return bar(<FontCycleSettings overlay={o} peers={peers} />);+if (slot === "loop") {const write = (speed: number) => {if (!anim.loop) return;@@ -3261,6 +3423,15 @@ function AnimationToolbar({ overlay: o, slot, peers }: { overlay: Overlay; slot:};// A typing entrance on a title carries its typing bar's settings too.const types = slot === "in" && isTextOverlay(o) && !!edgeMotion(edge)?.animate.typed;+ // A per-letter style on a title carries its letter hand-off.+ const stagger = isTextOverlay(o) ? edgeStagger(edge) : undefined;+ const writeStagger = (v: number) => {+ if (!edge) {+ return;+ }+ ck.begin();+ writeOverlayAnim(o, anim, { [slot]: { ...edge, stagger: v } }, peers);+ };return bar(<><Row label="Duration">@@ -3270,7 +3441,7 @@ function AnimationToolbar({ overlay: o, slot, peers }: { overlay: Overlay; slot:valueClassName="w-9 text-muted-foreground"value={edge?.seconds ?? OVERLAY_ANIM_DEFAULT_SECONDS}min={OVERLAY_ANIM_MIN_SECONDS}- max={Math.min(OVERLAY_ANIM_MAX_SECONDS, dur)}+ max={Math.min(edgeMaxSeconds(edge?.style), dur)}step={0.05}snap={[OVERLAY_ANIM_DEFAULT_SECONDS]}format={(v) => `${v.toFixed(2)}s`}@@ -3283,6 +3454,27 @@ function AnimationToolbar({ overlay: o, slot, peers }: { overlay: Overlay; slot:}}/></Row>+ {stagger !== undefined && (+ <Row label="Stagger">+ <ValueSlider+ label="Stagger"+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={stagger}+ min={0}+ max={EDGE_STAGGER_MAX}+ step={0.01}+ snap={[edgeMotion(edge)?.selector?.spread ?? 0]}+ format={(v) => String(Math.round(v * 100))}+ parse={parsePercentInput}+ onDraft={writeStagger}+ onCommit={(v) => {+ writeStagger(v);+ ck.end();+ }}+ />+ </Row>+ )}{types && <CaretSettings overlay={o} peers={peers} />}</>);@@ -3354,6 +3546,59 @@ function PositionRow({);}+/** Two numbers a key holds per axis — the stretch of each side, the tilt+ * about each axis — scrubbed side by side like Position. */+function KeyAxisRow({+ label,+ axes,+ min,+ max,+ snap,+ format,+ parse,+ ck,+}: {+ label: string;+ axes: { name: string; value: number; set: (v: number) => void }[];+ min: number;+ max: number;+ snap: number[];+ format: (v: number) => string;+ parse: (s: string) => number | null;+ ck: { begin: () => void; end: () => void };+}) {+ return (+ <Row label={label}>+ {axes.map((axis) => (+ <span key={axis.name} className="flex items-center gap-1">+ <span className="text-[11px] text-muted-foreground/70 uppercase">{axis.name}</span>+ <ScrubValue+ label={`${label} ${axis.name}`}+ className="w-10 text-muted-foreground"+ value={axis.value}+ min={min}+ max={max}+ step={(max - min) / 400}+ keyStep={(max - min) / 80}+ snap={snap}+ format={format}+ parse={parse}+ onScrub={(v) => {+ ck.begin();+ axis.set(v);+ }}+ onCommit={(v) => {+ ck.begin();+ axis.set(v);+ ck.end();+ }}+ />+ </span>+ ))}+ </Row>+ );+}+/** One caption's own settings: where it sits, and which of its words are set* apart in the track's emphasis style (the Subtitles panel styles it). */function CuePanel({ cue }: { cue: SubtitleCue }) {@@ -3418,6 +3663,9 @@ function TransformRows({ overlay: o }: { overlay: Overlay }) {const keyed = hasOverlayKeys(o);const tLocal = localTimeOf(o, now);const pose = poseAt(o, tLocal);+ const keyPose = keyPoseAt(o, tLocal);+ const stretchCk = useSliderCheckpoint();+ const tiltCk = useSliderCheckpoint();const setKey = (patch: Partial<Omit<OverlayKey, "t">>) =>useEditor.getState().setOverlayKey(o.id, tLocal, patch, { transient: true });const setRotation = (v: number) => {@@ -3448,7 +3696,7 @@ function TransformRows({ overlay: o }: { overlay: Overlay }) {label="Scale"sliderClassName="data-horizontal:w-20"valueClassName="w-12 text-muted-foreground"- value={pose.scale}+ value={keyPose.scale}min={0.1}max={4}// The slider stays fine-grained where most work happens; the@@ -3473,6 +3721,36 @@ function TransformRows({ overlay: o }: { overlay: Overlay }) {/></Row>)}+ {keyed && (+ <KeyAxisRow+ label="Stretch"+ axes={[+ { name: "W", value: keyPose.scaleX ?? 1, set: (v) => setKey({ scaleX: v }) },+ { name: "H", value: keyPose.scaleY ?? 1, set: (v) => setKey({ scaleY: v }) },+ ]}+ min={0}+ max={4}+ snap={[1]}+ format={(v) => `${Math.round(v * 100)}%`}+ parse={parsePercentInput}+ ck={stretchCk}+ />+ )}+ {keyed && (+ <KeyAxisRow+ label="Tilt"+ axes={[+ { name: "X", value: keyPose.tiltX ?? 0, set: (v) => setKey({ tiltX: Math.round(v) }) },+ { name: "Y", value: keyPose.tiltY ?? 0, set: (v) => setKey({ tiltY: Math.round(v) }) },+ ]}+ min={-TILT_MAX}+ max={TILT_MAX}+ snap={[0]}+ format={(v) => `${Math.round(v)}°`}+ parse={parseNumberInput}+ ck={tiltCk}+ />+ )}<Row label="Rotation"><ValueSliderlabel="Rotation"@@ -3805,6 +4083,7 @@ function ClipTransformSection({ clip }: { clip: VideoClip }) {</Row></>)}+ <ClipMotionRows clip={clip} tLocal={tLocal} /></Section>);}@@ -4359,7 +4638,8 @@ function ShapePanel({ overlay: o }: { overlay: ShapeOverlay }) {const fillCk = useSliderCheckpoint();const radiusCk = useSliderCheckpoint();const strokeCk = useSliderCheckpoint();- const boxShape = !lineLikeShape(o.shape);+ const marks = dealsMarks(o.shape);+ const boxShape = !lineLikeShape(o.shape) && !marks;return (<><ElementTitle overlay={o} />@@ -4478,6 +4758,9 @@ function ShapePanel({ overlay: o }: { overlay: ShapeOverlay }) {)}</Section>)}+ {boxShape && <PatternSection overlay={o} />}+ {marks && <InksRow overlay={o} />}+ <ShadowSection id={o.id} shadow={o.shadow} onToggle={(v) => update(o.id, { shadow: v || undefined })} /><OverlayMaskSection overlay={o} /><TransformRows overlay={o} /></div>@@ -4485,6 +4768,170 @@ function ShapePanel({ overlay: o }: { overlay: ShapeOverlay }) {);}+/** A doodle's other paints: one swatch per ink, each with a remove button,+ * and an add button while there is room. Each mark takes the fill or one of+ * these. */+function InksRow({ overlay: o }: { overlay: ShapeOverlay }) {+ const update = useEditor((s) => s.updateOverlay);+ const inks = o.inks ?? [];++ // Every change writes the whole list back with one entry changed.+ const swap = (i: number, c: string) => inks.map((x, j) => (j === i ? c : x));+ const write = (next: string[]) => update(o.id, { inks: next.length > 0 ? next : undefined });+ return (+ <Row label="Inks">+ <div className="flex flex-col items-end gap-1">+ {inks.map((ink, i) => (+ <div key={i} className="flex items-center gap-1">+ <ColorField+ value={ink}+ label={`Ink ${i + 1}`}+ onBegin={() => useEditor.getState().pushHistory()}+ onLive={(c) => useEditor.getState().updateOverlayTransient(o.id, { inks: swap(i, c) })}+ onCommit={(c) => update(o.id, { inks: swap(i, c) })}+ />+ <Button variant="ghost" size="icon-xs" aria-label={`Remove ink ${i + 1}`} onClick={() => write(inks.filter((_, j) => j !== i))}>+ <X />+ </Button>+ </div>+ ))}+ {inks.length < DOODLE_INKS_MAX && (+ <Button variant="ghost" size="icon-xs" aria-label="Add ink" onClick={() => write([...inks, DOODLE_INKS[inks.length % DOODLE_INKS.length]])}>+ <Plus />+ </Button>+ )}+ </div>+ </Row>+ );+}++/** The drop shadow controls text and shapes share. A colored shadow at+ * offset 0 reads as a glow. */+function ShadowSection({+ id,+ shadow,+ onToggle,+}: {+ id: string;+ shadow: boolean | ShadowSpec | undefined;+ onToggle: (on: boolean) => void;+}) {+ const update = useEditor((s) => s.updateOverlay);+ const shadowCk = useSliderCheckpoint();+ const spec: ShadowSpec = typeof shadow === "object" ? shadow : {};++ // A slider drag lands live and checkpoints once, the way every row does.+ const draft = (patch: ShadowSpec) => {+ shadowCk.begin();+ useEditor.getState().updateOverlayTransient(id, { shadow: { ...spec, ...patch } });+ };+ const commit = (patch: ShadowSpec) => {+ draft(patch);+ shadowCk.end();+ };+ return (+ <Section title="Shadow" enabled={!!shadow} onEnabledChange={onToggle}>+ <>+ <Row label="Color">+ <ColorField+ value={spec.color ?? "#000000"}+ label="Shadow color"+ onBegin={() => useEditor.getState().pushHistory()}+ onLive={(c) => useEditor.getState().updateOverlayTransient(id, { shadow: { ...spec, color: c } })}+ onCommit={(c) => update(id, { shadow: { ...spec, color: c } })}+ opacity={{+ label: "Shadow opacity",+ value: spec.opacity ?? 0.65,+ onDraft: (v) => draft({ opacity: v }),+ onCommit: (v) => commit({ opacity: v }),+ }}+ />+ </Row>+ <Row label="Blur">+ <ValueSlider+ label="Shadow blur"+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={spec.blur ?? SHADOW.blur}+ min={0}+ max={SHADOW_BLUR_MAX}+ step={1}+ snap={[SHADOW.blur]}+ format={(v) => String(Math.round(v))}+ parse={parseNumberInput}+ onDraft={(v) => draft({ blur: v })}+ onCommit={(v) => commit({ blur: v })}+ />+ </Row>+ <Row label="Offset">+ <ValueSlider+ label="Shadow offset"+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={spec.offsetY ?? SHADOW.offsetY}+ min={-SHADOW_OFFSET_MAX}+ max={SHADOW_OFFSET_MAX}+ step={1}+ snap={[0, SHADOW.offsetY]}+ format={(v) => String(Math.round(v))}+ parse={parseNumberInput}+ onDraft={(v) => draft({ offsetY: Math.round(v) })}+ onCommit={(v) => commit({ offsetY: Math.round(v) })}+ />+ </Row>+ </>+ </Section>+ );+}++/** A shape's pattern fill: stripes in the fill color, with line, gap and+ * angle sliders. Off is a solid fill. */+function PatternSection({ overlay: o }: { overlay: ShapeOverlay }) {+ const update = useEditor((s) => s.updateOverlay);+ const patternCk = useSliderCheckpoint();+ const pattern = o.pattern ?? STRIPES_DEFAULT;++ // Each slider writes the whole pattern back with its one field changed.+ const draft = (patch: Partial<StripesPattern>) => {+ patternCk.begin();+ useEditor.getState().updateOverlayTransient(o.id, { pattern: { ...pattern, ...patch } });+ };+ const commit = (patch: Partial<StripesPattern>) => {+ draft(patch);+ patternCk.end();+ };+ const slider = (label: string, value: number, min: number, max: number, step: number, write: (v: number) => Partial<StripesPattern>) => (+ <ValueSlider+ label={label}+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={value}+ min={min}+ max={max}+ step={step}+ format={(v) => String(Math.round(v * 10) / 10)}+ parse={parseNumberInput}+ onDraft={(v) => draft(write(v))}+ onCommit={(v) => commit(write(v))}+ />+ );+ return (+ <Section+ title="Pattern"+ enabled={!!o.pattern}+ onEnabledChange={(v) => update(o.id, { pattern: v ? STRIPES_DEFAULT : undefined })}+ >+ {o.pattern && (+ <>+ <Row label="Line">{slider("Stripe line width", pattern.width, STRIPE_LINE_MIN, STRIPE_LINE_MAX, 0.5, (v) => ({ width: v }))}</Row>+ <Row label="Gap">{slider("Stripe gap", pattern.gap, 0, STRIPE_GAP_MAX, 0.5, (v) => ({ gap: v }))}</Row>+ <Row label="Angle">{slider("Stripe angle", pattern.angle ?? 0, -90, 90, 1, (v) => ({ angle: Math.round(v) || undefined }))}</Row>+ </>+ )}+ </Section>+ );+}+function EffectPanel({ overlay: o }: { overlay: EffectOverlay }) {const amountCk = useSliderCheckpoint();return (@@ -4496,6 +4943,17 @@ function EffectPanel({ overlay: o }: { overlay: EffectOverlay }) {{o.effect === "zoom" ? (<ZoomTarget overlay={o} />) : AMOUNTLESS_EFFECTS.includes(o.effect) ? null : (+ <>+ {o.effect === "lightleak" && <LeakCourseRow overlay={o} />}+ {o.effect === "glitch" && <GlitchKindRow overlay={o} />}+ {o.effect === "flash" && (+ <FlashPulseRows+ pulse={o}+ write={(patch, how) =>+ how === "draft" ? useEditor.getState().updateOverlayTransient(o.id, patch) : useEditor.getState().updateOverlay(o.id, patch)+ }+ />+ )}<Row label="Amount"><ValueSliderlabel="Effect amount"@@ -4519,6 +4977,7 @@ function EffectPanel({ overlay: o }: { overlay: EffectOverlay }) {}}/></Row>+ </>)}<HiddenRow overlay={o} /></div>@@ -4526,6 +4985,100 @@ function EffectPanel({ overlay: o }: { overlay: EffectOverlay }) {);}+/** What a glitch's kinds are called in the panel; "mix" is the random+ * schedule, stored as absence. */+const GLITCH_LABELS: Record<GlitchKind | "mix", string> = {+ mix: "Mix",+ smear: "Smear",+ split: "Split",+ tint: "Tint",+ blowout: "Blowout",+ rgb: "RGB split",+};++/** A glitch's kind: the random mix, or one kind pinned to every frame. */+function GlitchKindRow({ overlay: o }: { overlay: EffectOverlay }) {+ const kinds = ["mix", ...GLITCH_KINDS] as const;+ return (+ <Row label="Kind">+ <Select+ value={o.glitch ?? "mix"}+ items={Object.fromEntries(kinds.map((k) => [k, GLITCH_LABELS[k]]))}+ onValueChange={(v) => useEditor.getState().updateOverlay(o.id, { glitch: v === "mix" ? undefined : (v as GlitchKind) })}+ >+ <SelectTrigger className="h-8 w-36 text-[12px]">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ {kinds.map((k) => (+ <SelectItem key={k} value={k} className="text-[12px]">+ {GLITCH_LABELS[k]}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </Row>+ );+}++/** What each light leak course is called in the panel. */+const LEAK_LABELS: Record<LeakCourse, string> = { drift: "Drift", burn: "Burn", scorch: "Scorch" };++/** A light leak's course: a drifting glow for the whole element, the film+ * burn played once across it, or the scorch flashing hot tones through it. */+function LeakCourseRow({ overlay: o }: { overlay: EffectOverlay }) {+ const leak = o.leak ?? "drift";+ return (+ <Row label="Course">+ <div className="flex w-44 rounded-lg border border-input p-0.5 text-[11.5px] font-medium">+ {LEAK_COURSES.map((c) => (+ <button+ key={c}+ type="button"+ aria-pressed={leak === c}+ className={cn(+ "flex-1 rounded-md px-1 py-1 whitespace-nowrap transition-colors",+ leak === c ? "bg-neutral-900 text-white" : "text-muted-foreground hover:text-foreground"+ )}+ onClick={() => useEditor.getState().updateOverlay(o.id, { leak: c === "drift" ? undefined : c })}+ >+ {LEAK_LABELS[c]}+ </button>+ ))}+ </div>+ </Row>+ );+}++/** What each text texture is called in the panel. */+const TEXT_TEXTURE_LABELS: Record<TextTexture | "none", string> = { none: "None", stipple: "Stipple" };++/** The grain a title wears inside its letters, or none. */+function TextTextureRow({ overlay: o }: { overlay: TextOverlay }) {+ const texture = o.texture ?? "none";+ return (+ <Row label="Texture">+ <div className="flex w-36 rounded-lg border border-input p-0.5 text-[11.5px] font-medium">+ {(["none", ...TEXT_TEXTURES] as const).map((c) => (+ <button+ key={c}+ type="button"+ aria-pressed={texture === c}+ className={cn(+ "flex-1 rounded-md px-1 py-1 whitespace-nowrap transition-colors",+ texture === c ? "bg-neutral-900 text-white" : "text-muted-foreground hover:text-foreground"+ )}+ onClick={() => useEditor.getState().updateOverlay(o.id, { texture: c === "none" ? undefined : c })}+ >+ {TEXT_TEXTURE_LABELS[c]}+ </button>+ ))}+ </div>+ </Row>+ );+}++/** What each flash tone is called in the panel. *//** The focus puck's diameter, in px — `size-5` on the element below. */const PUCK = 20;diff --git a/site/src/cut/components/MotionControls.tsx b/site/src/cut/components/MotionControls.tsxindex 8badc080..c2b781e4 100644--- a/site/src/cut/components/MotionControls.tsx+++ b/site/src/cut/components/MotionControls.tsx@@ -28,7 +28,7 @@ import { sharedNumber, sharedValue } from "@/cut/lib/groupEdit";import { cutMotion } from "@/cut/lib/motionSettings";import { usePreviewTime } from "@/cut/lib/playhead";import { useEditor } from "@/cut/lib/store";-import { isEffectOverlay, type Overlay } from "@/cut/lib/types";+import { clipPoseAt, isEffectOverlay, type Overlay, type VideoClip } from "@/cut/lib/types";/*** The motion rows an element, a selection of elements, and a group's camera@@ -204,6 +204,38 @@ export function GroupMotionRows({ overlays }: { overlays: readonly Overlay[] }));}+/** A video clip's blur rows: the blur of its key at the playhead, once it+ * has keys (a clip has no resting blur), and its motion blur. `tLocal` is+ * the playhead in the clip's own seconds. */+export function ClipMotionRows({ clip, tLocal }: { clip: VideoClip; tLocal: number }) {+ const blurCk = useSliderCheckpoint();+ const shutterCk = useSliderCheckpoint();+ const keyed = !!clip.kf?.length;+ return (+ <>+ {keyed && (+ <BlurRow+ blur={{ value: clipPoseAt(clip, tLocal).blur ?? 0, mixed: false }}+ ck={blurCk}+ onSet={(v) =>+ st().setClipKey(clip.id, tLocal, { blur: Math.max(0, Math.min(ELEMENT_BLUR_MAX, v)) }, { transient: true })+ }+ />+ )}+ <MotionBlurRows+ shutter={clip.motionBlur ? { value: clip.motionBlur, mixed: false } : null}+ ck={shutterCk}+ onChange={(motionBlur, phase) => {+ if (phase === "once") {+ st().pushHistory();+ }+ st().updateClipTransient(clip.id, { motionBlur });+ }}+ />+ </>+ );+}+/*** A group's camera: its key track over the group's span, the pose at the* playhead (where the frame's center looks, the zoom, the roll), the ease outdiff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsxindex fe4400b1..18753d9e 100644--- a/site/src/cut/components/OverlayLayer.tsx+++ b/site/src/cut/components/OverlayLayer.tsx@@ -1,7 +1,7 @@"use client";import { guideSnapLines } from "@/cut/lib/guides";-import { createContext, Fragment, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, type CSSProperties } from "react";+import { createContext, Fragment, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type CSSProperties } from "react";import { createPortal } from "react-dom";import { releaseAnimRest, useAnimPreview } from "@/cut/lib/animPreview";import { startDrag } from "@/cut/lib/drag";@@ -18,10 +18,14 @@ import {subtitleLaneCount,trackPos,} from "@/cut/lib/subtitles";-import { drawStreak, elementAtMoment, paintElementCrop, streakTaps } from "@donkeycut/effects-kit";+import { dealsMarks, doodleBeat, doodleSeed, isInkless, paintDoodle, paintZapInto, SPLIT_CYAN, SPLIT_RED, zapSeed, type TextTexture } from "@donkeycut/effects-kit";+import { erodeMask, textureMask } from "./textureCss";+import { countText, drawStreak, elementAtMoment, fontCycleVariants, paintElementCrop, streakTaps, textCssFont, typedChars, withFontCycle } from "@donkeycut/effects-kit";import { holdMemory } from "@/cut/lib/memoryBudget";+import { stripeGeometry, stripesSolid } from "@donkeycut/effects-kit";import { elementCameraAt } from "@/cut/lib/groupCamera";import { cameraPoint, worldPoint } from "@donkeycut/effects-kit";+import { TILT_PERSPECTIVE } from "@donkeycut/effects-kit";import { CARET_GAP_EM, CARET_HEIGHT_EM, CARET_WIDTH_EM, typeCaret } from "@donkeycut/effects-kit";import { diveView, evalOverlayFrame, glyphStateAt, measureDiveFocus, paintElementInto, slotReel, slotSeed, hasGlyphMotion, hasMaskKeys, hasOverlayKeys, isOverlayAnimated, lineLikeShape, MASK_FEATHER_MAX, MASK_RADIUS_MAX, maskFrameAt, maskHasRadius, maskInverts, maskOutlinePathD, maskSizeAxes, overlayWords, paintMaskCoverage, PEN_MIN_POINTS, penClosed, resolveShadow, shapeMetrics, shapePathD, WORD_ACCENT_DEFAULT, wordDrawsAt, type LottieHandle, type Mask, type MaskKey, type DiveFocus, type MaskPoint, type OverlayFrameState, type WordDraw } from "@donkeycut/effects-kit";import {@@ -57,6 +61,7 @@ import {type ResizeHandle,} from "./TransformHandles";import { stageEffectFilter, stageEffectTransform } from "@/cut/lib/effectStack";+import { rgbSplitFilter } from "./rgbSplit";import { useLiveEffects } from "./StageEffects";import { cn } from "@/lib/utils";@@ -185,8 +190,20 @@ export function OverlayLayer({.sort((a, b) => laneOf(b) - laneOf(a)),[allOverlays, from, to]);+ // A font cycle shows each face for a frame or two, too short for the+ // page to fetch one on sight, so every face in play is loaded ahead.+ useEffect(() => {+ if (typeof document === "undefined" || !document.fonts?.load) return;+ const env = cutRenderEnv([]);+ for (const o of overlays) {+ for (const face of fontCycleVariants(o)) {+ if (isTextOverlay(face)) void document.fonts.load(textCssFont(face, 32, env), face.text).catch(() => {});+ }+ }+ }, [overlays]);const selection = useEditor((s) => s.selection);const multiSelection = useEditor((s) => s.multiSelection);+ const playing = useEditor((s) => s.playing);const aspect = useEditor((s) => s.aspect);const shownGuides = useEditor((s) => s.guides);const guideLines = useEditor((s) => s.guideLines);@@ -200,7 +217,6 @@ export function OverlayLayer({() => (gradeAbove === null ? [] : live.filter((e) => e.lane < gradeAbove).map((e) => e.state)),[live, gradeAbove]);- const filter = stageEffectFilter(graded);const transform = stageEffectTransform(graded);const rootRef = useRef<HTMLDivElement>(null);@@ -215,6 +231,8 @@ export function OverlayLayer({const frame = frameOf(aspect);const stageHeight = (stageWidth * frame.h) / frame.w;+ const split = rgbSplitFilter(graded, stageWidth, stageHeight);+ const filter = [stageEffectFilter(graded), split].filter(Boolean).join(" ") || undefined;// Smart snapping: while dragging an item, pull its left/center/// right edges to the frame edges, safe margins, center line, and the edges@@ -313,8 +331,12 @@ export function OverlayLayer({const grouped = pictureGroupSelected(multiSelection);const selectedIds = useMemo(() => new Set(multiSelection.flatMap((item) => item?.kind === "overlay" ? [item.id] : [])), [multiSelection]);const isolate = !multiple && !!sel && !scrubbing && !(t >= sel.start && t <= sel.end);+ // A glitch's split doubles the slice too: the unmoved elements, lightened in+ // over the moved ones at the ghost's share, the way the export draws them.+ const ghostAlpha = Math.max(0, ...graded.map((s) => s.ghost ?? 0));return (+ <><divref={rootRef}className="pointer-events-none absolute inset-0"@@ -336,7 +358,10 @@ export function OverlayLayer({return (<OverlayItemkey={o.id}- overlay={o}+ // A font cycle sets the title in another face at this moment.+ // A selected title holds its own face while paused, since that is+ // the face its grips and the inspector edit.+ overlay={inRange && (playing || !selected) ? withFontCycle(o, t - o.start) : o}// The skimmer paints the bare frame: the item still renders, its// selection chrome (outline, resize handle) does not.selected={selected && !scrubbing}@@ -377,9 +402,41 @@ export function OverlayLayer({/>))}</div>+ {ghostAlpha > 0 && (+ <div+ aria-hidden+ inert+ className="pointer-events-none absolute inset-0 mix-blend-lighten"+ style={{ filter, opacity: ghostAlpha }}+ >+ {overlays.map((o) => {+ if (o.hidden || o.kind === "effect" || t < o.start || t > o.end) return null;+ return (+ <OverlayItem+ key={o.id}+ overlay={withFontCycle(o, t - o.start)}+ selected={false}+ armed={false}+ grouped={false}+ ghost={false}+ t={t}+ stageWidth={stageWidth}+ registerBox={ignoreBox}+ snap={snap}+ onSnapEnd={clearGuides}+ rotationGuide={rotationGuide}+ />+ );+ })}+ </div>+ )}+ </>);}+/** The ghost copy's boxes stay out of the snap registry. */+const ignoreBox = () => {};+/** Text drawn word by word, each word wearing what the word engine resolved* for it. The words are walked exactly the way the canvas painter walks them* — line by line, single-spaced — so the word the preview lights is the word@@ -389,38 +446,94 @@ function WordText({draws,lineHeight,tail,+ texture,+ typed,}: {text: string;draws: WordDraw[];lineHeight?: number;- /** Drawn at the end of the last line (the typing bar). */+ /** Drawn just past the last typed character (the typing bar). */tail?: React.ReactNode;+ /** Grain each word wears over its own box, as the canvas cuts it. */+ texture?: TextTexture;+ /** Characters a typewriter has typed; the rest hold their room unseen. */+ typed?: number;}) {let k = 0;const lines = text.split("\n");+ const at = typingAt(lines, typed);+ // Where each line starts in the text; a line break takes a turn.+ const starts = lines.map((_, li) => lines.slice(0, li).reduce((n, l) => n + [...l].length + 1, 0));return (<>- {lines.map((line, li) => (- <span key={li} className="block">- {line- .split(" ")- .filter(Boolean)- .map((w, wi) => {+ {lines.map((line, li) => {+ const start = starts[li];+ const words = line.split(" ").filter(Boolean);++ // Where each word starts in the line, and the word the bar follows on+ // the line the typing has reached: the last one it has started.+ let from = 0;+ const wordAt = words.map((w) => {+ const s = line.indexOf(w, from);+ from = s + w.length;+ return [...line.slice(0, s)].length;+ });+ const barWord = li === at.line ? wordAt.filter((s) => s < at.chars).length - 1 : -2;+ return (+ <span key={li} className="block">+ {barWord === -1 && tail}+ {words.map((w, wi) => {const d = draws[k++];+ const letters = [...w];+ const typedIn = typed === undefined ? letters.length : typed - start - wordAt[wi];+ const head = letters.slice(0, Math.max(0, typedIn)).join("");+ const rest = letters.slice(Math.max(0, typedIn)).join("");return (<span key={wi}>{wi > 0 && " "}- <span style={d ? wordDrawCss(d, lineHeight) : undefined}>{w}</span>+ <span+ style={+ texture+ ? { ...(d ? wordDrawCss(d, lineHeight) : {}), display: "inline-block", ...textureMask(texture) }+ : d+ ? wordDrawCss(d, lineHeight)+ : undefined+ }+ >+ {head}+ {wi === barWord && tail}+ {rest && <span style={{ visibility: "hidden" }}>{rest}</span>}+ </span></span>);})}- {li === lines.length - 1 && tail}- </span>- ))}+ </span>+ );+ })}</>);}+/** Where the typing bar stands: the line the typing has reached and how many+ * of that line's characters are typed. A line break takes a turn, the way+ * the painters count. Untyped text stands at the end of the last line. */+function typingAt(lines: string[], typed: number | undefined): { line: number; chars: number } {+ const last = lines.length - 1;+ if (typed === undefined) {+ return { line: last, chars: [...lines[last]].length };+ }+ let start = 0;+ let at = { line: 0, chars: 0 };+ lines.forEach((l, i) => {+ const n = [...l].length;+ if (start <= typed) {+ at = { line: i, chars: Math.min(n, typed - start) };+ }+ start += n + 1;+ });+ return at;+}+/** Every subtitle track's active cue, one caption per language. */function SubtitleCaptions(props: {stageWidth: number;@@ -650,17 +763,35 @@ function OverlayItem({// wears it as a transform, the chrome twin folds it into its size instead,// so a grip and a hairline stay the size they were drawn at.const poseZoom = live && live.scale !== 1 ? live.scale : 1;+ // A squash stretches one axis over the zoom; the chrome stays uniform.+ const stretchX = live?.sx ?? 1;+ const stretchY = live?.sy ?? 1;+ // A tilt turns the box in perspective about its posed center, the same+ // depth the painters project through.+ const tilt =+ live && (live.tiltX || live.tiltY)+ ? ` perspective(${(TILT_PERSPECTIVE * scale).toFixed(1)}px) rotateX(${live.tiltX ?? 0}deg) rotateY(${live.tiltY ?? 0}deg)`+ : "";const animMove = live? ` translate(${live.dx * scale}px, ${live.dy * scale}px)` ++ tilt +(live.rotation ? ` rotate(${live.rotation}deg)` : ""): o.rotation? ` rotate(${o.rotation}deg)`: "";- const animTransform = poseZoom !== 1 ? `${animMove} scale(${poseZoom})` : animMove;+ const animTransform =+ poseZoom !== 1 || stretchX !== 1 || stretchY !== 1+ ? `${animMove} scale(${poseZoom * stretchX}, ${poseZoom * stretchY})`+ : animMove;// Per-glyph ramps and loops draw character by character; a wipe uncovers the// box from its left edge. Neither runs while the box is being edited.const glyphs = isText && !editing && live && hasGlyphMotion(live) ? live : null;+ // A textured title wears its grain letter by letter, so it lays out by+ // glyph too.+ const texture = isTextOverlay(o) && !editing ? o.texture : undefined;const reveal = !editing ? live?.reveal : undefined;+ // A disintegration eats the box's ink through the kit's erosion tile.+ const erode = !editing ? live?.erode : undefined;// Word effects: the line plays word by word on its own clock. Off while// editing — the box is plain text then — and a per-glyph ramp takes the// frame for itself while it runs.@@ -678,11 +809,14 @@ function OverlayItem({wordLocal): null;- // Typewriter: the visible slice of the text (display only, never while- // the box is being edited).+ // Typewriter: how many characters are typed, the rest holding their room+ // unseen; count: the text with its numbers where the count stands (display+ // only, never while the box is being edited).+ const typed =+ isText && live?.textProgress !== undefined && !editing ? typedChars(drawnText, live.textProgress) : undefined;const shownText =- isText && live?.textProgress !== undefined && !editing- ? drawnText.slice(0, Math.ceil(live.textProgress * drawnText.length))+ isText && typed === undefined && live?.countProgress !== undefined && !editing+ ? countText(drawnText, live.countProgress): isText? drawnText: "";@@ -715,6 +849,8 @@ function OverlayItem({// sits become the width of the frame, so while it dives the element is// drawn by the kit's own painter — the picture the export draws.const diving = !!live?.dive && !editing && !behindHidden;+ // A zap's arcs are kit pixels, laid on their own canvas over the box.+ const zapping = !!live?.zap && !editing && !behindHidden && !diving;// Blur is a CSS filter inside the box, so the box's zoom would grow it:// it is divided back out to land the radius every renderer draws.const blurPx = (((live ? live.blur : o.blur) ?? 0) * scale) / poseZoom;@@ -810,7 +946,7 @@ function OverlayItem({};})(): {}),- ...(maskCss ?? {}),+ ...(erode !== undefined && erode > 0 ? erodeMask(erode, maskCss) : (maskCss ?? {})),...(diving || streaking ? { visibility: "hidden" as const } : {}),...(reveal !== undefined? { clipPath: `inset(0 ${(1 - Math.min(1, Math.max(0, reveal))) * 100}% 0 0)` }@@ -1240,21 +1376,40 @@ function OverlayItem({>{o.text}</div>- ) : glyphs ? (- <GlyphText text={shownText} phase={glyphs} scale={scale} tail={caretBar} />+ ) : glyphs || (texture && !wordDraw) ? (+ <GlyphText+ text={shownText}+ phase={glyphs ?? {}}+ scale={scale}+ zoom={poseZoom}+ ink={isTextOverlay(o) ? o.color : undefined}+ tail={caretBar}+ texture={texture}+ typed={typed}+ />) : wordDraw ? (<WordTexttext={shownText}draws={wordDraw}lineHeight={(isTextOverlay(o) ? o.lineHeight : undefined) ?? LINE_HEIGHT}tail={caretBar}+ texture={texture}+ typed={typed}/>+ ) : typed !== undefined ? (+ <span>+ {[...shownText].slice(0, typed).join("")}+ {caretBar}+ <span style={{ visibility: "hidden" }}>{[...shownText].slice(typed).join("")}</span>+ </span>) : (<span>{shownText}{caretBar}</span>)+ ) : o.kind === "shape" && dealsMarks(o.shape) ? (+ <DoodleView shape={o} stageWidth={stageWidth} stageHeight={stageHeight} scale={scale} t={t} />) : o.kind === "shape" ? (<ShapeView shape={o} stageWidth={stageWidth} stageHeight={stageHeight} scale={scale} />) : o.kind === "sticker" ? (@@ -1279,6 +1434,9 @@ function OverlayItem({holds through that first frame — a select never blinks. */}{!chromeLifted && chrome}</div>+ {zapping && live && (+ <ZapCanvas o={o} live={live} tLocal={tLocal} stageWidth={stageWidth} stageHeight={stageHeight} />+ )}{chromeLifted && chromeHost && chromeSize &&createPortal((() => {@@ -1377,27 +1535,61 @@ function GlyphText({text,phase,scale,+ zoom,+ ink,tail,+ texture,+ typed,}: {text: string;- phase: OverlayFrameState;+ phase: Parameters<typeof glyphStateAt>[0];scale: number;- /** Drawn at the end of the last line (the typing bar). */+ /** The box's own zoom: a letter's blur is divided back out of it, so the+ * radius lands where the painters draw it. */+ zoom: number;+ /** The title's fill; an inkless one keeps a split letter's copies hollow. */+ ink?: string;+ /** Drawn just past the last typed character (the typing bar). */tail?: React.ReactNode;+ /** Grain each letter wears over its own box, as the canvas cuts it. */+ texture?: TextTexture;+ /** Characters a typewriter has typed; the rest hold their room unseen. */+ typed?: number;}) {const total = Math.max(1, [...text].length);- const lineCount = text.split("\n").length;+ const lines = text.split("\n");+ const at = typingAt(lines, typed);const seed = slotSeed(text);let gi = 0;return (<>- {text.split("\n").map((line, li) => {+ {lines.map((line, li) => {const chars = [...line].map((ch) => {const i = gi++;const g = glyphStateAt(phase, i, total);+ const unseen = typed !== undefined && i >= typed;// A slot character shows its reel through its own line band; the// band clips top and bottom only, so a wide filler is not cut.const reel = g.roll ? slotReel(ch, i, g.roll, seed) : null;+ // A letter sharpening out of a blur wears its own defocus.+ const blur = g.blur ? (g.blur * scale) / zoom : 0;+ // A splitting letter shows a red and a cyan copy either side.+ const split = g.split && ch !== " " ? g.split * scale : 0;+ const copy = (tint: string, dx: number) => (+ <span+ aria-hidden+ style={{+ position: "absolute",+ left: 0,+ top: 0,+ transform: `translateX(${dx}px)`,+ color: ink && isInkless(ink) ? "transparent" : tint,+ WebkitTextStrokeColor: tint,+ }}+ >+ {ch}+ </span>+ );return (<spankey={i}@@ -1405,12 +1597,16 @@ function GlyphText({display: "inline-block",whiteSpace: "pre",opacity: Math.min(1, Math.max(0, g.alpha)),+ ...(blur >= 0.1 ? { filter: `blur(${blur.toFixed(2)}px)` } : {}),+ ...(split ? { position: "relative" } : {}),+ ...(unseen ? { visibility: "hidden" } : {}),transform:`translate(${g.dx * scale}px, ${g.dy * scale}px)` +` rotate(${g.rotate}deg) scale(${g.sx}, ${g.sy})`,...(reel? { position: "relative", overflowX: "visible", overflowY: "clip", verticalAlign: "top" }: {}),+ ...(texture && ch !== " " ? textureMask(texture) : {}),}}>{reel ? (@@ -1431,6 +1627,12 @@ function GlyphText({</span>))}</>+ ) : split ? (+ <>+ {copy(SPLIT_RED, -split)}+ {copy(SPLIT_CYAN, split)}+ <span style={{ position: "relative" }}>{ch}</span>+ </>) : (ch)}@@ -1438,10 +1640,12 @@ function GlyphText({);});gi++;+ // The bar follows the last typed character on the line typing reached.+ const withBar =+ li === at.line ? [...chars.slice(0, at.chars), <Fragment key="bar">{tail}</Fragment>, ...chars.slice(at.chars)] : chars;return (<span key={li} style={{ display: "block", minHeight: "1em" }}>- {chars}- {li === lineCount - 1 && tail}+ {withBar}</span>);})}@@ -1449,6 +1653,68 @@ function GlyphText({);}+/**+ * A zap's arcs, painted by the kit over a stage-sized canvas under the+ * element's pose: the same painter the export bakes them with, laid over the+ * element the DOM box draws. One paint is in flight at a time; the newest+ * moment waits behind it.+ */+function ZapCanvas({+ o,+ live,+ tLocal,+ stageWidth,+ stageHeight,+}: {+ o: Overlay;+ live: OverlayFrameState;+ tLocal: number;+ stageWidth: number;+ stageHeight: number;+}) {+ const ref = useRef<HTMLCanvasElement>(null);+ const assets = useEditor((s) => (o.kind === "sticker" ? s.assets : null));+ const dpr = typeof window === "undefined" ? 1 : Math.min(2, window.devicePixelRatio || 1);+ const w = Math.max(1, Math.round(stageWidth * dpr));+ const h = Math.max(1, Math.round(stageHeight * dpr));+ const seed = zapSeed(tLocal);+ const pending = useRef<{ live: OverlayFrameState; seed: number } | null>(null);+ const busy = useRef(false);+ useEffect(() => {+ const canvas = ref.current;+ if (!canvas) {+ return;+ }+ pending.current = { live, seed };+ if (busy.current) {+ return;+ }+ busy.current = true;+ const env = cutRenderEnv(assets ?? []);+ const el = { ...o, rotation: undefined, opacity: undefined } as Overlay;+ void (async () => {+ try {+ while (pending.current) {+ const next = pending.current;+ pending.current = null;+ await paintZapInto(canvas, el, env, { pose: next.live, zap: { amount: next.live.zap ?? 0, seed: next.seed } });+ }+ } finally {+ busy.current = false;+ }+ })();+ }, [o, live, seed, w, h, assets]);+ return (+ <canvas+ ref={ref}+ width={w}+ height={h}+ className="pointer-events-none absolute inset-0"+ style={{ width: stageWidth, height: stageHeight, opacity: live.opacity }}+ />+ );+}+/*** A diving element, painted by the kit over a stage-sized canvas. The painter* applies the pose and dive before the frame clips it. One paint is in@@ -1567,7 +1833,9 @@ function StreakCanvas({!!live.glyphs ||!!live.glyphLoop ||live.reveal !== undefined ||+ live.erode !== undefined ||live.textProgress !== undefined ||+ live.countProgress !== undefined ||!!overlayWords(o) ||(o.kind === "sticker" && !!o.lottie);const moving = !!live.streak;@@ -1642,8 +1910,8 @@ function StreakCanvas({const sx = streak.x * px;const sy = streak.y * px;const r = (-live.rotation * Math.PI) / 180;- const lx = (Math.cos(r) * sx - Math.sin(r) * sy) / poseZoom;- const ly = (Math.sin(r) * sx + Math.cos(r) * sy) / poseZoom;+ const lx = (Math.cos(r) * sx - Math.sin(r) * sy) / (poseZoom * (live.sx ?? 1));+ const ly = (Math.sin(r) * sx + Math.cos(r) * sy) / (poseZoom * (live.sy ?? 1));// Grows in steps and never shrinks while mounted, so a changing streak// does not reallocate the canvas every frame.const cw = Math.max(canvas.width, Math.ceil((pic.canvas.width + Math.abs(lx) + 4) / 64) * 64);@@ -1667,7 +1935,7 @@ function StreakCanvas({canvas.style.width = `${cw / dpr}px`;canvas.style.height = `${ch / dpr}px`;onReady(true);- }, [streak, live.rotation, poseZoom, painted, w, h, dpr, onReady, selfMoving, tLocal]);+ }, [streak, live.rotation, live.sx, live.sy, poseZoom, painted, w, h, dpr, onReady, selfMoving, tLocal]);return (<divclassName="pointer-events-none absolute"@@ -2485,7 +2753,10 @@ function MaskGizmo({}/** A shape drawn as inline SVG with the painter's own pixel geometry- * (`shapeMetrics` in stage space), so preview and export burn-in match. */+ * (`shapeMetrics` in stage space), so preview and export burn-in match. A+ * pattern fill is an SVG <pattern> on the kit's stripe geometry, and a shadow+ * is the silhouette's blurred, flooded alpha drawn under the body — the same+ * order the canvas painter casts it in. */function ShapeView({shape: o,stageWidth,@@ -2497,74 +2768,180 @@ function ShapeView({stageHeight: number;scale: number;}) {+ const uid = useId().replace(/[^\w-]/g, "");const m = shapeMetrics(o, { width: stageWidth, height: stageHeight, scale });- if (lineLikeShape(o.shape)) {- const h = Math.max(m.thickness, m.headHalf * 2);- const mid = h / 2;- return (- <svg- width={m.w}- height={h}- className="block overflow-visible"- style={{ pointerEvents: "none" }}- >- <line- x1={m.thickness / 2}- y1={mid}- x2={o.shape === "arrow" ? m.w - m.headLen : m.w - m.thickness / 2}- y2={mid}- stroke={o.fill}- strokeWidth={m.thickness}- strokeLinecap="round"- />- {o.shape === "arrow" && (- <polygon- points={`${m.w},${mid} ${m.w - m.headLen},${mid - m.headHalf} ${m.w - m.headLen},${mid + m.headHalf}`}- fill={o.fill}+ const lineLike = lineLikeShape(o.shape);+ const boxH = lineLike ? Math.max(m.thickness, m.headHalf * 2) : m.h;+ const shadow = resolveShadow(o.shadow ?? false);+ const stripes = o.pattern && !lineLike ? stripeGeometry(o.pattern, scale) : null;+ const patterned = stripes && !stripesSolid(stripes) ? stripes : null;++ // The body, painted in `fill`: once as the shadow's silhouette in the+ // plain fill color, once on top in the pattern (or the same color).+ const body = (fill: string) => {+ if (lineLike) {+ const mid = boxH / 2;+ return (+ <>+ <line+ x1={m.thickness / 2}+ y1={mid}+ x2={o.shape === "arrow" ? m.w - m.headLen : m.w - m.thickness / 2}+ y2={mid}+ stroke={o.fill}+ strokeWidth={m.thickness}+ strokeLinecap="round"/>- )}- </svg>+ {o.shape === "arrow" && (+ <polygon+ points={`${m.w},${mid} ${m.w - m.headLen},${mid - m.headHalf} ${m.w - m.headLen},${mid + m.headHalf}`}+ fill={o.fill}+ />+ )}+ </>+ );+ }+ return o.shape === "rect" ? (+ <rect+ x={0}+ y={0}+ width={m.w}+ height={m.h}+ rx={m.radius}+ fill={fill}+ fillOpacity={o.fillOpacity ?? 1}+ stroke={o.stroke?.color}+ strokeWidth={m.strokeWidth || undefined}+ />+ ) : o.shape === "ellipse" ? (+ <ellipse+ cx={m.w / 2}+ cy={m.h / 2}+ rx={m.w / 2}+ ry={m.h / 2}+ fill={fill}+ fillOpacity={o.fillOpacity ?? 1}+ stroke={o.stroke?.color}+ strokeWidth={m.strokeWidth || undefined}+ />+ ) : (+ <path+ d={shapePathD(o.shape, m.w, m.h)}+ fill={fill}+ fillOpacity={o.fillOpacity ?? 1}+ stroke={o.stroke?.color}+ strokeWidth={m.strokeWidth || undefined}+ strokeLinejoin="round"+ />);- }+ };++ // The filter region reaches past the blur's visible tail (3 sigma) and the+ // offset, so a thin bar's glow is never cut at the default 10% margin.+ const spill = shadow ? (1.5 * shadow.blur + Math.abs(shadow.offsetY)) * scale + m.strokeWidth + (lineLike ? m.thickness : 0) : 0;return (- <svg width={m.w} height={m.h} className="block overflow-visible" style={{ pointerEvents: "none" }}>- {o.shape === "rect" ? (- <rect- x={0}- y={0}- width={m.w}- height={m.h}- rx={m.radius}- fill={o.fill}- fillOpacity={o.fillOpacity ?? 1}- stroke={o.stroke?.color}- strokeWidth={m.strokeWidth || undefined}- />- ) : o.shape === "ellipse" ? (- <ellipse- cx={m.w / 2}- cy={m.h / 2}- rx={m.w / 2}- ry={m.h / 2}- fill={o.fill}- fillOpacity={o.fillOpacity ?? 1}- stroke={o.stroke?.color}- strokeWidth={m.strokeWidth || undefined}- />- ) : (- <path- d={shapePathD(o.shape, m.w, m.h)}- fill={o.fill}- fillOpacity={o.fillOpacity ?? 1}- stroke={o.stroke?.color}- strokeWidth={m.strokeWidth || undefined}- strokeLinejoin="round"- />+ <svg width={m.w} height={boxH} className="block overflow-visible" style={{ pointerEvents: "none" }}>+ {(patterned || shadow) && (+ <defs>+ {patterned && (+ <pattern+ id={`${uid}-fill`}+ patternUnits="userSpaceOnUse"+ width={patterned.period}+ height={boxH}+ patternTransform={patterned.angle ? `rotate(${patterned.angle} ${m.w / 2} ${boxH / 2})` : undefined}+ >+ <rect x={0} y={0} width={patterned.line} height={boxH} fill={o.fill} />+ </pattern>+ )}+ {shadow && (+ <filter+ id={`${uid}-shadow`}+ filterUnits="userSpaceOnUse"+ x={-spill}+ y={-spill}+ width={m.w + spill * 2}+ height={boxH + spill * 2}+ colorInterpolationFilters="sRGB"+ >+ <feGaussianBlur in="SourceAlpha" stdDeviation={(shadow.blur * scale) / 2} />+ <feOffset dy={shadow.offsetY * scale} result="spill" />+ <feFlood floodColor={shadow.color} />+ <feComposite in2="spill" operator="in" />+ </filter>+ )}+ </defs>)}+ {shadow && <g filter={`url(#${uid}-shadow)`}>{body(o.fill)}</g>}+ {body(patterned ? `url(#${uid}-fill)` : o.fill)}</svg>);}+/** A doodle: the kit's painter draws the mark dealt on the current beat into+ * a canvas the size of the shape's box, so preview and export deal the same+ * marks. It repaints only when the beat, the box or the paints change. */+function DoodleView({+ shape: o,+ stageWidth,+ stageHeight,+ scale,+ t,+}: {+ shape: ShapeOverlay;+ stageWidth: number;+ stageHeight: number;+ scale: number;+ t: number;+}) {+ const ref = useRef<HTMLCanvasElement>(null);+ const m = shapeMetrics(o, { width: stageWidth, height: stageHeight, scale });+ const beat = doodleBeat(t - o.start);+ const inks = [o.fill, ...(o.inks ?? [])].join("|");+ useEffect(() => {+ const canvas = ref.current;+ if (!canvas) {+ return;+ }++ // Drawn at device pixels so the bristles stay crisp on a dense screen.+ const dpr = window.devicePixelRatio || 1;+ const w = Math.max(1, Math.round(m.w * dpr));+ const h = Math.max(1, Math.round(m.h * dpr));+ if (canvas.width !== w) {+ canvas.width = w;+ }+ if (canvas.height !== h) {+ canvas.height = h;+ }+ const ctx = canvas.getContext("2d");+ if (!ctx) {+ return;+ }+ ctx.setTransform(1, 0, 0, 1, 0, 0);+ ctx.clearRect(0, 0, w, h);+ ctx.globalAlpha = o.fillOpacity ?? 1;+ ctx.scale(dpr, dpr);+ paintDoodle(ctx, { cx: m.w / 2, cy: m.h / 2, w: m.w, h: m.h, scale }, inks.split("|"), beat, doodleSeed(o.id));+ }, [m.w, m.h, scale, inks, beat, o.id, o.fillOpacity]);++ // A shadow is the marks' own silhouette, blurred and dropped, the way the+ // painter casts it.+ const shadow = resolveShadow(o.shadow ?? false);+ return (+ <canvas+ ref={ref}+ className="block"+ style={{+ width: m.w,+ height: m.h,+ pointerEvents: "none",+ filter: shadow ? `drop-shadow(0 ${shadow.offsetY * scale}px ${shadow.blur * scale}px ${shadow.color})` : undefined,+ }}+ />+ );+}+/** A sticker: the project image at its stored width (height from the asset's* own aspect), or a Lottie animation frame-seeked from timeline time. */function StickerView({diff --git a/site/src/cut/components/RemovalBrush.tsx b/site/src/cut/components/RemovalBrush.tsxindex e6278a9c..f7d1f2f0 100644--- a/site/src/cut/components/RemovalBrush.tsx+++ b/site/src/cut/components/RemovalBrush.tsx@@ -11,6 +11,7 @@ import { decodeRasterImageUrl } from "@/cut/lib/raster";import { useEditor } from "@/cut/lib/store";import {clipCovers,+ CLIP_MIN_SECONDS,clipKeyed,clipPoseAt,clipZoom,@@ -58,7 +59,7 @@ export function RemovalBrush({ stage }: { stage: { w: number; h: number } }) {// pointer rides the timeline and the overlay tracks the frame on screen.const tLocal = usePreviewSelector((t) => (armed && clip ? t - clip.start : -1));if (!armed || !clip) return null;- const len = Math.max(0.1, retimeOf(clip).len);+ const len = Math.max(CLIP_MIN_SECONDS, retimeOf(clip).len);if (tLocal < 0 || tLocal >= len) return null;const asset = assets.find((a) => a.id === clip.assetId);if (!asset?.width || !asset?.height) return null;diff --git a/site/src/cut/components/StageEffects.tsx b/site/src/cut/components/StageEffects.tsxindex 60e9005f..9b61bfc3 100644--- a/site/src/cut/components/StageEffects.tsx+++ b/site/src/cut/components/StageEffects.tsx@@ -2,11 +2,14 @@import { useEffect, useMemo, useRef } from "react";import {+ burnGradient,+ burnStreakGradient,grainTileUrl,isAudioEffect,LEAK_TINT,leakGradient,streakGradient,+ washBackground,} from "@donkeycut/effects-kit";import { previewAt, subscribePlayhead, usePreviewTime } from "@/cut/lib/playhead";import { useEditor } from "@/cut/lib/store";@@ -19,6 +22,7 @@ import {type LiveEffect,} from "@/cut/lib/effectStack";import "./grain.css";+import { rgbSplitFilter } from "./rgbSplit";/*** Effect elements in the preview's stack.@@ -59,7 +63,33 @@ export function useLiveEffects(): LiveEffect[] {export function StagePictureFx({ children }: { children: React.ReactNode }) {const overlays = useEditor((s) => s.overlays);const ref = useRef<HTMLDivElement>(null);+ const ghostRef = useRef<HTMLCanvasElement>(null);useEffect(() => {+ // A glitch's split doubles the picture: the unmoved frame is copied off+ // the picture canvas onto one laid over the moved box, lightened in. The+ // copy only runs on the frames a split is live.+ const paintGhost = (alpha: number) => {+ const ghost = ghostRef.current;+ if (!ghost) return;+ if (alpha <= 0) {+ if (ghost.style.display !== "none") ghost.style.display = "none";+ return;+ }+ // The picture shows on the SDR canvas, or on the present canvas over+ // it in HDR; the hidden one is skipped.+ let source: HTMLCanvasElement | null = null;+ for (const c of ref.current?.querySelectorAll("canvas") ?? []) {+ if (!c.classList.contains("hidden")) source = c;+ }+ const ctx = ghost.getContext("2d");+ if (!source || !ctx || source.width === 0) return;+ if (ghost.width !== source.width) ghost.width = source.width;+ if (ghost.height !== source.height) ghost.height = source.height;+ ctx.clearRect(0, 0, ghost.width, ghost.height);+ ctx.drawImage(source, 0, 0);+ ghost.style.opacity = String(alpha);+ ghost.style.display = "block";+ };const write = (filter: string, transform: string) => {const style = ref.current?.style;if (!style) return;@@ -68,23 +98,31 @@ export function StagePictureFx({ children }: { children: React.ReactNode }) {};if (!hasEffects(overlays)) {write("", "");+ paintGhost(0);return;}const apply = () => {const states = liveEffectsAt(overlays, previewAt()).map((e) => e.state);- write(stageEffectFilter(states) ?? "", stageEffectTransform(states) ?? "");+ const box = ref.current;+ const split = box ? rgbSplitFilter(states, box.clientWidth, box.clientHeight) : "";+ write([stageEffectFilter(states), split].filter(Boolean).join(" "), stageEffectTransform(states) ?? "");+ paintGhost(Math.max(0, ...states.map((s) => s.ghost ?? 0)));};apply();const stop = subscribePlayhead(apply);return () => {stop();write("", "");+ paintGhost(0);};}, [overlays]);return (- <div ref={ref} className="absolute inset-0">- {children}- </div>+ <>+ <div ref={ref} className="absolute inset-0">+ {children}+ </div>+ <canvas ref={ghostRef} className="pointer-events-none absolute inset-0 size-full mix-blend-lighten" style={{ display: "none" }} />+ </>);}@@ -116,10 +154,12 @@ export function StageEffectPaint({ lane }: { lane: number }) {const grainUrl = grainTileUrl();const grain = Math.max(0, ...states.map((s) => s.grain ?? 0));const vignette = Math.max(0, ...states.map((s) => s.vignette ?? 0));- const flash = Math.max(0, ...states.map((s) => s.flash ?? 0));+ const flash = Math.max(0, ...states.map((s) => (s.flashTone ? 0 : (s.flash ?? 0))));+ const dark = Math.max(0, ...states.map((s) => (s.flashTone === "black" ? (s.flash ?? 0) : 0)));const washes = states.flatMap((s) => s.washes ?? []);const leaks = states.flatMap((s) => (s.leak ? [s.leak] : []));- if (!grain && !vignette && !flash && washes.length === 0 && leaks.length === 0) return null;+ const burns = states.flatMap((s) => (s.burn ? [s.burn] : []));+ if (!grain && !vignette && !flash && !dark && washes.length === 0 && leaks.length === 0 && burns.length === 0) return null;return (<div className="pointer-events-none absolute inset-0">{grain > 0 && grainUrl && (@@ -163,7 +203,7 @@ export function StageEffectPaint({ lane }: { lane: number }) {key={i}className="absolute inset-0"style={{- background: w.color,+ background: washBackground(w.color, w.area),opacity: w.alpha,// The recipe names its wash with a canvas composite op; the ones// effects use are CSS blend modes by the same name.@@ -172,6 +212,20 @@ export function StageEffectPaint({ lane }: { lane: number }) {/>))}{flash > 0 && <div className="absolute inset-0 bg-white" style={{ opacity: flash }} />}+ {dark > 0 && <div className="absolute inset-0 bg-black" style={{ opacity: dark }} />}+ {burns.map((b, i) => (+ // The burned body, then the flame band over it — the canvas pass's+ // two gradients at the same alphas.+ <div key={`burn-${i}`} className="absolute inset-0">+ <div className="absolute inset-0" style={{ opacity: b.alpha, background: burnGradient(b) }} />+ {b.streak && (+ <div+ className="absolute inset-0"+ style={{ opacity: b.streak.alpha * b.alpha, background: burnStreakGradient(b.streak) }}+ />+ )}+ </div>+ ))}</div>);}diff --git a/site/src/cut/components/SubtitlesPanel.tools.ts b/site/src/cut/components/SubtitlesPanel.tools.tsindex 4b14ff73..f8fc1a4c 100644--- a/site/src/cut/components/SubtitlesPanel.tools.ts+++ b/site/src/cut/components/SubtitlesPanel.tools.ts@@ -23,6 +23,7 @@ import {WORD_POP_SCALE,WORD_SWELL_MAX,WORD_SWELL_MIN,+ TEXT_WEIGHTS,} from "@donkeycut/effects-kit";export const SUBTITLES_TOOLS = [@@ -127,8 +128,8 @@ export const SUBTITLES_TOOLS = [emphasis_italic: bool(`Emphasized words in italic (default ${CAPTION_EMPHASIS_DEFAULT.italic})`),emphasis_weight: {type: "number",- enum: [400, 700],- description: `Emphasized word weight, 400 regular or 700 bold (default ${CAPTION_EMPHASIS_DEFAULT.weight})`,+ enum: [...TEXT_WEIGHTS],+ description: `Emphasized word weight, 400 regular, 700 bold, 800 extra bold or 900 black (default ${CAPTION_EMPHASIS_DEFAULT.weight})`,},emphasis_scale: num(`Emphasized word size over the caption's own, ${EMPHASIS_SCALE_MIN}..${EMPHASIS_SCALE_MAX} (default ${CAPTION_EMPHASIS_DEFAULT.scale})`diff --git a/site/src/cut/components/SubtitlesPanel.tsx b/site/src/cut/components/SubtitlesPanel.tsxindex f00bb436..c143ec31 100644--- a/site/src/cut/components/SubtitlesPanel.tsx+++ b/site/src/cut/components/SubtitlesPanel.tsx@@ -436,7 +436,7 @@ function EmphasisRows() {Bold<SwitchclassName="sub-emphasis-weight"- checked={em.weight === 700}+ checked={em.weight >= 700}onCheckedChange={(v) => {useEditor.getState().pushHistory();set({ emphasisWeight: v ? 700 : 400 });diff --git a/site/src/cut/components/Timeline.tools.ts b/site/src/cut/components/Timeline.tools.tsindex 3cdd2d12..603e4f1b 100644--- a/site/src/cut/components/Timeline.tools.ts+++ b/site/src/cut/components/Timeline.tools.ts@@ -16,7 +16,7 @@ import {TEXT_VARIATION_IDS,} from "@/cut/lib/textCompose";import { TEXT_MOVE_IDS, TEXT_MOVE_NOTES } from "@/cut/lib/textMotion";-import { OVERLAY_ANIM_STYLE_IDS } from "@donkeycut/effects-kit";+import { LETTER_SPACING_MAX, LETTER_SPACING_MIN, OVERLAY_ANIM_STYLE_IDS, TEXT_TEXTURES, TEXT_WEIGHTS } from "@donkeycut/effects-kit";import { TIMELINE_IDS, TIMELINE_LABELS } from "@/cut/lib/types";export const TIMELINE_TOOLS = [@@ -254,15 +254,20 @@ export const TIMELINE_TOOLS = [size: num("Font size px at 1080w (default 88)"),color: str("CSS color (default #FFFFFF)"),font: str("Font id (see the graphics skill; default sf)"),- weight: { type: "number", enum: [400, 700], description: "Font weight" },+ weight: { type: "number", enum: [...TEXT_WEIGHTS], description: "Font weight: 400 regular, 700 bold, 800 extra bold, 900 black" },italic: bool("Italic"),align: { type: "string", enum: ["left", "center", "right"], description: "Multi-line alignment (default center)" },- letter_spacing: num("Tracking in em (0 = normal, 0.1 = airy)"),+ letter_spacing: num(`Tracking in em, ${LETTER_SPACING_MIN}..${LETTER_SPACING_MAX} (0 = normal, 0.1 = airy, negative = tight)`),line_height: num("Line height multiplier (default 1.25)"),stroke_color: str("Text outline color"),stroke_width: num("Text outline width in em (0..0.15; 0 removes it)"),shadow: bool("Drop shadow (default true)"),plate: bool("Translucent plate behind text (default false)"),+ texture: {+ type: "string",+ enum: [...TEXT_TEXTURES, "none"],+ description: 'Grain inside the letters: "stipple" breaks each letter into spray-paint specks along its left edge, solid by its middle (the gritty teaser title); "none" makes the type solid again',+ },rotation: num("Degrees clockwise, -180..180"),opacity: num("Whole-element opacity 0..1"),lane: num("Element row (0 = the front row, drawn over every higher row). Elements on one row never overlap — a title over a shape needs a lower row than the shape."),diff --git a/site/src/cut/components/TransitionsPanel.tools.ts b/site/src/cut/components/TransitionsPanel.tools.tsindex 849a91f1..34496629 100644--- a/site/src/cut/components/TransitionsPanel.tools.ts+++ b/site/src/cut/components/TransitionsPanel.tools.ts@@ -9,6 +9,7 @@import { bool, ids, num, obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";import { ANIM_STYLE_IDS, TRANSITION_STYLE_IDS } from "@/cut/lib/types";+import { FEATHER_STYLES, TRANSITION_FEATHER_MAX } from "@/cut/lib/transitionShape";export const TRANSITIONS_TOOLS = [{@@ -24,6 +25,9 @@ export const TRANSITIONS_TOOLS = [enum: [...TRANSITION_STYLE_IDS],description: "Transition look (default crossfade)",},+ feather: num(+ `Softness of the reveal edge for ${FEATHER_STYLES.join(", ")}: a share of the frame the edge crosses, 0..${TRANSITION_FEATHER_MAX} (0 = hard edge, 0.15 = a soft blurred band); other styles ignore it`+ ),}, ["seconds"]),},{@@ -46,7 +50,7 @@ export const TRANSITIONS_TOOLS = [{name: "set_animation",description:- "Animate one clip's own entrance (which:'in') or exit (which:'out'): fade, zoom, pop, or a slide named by its motion direction. style 'none' clears. Track-0 clips take every style; upper-track clips only fade and zoom. Each edge holds one effect, last pick wins: animating an edge a transition owns replaces that transition, and setting a transition clears the animations adjacent to its joint. At an abutting cut the animation plays over the neighbor's held frame; at the timeline's ends and across gaps it plays against black.",+ "Animate one clip's own entrance (which:'in') or exit (which:'out'): fade, blur (defocuses as it fades), zoom, pop, or a slide named by its motion direction. style 'none' clears. Track-0 clips take every style; upper-track clips only fade and zoom. Each edge holds one effect, last pick wins: animating an edge a transition owns replaces that transition, and setting a transition clears the animations adjacent to its joint. At an abutting cut the animation plays over the neighbor's held frame; at the timeline's ends and across gaps it plays against black.",inputSchema: obj({clipId: str("Video clip id"),which: { type: "string", enum: ["in", "out"], description: "Entrance or exit" },diff --git a/site/src/cut/components/TransitionsPanel.tsx b/site/src/cut/components/TransitionsPanel.tsxindex 6ba41620..a3f983e0 100644--- a/site/src/cut/components/TransitionsPanel.tsx+++ b/site/src/cut/components/TransitionsPanel.tsx@@ -23,6 +23,10 @@ import {type TransitionStyle,type VideoClip,} from "@/cut/lib/types";+import { CLOCK_SWEEP, shapeEase, takesFeather, TRANSITION_FEATHER_MAX } from "@/cut/lib/transitionShape";+import { Row, useSliderCheckpoint } from "@/cut/components/panelBits";+import { parseNumberInput } from "@/cut/components/ScrubValue";+import { ValueSlider } from "@/cut/components/ValueSlider";import { cn } from "@/lib/utils";import { ScrollArea } from "@/components/ui/scroll-area";@@ -64,6 +68,7 @@ export function TransitionsPanel() {<ScrollArea className="min-h-0 flex-1" contentClassName="px-3.5 pt-5 pb-4">{/* One key handler over every group, so arrows walk the whole listacross section boundaries. */}+ {live && takesFeather(live.style) && <SoftnessRow bar={live} />}<div onKeyDown={pickGridNav}>{TRANSITION_STYLE_GROUPS.map((g) => (<section key={g.label} className="mb-3 flex flex-col gap-1.5">@@ -81,6 +86,44 @@ export function TransitionsPanel() {);}+/** How soft the selected bar's reveal edge is: 0 is the hard edge, the top+ * of the range a ramp half the frame wide. A drag is one undo step. */+function SoftnessRow({ bar }: { bar: TimelineTransition }) {+ const ck = useSliderCheckpoint();+ const write = (v: number) => {+ ck.begin();+ useEditor.getState().updateTransitionTransient(bar.id, { feather: v > 0 ? v : undefined });+ };+ return (+ <div className="mb-3">+ <Row label="Softness">+ <ValueSlider+ label="Edge softness"+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={bar.feather ?? 0}+ min={0}+ max={TRANSITION_FEATHER_MAX}+ step={0.01}+ format={(v) => (v ? `${Math.round(v * 100)}%` : "Hard")}+ parse={(raw) => {+ if (raw.trim().toLowerCase() === "hard") {+ return 0;+ }+ const n = parseNumberInput(raw.replace(/%$/, ""));+ return n === null ? null : n / 100;+ }}+ onDraft={write}+ onCommit={(v) => {+ write(v);+ ck.end();+ }}+ />+ </Row>+ </div>+ );+}+/** The frame an asset shows at `srcT` seconds into its source: the nearest* filmstrip thumb for video, the picture itself for an image. */function frameAt(asset: MediaAsset, srcT: number): string | null {@@ -512,6 +555,41 @@ function TransitionSwatch({</>);break;+ case "clockwipe": {+ // A wedge pinned at the top-left corner sweeps from the top edge down+ // to the left one. Past the diagonal the wedge takes the far corner.+ const angle = shapeEase(style, p) * CLOCK_SWEEP;+ const wedge =+ angle <= Math.PI / 4+ ? `polygon(0 0, 100% 0, 100% ${pc(Math.tan(angle))})`+ : `polygon(0 0, 100% 0, 100% 100%, ${pc(1 / Math.tan(Math.max(angle, 1e-3)))} 100%)`;+ layers = (+ <>+ {A()}+ {B({ clipPath: wedge })}+ </>+ );+ break;+ }+ case "sliceleft":+ case "sliceup": {+ // Two halves slide opposite ways: the top half left and the bottom+ // right, or the left column up and the right one down.+ const e = shapeEase(style, p);+ const across = style === "sliceleft";+ const head = across ? "inset(0 0 50% 0)" : "inset(0 50% 0 0)";+ const tail = across ? "inset(50% 0 0 0)" : "inset(0 0 0 50%)";+ const move = (n: number) => (across ? `translateX(${pc(n)})` : `translateY(${pc(n)})`);+ layers = (+ <>+ {A({ clipPath: head, transform: move(-e) })}+ {B({ clipPath: head, transform: move(1 - e) })}+ {A({ clipPath: tail, transform: move(e) })}+ {B({ clipPath: tail, transform: move(e - 1) })}+ </>+ );+ break;+ }}return (<spandiff --git a/site/src/cut/components/entityIcons.ts b/site/src/cut/components/entityIcons.tsindex 32bfc85d..6afc3987 100644--- a/site/src/cut/components/entityIcons.ts+++ b/site/src/cut/components/entityIcons.ts@@ -12,14 +12,18 @@ import {Blend,Captions,Circle,+ Clock,Diamond,Droplets,Expand,FoldHorizontal,+ SplitSquareHorizontal,+ SplitSquareVertical,Folder,StickyNote,Heart,Hexagon,+ Brush,Layers,Minus,Moon,@@ -50,6 +54,7 @@ export const SHAPE_CHIP_ICONS: Record<ShapeKind, LucideIcon> = {hexagon: Hexagon,line: Minus,arrow: MoveRight,+ doodle: Brush,};/** The glyph a transition wears on its bar and on its mention pill. */@@ -72,6 +77,9 @@ export const TRANSITION_ICONS: Record<TransitionStyle, LucideIcon> = {circleclose: Target,splitopen: UnfoldHorizontal,splitclose: FoldHorizontal,+ clockwipe: Clock,+ sliceleft: SplitSquareHorizontal,+ sliceup: SplitSquareVertical,};/** The glyph an effect wears on its bar and on its mention pill: a waveformdiff --git a/site/src/cut/components/inspectorToolFields.test.ts b/site/src/cut/components/inspectorToolFields.test.tsindex 77c83247..e45f62d4 100644--- a/site/src/cut/components/inspectorToolFields.test.ts+++ b/site/src/cut/components/inspectorToolFields.test.ts@@ -67,6 +67,7 @@ const PANEL_SOURCES = ["RemovalPanel.tsx","CameraCardSection.tsx","ClipEffectsSection.tsx",+ "FlashPulseRows.tsx","AnimationTiles.tsx","TransitionsPanel.tsx","GroupPanel.tsx",@@ -94,12 +95,17 @@ const PANEL_NAME: Record<string, string> = {shadow_y: "shadow",shadow_color: "shadow",shadow_opacity: "shadow",+ pattern_width: "pattern",+ pattern_gap: "pattern",+ pattern_angle: "pattern",move_strength: "strength",in_style: "AnimationTiles",out_style: "AnimationTiles",loop_style: "AnimationTiles",in_seconds: "seconds",out_seconds: "seconds",+ in_stagger: "edgeStagger",+ out_stagger: "edgeStagger",loop_speed: "speed",words_style: "words",words_color: "words",diff --git a/site/src/cut/components/rgbSplit.ts b/site/src/cut/components/rgbSplit.tsnew file mode 100644index 00000000..1045d54d--- /dev/null+++ b/site/src/cut/components/rgbSplit.ts@@ -0,0 +1,105 @@+import type { EffectPreviewState } from "@donkeycut/effects-kit";++/**+ * The preview's channel split. A stage slice wears a glitch's rgb hit as an+ * SVG filter: the slice seen three times through a channel matrix — red moved+ * by the offset, green in place, blue moved back — and the three added up,+ * the same sum the canvas pass draws. The filters live in one hidden <svg>,+ * one per offset in whole px, and only the latest few are kept.+ */++const SVG_NS = "http://www.w3.org/2000/svg";++/** How many offsets keep their filter; a burst deals a new one per step. */+const KEPT_FILTERS = 16;++/** Each channel's matrix row set: the channel kept, the others zeroed. */+const CHANNELS: [string, number][] = [+ ["1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0", 1],+ ["0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0", 0],+ ["0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0", -1],+];++let defs: SVGDefsElement | null = null;+const made: string[] = [];++/** The CSS filter for the live split, "" when no effect is splitting. The+ * offsets are design px, scaled to the box's short side. */+export function rgbSplitFilter(states: EffectPreviewState[], width: number, height: number): string {+ const rgb = states.find((s) => s.rgb)?.rgb;+ if (!rgb) {+ return "";+ }+ const k = Math.min(width, height) / 1080;+ const dx = Math.round(rgb.dx * k);+ const dy = Math.round(rgb.dy * k);+ if (!dx && !dy) {+ return "";+ }+ const id = `cut-rgb-${dx}_${dy}`;+ ensureFilter(id, dx, dy);+ return `url(#${id})`;+}++/** Add the filter for one offset to the shared defs, dropping the oldest+ * past the keep. */+function ensureFilter(id: string, dx: number, dy: number): void {+ if (made.includes(id)) {+ return;+ }+ defs ??= createDefs();+ const filter = document.createElementNS(SVG_NS, "filter");+ filter.setAttribute("id", id);+ filter.setAttribute("color-interpolation-filters", "sRGB");+ filter.setAttribute("x", "-10%");+ filter.setAttribute("y", "-10%");+ filter.setAttribute("width", "120%");+ filter.setAttribute("height", "120%");++ // One moved, tinted copy per channel, then summed: "r" + "g", then + "b".+ CHANNELS.forEach(([values, sign], i) => {+ const offset = document.createElementNS(SVG_NS, "feOffset");+ offset.setAttribute("in", "SourceGraphic");+ offset.setAttribute("dx", String(sign * dx));+ offset.setAttribute("dy", String(sign * dy));+ offset.setAttribute("result", `o${i}`);+ const matrix = document.createElementNS(SVG_NS, "feColorMatrix");+ matrix.setAttribute("in", `o${i}`);+ matrix.setAttribute("type", "matrix");+ matrix.setAttribute("values", values);+ matrix.setAttribute("result", `c${i}`);+ filter.append(offset, matrix);+ });+ const sum = (a: string, b: string, result: string) => {+ const add = document.createElementNS(SVG_NS, "feComposite");+ add.setAttribute("in", a);+ add.setAttribute("in2", b);+ add.setAttribute("operator", "arithmetic");+ add.setAttribute("k2", "1");+ add.setAttribute("k3", "1");+ add.setAttribute("result", result);+ return add;+ };+ filter.append(sum("c0", "c1", "c01"), sum("c01", "c2", "out"));+ defs.append(filter);+ made.push(id);++ // The oldest offset goes once the keep is full.+ while (made.length > KEPT_FILTERS) {+ defs.querySelector(`#${CSS.escape(made.shift()!)}`)?.remove();+ }+}++/** The hidden <svg> the filters live in, made on first use. */+function createDefs(): SVGDefsElement {+ const svg = document.createElementNS(SVG_NS, "svg");+ svg.setAttribute("aria-hidden", "true");+ svg.setAttribute("width", "0");+ svg.setAttribute("height", "0");+ svg.style.position = "absolute";+ svg.style.pointerEvents = "none";+ const d = document.createElementNS(SVG_NS, "defs");+ svg.append(d);+ document.body.append(svg);+ return d;+}diff --git a/site/src/cut/components/textureCss.ts b/site/src/cut/components/textureCss.tsnew file mode 100644index 00000000..22812eca--- /dev/null+++ b/site/src/cut/components/textureCss.ts@@ -0,0 +1,69 @@+/**+ * The kit's text textures and erosion as CSS masks: each tile painted once on+ * a page canvas and handed to the DOM as a data URL, stretched over the box+ * the canvas painter cuts the same tile out of.+ */++import type { CSSProperties } from "react";+import { erodeTile, textureTile, type TextTexture, type TextureSurface } from "@donkeycut/effects-kit";++/** Where the DOM paints texture tiles: a page canvas, made once per texture. */+const DOM_TEXTURE: TextureSurface = {+ owner: {},+ make: (w, h) => {+ const c = document.createElement("canvas");+ c.width = w;+ c.height = h;+ return c;+ },+};+const textureMasks = new Map<TextTexture, CSSProperties>();++/** A texture as one box's CSS mask: the kit's tile of kept ink, stretched+ * over the glyph or word it sits on — the box the canvas cuts the same tile's+ * holes out of. */+export function textureMask(texture: TextTexture): CSSProperties {+ const known = textureMasks.get(texture);+ if (known) {+ return known;+ }+ const url = `url(${textureTile(texture, "keep", DOM_TEXTURE).toDataURL()})`;+ const mask: CSSProperties = {+ maskImage: url,+ WebkitMaskImage: url,+ maskSize: "100% 100%",+ WebkitMaskSize: "100% 100%",+ maskRepeat: "no-repeat",+ WebkitMaskRepeat: "no-repeat",+ };+ textureMasks.set(texture, mask);+ return mask;+}++/** Erosion tiles as data URLs, one per step the kit paints. */+const erodeUrls = new WeakMap<HTMLCanvasElement, string>();++/** A disintegration as the box's CSS mask: the kit's erosion tile of kept+ * ink stretched over the box, the box the canvas cuts the same tile's holes+ * out of. An element mask stays on as a second layer, the two intersected. */+export function erodeMask(erode: number, under: CSSProperties | null | undefined): CSSProperties {+ const tile = erodeTile(erode, "keep", DOM_TEXTURE);+ let url = erodeUrls.get(tile);+ if (!url) {+ url = `url(${tile.toDataURL()})`;+ erodeUrls.set(tile, url);+ }+ const image = under?.maskImage ? `${url}, ${under.maskImage}` : url;+ const size = under?.maskImage ? "100% 100%, 100% 100%" : "100% 100%";+ return {+ ...(under ?? {}),+ maskImage: image,+ WebkitMaskImage: image,+ maskSize: size,+ WebkitMaskSize: size,+ maskRepeat: "no-repeat",+ WebkitMaskRepeat: "no-repeat",+ ...(under?.maskImage ? { maskComposite: "intersect", WebkitMaskComposite: "source-in" } : {}),+ };+}+diff --git a/site/src/cut/hooks/usePlayback.ts b/site/src/cut/hooks/usePlayback.tsindex 7cbb0ff6..6bc73aa5 100644--- a/site/src/cut/hooks/usePlayback.ts+++ b/site/src/cut/hooks/usePlayback.ts@@ -12,7 +12,7 @@ import {import { headSrc, retimeOf, smoothsAt } from "@donkeycut/effects-kit";import { blendInto, SYNTH_EDGE, synthWeight } from "@/cut/lib/frameSynth";import { playheadAt, previewAt, setPlayhead, subscribePlayhead } from "@/cut/lib/playhead";-import { assetIsSilent, clipCovers, rectOf } from "@/cut/lib/types";+import { assetIsSilent, CLIP_MIN_SECONDS, clipCovers, rectOf } from "@/cut/lib/types";import type { AudioClip, ClipSpan, MediaAsset, VideoClip } from "@/cut/lib/types";import { OWN_SOUND, soundSourceOf, soundWindow } from "@/cut/lib/soundSource";import { SubjectMaskCompositor } from "@/cut/lib/behindPass";@@ -754,7 +754,7 @@ class Engine {const asset = s.assets.find((x) => x.id === a.assetId);if (!asset || a.hidden) continue;const rt = retimeOf(a);- const len = Math.max(0.1, rt.len);+ const len = Math.max(CLIP_MIN_SECONDS, rt.len);if (t < a.start || t >= a.start + len) continue;const v = audioVoice(a, asset, s.assets);if (!v || (v.url === asset.url && assetIsSilent(asset))) continue;@@ -849,6 +849,7 @@ class Engine {masterFx: {dx: plan.masterFxFrac.dx * this.canvas.width,dy: plan.masterFxFrac.dy * this.canvas.height,+ blur: plan.masterFxFrac.blur * Math.min(this.canvas.width, this.canvas.height),},incFrame,incClip: plan.incoming?.clip,diff --git a/site/src/cut/lib/aiContext.ts b/site/src/cut/lib/aiContext.tsindex 03e0b052..7e98b656 100644--- a/site/src/cut/lib/aiContext.ts+++ b/site/src/cut/lib/aiContext.ts@@ -48,7 +48,7 @@ function transitionToNext(sp: ClipSpan, index: number, spans: ClipSpan[]) {const seconds = sp.clip.transition ?? 0;if (seconds <= 0 || index >= spans.length - 1) return null;const style = sp.clip.transitionStyle ?? "crossfade";- return { style, seconds: r(sp.transitionOut) };+ return { style, seconds: r(sp.transitionOut), ...(sp.clip.transitionFeather ? { feather: sp.clip.transitionFeather } : {}) };}/** The clip's own effects — entrance/exit animations, box styling, and its@@ -634,6 +634,7 @@ function describeState(...(sp.clip.kf?.length? { keyframes: sp.clip.kf.map((k) => ({ ...k, t: r(k.t), x: r(k.x), y: r(k.y) })) }: {}),+ ...(sp.clip.motionBlur ? { motionBlur: r(sp.clip.motionBlur) } : {}),...clipEffects(sp.clip, live, { aspect: s.aspect, assetById }),})),// Video layers composited over track 0 in track order (the topmost@@ -670,6 +671,7 @@ function describeState(start: r(t.start),seconds: r(t.seconds),style: t.style,+ ...(t.feather ? { feather: t.feather } : {}),...(t.hidden ? { hidden: true } : {}),...(plays.length > 0? { plays: plays.map((p) => ({ at: p.kind, clipId: p.clipId })) }@@ -842,6 +844,7 @@ function describeOverlayClip(c: VideoClip, assets: Map<string, { name: string }>...(c.kf?.length? { keyframes: c.kf.map((k) => ({ ...k, t: r(k.t), x: r(k.x), y: r(k.y) })) }: {}),+ ...(c.motionBlur ? { motionBlur: r(c.motionBlur) } : {}),};}@@ -876,6 +879,8 @@ function describeOverlay(o: Overlay) {...(o.fillOpacity !== undefined ? { fillOpacity: r(o.fillOpacity) } : {}),...(o.radius ? { radius: o.radius } : {}),...(o.stroke ? { stroke: o.stroke } : {}),+ ...(o.pattern ? { pattern: o.pattern } : {}),+ ...(o.shadow ? { shadow: o.shadow } : {}),};}if (o.kind === "sticker") {@@ -889,6 +894,9 @@ function describeOverlay(o: Overlay) {...(o.effect === "zoom"? { focus: { x: r(o.focus?.x ?? 0.5), y: r(o.focus?.y ?? 0.5) } }: {}),+ ...(o.effect === "glitch" && o.glitch ? { glitch: o.glitch } : {}),+ ...(o.effect === "flash" && o.tone ? { tone: o.tone } : {}),+ ...(o.effect === "flash" && o.rate ? { rate: r(o.rate), rhythm: o.rhythm ?? "strobe" } : {}),};}return {@@ -902,5 +910,6 @@ function describeOverlay(o: Overlay) {plate: o.plate,...(o.plateRadius !== undefined && { plateRadius: r(o.plateRadius) }),...(o.wrapWidth !== undefined ? { wrapWidth: r(o.wrapWidth) } : {}),+ ...(o.texture ? { texture: o.texture } : {}),};}diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex e1b2108f..678144e1 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -62,7 +62,8 @@ import {type WheelTuple,type Mask,OVERLAY_ANIM_DEFAULT_SECONDS,- OVERLAY_ANIM_MAX_SECONDS,+ EDGE_STAGGER_MAX,+ edgeMaxSeconds,OVERLAY_ANIM_MIN_SECONDS,OVERLAY_ANIM_STYLE_IDS,TEXT_ONLY_ANIM_STYLE_IDS,@@ -95,6 +96,25 @@ import {penClosed,type RemovalStroke,type StrokeStyleId,+ CYCLE_SCALE_MAX,+ CYCLE_SCALE_MIN,+ FONT_CYCLE_FACES_MAX,+ FONT_CYCLE_RATE,+ FONT_CYCLE_RATE_MAX,+ FONT_CYCLE_RATE_MIN,+ TEXT_WEIGHTS,+ LEAK_COURSES,+ type LeakCourse,+ GLITCH_KINDS,+ type GlitchKind,+ FLASH_RATE_MAX,+ FLASH_RATE_MIN,+ FLASH_RHYTHMS,+ FLASH_TONES,+ TEXT_TEXTURES,+ type CycleFace,+ type FlashRhythm,+ type TextWeight,} from "@donkeycut/effects-kit";import type { AiPanelToolName } from "@/cut/components/AiPanel.tools";import type { OverlayAnimationToolName } from "@/cut/components/AnimationTiles.tools";@@ -107,7 +127,8 @@ import type { VideoGenToolName } from "@/cut/components/GeneratePanel.tools";import type { ImageGenToolName } from "@/cut/components/ImageGenPanel.tools";import type { InspectorToolName } from "@/cut/components/Inspector.tools";import type { GroupPanelToolName } from "@/cut/components/GroupPanel.tools";-import { CAMERA_SCALE_MAX, CAMERA_SCALE_MIN, CAMERA_WORLD_MAX, CAMERA_WORLD_MIN, ELEMENT_BLUR_MAX, isEaseId, upsertKey as upsertCameraKey, type CameraKey } from "@donkeycut/effects-kit";+import { CAMERA_SCALE_MAX, CAMERA_SCALE_MIN, CAMERA_WORLD_MAX, CAMERA_WORLD_MIN, ELEMENT_BLUR_MAX, TILT_MAX, isEaseId, upsertKey as upsertCameraKey, type CameraKey } from "@donkeycut/effects-kit";+import { cleanInks, LETTER_SPACING_MAX, LETTER_SPACING_MIN, PATTERN_KINDS, SHADOW_BLUR_MAX, SHADOW_OFFSET_MAX, STRIPE_GAP_MAX, STRIPE_LINE_MAX, STRIPE_LINE_MIN, STRIPES_DEFAULT, type PatternSpec, type ShadowSpec } from "@donkeycut/effects-kit";import { groupCameraOf, groupCameraPoseAt } from "@/cut/lib/groupCamera";import { cutMotion } from "@/cut/lib/motionSettings";import type { LibraryToolName } from "@/cut/components/LibraryView.tools";@@ -309,6 +330,7 @@ import {WATCH_DETAILS,type WatchDetail,} from "./types";+import { clampFeather } from "./transitionShape";import { soundSourceOf, SPLIT_EDIT_MAX_S } from "./soundSource";import { bindRecording, unbindRecording } from "./soundBind";@@ -485,6 +507,15 @@ const MAX_COMPARE = 4;const clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, v));const isNum = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);+const isTextWeight = (v: unknown): v is TextWeight => (TEXT_WEIGHTS as readonly unknown[]).includes(v);++/** A flash rhythm from a tool call: strobe, the default, stores as absence. */+const flashRhythm = (v: unknown): FlashRhythm | undefined => {+ if (!(FLASH_RHYTHMS as readonly unknown[]).includes(v)) {+ throw new ToolError(`rhythm is one of ${FLASH_RHYTHMS.join(", ")}.`);+ }+ return v === "flicker" ? "flicker" : undefined;+};/** A camera card places its clip's footage itself, so a framing write on* one of `fields` would change nothing anyone sees: refused, naming the@@ -598,6 +629,32 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {},});}+ // A light leak's course: drift is the default and stores as absence.+ if (effect === "lightleak" && input.leak !== undefined) {+ if (!(LEAK_COURSES as readonly unknown[]).includes(input.leak))+ throw new ToolError(`leak is one of ${LEAK_COURSES.join(", ")}.`);+ useEditor.getState().updateOverlay(sel.id, { leak: input.leak === "drift" ? undefined : (input.leak as LeakCourse) });+ }+ // A glitch pinned to one kind; the random mix stores as absence.+ if (effect === "glitch" && input.glitch !== undefined) {+ if (!(GLITCH_KINDS as readonly unknown[]).includes(input.glitch)) {+ throw new ToolError(`glitch is one of ${GLITCH_KINDS.join(", ")}.`);+ }+ useEditor.getState().updateOverlay(sel.id, { glitch: input.glitch as GlitchKind });+ }+ // A flash's tone and strobe: white and a single pop store as absence.+ if (effect === "flash" && input.tone !== undefined) {+ if (!(FLASH_TONES as readonly unknown[]).includes(input.tone)) {+ throw new ToolError(`tone is one of ${FLASH_TONES.join(", ")}.`);+ }+ useEditor.getState().updateOverlay(sel.id, { tone: input.tone === "black" ? "black" : undefined });+ }+ if (effect === "flash" && isNum(input.rate)) {+ useEditor.getState().updateOverlay(sel.id, { rate: input.rate > 0 ? clamp(input.rate, FLASH_RATE_MIN, FLASH_RATE_MAX) : undefined });+ }+ if (effect === "flash" && input.rhythm !== undefined) {+ useEditor.getState().updateOverlay(sel.id, { rhythm: flashRhythm(input.rhythm) });+ }const o = useEditor.getState().overlays.find((x) => x.id === sel.id)!;return { id: o.id, effect, start: round2(o.start), end: round2(o.end) };},@@ -607,8 +664,10 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const anim: OverlayAnim = { ...(o.anim ?? {}) };const edge = (slot: "in" | "out", styleKey: string, secondsKey: string) => {const raw = input[styleKey];+ // The cap is the style's the slot ends up with (a count runs longer).+ const styled = typeof raw === "string" && raw !== "none" ? raw : anim[slot]?.style;const secs = isNum(input[secondsKey])- ? clamp(input[secondsKey], OVERLAY_ANIM_MIN_SECONDS, OVERLAY_ANIM_MAX_SECONDS)+ ? clamp(input[secondsKey], OVERLAY_ANIM_MIN_SECONDS, edgeMaxSeconds(styled)): undefined;if (typeof raw === "string") {if (raw === "none") {@@ -622,12 +681,21 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const caret = keepCaret(anim[slot], raw);anim[slot] = {style: raw as OverlayAnimStyle,- seconds: secs ?? anim[slot]?.seconds ?? OVERLAY_ANIM_DEFAULT_SECONDS,+ // A long count's length shrinks to the new style's cap.+ seconds: Math.min(edgeMaxSeconds(raw), secs ?? anim[slot]?.seconds ?? OVERLAY_ANIM_DEFAULT_SECONDS),...(caret ? { caret } : {}),};} else if (secs !== undefined && anim[slot]) {anim[slot] = { ...anim[slot]!, seconds: secs };}+ // The letter hand-off rides the slot; a negative hands it back to+ // the style's own.+ const stagger = input[`${slot}_stagger`];+ if (isNum(stagger) && anim[slot]) {+ const rest = { ...anim[slot]! };+ delete rest.stagger;+ anim[slot] = stagger < 0 ? rest : { ...rest, stagger: clamp(stagger, 0, EDGE_STAGGER_MAX) };+ }};edge("in", "in_style", "in_seconds");edge("out", "out_style", "out_seconds");@@ -695,6 +763,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {}applyCaretInput(o, anim, input);applyHitInput(o, anim, input);+ applyFontCycleInput(o, anim, input);s.updateOverlay(o.id, { anim: hasOverlayAnim(anim) ? anim : undefined });const next = useEditor.getState().overlays.find((x) => x.id === o.id)!;return { id: next.id, anim: next.anim ?? null };@@ -722,7 +791,11 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {...(isNum(k.x) ? { x: clampOverlayPos(k.x, o) } : {}),...(isNum(k.y) ? { y: clampOverlayPos(k.y, o) } : {}),...(isNum(k.scale) ? { scale: clamp(k.scale, 0.1, 4) } : {}),+ ...(isNum(k.scale_x) ? { scaleX: clamp(k.scale_x, 0, 4) } : {}),+ ...(isNum(k.scale_y) ? { scaleY: clamp(k.scale_y, 0, 4) } : {}),...(isNum(k.rotation) ? { rotation: clamp(Math.round(k.rotation), -180, 180) } : {}),+ ...(isNum(k.tilt_x) ? { tiltX: clamp(Math.round(k.tilt_x), -TILT_MAX, TILT_MAX) } : {}),+ ...(isNum(k.tilt_y) ? { tiltY: clamp(Math.round(k.tilt_y), -TILT_MAX, TILT_MAX) } : {}),...(isNum(k.opacity) ? { opacity: clamp(k.opacity, 0, 1) } : {}),...(isNum(k.blur) ? { blur: clamp(k.blur, 0, ELEMENT_BLUR_MAX) } : {}),...(k.ease !== undefined ? { ease: easeInput(k.ease) } : {}),@@ -779,9 +852,21 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {set_clip_keyframes: (s, input) => {const clip = requireItem(s.clips, input.clipId, "video clip");const raw = input.keys;- if (!Array.isArray(raw)) throw new ToolError("keys must be a list.");const dur = clipLen(clip);let kf = clip.kf;++ // The shutter rides beside the track: on, off, or kept. With no keys+ // in the call it is the whole edit and its own undo step; with keys it+ // joins theirs.+ const motionBlur = shutterInput(input, clip.motionBlur);+ if (raw === undefined) {+ if (motionBlur !== clip.motionBlur) {+ s.pushHistory();+ s.updateClipTransient(clip.id, { motionBlur });+ }+ return { id: clip.id, keys: (kf ?? []).map((k) => ({ ...k, t: round2(k.t) })), motionBlur: motionBlur ?? null };+ }+ if (!Array.isArray(raw)) throw new ToolError("keys must be a list.");if (raw.length === 0) {s.clearClipKeys(clip.id);kf = undefined;@@ -800,13 +885,17 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {...(isNum(k.scale) ? { scale: clamp(k.scale, 0.1, 4) } : {}),...(isNum(k.rotation) ? { rotation: clamp(Math.round(k.rotation), -180, 180) } : {}),...(isNum(k.opacity) ? { opacity: clamp(k.opacity, 0, 1) } : {}),+ ...(isNum(k.blur) ? { blur: clamp(k.blur, 0, ELEMENT_BLUR_MAX) } : {}),},{ transient: true });}kf = useEditor.getState().clips.find((c) => c.id === clip.id)?.kf;}- return { id: clip.id, keys: (kf ?? []).map((k) => ({ ...k, t: round2(k.t) })) };+ if (motionBlur !== clip.motionBlur) {+ useEditor.getState().updateClipTransient(clip.id, { motionBlur });+ }+ return { id: clip.id, keys: (kf ?? []).map((k) => ({ ...k, t: round2(k.t) })), motionBlur: motionBlur ?? null };},set_mask: (s, input) => {@@ -944,18 +1033,25 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {if (!style) throw new ToolError(`Unknown style. Use one of: ${TRANSITION_STYLE_IDS.join(", ")}.`);}s.setClipTransition(clip.id, input.seconds, style);- const after = useEditor.getState();- const next = after.clips.find((c) => c.id === clip.id)!;+ let after = useEditor.getState();// The bar's own id rides the result so remove_transition can round-trip it.const roles = resolveTransitions(after.clips, after.transitions);const bar = after.transitions.find((t) =>(roles.get(t.id) ?? []).some((r) => r.kind !== "in" && r.clipId === clip.id));+ // The softness lands on the bar inside the same undo step.+ if (bar && isNum(input.feather)) {+ const feather = clampFeather(input.feather);+ after.updateTransitionTransient(bar.id, { feather: feather > 0 ? feather : undefined });+ after = useEditor.getState();+ }+ const next = after.clips.find((c) => c.id === clip.id)!;return {id: next.id,transitionId: bar?.id ?? null,transition: next.transition ?? 0,style: next.transitionStyle ?? "crossfade",+ ...(next.transitionFeather ? { feather: next.transitionFeather } : {}),};},@@ -2465,6 +2561,10 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {add_title: (s, input) => {if (typeof input.text !== "string" || !input.text.trim())throw new ToolError("text is required.");+ // Refused before the title exists, so a bad font leaves nothing behind.+ if (input.font !== undefined) {+ knownFont(input.font);+ }if (isNum(input.start)) s.seek(input.start);s.addOverlay(aimedLane(input));const sel = useEditor.getState().selection;@@ -2590,8 +2690,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {patch.style = input.style as CaptionStyleId;}if (isNum(input.size)) patch.size = clamp(Math.round(input.size), 16, 200);- if (typeof input.font === "string" && allFonts().some((f) => f.id === input.font))- patch.font = input.font;+ if (input.font !== undefined) {+ patch.font = knownFont(input.font);+ }if (typeof input.word_highlight === "boolean") patch.wordHighlight = input.word_highlight;if (typeof input.accent_color === "string") patch.accentColor = input.accent_color;if (isNum(input.accent_scale))@@ -2614,8 +2715,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {if (typeof input.emphasis_color === "string") patch.emphasisColor = input.emphasis_color;if (typeof input.emphasis_italic === "boolean") patch.emphasisItalic = input.emphasis_italic;if (input.emphasis_weight !== undefined) {- if (input.emphasis_weight !== 400 && input.emphasis_weight !== 700)- throw new ToolError("emphasis_weight is 400 or 700.");+ if (!isTextWeight(input.emphasis_weight))+ throw new ToolError(`emphasis_weight is one of ${TEXT_WEIGHTS.join(", ")}.`);patch.emphasisWeight = input.emphasis_weight;}if (isNum(input.emphasis_scale))@@ -2968,13 +3069,30 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const clip = requireItem(s.clips, input.clipId, "video clip");if (!Array.isArray(input.effects)) throw new ToolError("effects must be a list; [] removes them all.");- // Each entry names a clip effect once, with an optional amount in range.+ // Each entry names a clip effect once, with an optional amount in range;+ // a flash also takes its tone, strobe rate and rhythm.const effects: ClipEffect[] = [];- for (const e of input.effects as { effect?: unknown; amount?: unknown }[]) {+ for (const e of input.effects as { effect?: unknown; amount?: unknown; tone?: unknown; rate?: unknown; rhythm?: unknown }[]) {const effect = CLIP_EFFECT_IDS.find((id) => id === e?.effect);if (!effect) throw new ToolError(`effect must be one of ${CLIP_EFFECT_IDS.join(", ")}.`);if (effects.some((x) => x.effect === effect)) throw new ToolError(`${effect} is listed twice; a clip wears an effect once.`);- effects.push(isNum(e.amount) ? { effect, amount: Math.max(0.05, Math.min(1, e.amount)) } : { effect });+ const entry: ClipEffect = isNum(e.amount) ? { effect, amount: Math.max(0.05, Math.min(1, e.amount)) } : { effect };+ if (effect === "flash") {+ if (e.tone !== undefined && !(FLASH_TONES as readonly unknown[]).includes(e.tone)) {+ throw new ToolError(`tone is one of ${FLASH_TONES.join(", ")}.`);+ }+ if (e.tone === "black") {+ entry.tone = "black";+ }+ if (isNum(e.rate) && e.rate > 0) {+ entry.rate = clamp(e.rate, FLASH_RATE_MIN, FLASH_RATE_MAX);+ }+ const rhythm = e.rhythm === undefined ? undefined : flashRhythm(e.rhythm);+ if (rhythm) {+ entry.rhythm = rhythm;+ }+ }+ effects.push(entry);}s.updateClip(clip.id, { effects: effects.length ? effects : undefined });return { id: clip.id, effects };@@ -6239,6 +6357,71 @@ function applyHitInput(};}+/** set_overlay_animation's font-cycle fields, written onto `anim`. A new+ * cycle runs from the element's start to its end at the default rate; an+ * empty face list removes it. Only font ids the registry knows are kept, so+ * the cycle never shows a face the user could not pick. */+function applyFontCycleInput(+ o: { kind?: string; start: number; end: number },+ anim: OverlayAnim,+ input: Record<string, unknown>+): void {+ const raw = input.font_cycle;+ const named =+ raw !== undefined ||+ isNum(input.font_cycle_at) ||+ isNum(input.font_cycle_seconds) ||+ isNum(input.font_cycle_rate);+ if (!named) return;++ // An empty list clears the cycle.+ if (Array.isArray(raw) && raw.length === 0) {+ delete anim.fonts;+ return;+ }+ if ((o.kind ?? "text") !== "text") throw new ToolError("A font cycle changes a title's face; it needs a title.");+ if (raw !== undefined && !Array.isArray(raw)) throw new ToolError("font_cycle must be a list of faces.");+ const faces = Array.isArray(raw) ? raw.slice(0, FONT_CYCLE_FACES_MAX).map(cycleFaceOf) : anim.fonts?.faces;+ if (!faces) throw new ToolError("Name font_cycle to add a font cycle.");++ // Timing: held inside the element, with the rate in its supported range.+ const dur = Math.max(0.1, o.end - o.start);+ const at = clamp(isNum(input.font_cycle_at) ? input.font_cycle_at : (anim.fonts?.at ?? 0), 0, dur - 0.05);+ const seconds = clamp(+ isNum(input.font_cycle_seconds) ? input.font_cycle_seconds : (anim.fonts?.seconds ?? dur - at),+ 0.05,+ dur - at+ );+ const rate = isNum(input.font_cycle_rate)+ ? clamp(input.font_cycle_rate, FONT_CYCLE_RATE_MIN, FONT_CYCLE_RATE_MAX)+ : (anim.fonts?.rate ?? FONT_CYCLE_RATE);+ const round = (v: number) => Math.round(v * 1000) / 1000;+ anim.fonts = { faces, at: round(at), seconds: round(seconds), rate };+}++/** A font id the registry knows, or a refusal naming the one it does not. */+function knownFont(v: unknown): FontId {+ if (typeof v !== "string" || !allFonts().some((f) => f.id === v)) {+ throw new ToolError(`Unknown font id "${String(v)}".`);+ }+ return v as FontId;+}++/** One font_cycle entry as a stored face, checked against the registry. */+function cycleFaceOf(raw: unknown): CycleFace {+ const f = (raw ?? {}) as Record<string, unknown>;+ if (typeof f.font !== "string" || !allFonts().some((x) => x.id === f.font))+ throw new ToolError(`Unknown font id "${String(f.font)}".`);+ if (f.weight !== undefined && !isTextWeight(f.weight))+ throw new ToolError(`A face's weight is one of ${TEXT_WEIGHTS.join(", ")}.`);+ return {+ font: f.font,+ ...(isTextWeight(f.weight) ? { weight: f.weight } : {}),+ ...(isNum(f.scale) && f.scale !== 1 ? { scale: clamp(f.scale, CYCLE_SCALE_MIN, CYCLE_SCALE_MAX) } : {}),+ ...(isNum(f.tracking) ? { tracking: clamp(f.tracking, -0.05, 0.5) } : {}),+ };+}+function requireItem<T extends { id: string }>(pool: T[], id: unknown, label: string): T {const item = pool.find((x) => x.id === String(id ?? ""));if (!item) throw new ToolError(`No ${label} with id ${String(id)}. Call get_state for current ids.`);@@ -6586,10 +6769,82 @@ function easeInput(v: unknown) {return v === "linear" ? undefined : v;}+/** A shadow from a call, or null when the call leaves it alone: `shadow`+ * false turns it off (as `off`), true turns it on, and any shadow_* field+ * lands on top of the current one. Example: shadow_color "#00E5FF" with+ * shadow_y 0 is a cyan glow. */+function shadowInput(+ input: Record<string, unknown>,+ current: boolean | ShadowSpec | undefined,+ off: false | undefined+): { value: boolean | ShadowSpec | undefined } | null {+ if (input.shadow === false) {+ return { value: off };+ }+ const tuned =+ typeof input.shadow_color === "string" || isNum(input.shadow_blur) || isNum(input.shadow_opacity) || isNum(input.shadow_y);+ if (!tuned) {+ return input.shadow === true ? { value: current || true } : null;+ }++ // Tuning starts from the current spec, or the default look when it is off.+ const spec: ShadowSpec = typeof current === "object" ? { ...current } : {};+ if (typeof input.shadow_color === "string") {+ if (!HEX_COLOR.test(input.shadow_color)) {+ throw new ToolError("shadow_color is a hex color like #00E5FF.");+ }+ spec.color = input.shadow_color;+ }+ if (isNum(input.shadow_blur)) {+ spec.blur = clamp(input.shadow_blur, 0, SHADOW_BLUR_MAX);+ }+ if (isNum(input.shadow_opacity)) {+ spec.opacity = clamp(input.shadow_opacity, 0, 1);+ }+ if (isNum(input.shadow_y)) {+ spec.offsetY = clamp(input.shadow_y, -SHADOW_OFFSET_MAX, SHADOW_OFFSET_MAX);+ }+ return { value: spec };+}++/** Six-digit hex, the form a shadow color is stored in. */+const HEX_COLOR = /^#[0-9a-fA-F]{6}$/;++/** A shape's pattern from a call, or null when the call leaves it alone:+ * "none" clears it, a kind or any pattern_* field lands on the current one. */+function patternInput(+ input: Record<string, unknown>,+ current: PatternSpec | undefined+): { value: PatternSpec | undefined } | null {+ if (input.pattern === "none") {+ return { value: undefined };+ }+ if (input.pattern !== undefined && !(PATTERN_KINDS as readonly unknown[]).includes(input.pattern)) {+ throw new ToolError(`pattern is one of ${[...PATTERN_KINDS, "none"].join(", ")}.`);+ }+ const tuned = isNum(input.pattern_width) || isNum(input.pattern_gap) || isNum(input.pattern_angle);+ if (input.pattern === undefined && !tuned) {+ return null;+ }++ // Stripes are the one kind today; fields land on the current pattern.+ const spec: PatternSpec = { ...(current ?? STRIPES_DEFAULT) };+ if (isNum(input.pattern_width)) {+ spec.width = clamp(input.pattern_width, STRIPE_LINE_MIN, STRIPE_LINE_MAX);+ }+ if (isNum(input.pattern_gap)) {+ spec.gap = clamp(input.pattern_gap, 0, STRIPE_GAP_MAX);+ }+ if (isNum(input.pattern_angle)) {+ spec.angle = Math.round(clamp(input.pattern_angle, -90, 90)) || undefined;+ }+ return { value: spec };+}+function overlayPatch(input: Record<string, unknown>,kind: "text" | "shape" | "sticker" | "effect",- place?: { groupId?: string }+ place?: { groupId?: string; shadow?: boolean | ShadowSpec; pattern?: PatternSpec }) {const patch: Record<string, unknown> = {};if (isNum(input.start)) patch.start = Math.max(0, input.start);@@ -6615,23 +6870,34 @@ function overlayPatch(patch.text = input.text.replace(/\\n/g, "\n");if (isNum(input.size)) patch.size = clamp(Math.round(input.size), 16, 320);if (typeof input.color === "string") patch.color = input.color;- // Only ids the registry knows: an unknown one resolves to the default- // face, so storing it would silently render a font the user never chose- // and leave the Inspector's font menu blank.- if (typeof input.font === "string" && allFonts().some((f) => f.id === input.font))- patch.font = input.font as FontId;- if (input.weight === 400 || input.weight === 700) patch.weight = input.weight;+ // Only ids the registry knows: an unknown one is refused, since it would+ // render the default face and leave the Inspector's font menu blank.+ if (input.font !== undefined) {+ patch.font = knownFont(input.font);+ }+ if (isTextWeight(input.weight)) patch.weight = input.weight;if (typeof input.italic === "boolean") patch.italic = input.italic || undefined;if (["left", "center", "right"].includes(String(input.align)))patch.align = input.align === "center" ? undefined : input.align;if (isNum(input.letter_spacing))- patch.letterSpacing = Math.abs(input.letter_spacing) < 0.0025 ? undefined : clamp(input.letter_spacing, -0.05, 0.5);+ patch.letterSpacing = Math.abs(input.letter_spacing) < 0.0025 ? undefined : clamp(input.letter_spacing, LETTER_SPACING_MIN, LETTER_SPACING_MAX);if (isNum(input.line_height))patch.lineHeight = Math.abs(input.line_height - 1.25) < 0.01 ? undefined : clamp(input.line_height, 0.7, 2.5);if (isNum(input.wrap_width))patch.wrapWidth = input.wrap_width <= 0 ? undefined : clamp(input.wrap_width, 0.01, 2);- if (typeof input.shadow === "boolean") patch.shadow = input.shadow;+ const shadow = shadowInput(input, place?.shadow, false);+ if (shadow) {+ patch.shadow = shadow.value;+ }if (typeof input.plate === "boolean") patch.plate = input.plate;+ // A texture names one of the kit's grains; "none" stores as absence.+ if (input.texture === "none") {+ patch.texture = undefined;+ } else if ((TEXT_TEXTURES as readonly unknown[]).includes(input.texture)) {+ patch.texture = input.texture;+ } else if (input.texture !== undefined) {+ throw new ToolError(`texture is one of ${[...TEXT_TEXTURES, "none"].join(", ")}.`);+ }if (isNum(input.plateRadius)) patch.plateRadius = clamp(input.plateRadius, 0, 1);if (typeof input.stroke_color === "string" || isNum(input.stroke_width)) {const width = isNum(input.stroke_width) ? clamp(input.stroke_width, 0, 0.3) : 0.04;@@ -6657,6 +6923,21 @@ function overlayPatch(? { color: typeof input.stroke_color === "string" ? input.stroke_color : "#111114", width }: undefined;}+ const pattern = patternInput(input, place?.pattern);+ if (pattern) {+ patch.pattern = pattern.value;+ }++ // A doodle's inks replace the list whole; an empty list leaves the fill+ // as its one paint.+ if (input.inks !== undefined) {+ const inks = cleanInks(input.inks);+ patch.inks = inks.length > 0 ? inks : undefined;+ }+ const shadow = shadowInput(input, place?.shadow, undefined);+ if (shadow) {+ patch.shadow = shadow.value;+ }return patch;}diff --git a/site/src/cut/lib/audioMix.ts b/site/src/cut/lib/audioMix.tsindex 0380dbfb..c026ef14 100644--- a/site/src/cut/lib/audioMix.ts+++ b/site/src/cut/lib/audioMix.ts@@ -27,6 +27,7 @@ import {type SpeedNode,} from "@donkeycut/effects-kit";import { decodeAudioSpan } from "./mediaRead";+import { CLIP_MIN_SECONDS } from "./types";import { fitSpan, retimeFits } from "./retimeFit";import { OWN_SOUND, shiftSpan, soundWindow, type SoundWindow, type SpecSound } from "./soundSource";@@ -138,7 +139,7 @@ export interface MixOptions {* spacer keeps its exact length — flooring it would land everything after it* late. */const clipDur = (c: MixClip) =>- c.file ? Math.max(0.1, retimeOf(c).len) : Math.max(0, c.out - c.in);+ c.file ? Math.max(CLIP_MIN_SECONDS, retimeOf(c).len) : Math.max(0, c.out - c.in);/** Timeline footprint of a placed item. */const itemDur = (a: MixItem) => Math.max(0, retimeOf(a).len);diff --git a/site/src/cut/lib/behindPass.ts b/site/src/cut/lib/behindPass.tsindex e44d0ad3..ce801111 100644--- a/site/src/cut/lib/behindPass.ts+++ b/site/src/cut/lib/behindPass.ts@@ -11,25 +11,34 @@import {applyWordDraw,+ countStep,+ countText,+ cutAtFaces,diveView,divesAt,+ erodeLevel,evalOverlayFrame,+ fontCycleCuts,measureDiveFocus,peekDiveFocus,glyphStateAt,hasGlyphMotion,maskComposite,overlayWords,- paintElementInto,+ paintElementCrop,+ typedChars,+ withFontCycle,wordSampleWindows,+ zapSeed,type OverlayFrameState,type PaintPhase,+ type ZapPhase,} from "@donkeycut/effects-kit";import { ElementFx, elementLook } from "@donkeycut/effects-kit";import { personSegmenter, segmentSubjectAlpha } from "./cutout";import { allowance, holdMemory } from "./memoryBudget";-import { createRasterCanvas } from "./raster";-import { cutRenderEnv, renderElementPng } from "./textRender";+import { createRasterCanvas, snapshotRaster } from "./raster";+import { cutRenderEnv } from "./textRender";import {behindSubjectOverlay,frontSubjectOverlay,@@ -53,9 +62,14 @@ const BEHIND_WORD_FPS = 8;/** The spans an element's word effect holds still for — one picture each.* Nothing here means the element draws as one piece. */function rasterSpans(o: Overlay): { start: number; end: number }[] | null {+ if (!isTextOverlay(o)) return null;+ // A font cycle changes the type at every step: each face gets its own+ // picture, cut inside the word spans when both run.const words = overlayWords(o);- if (!words || !isTextOverlay(o)) return null;- const spans = wordSampleWindows(o.text, words, spanOf(o), BEHIND_WORD_FPS);+ const faces = fontCycleCuts(o, spanOf(o));+ if (!words && faces.length === 0) return null;+ const base = words ? wordSampleWindows(o.text, words, spanOf(o), BEHIND_WORD_FPS) : [{ start: 0, end: spanOf(o) }];+ const spans = cutAtFaces(base, faces);return spans.length > 0 ? spans : null;}@@ -80,27 +94,54 @@ function rasterSliceAt(o: Overlay, tLocal: number): number {* typing bar is lit — the same reading the front path's painters take from* the evaluator. A whole-text picture keys at zero or above (two per word* slice, bar dark and lit); a typing picture keys below zero, one per typed- * count.+ * count, and a counting picture likewise, one per step its numbers take.+ * A disintegrating or crackling moment keys below zero too, one picture per+ * erosion step and arc deal.*/interface Look {key: number;word: number;chars: number;caret: boolean;+ /** Where a counting picture's numbers stand, 0..1 (count). */+ count?: number;+ /** The share of the ink a disintegration has eaten, at its erosion step. */+ erode?: number;+ /** The arcs a zap hit paints over the picture. */+ zap?: ZapPhase;}/** The picture `o` needs at `tLocal`. */function lookAt(o: Overlay, tLocal: number, ev: OverlayFrameState): Look {const word = rasterSliceAt(o, tLocal);const caret = ev.caret === true;- const n = isTextOverlay(o) ? o.text.length : 0;- const chars =- isTextOverlay(o) && ev.textProgress !== undefined- ? Math.max(0, Math.min(n, Math.ceil(ev.textProgress * n)))- : -1;+ const n = isTextOverlay(o) ? [...o.text].length : 0;+ const chars = isTextOverlay(o) && ev.textProgress !== undefined ? typedChars(o.text, ev.textProgress) : -1;const bar = caret ? 1 : 0;- const key = chars < 0 ? (word + 1) * 2 + bar : -1 - (((word + 1) * (n + 1) + chars) * 2 + bar);- return { key, word, chars, caret };+ // A count keys its pictures by how far its numbers have counted, out of+ // every step they take ("100 %" takes 100).+ const count = isTextOverlay(o) ? ev.countProgress : undefined;+ const text = isTextOverlay(o) ? o.text : "";+ const step = count !== undefined ? countStep(text, count) : chars;+ const steps = count !== undefined ? countStep(text, 1) : n;+ // An erosion step and an arc deal fold into a phase slot: 0 is neither,+ // e.g. step 3 under deal 5 is 3 + 25 × 6 with 24 erosion steps.+ const level = ev.erode !== undefined ? erodeLevel(ev.erode) : 0;+ const zap = ev.zap ? { amount: ev.zap, seed: zapSeed(tLocal) } : undefined;+ const levels = erodeLevel(1) + 1;+ const phase = level + levels * (zap ? zap.seed + 1 : 0);+ const phases = levels * (zapSeed(spanOf(o)) + 2);+ const picture = ((word + 1) * (steps + 2) + step + 1) * 2 + bar;+ const key = step < 0 && phase === 0 ? (word + 1) * 2 + bar : -1 - (picture * phases + phase);+ return {+ key,+ word,+ chars,+ caret,+ ...(count !== undefined ? { count } : {}),+ ...(level > 0 ? { erode: level / (levels - 1) } : {}),+ ...(zap ? { zap } : {}),+ };}/** The phase a picture with its typing bar lit is painted at. */@@ -228,6 +269,12 @@ export class SubjectMaskCompositor {private person: HTMLCanvasElement | null = null;/** Scratch for blurred and motion-blurred behind elements. */private fx = new ElementFx(createRasterCanvas);+ /** Where each picture's top-left sits in the frame: pictures are cropped to+ * the element's ink, however far past the frame it reaches, so a pose that+ * shrinks an oversize element keeps its edges. */+ private origins = new WeakMap<ImageBitmap, { x: number; y: number }>();+ /** The crop's mask scratch, made on first use. */+ private cropMask: HTMLCanvasElement | null = null;private small: HTMLCanvasElement | null = null;private mask: { at: number; alpha: HTMLCanvasElement | null } = { at: -1e9, alpha: null };/** A second matte slot for mid-stack clip masks: it snapshots the canvas as@@ -328,13 +375,17 @@ export class SubjectMaskCompositor {}/** The element as one picture shows it: its words as they stand across- * the span the picture covers, `chars` of its text typed (-1 = all). */- private pictureOf(o: Overlay, word: number, chars: number): Overlay {+ * the span the picture covers, `chars` of its text typed (-1 = all), its+ * numbers counted to `count`. */+ private pictureOf(o: Overlay, word: number, count?: number): Overlay {const spans = rasterSpans(o);const span = word >= 0 ? spans?.[word] : undefined;const at = span ? (span.start + span.end) / 2 : 0;- const el = applyWordDraw({ ...o, rotation: undefined, opacity: undefined }, at, spanOf(o));- return chars >= 0 && isTextOverlay(el) ? { ...el, text: el.text.slice(0, chars) } : el;+ const el = withFontCycle(applyWordDraw({ ...o, rotation: undefined, opacity: undefined }, at, spanOf(o)), at);+ if (count !== undefined && isTextOverlay(el)) {+ return { ...el, text: countText(el.text, count) };+ }+ return el;}/** One whole-text picture, kept under the element it came from. */@@ -347,22 +398,38 @@ export class SubjectMaskCompositor {word: number,caret: boolean): Promise<void> {- const png = await renderElementPng(this.pictureOf(o, word, -1), w, h, assets, caret ? CARET_LIT : undefined);- this.land(o, key, await createImageBitmap(png));+ const surface = scratchCanvas();+ const origin = await paintElementCrop(surface, this.pictureOf(o, word), w, h, cutRenderEnv(assets), {+ t: 0,+ phase: caret ? CARET_LIT : undefined,+ maskScratch: (this.cropMask ??= scratchCanvas()),+ });+ const bmp = await snapshotRaster(surface);+ surface.width = surface.height = 1;+ this.origins.set(bmp, origin);+ this.land(o, key, bmp);}/** One typing picture, painted on the shared surface. */private async drawTyped(o: Overlay, w: number, h: number, assets: MediaAsset[], look: Look): Promise<void> {const surface = (this.typeSurface ??= scratchCanvas());- if (surface.width !== w) surface.width = w;- if (surface.height !== h) surface.height = h;- await paintElementInto(- surface,- this.pictureOf(o, look.word, look.chars),- cutRenderEnv(assets),- look.caret ? CARET_LIT : undefined- );- this.land(o, look.key, await createImageBitmap(surface));+ // A typing picture keeps the whole text's layout and shows its typed head.+ // A disintegrating or crackling picture carries its erosion and arcs.+ const typing: PaintPhase | undefined = look.chars >= 0 ? { typed: look.chars } : undefined;+ const lived: PaintPhase = {+ ...(look.caret ? CARET_LIT : {}),+ ...typing,+ ...(look.erode !== undefined ? { erode: look.erode } : {}),+ ...(look.zap ? { zap: look.zap } : {}),+ };+ const origin = await paintElementCrop(surface, this.pictureOf(o, look.word, look.count), w, h, cutRenderEnv(assets), {+ t: 0,+ phase: Object.keys(lived).length > 0 ? lived : undefined,+ maskScratch: (this.cropMask ??= scratchCanvas()),+ });+ const bmp = await snapshotRaster(surface);+ this.origins.set(bmp, origin);+ this.land(o, look.key, bmp);}/** Paint the typing pictures elements are waiting on, one after another,@@ -448,6 +515,10 @@ export class SubjectMaskCompositor {this.typeSurface.height = 1;this.typeSurface = null;}+ if (this.cropMask) {+ this.cropMask.width = this.cropMask.height = 1;+ this.cropMask = null;+ }this.releaseMemory();}@@ -669,17 +740,18 @@ export class SubjectMaskCompositor {if (alphaOf <= 0.001) continue;const cx = o.x * W;const cy = o.y * H;- // A blurred, streaking or darkening element poses into the scratch- // alone and lands softened or dimmed.- const target = this.fx.begin(ctx, W, H, elementLook(ev, scale)) as CanvasRenderingContext2D;+ // A blurred, streaking, darkening or tilted element poses into the+ // scratch alone and lands softened, dimmed or turned.+ const at = {+ x: ev.x * W + (ev.dx + (g?.dx ?? 0)) * scale,+ y: ev.y * H + (ev.dy + (g?.dy ?? 0)) * scale,+ };+ const target = this.fx.begin(ctx, W, H, elementLook(ev, scale, at)) as CanvasRenderingContext2D;target.save();target.globalAlpha = alphaOf;- target.translate(- ev.x * W + (ev.dx + (g?.dx ?? 0)) * scale,- ev.y * H + (ev.dy + (g?.dy ?? 0)) * scale- );+ target.translate(at.x, at.y);target.rotate(((ev.rotation + (g?.rotate ?? 0)) * Math.PI) / 180);- target.scale(ev.scale * (g?.sx ?? 1), ev.scale * (g?.sy ?? 1));+ target.scale(ev.scale * (ev.sx ?? 1) * (g?.sx ?? 1), ev.scale * (ev.sy ?? 1) * (g?.sy ?? 1));target.translate(-cx, -cy);// A dive flies the same one picture in, under the view the other// renderers draw their type under.@@ -690,7 +762,8 @@ export class SubjectMaskCompositor {target.scale(v.s, v.s);target.translate(-v.fx, -v.fy);}- target.drawImage(bmp, 0, 0, W, H);+ const origin = this.origins.get(bmp);+ target.drawImage(bmp, origin?.x ?? 0, origin?.y ?? 0);target.restore();this.fx.end(ctx);}diff --git a/site/src/cut/lib/behindPassPhase.test.ts b/site/src/cut/lib/behindPassPhase.test.tsnew file mode 100644index 00000000..b80b3415--- /dev/null+++ b/site/src/cut/lib/behindPassPhase.test.ts@@ -0,0 +1,84 @@+import { beforeAll, describe, expect, test } from "bun:test";+import { evalOverlayFrame } from "@donkeycut/effects-kit";+import { SubjectMaskCompositor } from "./behindPass";+import { installSkiaRaster } from "./headless/skiaRaster";+import { createRasterCanvas } from "./raster";+import type { TextOverlay } from "./types";++/**+ * Elements behind the speaker disintegrate and crackle like the front path:+ * a moment of an exit or a zap hit is its own picture with the phase painted.+ */++const W = 540;+const H = 960;+const title: TextOverlay = {+ id: "soon",+ text: "SOON",+ start: 1,+ end: 4,+ x: 0.5,+ y: 0.5,+ size: 160,+ font: "sf",+ weight: 900,+ color: "#ffffff",+ plate: false,+ shadow: false,+ mask: { kind: "subject", invert: true } as TextOverlay["mask"],+ anim: { out: { style: "disintegrate", seconds: 0.6 }, hit: { style: "zap", at: 0.5, seconds: 0.6 } },+};++type Entry = { byWord: Map<number, CanvasImageSource> };+const pictures = (pass: SubjectMaskCompositor) =>+ [...(pass as unknown as { rasters: Map<string, Entry> }).rasters.get(title.id)!.byWord.values()];++/** White ink and blue arc pixels in a picture. */+function ink(picture: CanvasImageSource) {+ const c = createRasterCanvas(W, H) as HTMLCanvasElement;+ const ctx = c.getContext("2d")!;+ ctx.drawImage(picture, 0, 0, W, H);+ const d = ctx.getImageData(0, 0, W, H).data;+ let white = 0;+ let blue = 0;+ for (let i = 0; i < d.length; i += 4) {+ if (d[i + 3] < 120) continue;+ if (d[i] > 220 && d[i + 1] > 220 && d[i + 2] > 220) white++;+ else if (d[i + 2] > 180 && d[i] < 200) blue++;+ }+ return { white, blue };+}++/** The one picture the pass holds for the element at `tLocal`. */+async function pictureAt(tLocal: number) {+ const pass = new SubjectMaskCompositor();+ await pass.ready([title], W, H, [], title.start + tLocal);+ const all = pictures(pass);+ expect(all.length).toBe(1);+ const out = ink(all[0]);+ pass.dispose();+ return out;+}++beforeAll(async () => {+ expect(await installSkiaRaster()).toBe(true);+});++describe("the behind pass paints phases", () => {+ test("a disintegrating moment eats the ink", async () => {+ const tLocal = 3 - 0.3 + 0.3 / 2;+ expect(evalOverlayFrame(title, tLocal).erode ?? 0).toBeGreaterThan(0.2);+ const rest = await pictureAt(1.5);+ const eaten = await pictureAt(tLocal);+ expect(eaten.white).toBeLessThan(rest.white * 0.85);+ });++ test("a zap moment crackles", async () => {+ const tLocal = 0.7;+ expect(evalOverlayFrame(title, tLocal).zap ?? 0).toBeGreaterThan(0);+ const rest = await pictureAt(1.5);+ const lit = await pictureAt(tLocal);+ expect(rest.blue).toBe(0);+ expect(lit.blue).toBeGreaterThan(50);+ });+});diff --git a/site/src/cut/lib/cameraCardVideo.ts b/site/src/cut/lib/cameraCardVideo.tsindex 433498df..ef10014f 100644--- a/site/src/cut/lib/cameraCardVideo.ts+++ b/site/src/cut/lib/cameraCardVideo.ts@@ -18,7 +18,7 @@ import { ensureClipLuts } from "./lutBuild";import { matteStage } from "./matteAlpha";import { createRasterCanvas, decodeRasterImageUrl } from "./raster";import { splitFrame, type RemovalPieces } from "./removalVideo";-import type { MediaAsset, VideoClip } from "./types";+import { CLIP_MIN_SECONDS, type MediaAsset, type VideoClip } from "./types";import { liveReader } from "./liveReader";/**@@ -35,7 +35,7 @@ export async function renderCardPieces(if (!clip.card) return null;const rt = retimeOf(clip);const still = asset.type === "image";- const dur = Math.max(0.1, rt.len);+ const dur = Math.max(CLIP_MIN_SECONDS, rt.len);const frames = Math.max(1, Math.ceil(dur * opts.fps));const w = opts.box.w;const h = opts.box.h;diff --git a/site/src/cut/lib/composite.test.ts b/site/src/cut/lib/composite.test.tsindex 38d5189d..dc2b7174 100644--- a/site/src/cut/lib/composite.test.ts+++ b/site/src/cut/lib/composite.test.ts@@ -284,3 +284,59 @@ describe("the compositor's color path", () => {expect(peekClipLut(first.key)).toBeUndefined();});});++describe("a keyed clip's softening", () => {+ /** Every filter and blend mode the compositor sets while `draw` runs, on+ * the frame and on each scratch it makes. */+ const settingsDuring = (draw: (c: FrameCompositor) => void): string[] => {+ const seen: string[] = [];+ const spy = (c: PixelCanvas): PixelCanvas => {+ const inner = c.ctx;+ (c as unknown as { ctx: unknown }).ctx = new Proxy(inner, {+ set: (target, prop, value) => {+ if (prop === "filter" || prop === "globalCompositeOperation") seen.push(String(value));+ (target as Record<string, unknown>)[prop as string] = value;+ return true;+ },+ });+ return c;+ };+ setRasterFactory({+ createCanvas: (w, h) => spy(new PixelCanvas(w, h)) as unknown as RasterSurface,+ decodeImage: async () => null,+ canvasToBlob: async () => new Blob(),+ snapshot: async (canvas) => canvas as unknown as ImageBitmap,+ });+ try {+ draw(new FrameCompositor(spy(new PixelCanvas(W, H)) as unknown as RasterSurface));+ } finally {+ setRasterFactory({+ createCanvas: (w, h) => new PixelCanvas(w, h) as unknown as RasterSurface,+ decodeImage: async () => null,+ canvasToBlob: async () => new Blob(),+ snapshot: async (canvas) => canvas as unknown as ImageBitmap,+ });+ }+ return seen;+ };+ const key = (t: number, x: number, blur?: number) => ({ t, x, y: 0.5, scale: 1, rotation: 0, opacity: 1, ...(blur !== undefined ? { blur } : {}) });+ const rect = { x: 0.25, y: 0, w: 0.5, h: 1 };++ test("pose keys carrying blur blur the clip as it lands", () => {+ // 540 design px at the 1080 short side is 4 px on this 8 px frame; half+ // way down the ramp it is 2.+ const clip = clipOf({ frame: rect, kf: [key(0, 0.5, 540), key(1, 0.5, 0)] });+ const seen = settingsDuring((c) => c.drawIntoRect(frameOf(picture()), rect, true, 1, 0.5, 1, clip));+ expect(seen).toContain("blur(2.00px)");+ });++ test("motion blur streaks a moving clip and only when it is on", () => {+ const kf = [key(0, 0.2), key(1, 0.8)];+ const still = settingsDuring((c) => c.drawIntoRect(frameOf(picture()), rect, true, 1, 0.5, 1, clipOf({ frame: rect, kf })));+ expect(still).not.toContain("lighter");+ const moving = settingsDuring((c) =>+ c.drawIntoRect(frameOf(picture()), rect, true, 1, 0.5, 1, clipOf({ frame: rect, kf, motionBlur: 1 }))+ );+ expect(moving).toContain("lighter");+ });+});diff --git a/site/src/cut/lib/composite.ts b/site/src/cut/lib/composite.tsindex f6cb6a57..21ec8786 100644--- a/site/src/cut/lib/composite.ts+++ b/site/src/cut/lib/composite.ts@@ -19,13 +19,14 @@* it was reached by playing there or by rendering the 135th frame.*/-import { applyClipEffects, applyDetail, applyLutToImageData, applyMaskToCanvas, detailActive, grainTile, hexToHlgHex, lookCssFilter, lookPost, maskComposite, paintStrokeInk, removalActive, type ClipColorRecipe, type OutputSpace } from "@donkeycut/effects-kit";+import { applyClipEffects, applyDetail, applyLutToImageData, applyMaskToCanvas, detailActive, drawStreak, elementLook, grainTile, hexToHlgHex, lookCssFilter, lookPost, maskComposite, paintStrokeInk, removalActive, type ClipColorRecipe, type OutputSpace } from "@donkeycut/effects-kit";import { cameraCardLayout, traceCardPath, type CardLayout } from "./cameraCard";import { applyDetailGpu } from "./detailGpu";import { applyLutGpu } from "./gradeGpu";import { clipRecipe, peekClipLut, requestClipLut, type ClipLut, type RecipeSource } from "./lutBuild";import { createRasterCanvas } from "./raster";-import { clipCovers, clipPosed, clipPoseAt, clipZoom, contentRect, DEFAULT_BACKGROUND, isFullRect, rectOf, shadowInk } from "./types";+import { CLOCK_SWEEP, clampFeather, featherAlpha, shapeEase, SLICE_TAPS, sliceStreak, takesFeather } from "./transitionShape";+import { clipCovers, clipFrameAt, clipPosed, clipZoom, contentRect, DEFAULT_BACKGROUND, isFullRect, rectOf, shadowInk, TRANSITION_BLUR } from "./types";import type { ClipShadow, FrameRect, TransitionStyle, VideoClip } from "./types";/** A clip's picture at some instant, or the reasons there isn't one.@@ -56,6 +57,8 @@ export const PENDING_FRAME: Frame = { kind: "pending" };export interface LayerFx {dx?: number;dy?: number;+ /** Defocus as the layer lands, output px (an edge blur). */+ blur?: number;}/** The two clips a transition joins, each with its ramps already resolved. */@@ -64,7 +67,7 @@ export interface CrossJoin {masterClip: VideoClip;masterAlpha: number;masterZoom: number;- masterFx: { dx: number; dy: number };+ masterFx: { dx: number; dy: number; blur?: number };incFrame: Frame;incClip: VideoClip | undefined;incAlpha: number;@@ -131,6 +134,9 @@ export class FrameCompositor {private cardPop: Surface | null = null;private cardLayer: Surface | null = null;private cardPost: Surface | null = null;+ /** A soft-edged transition draws its incoming shot here, then fades it+ * out along the edge before laying it over the outgoing one. */+ private featherScratch: Surface | null = null;/** The fade under the card edge, rebuilt only when the band moves. */private cardFade: { ctx: Ctx; edge: number; bottom: number; fill: CanvasGradient } | null = null;/** The card shadow's ink, rebuilt only when its opacity changes. */@@ -244,7 +250,8 @@ export class FrameCompositor {| "detailScratch"| "cardPop"| "cardLayer"- | "cardPost",+ | "cardPost"+ | "featherScratch",w: number,h: number): { surface: Surface; resized: boolean } {@@ -743,8 +750,10 @@ export class FrameCompositor {const W = this.canvas.width;const H = this.canvas.height;const tLocal = Math.max(0, at - clip.start);- const pose = clipPosed(clip) ? clipPoseAt(clip, tLocal) : null;+ const pose = clipPosed(clip) ? clipFrameAt(clip, tLocal, W / H) : null;if (pose && pose.opacity <= 0.001) return;+ // The keyed blur and the motion streak, in output px, as an element's.+ const look = pose ? elementLook(pose, Math.min(W, H) / 1080) : null;const mask = clip.mask;const { surface: layer } = this.scratch("layerScratch", W, H);const lctx = layer.getContext("2d") as Ctx | null;@@ -814,6 +823,18 @@ export class FrameCompositor {pctx.restore();out = posed;octx = pctx;++ // Motion blur smears the posed picture along its streak; the layer+ // scratch is free again by now, so it takes the smear.+ if (look && look.taps >= 2) {+ lctx.setTransform(1, 0, 0, 1, 0, 0);+ lctx.globalAlpha = 1;+ lctx.globalCompositeOperation = "source-over";+ lctx.clearRect(0, 0, W, H);+ drawStreak(lctx, posed as CanvasImageSource, 0, 0, look);+ out = layer;+ octx = lctx;+ }}if (mask?.kind === "subject") {// The person matte is the coverage: blur its edge by the feather and@@ -844,8 +865,20 @@ export class FrameCompositor {ctx.translate(Math.round(fx.dx ?? 0), Math.round(fx.dy ?? 0));}const shade = clip.card ? null : this.shadowOf(out, clip.boxStyle?.shadow);++ // The keyed blur softens the picture and its shadow as they land, on+ // top of any filter the caller set (a blur transition's own).+ const soft = !!look && look.blur > 0 && "filter" in ctx;+ const before = soft ? ctx.filter : "none";+ if (soft) {+ const own = `blur(${look.blur.toFixed(2)}px)`;+ ctx.filter = before && before !== "none" ? `${before} ${own}` : own;+ }if (shade) ctx.drawImage(shade, 0, 0);ctx.drawImage(out, 0, 0);+ if (soft) {+ ctx.filter = before;+ }if (hasFx) ctx.restore();}@@ -1210,6 +1243,20 @@ export class FrameCompositor {// instead of strobing black (matters while skimming).if (frame.kind === "pending") return;blank();++ // An edge blur softens the whole layer as it lands, over any filter the+ // caller set; the layer draws with the filter in place.+ if (fx?.blur && fx.blur > 0.1 && "filter" in ctx) {+ const before = ctx.filter;+ const own = `blur(${fx.blur.toFixed(2)}px)`;+ ctx.filter = before && before !== "none" ? `${before} ${own}` : own;+ try {+ this.drawLayer(frame, clip, false, alpha, at, zoom, { dx: fx.dx, dy: fx.dy });+ } finally {+ ctx.filter = before;+ }+ return;+ }if (this.needsFx(clip)) {// The blank above already settled who owns the black; the layer itself// draws through the mask/pose pass, and any transition motion lands on@@ -1302,11 +1349,21 @@ export class FrameCompositor {style.startsWith("push") ||style.startsWith("wipe") ||style.startsWith("circle") ||- style.startsWith("split");+ style.startsWith("split") ||+ style.startsWith("slice") ||+ style === "clockwipe";if (carves && d.incFrame.kind === "pending") {drawMaster();return;}+ // A softened edge: the incoming shot fades in across a ramp along the+ // edge, the same profile the ffmpeg expression computes per pixel.+ const feather = clampFeather(d.masterClip.transitionFeather);+ if (feather > 0 && takesFeather(style)) {+ drawMaster();+ this.drawFeathered(style, p, feather, () => drawInc(1));+ return;+ }switch (style) {case "dipblack":case "dipwhite": {@@ -1323,7 +1380,7 @@ export class FrameCompositor {// here vs ffmpeg's horizontal hblur — reads the same). Without// ctx.filter this degrades to the plain crossfade below.const supports = "filter" in ctx;- const r = Math.max(0.5, (Math.min(W, H) / 24) * (1 - Math.abs(2 * p - 1)));+ const r = Math.max(0.5, Math.min(W, H) * TRANSITION_BLUR * (1 - Math.abs(2 * p - 1)));if (supports) ctx.filter = `blur(${r.toFixed(2)}px)`;drawMaster();drawInc();@@ -1407,6 +1464,62 @@ export class FrameCompositor {);break;}+ case "clockwipe": {+ // A hand pinned at the top-left corner sweeps from the top edge down+ // to the left one, uncovering the incoming shot behind it.+ const angle = shapeEase(style, p) * CLOCK_SWEEP;+ drawMaster();+ clipped(+ () => {+ ctx.moveTo(0, 0);+ ctx.arc(0, 0, Math.hypot(W, H), 0, angle);+ ctx.closePath();+ },+ () => drawInc(1)+ );+ break;+ }+ case "sliceleft":+ case "sliceup": {+ // The frame cut in two: across, the top half slides left and the+ // bottom right; up, the left column slides up and the right down. In+ // each half the incoming shot follows the outgoing one in. The halves+ // streak along their slide: copies across the shutter's travel,+ // folded into a running average (copy k at 1/(k+1)), each copy an+ // opaque outgoing+incoming pair so the average needs no scratch.+ const across = style === "sliceleft";+ const e = shapeEase(style, p);+ const len = across ? W : H;+ const streak = sliceStreak(p, d.masterClip.transition ?? 0) * len;+ const taps = streak >= 1 ? SLICE_TAPS : 1;+ const halves: { rect: [number, number, number, number]; dir: number }[] = across+ ? [+ { rect: [0, 0, W, H / 2], dir: -1 },+ { rect: [0, H / 2, W, H - H / 2], dir: 1 },+ ]+ : [+ { rect: [0, 0, W / 2, H], dir: -1 },+ { rect: [W / 2, 0, W - W / 2, H], dir: 1 },+ ];+ for (const half of halves) {+ clipped(+ () => ctx.rect(...half.rect),+ () => {+ for (let k = 0; k < taps; k++) {+ const off = taps > 1 ? streak * (k / (taps - 1) - 0.5) : 0;+ const shift = half.dir * e * len + off;+ const at = (v: number) => (across ? { dx: v, dy: 0 } : { dx: 0, dy: v });+ const a = 1 / (k + 1);+ const m = at(shift);+ const i = at(shift - half.dir * len);+ drawMaster({ ...d.masterFx, dx: d.masterFx.dx + m.dx, dy: d.masterFx.dy + m.dy }, a);+ drawInc(a, i);+ }+ }+ );+ }+ break;+ }default: {// crossfade / crosszoom: the classic A·(1−α)+B·α blend (zooms already// folded into the layer zoom factors).@@ -1415,4 +1528,55 @@ export class FrameCompositor {}}}++ /**+ * Lay the incoming shot over the frame through a soft edge: drawn into the+ * scratch, faded by the style's profile (`featherAlpha`, sampled into a+ * gradient along the edge's axis or out from the center), then composited.+ */+ private drawFeathered(style: TransitionStyle, p: number, feather: number, drawInc: () => void) {+ const ctx = this.ctx();+ if (!ctx) return;+ const W = this.canvas.width;+ const H = this.canvas.height;+ const { surface } = this.scratch("featherScratch", W, H);+ const sctx = surface.getContext("2d") as Ctx | null;+ if (!sctx) return;+ sctx.clearRect(0, 0, W, H);+ const main = this.canvas;+ this.canvas = surface;+ try {+ drawInc();+ } finally {+ this.canvas = main;+ }++ // The profile, sampled along the axis it varies on: x for the sideways+ // styles, y for the vertical wipes, the distance from the center for the+ // circles.+ const STOPS = 48;+ const radial = style.startsWith("circle");+ const vertical = style === "wipeup" || style === "wipedown";+ const R = Math.hypot(W, H) / 2;+ const grad = radial+ ? sctx.createRadialGradient(W / 2, H / 2, 0, W / 2, H / 2, R)+ : vertical+ ? sctx.createLinearGradient(0, 0, 0, H)+ : sctx.createLinearGradient(0, 0, W, 0);+ for (let i = 0; i <= STOPS; i++) {+ const u = i / STOPS;+ const a = radial+ ? featherAlpha(style, p, W / 2 + u * R, H / 2, W, H, feather)+ : vertical+ ? featherAlpha(style, p, 0, u * H, W, H, feather)+ : featherAlpha(style, p, u * W, 0, W, H, feather);+ grad.addColorStop(u, `rgba(0,0,0,${a.toFixed(4)})`);+ }+ sctx.save();+ sctx.globalCompositeOperation = "destination-in";+ sctx.fillStyle = grad;+ sctx.fillRect(0, 0, W, H);+ sctx.restore();+ ctx.drawImage(surface as CanvasImageSource, 0, 0);+ }}diff --git a/site/src/cut/lib/effectStack.ts b/site/src/cut/lib/effectStack.tsindex 60919efd..17261cf6 100644--- a/site/src/cut/lib/effectStack.ts+++ b/site/src/cut/lib/effectStack.ts@@ -11,7 +11,7 @@* The math is here, apart from React, because both exports walk the same stack.*/-import { effectPreviewState, isAudioEffect, type EffectPreviewState } from "@donkeycut/effects-kit";+import { effectPreviewState, effectRecipe, isAudioEffect, type EffectPreviewState } from "@donkeycut/effects-kit";import { isEffectOverlay, laneOf, type Overlay } from "./types";/** One effect live at a moment, with the lane that places it in the stack. */@@ -28,7 +28,7 @@ export function liveEffectsAt(overlays: Overlay[], t: number): LiveEffect[] {if (o.hidden || t < o.start || t > o.end) continue;live.push({lane: laneOf(o),- state: effectPreviewState(o.effect, o.amount, t - o.start, o.focus, o.ramp, o.end - o.start),+ state: effectPreviewState(effectRecipe(o), o.amount, t - o.start, o.focus, o.ramp, o.end - o.start, o),});}return live.sort((a, b) => a.lane - b.lane);@@ -48,6 +48,7 @@ export function stageEffectTransform(states: EffectPreviewState[]): string | undlet dy = 0;let zoom = 1;let origin = { x: 0.5, y: 0.5 };+ let stretch: EffectPreviewState["stretch"];for (const st of states) {dx += st.dx ?? 0;dy += st.dy ?? 0;@@ -56,8 +57,9 @@ export function stageEffectTransform(states: EffectPreviewState[]): string | undif (st.ghostFrac) dx += st.ghostFrac * 1080 * 3;zoom *= st.zoom ?? 1;if (st.origin) origin = st.origin;+ if (st.stretch) stretch = st.stretch;}- if (!dx && !dy && zoom === 1) return undefined;+ if (!dx && !dy && zoom === 1 && !stretch) return undefined;// The offsets are design pixels against a 1080 short side.const pct = (px: number) => ((px / 1080) * 100).toFixed(3);// Percentage translates are of the stage's own box, so the scale can be sat@@ -67,10 +69,17 @@ export function stageEffectTransform(states: EffectPreviewState[]): string | und// half a frame off and the push in walks the picture out of frame.const ox = (origin.x - 0.5) * 100;const oy = (origin.y - 0.5) * 100;+ // A pixel stretch scales the picture tall about its row the same way, so+ // the thin band at the row fills the stage.+ const sy = stretch ? (stretch.row - 0.5) * 100 : 0;+ const smear = stretch+ ? ` translate(0%, ${sy.toFixed(3)}%) scaleY(${stretch.scale}) translate(0%, ${(-sy).toFixed(3)}%)`+ : "";return (`translate(${pct(dx)}%, ${pct(dy)}%) ` +`translate(${ox.toFixed(3)}%, ${oy.toFixed(3)}%) scale(${zoom}) ` +- `translate(${(-ox).toFixed(3)}%, ${(-oy).toFixed(3)}%)`+ `translate(${(-ox).toFixed(3)}%, ${(-oy).toFixed(3)}%)` ++ smear);}diff --git a/site/src/cut/lib/elementTools.test.ts b/site/src/cut/lib/elementTools.test.tsnew file mode 100644index 00000000..b2f5e89a--- /dev/null+++ b/site/src/cut/lib/elementTools.test.ts@@ -0,0 +1,58 @@+import { beforeEach, describe, expect, test } from "bun:test";+import { DOODLE_INKS_MAX } from "@donkeycut/effects-kit";+import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import { emptySubtitles, isShapeOverlay, isTextOverlay } from "./types";++/** Tight display tracking and graffiti doodles through the assistant's tools. */++const reset = () =>+ useEditor.setState({+ projectId: "p1",+ aspect: "9:16",+ assets: [],+ clips: [],+ overlays: [],+ subtitles: emptySubtitles(),+ background: "#000000",+ });++describe("title tracking", () => {+ beforeEach(reset);++ test("display type keeps a tight negative tracking", async () => {+ await runAiTool("add_text_sequence", { lines: [{ text: "STAY", start: 0, end: 2 }] });+ const id = useEditor.getState().overlays.filter(isTextOverlay)[0].id;+ await runAiTool("update_overlay", { id, letter_spacing: -0.1 });+ expect(useEditor.getState().overlays.filter(isTextOverlay)[0].letterSpacing).toBeCloseTo(-0.1, 6);+ });+});++describe("doodle shapes", () => {+ beforeEach(reset);++ test("a doodle arrives in the graffiti set and takes its own inks", async () => {+ const made = (await runAiTool("add_shape", { shape: "doodle", start: 0, end: 3 })) as { id: string };+ const fresh = useEditor.getState().overlays.filter(isShapeOverlay)[0];+ expect(fresh.shape).toBe("doodle");+ expect((fresh.inks ?? []).length).toBeGreaterThan(0);+ await runAiTool("update_overlay", { id: made.id, inks: ["#ff0000", 4, "#00ff00", "", "#0000ff", "#111", "#222", "#333"] });+ const inked = useEditor.getState().overlays.filter(isShapeOverlay)[0];+ expect(inked.inks).toEqual(["#ff0000", "#00ff00", "#0000ff", "#111", "#222"].slice(0, DOODLE_INKS_MAX));+ await runAiTool("update_overlay", { id: made.id, inks: [] });+ expect(useEditor.getState().overlays.filter(isShapeOverlay)[0].inks).toBeUndefined();+ });+});++describe("shape patterns", () => {+ beforeEach(reset);++ test("the pattern fields the catalog teaches set and clear stripes", async () => {+ const made = (await runAiTool("add_shape", { shape: "rect", start: 0, end: 3, pattern: "stripes", pattern_gap: 4 })) as { id: string };+ expect(useEditor.getState().overlays.filter(isShapeOverlay)[0].pattern?.gap).toBe(4);+ await runAiTool("update_overlay", { id: made.id, pattern_width: 6 });+ expect(useEditor.getState().overlays.filter(isShapeOverlay)[0].pattern?.width).toBe(6);+ await runAiTool("update_overlay", { id: made.id, pattern: "none" });+ expect(useEditor.getState().overlays.filter(isShapeOverlay)[0].pattern).toBeUndefined();+ });+});diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.tsindex 40ce8e55..fd27a2b0 100644--- a/site/src/cut/lib/exportClient.ts+++ b/site/src/cut/lib/exportClient.ts@@ -28,11 +28,11 @@ import { renderRemovalPieces } from "./removalVideo";import { renderCardPieces } from "./cameraCardVideo";import { drawBlock } from "./blockSource";import { createRasterCanvas, rasterCanvasToPng } from "./raster";-import { clipLen, clipSpeed, getClipSpans, openedTimeline, overlayLayers, projectDuration, spanSequence, useEditor } from "./store";+import { clipLen, clipSpeed, getClipSpans, MICRO_GAP_S, openedTimeline, overlayLayers, projectDuration, spanSequence, useEditor } from "./store";import { captionStyle, cueOverlay, cueWordFrames, laneCues, laneHidden, subtitleLaneCount, trackPos } from "./subtitles";-import { isMaskAnimated, isOverlayAnimated, matteLumaToAlpha, normalizeGrade, paintMaskLuma, paintStrokeInk, retimeOf, type Mask, type SpeedNode } from "@donkeycut/effects-kit";+import { drawStreak, elementLook, isMaskAnimated, isOverlayAnimated, matteLumaToAlpha, normalizeGrade, paintMaskLuma, paintStrokeInk, retimeOf, type FlashRhythm, type FlashTone, type GlitchKind, type LeakCourse, type Mask, type SpeedNode } from "@donkeycut/effects-kit";import { renderElementFrames, renderElementPng, renderStillPng } from "./textRender";-import { clipCovers, clipKeyed, clipPosed, clipPoseAt, clipZoom, contentRect, frameOf, isStickerOverlay, isTextOverlay, laneOf, overlayAnimStyle, projectBackground, rectOf, regionPx, removalActive, shadowInk, subjectMasked, parkedTimeline } from "./types";+import { CLIP_MIN_SECONDS, clipCovers, clipFrameAt, clipKeyed, clipPosed, clipPoseAt, clipZoom, contentRect, frameOf, isStickerOverlay, isTextOverlay, laneOf, overlayAnimStyle, projectBackground, rectOf, regionPx, removalActive, shadowInk, subjectMasked, parkedTimeline } from "./types";import { liveReader } from "./liveReader";import { soundSourceOf, spanSoundSpec, spanUsesSoundFeature, specSound } from "./soundSource";import type {@@ -699,13 +699,17 @@ async function renderClipShadowPictures(const shape = createRasterCanvas(W, H);const cover = createRasterCanvas(W, H);const canvas = createRasterCanvas(W, H);+ // The smear and the blur the preview lays on the shadow, made on first use.+ let smear: ReturnType<typeof createRasterCanvas> | null = null;+ let soft: ReturnType<typeof createRasterCanvas> | null = null;const anchor = { x: rect.x + rect.w / 2, y: rect.y + rect.h / 2 };const blobAt = async (tLocal: number) => {const sil = silhouette ? await silhouette.at(tLocal) : null;const shapeCtx = shape.getContext("2d") as CanvasRenderingContext2D;shapeCtx.setTransform(1, 0, 0, 1, 0, 0);shapeCtx.clearRect(0, 0, W, H);- const pose = clipPosed(clip) ? clipPoseAt(clip, tLocal) : null;+ const pose = clipPosed(clip) ? clipFrameAt(clip, tLocal, W / H) : null;+ const look = pose ? elementLook(pose, scale) : null;if (pose) {shapeCtx.translate(pose.x * W, pose.y * H);shapeCtx.rotate((pose.rotation * Math.PI) / 180);@@ -741,6 +745,17 @@ async function renderClipShadowPictures(shapeCtx.drawImage(cover as CanvasImageSource, 0, 0);shapeCtx.globalCompositeOperation = "source-over";}++ // A moving clip with motion blur casts from its streaked shape.+ let lit = shape;+ if (look && look.taps >= 2) {+ smear ??= createRasterCanvas(W, H);+ const smearCtx = smear.getContext("2d") as CanvasRenderingContext2D;+ smearCtx.setTransform(1, 0, 0, 1, 0, 0);+ smearCtx.clearRect(0, 0, W, H);+ drawStreak(smearCtx, shape as CanvasImageSource, 0, 0, look);+ lit = smear;+ }const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;ctx.setTransform(1, 0, 0, 1, 0, 0);ctx.globalCompositeOperation = "source-over";@@ -750,12 +765,22 @@ async function renderClipShadowPictures(ctx.shadowBlur = Math.max(0, sh.blur) * scale;ctx.shadowOffsetX = (sh.x ?? 0) * scale;ctx.shadowOffsetY = (sh.y ?? 0) * scale;- ctx.drawImage(shape as CanvasImageSource, 0, 0);+ ctx.drawImage(lit as CanvasImageSource, 0, 0);ctx.restore();ctx.globalCompositeOperation = "destination-out";- ctx.drawImage(shape as CanvasImageSource, 0, 0);+ ctx.drawImage(lit as CanvasImageSource, 0, 0);ctx.globalCompositeOperation = "source-over";- return rasterCanvasToPng(canvas);++ // The keyed blur softens the shadow as it softens the picture.+ if (!look || !(look.blur > 0) || !("filter" in ctx)) return rasterCanvasToPng(canvas);+ soft ??= createRasterCanvas(W, H);+ const softCtx = soft.getContext("2d") as CanvasRenderingContext2D;+ softCtx.setTransform(1, 0, 0, 1, 0, 0);+ softCtx.clearRect(0, 0, W, H);+ softCtx.filter = `blur(${look.blur.toFixed(2)}px)`;+ softCtx.drawImage(canvas as CanvasImageSource, 0, 0);+ softCtx.filter = "none";+ return rasterCanvasToPng(soft);};try {// A keyed subject moves every frame, so its shadow samples like an@@ -956,6 +981,7 @@ export async function buildExportPayload(// xfade name (and the cross-zoom ramps) itself, so the spec carries only// the id.transitionStyle: sp.clip.transitionStyle,+ transitionFeather: sp.clip.transitionFeather,animIn: sp.clip.animIn,animOut: sp.clip.animOut,look: sp.clip.look,@@ -971,6 +997,7 @@ export async function buildExportPayload(mask: undefined as SpecMask | undefined,shadow: undefined as SpecMask | undefined,kf: posed(sp.clip).kf,+ motionBlur: sp.clip.motionBlur,border: undefined as string | undefined,removal: undefined as { rgb: string; alpha: string } | undefined,staged: undefined as boolean | undefined,@@ -993,7 +1020,7 @@ export async function buildExportPayload(// opacity, since opacity ships as coverage luma.for (let i = 0; i < spans.length; i++) {const c = posed(cardBoxless(spans[i].clip));- const dur = Math.max(0.1, retimeOf(c).len);+ const dur = Math.max(CLIP_MIN_SECONDS, retimeOf(c).len);const pictures = await renderClipMaskPictures(c,{ x: 0, y: 0, w: settings.width, h: settings.height },@@ -1098,13 +1125,19 @@ export async function buildExportPayload(image: false,});// An overlay-only cut has no track-0 spans: the whole base is one black bed.+ // Titles or sound that outlast the last shot run over black to the end.+ const last = spans[spans.length - 1];+ const tail = last ? duration - (last.start + last.len) : 0;const clips =spans.length === 0? [spacer(duration)]- : spanSequence(spans).flatMap(({ gapBefore }, i) => [- ...(gapBefore > 0 ? [spacer(gapBefore)] : []),- clipEntries[i],- ]);+ : [+ ...spanSequence(spans).flatMap(({ gapBefore }, i) => [+ ...(gapBefore > 0 ? [spacer(gapBefore)] : []),+ clipEntries[i],+ ]),+ ...(tail > MICRO_GAP_S ? [spacer(tail)] : []),+ ];// Video tracks composited over track 0; hidden ones are dropped. Each// track's transitions and animations translate into per-clip head/tail@@ -1199,6 +1232,7 @@ export async function buildExportPayload(mask: undefined as SpecMask | undefined,shadow: undefined as SpecMask | undefined,kf: posed(c).kf,+ motionBlur: c.motionBlur,border: undefined as string | undefined,removal: undefined as { rgb: string; alpha: string } | undefined,...ramp,@@ -1218,7 +1252,7 @@ export async function buildExportPayload(const box = region? { x: region.rx, y: region.ry, w: region.rw, h: region.rh }: { x: 0, y: 0, w: settings.width, h: settings.height };- const olen = Math.max(0.1, retimeOf(c).len);+ const olen = Math.max(CLIP_MIN_SECONDS, retimeOf(c).len);const pictures = await renderClipMaskPictures(c,box,@@ -1354,6 +1388,11 @@ export async function buildExportPayload(amount: (o as { amount?: number }).amount,focus: (o as { focus?: { x: number; y: number } }).focus,ramp: (o as { ramp?: number }).ramp,+ leak: (o as { leak?: LeakCourse }).leak,+ glitch: (o as { glitch?: GlitchKind }).glitch,+ tone: (o as { tone?: FlashTone }).tone,+ rate: (o as { rate?: number }).rate,+ rhythm: (o as { rhythm?: FlashRhythm }).rhythm,lane: laneOf(o),start: o.start,end: Math.min(o.end, duration),diff --git a/site/src/cut/lib/exportPose.test.ts b/site/src/cut/lib/exportPose.test.tsnew file mode 100644index 00000000..a378eb94--- /dev/null+++ b/site/src/cut/lib/exportPose.test.ts@@ -0,0 +1,162 @@+import { beforeAll, expect, test } from "bun:test";+import type { OverlayKey, ShapeOverlay } from "@donkeycut/effects-kit";+import { installSkiaRaster } from "./headless/skiaRaster";+import { renderProjectFrame } from "./exportRender";+import { createRasterCanvas, decodeRasterImage } from "./raster";+import { renderElementFrames } from "./textRender";+import type { ExportDoc } from "./renderSnapshot";++const size = { width: 320, height: 180 };++beforeAll(async () => {+ expect(await installSkiaRaster()).toBe(true);+});++const shape = (over: Partial<ShapeOverlay>, kf: Omit<OverlayKey, "t">): ShapeOverlay => ({+ id: "s", kind: "shape", shape: "rect", start: 0, end: 2,+ x: 0.5, y: 0.5, w: 0.5, h: 0.5, fill: "#ffffff",+ kf: [{ t: 0, ...kf }, { t: 2, ...kf }],+ ...over,+});++const docOf = (o: ShapeOverlay): ExportDoc => ({+ aspect: "16:9", assets: [], clips: [], audioClips: [], overlays: [o],+ subtitles: { cues: [], showOnVideo: false, showOnTimeline: false },+});++type Alpha = (x: number, y: number) => number;++/** The element's alpha at one moment, through both renderers: the in-tab+ * export frame and the sampled frame sequence the ffmpeg path overlays. */+async function bothRenderers(o: ShapeOverlay, t: number): Promise<Alpha[]> {+ const frame = await renderProjectFrame(docOf(o), t, size, () => "");+ const fctx = frame.getContext("2d") as CanvasRenderingContext2D;+ const exported = fctx.getImageData(0, 0, size.width, size.height).data;+ frame.width = frame.height = 1;++ const frames = await renderElementFrames(o, size.width, size.height, 30);+ const image = await decodeRasterImage(frames.images[Math.min(frames.images.length - 1, Math.round(t * 30))]);+ expect(image).not.toBeNull();+ const sheet = createRasterCanvas(size.width, size.height);+ const sctx = sheet.getContext("2d") as CanvasRenderingContext2D;+ sctx.drawImage(image!.source, frames.x, frames.y);+ const sampled = sctx.getImageData(0, 0, size.width, size.height).data;+ sheet.width = sheet.height = 1;++ // The export composites over a black frame, so its ink reads as luma.+ return [+ (x, y) => exported[(y * size.width + x) * 4],+ (x, y) => sampled[(y * size.width + x) * 4 + 3],+ ];+}++// An element bigger than the frame, keyed down to fit: its ink past the+// frame edge must survive the shrink (no flat sides where the frame cut it).+test("a keyed-down oversize ellipse keeps its round sides", async () => {+ const o = shape({ shape: "ellipse", w: 1.6, h: 1.6 }, { x: 0.5, y: 0.5, scale: 0.4, rotation: 0, opacity: 1 });+ for (const alpha of await bothRenderers(o, 1)) {+ // 0.4 × 512 px wide: the rim sits 102 px right of center, so x 240 is+ // inside; a stamp cut at the frame edge would end at x 224.+ expect(alpha(240, 90)).toBeGreaterThan(200);+ expect(alpha(275, 90)).toBeLessThan(30);+ }+});++// scaleY squashes the height alone: a 90 px tall box keyed to 0.2 is 18 px.+test("scaleY squashes one axis", async () => {+ const o = shape({}, { x: 0.5, y: 0.5, scale: 1, scaleY: 0.2, rotation: 0, opacity: 1 });+ for (const alpha of await bothRenderers(o, 1)) {+ expect(alpha(160, 90)).toBeGreaterThan(200);+ expect(alpha(100, 90)).toBeGreaterThan(200);+ expect(alpha(160, 110)).toBeLessThan(30);+ }+});++// tiltX tips the top edge away: in perspective the top row draws narrower+// than the bottom one.+test("tiltX draws the box in perspective", async () => {+ const o = shape({ w: 0.6, h: 0.8 }, { x: 0.5, y: 0.5, scale: 1, rotation: 0, tiltX: 60, opacity: 1 });+ for (const alpha of await bothRenderers(o, 1)) {+ const width = (y: number) => {+ let n = 0;+ for (let x = 0; x < size.width; x++) {+ if (alpha(x, y) > 128) {+ n++;+ }+ }+ return n;+ };+ // Rows a quarter of the way in from each edge of the tipped box.+ const top = width(70);+ const bottom = width(110);+ expect(top).toBeGreaterThan(20);+ expect(bottom).toBeGreaterThan(top + 6);+ // Turned 60°, the 144 px tall box shows about half its height.+ expect(alpha(160, 30)).toBeLessThan(30);+ }+});++/** Where a point (x, y) px from the box center lands under CSS's+ * perspective(depth) rotateX(tx) rotateY(ty): turned about y, then about x,+ * then divided by the depth it sits at. */+function cssTilt(x: number, y: number, tx: number, ty: number, depth: number): [number, number] {+ const a = (tx * Math.PI) / 180;+ const b = (ty * Math.PI) / 180;+ const X = x * Math.cos(b);+ const Y = y * Math.cos(a) + x * Math.sin(a) * Math.sin(b);+ const Z = y * Math.sin(a) - x * Math.cos(a) * Math.sin(b);+ const f = depth / (depth - Z);+ return [160 + X * f, 90 + Y * f];+}++/** The keyed perspective at this test's 180 px short side. */+const DEPTH = (1600 * 180) / 1080;++// tiltY turns the right edge away, as CSS rotateY does: the right end of a+// tall box draws shorter than the left.+test("tiltY turns the right edge away", async () => {+ const o = shape({ w: 0.5, h: 0.6 }, { x: 0.5, y: 0.5, scale: 1, rotation: 0, tiltY: 50, opacity: 1 });+ for (const alpha of await bothRenderers(o, 1)) {+ const height = (x: number) => {+ let n = 0;+ for (let y = 0; y < size.height; y++) {+ if (alpha(x, y) > 128) {+ n++;+ }+ }+ return n;+ };+ const [left] = cssTilt(-60, 0, 0, 50, DEPTH);+ const [right] = cssTilt(60, 0, 0, 50, DEPTH);+ expect(height(Math.round(left))).toBeGreaterThan(height(Math.round(right)) + 10);++ // The top edge stays one straight line: neighboring columns start+ // within a pixel of each other, with no sawtooth between tiles.+ const top = (x: number) => {+ for (let y = 0; y < size.height; y++) {+ if (alpha(x, y) > 128) {+ return y;+ }+ }+ return -1;+ };+ for (let x = Math.round(left) + 1; x < Math.round(right); x++) {+ expect(Math.abs(top(x) - top(x - 1))).toBeLessThanOrEqual(1);+ }+ }+});++// Both tilts compose the way CSS does: turned about y first, a wide bar's+// ends sit at different depths, so turning it about x sends one end up and+// the other down — the bar runs diagonally.+test("tiltX and tiltY together tip a wide bar diagonally, as CSS composes them", async () => {+ const o = shape({ w: 0.8, h: 0.1 }, { x: 0.5, y: 0.5, scale: 1, rotation: 0, tiltX: 60, tiltY: 30, opacity: 1 });+ for (const alpha of await bothRenderers(o, 1)) {+ for (const x of [-100, -50, 50, 100]) {+ const [px, py] = cssTilt(x, 0, 60, 30, DEPTH);+ expect(alpha(Math.round(px), Math.round(py))).toBeGreaterThan(128);+ // The flat bar's row at that x stays clear.+ expect(alpha(Math.round(px), 90 + Math.sign(x) * -Math.round(Math.abs(py - 90)))).toBeLessThan(30);+ }+ }+});diff --git a/site/src/cut/lib/exportRender.test.ts b/site/src/cut/lib/exportRender.test.tsindex de88a342..bebbd3fa 100644--- a/site/src/cut/lib/exportRender.test.ts+++ b/site/src/cut/lib/exportRender.test.ts@@ -1,6 +1,7 @@import { describe, expect, test } from "bun:test";+import { planAnimatedLayers } from "@donkeycut/effects-kit";import { foldClips } from "./audioMix";-import { bitrateFor, mixSpecFor } from "./exportRender";+import { bitrateFor, exportFrameAt, mixSpecFor } from "./exportRender";import type { ExportDoc } from "./renderSnapshot";import type { MediaAsset, VideoClip } from "./types";@@ -166,3 +167,39 @@ describe("bitrateFor by delivery", () => {expect(bitrateFor({ ...base, codec: "prores4444", bitrate: 1_234_000 })).not.toBe(1_234_000);});});++describe("exportFrameAt", () => {+ // A title whose glyph ramps the kit cuts at every output frame: each frame+ // of the render reads the window that starts on that frame, the moment the+ // preview shows there.+ const title = {+ id: "t",+ kind: "text",+ text: "READY?",+ start: 2.8333,+ end: 3.7,+ x: 0.5,+ y: 0.5,+ anim: { in: { style: "glitch", seconds: 0.3 }, out: { style: "flicker", seconds: 0.5 } },+ } as Parameters<typeof planAnimatedLayers>[0];+ const layers = planAnimatedLayers(title, title.end);++ const starts: [string, number][] = [+ ["from the start", 0],+ ["from a range start", 2.1],+ ];+ for (const [label, from] of starts) {+ test(`each frame lands in its own window ${label}`, () => {+ const first = Math.ceil(title.start * 30 - 1e-3);+ for (let k = first; k / 30 < title.end - 1e-6; k++) {+ const t = exportFrameAt(from, k - Math.round(from * 30), 30);+ const layer = layers.find((l) => l.start <= t && t < l.end);+ expect(layer).toBeDefined();+ expect(Math.abs(t - k / 30)).toBeLessThan(1e-9);+ if (k / 30 > title.end - 0.5) {+ expect(layer!.start).toBeCloseTo(k / 30, 9);+ }+ }+ });+ }+});diff --git a/site/src/cut/lib/exportRender.ts b/site/src/cut/lib/exportRender.tsindex 7a2a9e28..833838fe 100644--- a/site/src/cut/lib/exportRender.ts+++ b/site/src/cut/lib/exportRender.ts@@ -32,6 +32,7 @@ import {import { drawBlock } from "./blockSource";import { audioFxSpans } from "./audioEffects";import { duckWindows, renderMix, type MixClip, type MixItem, type MixSpec } from "./audioMix";+import { encoderLead } from "./encoderLead";import { masterInPlace } from "./loudness";import { STEM_CHANNELS, STEM_RATE, wavBytes, wavChunks, writeStoredZip, type StemDef } from "./stems";import { spanSoundSpec, specSound } from "./soundSource";@@ -45,14 +46,14 @@ import type { InputVideoTrack, WrappedCanvas } from "mediabunny";import { allowance, canvasBytes, holdMemory } from "./memoryBudget";import { getClipSpans, overlayLayers, projectDuration, spanSequence } from "./store";import { captionStyle, cueOverlay, cueWordFrames, laneCues, laneHidden, subtitleLaneCount, trackPos } from "./subtitles";-import { ElementFx, elementLook } from "@donkeycut/effects-kit";-import { applyEffectToCanvas, diveView, divesAt, evalOverlayFrame, measureDiveFocus, retimeOf, grainTile, smoothsAt, isAudioEffect, isMaskAnimated, isOverlayAnimated, maskFrameAt, planAnimatedLayers, type DiveFocus, type LottieHandle, type OverlayAnim, type PaintPhase } from "@donkeycut/effects-kit";+import { ElementFx, elementLook, paintElementCrop } from "@donkeycut/effects-kit";+import { applyEffectToCanvas, effectRecipe, diveView, divesAt, evalOverlayFrame, measureDiveFocus, retimeOf, grainTile, smoothsAt, isAudioEffect, isMaskAnimated, isOverlayAnimated, maskFrameAt, planAnimatedLayers, type DiveFocus, type LottieHandle, type OverlayAnim, type PaintPhase } from "@donkeycut/effects-kit";import { backdropStill, loadBackdropStill } from "./backdropStills";import { hasSubjectOverlays, SubjectMaskCompositor } from "./behindPass";import { MatteAlpha, matteStamp } from "./matteAlpha";-import { createRasterCanvas, type RasterSurface } from "./raster";+import { createRasterCanvas, snapshotRaster, type RasterSurface } from "./raster";import { exportFrameSynth, SYNTH_EDGE, synthWeight, type FrameSynth } from "./frameSynth";-import { cutRenderEnv, renderElementCanvas, renderElementPng, renderStillPng } from "./textRender";+import { cutRenderEnv, renderElementCanvas, renderStillPng } from "./textRender";import { cardMatteKey } from "./cameraCard";import { assetIsSilent, behindSubjectOverlay, clipCovers, frameOf, frontSubjectOverlay, isEffectOverlay, isTextOverlay, laneOf, overlayAnimStyle, projectBackground, rectOf, removalActive } from "./types";import type { ClipAnim, ClipSpan, EffectOverlay, MediaAsset, Overlay, StickerOverlay } from "./types";@@ -730,9 +731,19 @@ async function stampText(doc: ExportDoc): Promise<StampedLayer[]> {* the rest keeps the memory a subtitled export uses flat in the length of the* cut instead of growing with every word in it.*/+/** A layer's picture and where its top-left sits in the frame: (0, 0) for a+ * frame-sized picture, the ink box's corner for a cropped one. */+interface Stamp {+ image: ImageBitmap;+ x: number;+ y: number;+}+class StampCache {- private drawn = new Map<StampedLayer, ImageBitmap>();- private maskFrames = new Map<StampedLayer, { t: number; bitmap: ImageBitmap }>();+ private drawn = new Map<StampedLayer, Stamp>();+ private maskFrames = new Map<StampedLayer, { t: number; stamp: Stamp }>();+ /** The mask pass's scratch for cropped pictures, made on first use. */+ private maskScratch: HTMLCanvasElement | null = null;/** Scratch for blurred, motion-blurred and darkening stamps, made on first* use. */readonly fx = new ElementFx(createRasterCanvas);@@ -743,38 +754,56 @@ class StampCache {private assets: MediaAsset[]) {}- async bitmapFor(layer: StampedLayer): Promise<ImageBitmap> {- let bitmap = this.drawn.get(layer);- if (!bitmap) {+ async bitmapFor(layer: StampedLayer): Promise<Stamp> {+ let stamp = this.drawn.get(layer);+ if (!stamp) {// A layer with no animation holds still, its blur baked into the- // picture; an animated one is neutral and softens as it is drawn.- const png = layer.anim- ? await renderElementPng(layer.overlay, this.width, this.height, this.assets, layer.phase)- : await renderStillPng(layer.overlay, this.width, this.height, this.assets);- bitmap = await createImageBitmap(png);- this.drawn.set(layer, bitmap);+ // picture; an animated one is neutral, cropped to its own ink so a+ // pose that shrinks an oversize element keeps what hung past the+ // frame, and softens as it is drawn.+ stamp = layer.anim+ ? await this.cropped(layer.overlay, 0, layer.phase)+ : { image: await createImageBitmap(await renderStillPng(layer.overlay, this.width, this.height, this.assets)), x: 0, y: 0 };+ this.drawn.set(layer, stamp);}- return bitmap;+ return stamp;+ }++ /** The neutral element painted into a canvas cropped to its ink box,+ * however far past the frame that reaches. */+ private async cropped(el: Overlay, t: number, phase?: PaintPhase): Promise<Stamp> {+ const canvas = createRasterCanvas(1, 1) as unknown as HTMLCanvasElement;+ this.maskScratch ??= createRasterCanvas(1, 1) as unknown as HTMLCanvasElement;+ const origin = await paintElementCrop(canvas, el, this.width, this.height, cutRenderEnv(this.assets), {+ t,+ phase,+ maskScratch: this.maskScratch,+ });+ const image = await snapshotRaster(canvas);+ canvas.width = canvas.height = 1;+ return { image, ...origin };}/** The layer's picture drawn for this one moment, for the animations that* change the pixels as time moves — a keyframed mask, a per-glyph loop.* There is one live bitmap per layer, replaced as the render walks forward:* the cached-stamp twin of the Lottie per-frame path. */- async bitmapAt(layer: StampedLayer, tLocal: number, phase?: PaintPhase): Promise<ImageBitmap> {+ async bitmapAt(layer: StampedLayer, tLocal: number, phase?: PaintPhase): Promise<Stamp> {const hit = this.maskFrames.get(layer);- if (hit && Math.abs(hit.t - tLocal) < 1e-6) return hit.bitmap;+ if (hit && Math.abs(hit.t - tLocal) < 1e-6) return hit.stamp;const m = layer.overlay.mask;const el = isMaskAnimated(m)? { ...layer.overlay, mask: { ...m!, ...maskFrameAt(m!, tLocal), kf: undefined } }: layer.overlay;- // Drawn once and shown once, so it goes straight from the canvas.- const bitmap = await createImageBitmap(- await renderElementCanvas(el, this.width, this.height, this.assets, phase)- );- hit?.bitmap.close();- this.maskFrames.set(layer, { t: tLocal, bitmap });- return bitmap;+ // Drawn once and shown once, so it goes straight from the canvas. A+ // dive's pose lives inside its frame-sized picture; everything else is+ // cropped to its ink like the cached stamps.+ const stamp = phase?.dive+ ? { image: await snapshotRaster(await renderElementCanvas(el, this.width, this.height, this.assets, phase)), x: 0, y: 0 }+ : await this.cropped(el, tLocal, phase);+ hit?.stamp.image.close();+ this.maskFrames.set(layer, { t: tLocal, stamp });+ return stamp;}/** Where a diving element flies into, measured once per look. */@@ -784,25 +813,29 @@ class StampCache {/** Release the pictures of layers that have finished by `t`. */retire(t: number) {- for (const [layer, bitmap] of this.drawn) {+ for (const [layer, stamp] of this.drawn) {if (layer.end <= t) {- bitmap.close();+ stamp.image.close();this.drawn.delete(layer);}}for (const [layer, hit] of this.maskFrames) {if (layer.end <= t) {- hit.bitmap.close();+ hit.stamp.image.close();this.maskFrames.delete(layer);}}}dispose() {- for (const bitmap of this.drawn.values()) bitmap.close();+ for (const stamp of this.drawn.values()) stamp.image.close();this.drawn.clear();- for (const hit of this.maskFrames.values()) hit.bitmap.close();+ for (const hit of this.maskFrames.values()) hit.stamp.image.close();this.maskFrames.clear();+ if (this.maskScratch) {+ this.maskScratch.width = this.maskScratch.height = 1;+ this.maskScratch = null;+ }this.fx.dispose();}}@@ -980,6 +1013,23 @@ export function mixHasSound(spec: MixSpec): boolean {return spec.clips.some((c) => c.file && !c.muted) || spec.items.some((i) => !i.muted);}+/** Slack, in frames, within which a render's start counts as on the grid. */+const GRID_SLACK = 1e-6;++/** The timeline time of output frame `i` of a render starting at `from`.+ * A frame on the timeline grid is exactly k / fps, the time the kit cuts its+ * windows at: `i * (1 / fps)` lands a hair early (111 * (1 / 30) is+ * 3.6999…97) and would show the window before its own. */+export function exportFrameAt(from: number, i: number, fps: number): number {+ const k = Math.round(from * fps);++ // A range start off the grid keeps its own frame times.+ if (Math.abs(from * fps - k) > GRID_SLACK) {+ return from + i / fps;+ }+ return (k + i) / fps;+}+/*** Render `doc` to an MP4 in scratch storage.*@@ -1124,10 +1174,16 @@ export async function renderProjectToMp4(sampleRate: settings.audioSampleRate ?? AUDIO_RATE,});if (!audioCodec) throw new Error("This browser can't encode AAC audio.");- audio = new AudioBufferSource({- codec: audioCodec,- ...(audioCodec === "aac" ? { quality: new Quality({ bitrate: settings.audioBitrate ?? 192_000 }) } : {}),- });+ // The mix starts its encoder's priming early, so the edit list trims+ // the priming and the sound stays on its picture.+ const lead = await encoderLead(audioCodec, mix.sampleRate, mix.numberOfChannels);+ audio = new AudioBufferSource(+ {+ codec: audioCodec,+ ...(audioCodec === "aac" ? { quality: new Quality({ bitrate: settings.audioBitrate ?? 192_000 }) } : {}),+ },+ { startTimestamp: -lead }+ );output.addAudioTrack(audio, { maximumPacketCount: Math.ceil(mix.length / 960) + 32 });}await output.start();@@ -1142,7 +1198,7 @@ export async function renderProjectToMp4(for (let i = 0; i < frames; i++) {stop();const t = i * frameDur;- await painter.drawAt(from + t);+ await painter.drawAt(exportFrameAt(from, i, settings.fps));await video.add(t, frameDur);if (i % 15 === 0) {@@ -1282,12 +1338,13 @@ async function drawStamps(const h = w / aspect;// A blurred, streaking or darkening sticker poses into the scratch// alone and lands softened or dimmed.- const lc = stamps.fx.begin(ctx, canvas.width, canvas.height, elementLook(ev, scale));+ const at = { x: ev.x * canvas.width + ev.dx * scale, y: ev.y * canvas.height + ev.dy * scale };+ const lc = stamps.fx.begin(ctx, canvas.width, canvas.height, elementLook(ev, scale, at));lc.save();lc.globalAlpha = ev.opacity;- lc.translate(ev.x * canvas.width + ev.dx * scale, ev.y * canvas.height + ev.dy * scale);+ lc.translate(at.x, at.y);lc.rotate((ev.rotation * Math.PI) / 180);- lc.scale(ev.scale, ev.scale);+ lc.scale(ev.scale * (ev.sx ?? 1), ev.scale * (ev.sy ?? 1));lc.drawImage(layer.lottie.seek(t - (layer.animStart ?? layer.start)), -w / 2, -h / 2, w, h);lc.restore();stamps.fx.end(ctx);@@ -1325,7 +1382,7 @@ async function drawStamps(...(dive && ev ? { dive, pose: ev } : {}),}: undefined;- const bitmap =+ const stamp =livePhase || isMaskAnimated(layer.overlay.mask)? await stamps.bitmapAt(layer, tLocal, livePhase ?? layer.phase): await stamps.bitmapFor(layer);@@ -1336,10 +1393,14 @@ async function drawStamps(if (!ev) {const picture = subjectFront? subject!.mattedStamp(layer.overlay, canvas.width, canvas.height, (c) =>- c.drawImage(bitmap, 0, 0, canvas.width, canvas.height)+ c.drawImage(stamp.image, stamp.x, stamp.y))- : bitmap;- ctx.drawImage(picture, 0, 0, canvas.width, canvas.height);+ : null;+ if (picture) {+ ctx.drawImage(picture, 0, 0, canvas.width, canvas.height);+ } else {+ ctx.drawImage(stamp.image, stamp.x, stamp.y);+ }continue;}if (ev.opacity <= 0.001) continue;@@ -1349,19 +1410,23 @@ async function drawStamps(const posed = (c: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) => {// A dive's pose is already inside its frame-sized picture.if (dive) {- c.drawImage(bitmap, 0, 0, canvas.width, canvas.height);+ c.drawImage(stamp.image, 0, 0, canvas.width, canvas.height);return;}c.translate(ev.x * canvas.width + ev.dx * scale, ev.y * canvas.height + ev.dy * scale);c.rotate((ev.rotation * Math.PI) / 180);- c.scale(ev.scale, ev.scale);+ c.scale(ev.scale * (ev.sx ?? 1), ev.scale * (ev.sy ?? 1));c.translate(-cx, -cy);- c.drawImage(bitmap, 0, 0, canvas.width, canvas.height);+ c.drawImage(stamp.image, stamp.x, stamp.y);};- // A blurred, streaking or darkening element poses into the scratch alone- // and lands softened or dimmed; a sharp one draws straight onto the frame.- const look = elementLook(ev, scale);+ // A blurred, streaking, darkening or tilted element poses into the+ // scratch alone and lands softened, dimmed or turned; a sharp one draws+ // straight onto the frame.+ const look = elementLook(ev, scale, {+ x: ev.x * canvas.width + ev.dx * scale,+ y: ev.y * canvas.height + ev.dy * scale,+ });const lay = (into: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) => {const target = stamps.fx.begin(into, canvas.width, canvas.height, look);target.save();@@ -1644,6 +1709,7 @@ export class FramePainter {masterFx: {dx: plan.masterFxFrac.dx * W,dy: plan.masterFxFrac.dy * H,+ blur: plan.masterFxFrac.blur * Math.min(W, H),},incFrame,incClip: plan.incoming?.clip,@@ -1692,13 +1758,15 @@ export class FramePainter {applyEffectToCanvas(canvas,this.fxScratch,- o.effect,+ effectRecipe(o),o.amount,t - o.start,grainTile,o.focus,o.ramp,- o.end - o.start+ o.end - o.start,+ "opaque",+ o);}await drawStamps(canvas, this.stacked.slice(drawn), stamps, t, this.behind);diff --git a/site/src/cut/lib/exportTail.test.ts b/site/src/cut/lib/exportTail.test.tsnew file mode 100644index 00000000..407abe38--- /dev/null+++ b/site/src/cut/lib/exportTail.test.ts@@ -0,0 +1,31 @@+import { expect, test } from "bun:test";+import type { ExportDoc } from "@/cut/lib/renderSnapshot";++// The server's base video is a fold of the clip list, so every second the+// cut runs needs a segment in it: a cut whose sound outlasts its last shot+// carries black to the end.++const { buildExportPayload, originalSettings } = await import("./exportClient");++const doc: ExportDoc = {+ aspect: "16:9",+ assets: [+ { id: "v", fileName: "v.mp4", name: "V", type: "video", duration: 10, width: 1920, height: 1080, url: "",+ color: { matrix: "bt709", fullRange: false, bitDepth: 8, detected: "rec709" } },+ { id: "m", fileName: "m.mp3", name: "M", type: "audio", duration: 30, url: "" },+ ],+ clips: [{ id: "c1", assetId: "v", track: 0, start: 1, in: 0, out: 2, muted: false }],+ audioClips: [{ id: "a1", assetId: "m", start: 0, in: 0, out: 5, volume: 1 }],+ overlays: [],+ subtitles: { cues: [], showOnVideo: false, showOnTimeline: false },+};++test("the clip list runs to the end of the cut, black after the last shot", async () => {+ const settings = originalSettings(doc.aspect, doc.clips, doc.assets, doc);+ const payload = await buildExportPayload("project", doc, settings, "export");+ const spec = payload.spec as { duration: number; clips: { file: string; in: number; out: number }[] };+ expect(spec.duration).toBeCloseTo(5);+ expect(spec.clips.map((c) => c.file)).toEqual(["", "v.mp4", ""]);+ const length = spec.clips.reduce((sum, c) => sum + (c.out - c.in), 0);+ expect(length).toBeCloseTo(5);+});diff --git a/site/src/cut/lib/flashFrame.test.ts b/site/src/cut/lib/flashFrame.test.tsnew file mode 100644index 00000000..8b2810d7--- /dev/null+++ b/site/src/cut/lib/flashFrame.test.ts@@ -0,0 +1,29 @@+import { describe, expect, test } from "bun:test";+import { clipLen, separateOverlaps } from "./store";+import type { VideoClip } from "./types";++/** A flash frame — a clip one frame long — keeps its length, and the shot+ * after it stays where it was cut. */++const FRAME = 1 / 30;+const clip = (id: string, start: number, len: number): VideoClip => ({+ id,+ assetId: "a",+ track: 0,+ start,+ in: 0,+ out: len,+ muted: true,+});++describe("flash frames", () => {+ test("a one-frame clip is one frame long on the timeline", () => {+ expect(clipLen(clip("pop", 0, FRAME))).toBeCloseTo(FRAME, 6);+ });++ test("the shot after a one-frame clip stays on its cut", () => {+ const doc = { clips: [clip("pop", 3.5, FRAME), clip("next", 3.5 + FRAME, 1)], audioClips: [], overlays: [], cues: [] };+ const next = separateOverlaps(doc).clips.find((c) => c.id === "next")!;+ expect(next.start).toBeCloseTo(3.5 + FRAME, 6);+ });+});diff --git a/site/src/cut/lib/fontCycleTool.test.ts b/site/src/cut/lib/fontCycleTool.test.tsnew file mode 100644index 00000000..7cf90691--- /dev/null+++ b/site/src/cut/lib/fontCycleTool.test.ts@@ -0,0 +1,86 @@+import { beforeEach, describe, expect, test } from "bun:test";+import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import { emptySubtitles, isTextOverlay } from "./types";++/** A title's font cycle and its heavy weights through the assistant's tools. */++const reset = () =>+ useEditor.setState({+ projectId: "p1",+ aspect: "9:16",+ assets: [],+ clips: [],+ overlays: [],+ subtitles: emptySubtitles(),+ background: "#000000",+ });++const titles = () => useEditor.getState().overlays.filter(isTextOverlay);+const aTitle = async () => {+ await runAiTool("add_text_sequence", {+ look: "serif-mood",+ lines: [{ text: "STAY TUNED", start: 0, end: 4 }],+ });+ return titles()[0];+};+const failure = async (args: Record<string, unknown>) => {+ try {+ await runAiTool("set_overlay_animation", args);+ } catch (err) {+ return err instanceof Error ? err.message : String(err);+ }+ return "";+};++describe("set_overlay_animation font cycle", () => {+ beforeEach(reset);++ test("a new cycle runs the title's length at the default rate", async () => {+ const o = await aTitle();+ await runAiTool("set_overlay_animation", {+ id: o.id,+ font_cycle: [{ font: "impact", weight: 900, scale: 1.4 }, { font: "mono", tracking: 0.1 }],+ });+ expect(titles()[0].anim?.fonts).toEqual({+ faces: [{ font: "impact", weight: 900, scale: 1.4 }, { font: "mono", tracking: 0.1 }],+ at: 0,+ seconds: 4,+ rate: 15,+ });+ });++ test("timing holds inside the title and the rate in range", async () => {+ const o = await aTitle();+ await runAiTool("set_overlay_animation", {+ id: o.id,+ font_cycle: [{ font: "serif" }],+ font_cycle_at: 3,+ font_cycle_seconds: 9,+ font_cycle_rate: 400,+ });+ expect(titles()[0].anim?.fonts).toMatchObject({ at: 3, seconds: 1, rate: 30 });+ // Timing alone retimes the cycle the title already has.+ await runAiTool("set_overlay_animation", { id: o.id, font_cycle_at: 1 });+ expect(titles()[0].anim?.fonts).toMatchObject({ at: 1, faces: [{ font: "serif" }] });+ });++ test("an empty list removes the cycle", async () => {+ const o = await aTitle();+ await runAiTool("set_overlay_animation", { id: o.id, font_cycle: [{ font: "serif" }] });+ await runAiTool("set_overlay_animation", { id: o.id, font_cycle: [] });+ expect(titles()[0].anim?.fonts).toBeUndefined();+ });++ test("an unknown font or weight is refused", async () => {+ const o = await aTitle();+ expect(await failure({ id: o.id, font_cycle: [{ font: "no-such-face" }] })).toContain("Unknown font id");+ expect(await failure({ id: o.id, font_cycle: [{ font: "serif", weight: 500 }] })).toContain("weight");+ });++ test("a title takes the heavy weights", async () => {+ const o = await aTitle();+ await runAiTool("update_overlay", { id: o.id, weight: 900 });+ expect(titles()[0].weight).toBe(900);+ });+});diff --git a/site/src/cut/lib/framePlan.test.ts b/site/src/cut/lib/framePlan.test.tsindex 9b15d278..b0b4e1dc 100644--- a/site/src/cut/lib/framePlan.test.ts+++ b/site/src/cut/lib/framePlan.test.ts@@ -9,7 +9,7 @@ import {soundCrossGain,trackZeroPlan,} from "./framePlan";-import { TRANSITION_ZOOM } from "./types";+import { TRANSITION_BLUR, TRANSITION_ZOOM } from "./types";import type { AudioClip, ClipSpan, MediaAsset, VideoClip } from "./types";const asset: MediaAsset = {@@ -254,6 +254,16 @@ describe("clipAnimFx", () => {expect(clipAnimFx(clip, 4, 4).dxFrac).toBeCloseTo(-1, 5);});+ test("defocuses an open-edge blur through the middle of its ramp as it fades", () => {+ const clip = videoClip({ animIn: { style: "blur", seconds: 1 } });+ const mid = clipAnimFx(clip, 0.5, 4);+ expect(mid.blur).toBeCloseTo(TRANSITION_BLUR, 5);+ expect(mid.veil).toBeCloseTo(0.5, 5);+ expect(mid.gain).toBeCloseTo(0.5, 5);+ expect(clipAnimFx(clip, 0.25, 4).blur).toBeCloseTo(TRANSITION_BLUR / 2, 5);+ expect(clipAnimFx(clip, 2, 4).blur).toBeUndefined();+ });+test("treats a style it does not know as a fade", () => {const fx = clipAnimFx(videoClip({ animIn: { style: "kaleidoscope" as never, seconds: 2 } }),diff --git a/site/src/cut/lib/framePlan.ts b/site/src/cut/lib/framePlan.tsindex 8e9edb3e..9170ef9b 100644--- a/site/src/cut/lib/framePlan.ts+++ b/site/src/cut/lib/framePlan.ts@@ -18,7 +18,7 @@ import { headSrc, retimeOf } from "@donkeycut/effects-kit";* watched the download.*/-import { overlayAnimStyle, TRANSITION_ZOOM } from "./types";+import { CLIP_MIN_SECONDS, overlayAnimStyle, TRANSITION_BLUR, TRANSITION_ZOOM } from "./types";import type { AudioClip, ClipAnim, ClipSpan, MediaAsset, TransitionStyle, VideoClip } from "./types";/** The ramps a transition or clip animation puts on one upper-track clip. */@@ -117,6 +117,8 @@ export interface ClipAnimFx {/** Frame translation, as a fraction of canvas width/height. */dxFrac: number;dyFrac: number;+ /** Defocus, as a share of the frame's short side; absent = sharp. */+ blur?: number;}/**@@ -172,6 +174,12 @@ export function clipAnimFx(case "slidedown":fx.dyFrac += side === "in" ? -(1 - p) : 1 - p;break;+ case "blur":+ // The blur transition on an open edge: the picture defocuses through+ // the middle of the ramp while it fades, as xfade's hblur does+ // against the frame.+ fx.blur = Math.max(fx.blur ?? 0, TRANSITION_BLUR * (1 - Math.abs(2 * p - 1)));+ // falls throughcase "fade":default:// Fade — and the graceful fallback for a stored style this build no@@ -196,7 +204,7 @@ export function duckGainAt(audioClips: AudioClip[], t: number): number {let g = 1;for (const a of audioClips) {if (a.hidden || a.duck === undefined || a.duck >= 1) continue;- const len = Math.max(0.1, retimeOf(a).len);+ const len = Math.max(CLIP_MIN_SECONDS, retimeOf(a).len);if (t >= a.start && t < a.start + len) g = Math.min(g, Math.max(0, a.duck));}return g;@@ -311,7 +319,7 @@ export interface TrackZeroPlan {masterAlpha: number;masterZoom: number;/** Frame motion on the master, as a fraction of canvas width/height. */- masterFxFrac: { dx: number; dy: number };+ masterFxFrac: { dx: number; dy: number; blur: number };incAlpha: number;incZoom: number;/** Fade-to-black over the master clip's own footprint, 0..1. */@@ -418,7 +426,7 @@ export function trackZeroPlan(master: ClipSpan, spans: ClipSpan[], t: number): Tp,masterAlpha: anim.alpha,masterZoom: masterZoom * anim.zoom,- masterFxFrac: { dx: anim.dxFrac, dy: anim.dyFrac },+ masterFxFrac: { dx: anim.dxFrac, dy: anim.dyFrac, blur: anim.blur ?? 0 },incAlpha,incZoom,veil: anim.veil,diff --git a/site/src/cut/lib/itemKinds.ts b/site/src/cut/lib/itemKinds.tsindex f675cacf..574a1663 100644--- a/site/src/cut/lib/itemKinds.ts+++ b/site/src/cut/lib/itemKinds.ts@@ -13,7 +13,7 @@// compile until it has an entry, and `itemKinds.test.ts` walks the table so// every entry proves it copies and pastes.-import { KEY_EPSILON, maskKeyAt, poseAt, retimeOf, shiftCamera, sortedKeys, type EaseId, type Mask, type OverlayAnim, type OverlayKey, type OverlayPose } from "@donkeycut/effects-kit";+import { KEY_EPSILON, maskKeyAt, OVERLAY_HIT_DEFAULT_SECONDS, poseAt, retimeOf, shiftCamera, sortedKeys, type EaseId, type Mask, type OverlayAnim, type OverlayKey, type OverlayPose } from "@donkeycut/effects-kit";import { splitEmphasis, withEmphasis } from "./captionEmphasis";import {clipPoseAt,@@ -144,7 +144,7 @@ function splitHit(anim: OverlayAnim | undefined, cut: number): [OverlayAnim | unvoid _drop;return [hit.at < cut ? anim : rest,- hit.at + hit.seconds > cut ? { ...rest, hit: { ...hit, at: hit.at - cut } } : rest,+ hit.at + (hit.seconds > 0 ? hit.seconds : OVERLAY_HIT_DEFAULT_SECONDS) > cut ? { ...rest, hit: { ...hit, at: hit.at - cut } } : rest,];}diff --git a/site/src/cut/lib/leakCourseTool.test.ts b/site/src/cut/lib/leakCourseTool.test.tsnew file mode 100644index 00000000..781b6ad4--- /dev/null+++ b/site/src/cut/lib/leakCourseTool.test.ts@@ -0,0 +1,33 @@+import { beforeEach, describe, expect, test } from "bun:test";+import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import { emptySubtitles } from "./types";++/** A light leak's course through add_effect. */++const reset = () =>+ useEditor.setState({+ projectId: "p1",+ aspect: "9:16",+ assets: [],+ clips: [],+ overlays: [],+ subtitles: emptySubtitles(),+ background: "#000000",+ });++describe("add_effect light leak course", () => {+ beforeEach(reset);++ test("a light leak takes the burn course and refuses an unknown one", async () => {+ const { id } = (await runAiTool("add_effect", { effect: "lightleak", start: 1, end: 1.5, leak: "burn" })) as { id: string };+ expect(useEditor.getState().overlays.find((o) => o.id === id)).toMatchObject({ effect: "lightleak", leak: "burn" });+ let message = "";+ try {+ await runAiTool("add_effect", { effect: "lightleak", leak: "flare" });+ } catch (err) {+ message = err instanceof Error ? err.message : String(err);+ }+ expect(message).toContain("leak is one of");+ });+});diff --git a/site/src/cut/lib/removalVideo.ts b/site/src/cut/lib/removalVideo.tsindex 10e362e5..2e6389c5 100644--- a/site/src/cut/lib/removalVideo.ts+++ b/site/src/cut/lib/removalVideo.ts@@ -16,7 +16,7 @@ import { FrameCompositor } from "./composite";import { ensureClipLuts } from "./lutBuild";import { matteStage } from "./matteAlpha";import { createRasterCanvas, decodeRasterImageUrl, type RasterSurface } from "./raster";-import type { MediaAsset, VideoClip } from "./types";+import { CLIP_MIN_SECONDS, type MediaAsset, type VideoClip } from "./types";import { liveReader } from "./liveReader";export interface RemovalPieces {@@ -75,7 +75,7 @@ export async function renderRemovalPieces(const rt = retimeOf(clip);const still = asset.type === "image";- const dur = Math.max(0.1, rt.len);+ const dur = Math.max(CLIP_MIN_SECONDS, rt.len);const frames = Math.max(1, Math.ceil(dur * opts.fps));const reader = liveReader(asset);diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex 309e9b15..06b96ff3 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -8,6 +8,9 @@ import {isAudioEffect,KEY_EPSILON,keyAt,+ dealsMarks,+ DOODLE_FILL,+ DOODLE_INKS,lineLikeShape,maskKeyAt,removeKeyAt,@@ -93,7 +96,8 @@ import { engineTranscribeSamples, withEngineStt } from "./localStt";import { laneCues, subtitleLaneCount, trackLocale } from "./subtitles";import { cueEmphasis, mergeEmphasis, remapEmphasis, splitEmphasis, toggleEmphasis, withEmphasis } from "./captionEmphasis";import { clipboardItemAssetIds, clipboardItemFor, listedAssetIds, type TimelineClipboardItem } from "./itemKinds";-import { ANIM_STYLE_IDS, animStyleOfTransition, assetIsSilent, clipPoseAt, DEFAULT_BACKGROUND, emptySubtitles, frameOf, IMAGE_CLIP_SECONDS, isAudioTransition, fontAssetId, isEffectOverlay, isStickerOverlay, MAX_SUBTITLE_LANES, mediaUrl, migrateBehindSubject, migrateLegacyTransitions, normalizeAspect, overlayAnimStyle, projectBackground, SPEED_FLOOR, SPEED_MIN, stampOverlayKinds, stripDefaultOverlayKinds, isTimelineId, sanitizeTimelines, timelineAspect, TRANSITION_MAX, TRANSITION_STYLE_IDS, transitionBarAt, transitionBarStart, transitionStyleOfAnim, type TransitionBoundaryKind } from "./types";+import { ANIM_STYLE_IDS, animStyleOfTransition, assetIsSilent, CLIP_MIN_SECONDS, clipPoseAt, DEFAULT_BACKGROUND, emptySubtitles, frameOf, IMAGE_CLIP_SECONDS, isAudioTransition, fontAssetId, isEffectOverlay, isStickerOverlay, MAX_SUBTITLE_LANES, mediaUrl, migrateBehindSubject, migrateLegacyTransitions, normalizeAspect, overlayAnimStyle, projectBackground, SPEED_FLOOR, SPEED_MIN, stampOverlayKinds, stripDefaultOverlayKinds, isTimelineId, sanitizeTimelines, timelineAspect, TRANSITION_MAX, TRANSITION_STYLE_IDS, transitionBarAt, transitionBarStart, transitionStyleOfAnim, type TransitionBoundaryKind } from "./types";+import { clampFeather, takesFeather } from "./transitionShape";import { liftMoveTracks } from "./textMotion";import { OWN_SOUND, soundRoom, soundSourceOf, soundWindow, SPLIT_EDIT_MAX_S } from "./soundSource";import { cutSound } from "./soundSettings";@@ -193,7 +197,7 @@ const templateTreatment = (l: TemplateLayer) => ({...(l.card ? { card: { ...l.card, matte: undefined } } : {}),});-const MIN_LEN = 0.1;+const MIN_LEN = CLIP_MIN_SECONDS;/** Where a video clip lands when dropped: an existing track or a brand-new* track inserted at z-level `level`. Tracks number 0..N bottom-up: track 0 is@@ -3050,14 +3054,18 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {addShape: (shape, aim) => {const frame = frameOf(get().aspect);// A square-reading default box whatever the aspect; lines/arrows are a- // wider box whose height is the stroke thickness.- const w = lineLikeShape(shape) ? 0.42 : 0.3;+ // wider box whose height is the stroke thickness. A doodle paints+ // across the whole frame in the graffiti set.+ const marks = dealsMarks(shape);+ const w = lineLikeShape(shape) ? 0.42 : marks ? 1 : 0.3;const h =shape === "line"? 6 / frame.h: shape === "arrow"? 10 / frame.h- : (w * frame.w) / frame.h;+ : marks+ ? 1+ : (w * frame.w) / frame.h;addElement("shape",(start, end, lane) => ({@@ -3070,7 +3078,8 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {y: 0.5,w,h,- fill: "#FFFFFF",+ fill: marks ? DOODLE_FILL : "#FFFFFF",+ ...(marks ? { inks: [...DOODLE_INKS] } : {}),lane,}),aim@@ -5732,6 +5741,9 @@ export function deriveTransitionFields(const cut = byBoundary.get(`cut:${c.id}`);const transition = cut ? clampBarSeconds(cut.seconds) : undefined;const transitionStyle = cut && cut.style !== "crossfade" ? cut.style : undefined;+ // Only a style with a reveal edge has anything to soften.+ const transitionFeather =+ cut && takesFeather(cut.style) && clampFeather(cut.feather) > 0 ? clampFeather(cut.feather) : undefined;const animOf = (t: TimelineTransition | undefined): ClipAnim | undefined => {if (!t) return undefined;// Only a style with an edge ramp reaches an open edge — a sound@@ -5749,12 +5761,13 @@ export function deriveTransitionFields(if (c.transition === transition &&c.transitionStyle === transitionStyle &&+ c.transitionFeather === transitionFeather &&sameAnim(c.animIn, animIn) &&sameAnim(c.animOut, animOut))return c;changed = true;- return { ...c, transition, transitionStyle, animIn, animOut };+ return { ...c, transition, transitionStyle, transitionFeather, animIn, animOut };});return changed ? next : clips;}@@ -5772,6 +5785,7 @@ function sanitizeTransitions(raw: TimelineTransition[] | undefined): TimelineTrastart: t.start,seconds: clampBarSeconds(t.seconds),style: TRANSITION_STYLE_IDS.includes(t.style) ? t.style : "crossfade",+ ...(clampFeather(t.feather) > 0 ? { feather: clampFeather(t.feather) } : {}),...(t.hidden ? { hidden: true as const } : {}),}));return bars.filter(diff --git a/site/src/cut/lib/subtitles.ts b/site/src/cut/lib/subtitles.tsindex 812e07b6..a35e9e4a 100644--- a/site/src/cut/lib/subtitles.ts+++ b/site/src/cut/lib/subtitles.ts@@ -8,6 +8,7 @@ import {wordEffect,wordSampleWindows,type OverlayWords,+ type TextWeight,type WordDraw,type WordFace,} from "@donkeycut/effects-kit";@@ -69,7 +70,7 @@ export interface CaptionEmphasisOverrides {emphasisFont?: FontId;emphasisColor?: string;emphasisItalic?: boolean;- emphasisWeight?: 400 | 700;+ emphasisWeight?: TextWeight;emphasisScale?: number;}@@ -174,7 +175,7 @@ export interface CaptionStyle {y: number;size: number;font: FontId;- weight: 400 | 700;+ weight: TextWeight;color: string;shadow: boolean;plate: boolean;@@ -379,7 +380,7 @@ export function wrapCaptionForSize(size: number,frameW: number,font: FontId = "sf",- weight: 400 | 700 = 700,+ weight: TextWeight = 700,x = 0.5): string {const flat = text.trim().replace(/\s+/g, " ");diff --git a/site/src/cut/lib/textCompose.ts b/site/src/cut/lib/textCompose.tsindex 5d6ab92f..cf773450 100644--- a/site/src/cut/lib/textCompose.ts+++ b/site/src/cut/lib/textCompose.ts@@ -1,5 +1,5 @@import { textRoom, wrapTextToRoom } from "@donkeycut/effects-kit";-import type { OverlayAnim, OverlayAnimStyle } from "@donkeycut/effects-kit";+import type { OverlayAnim, OverlayAnimStyle, TextWeight } from "@donkeycut/effects-kit";import { measureLine, textFontOf } from "./textFit";import type { TextLook } from "./textLooks";import type { TextMoveId } from "./textMotion";@@ -131,7 +131,7 @@ const EMPHASIS_SCALE: Record<TextEmphasis, number> = {/** One face in a look's ensemble. */export interface TextFace {font: FontId;- weight: 400 | 700;+ weight: TextWeight;/** Size multiplier against the look's own size — a condensed face carries a* bigger number than a wide one at the same optical weight. */scale?: number;@@ -183,7 +183,7 @@ export interface ComposedLine {y: number;rotation: number;font: FontId;- weight: 400 | 700;+ weight: TextWeight;italic: boolean;size: number;color: string;@@ -221,7 +221,7 @@ export function fitLineToFrame(size: number,opts: {font: FontId;- weight: 400 | 700;+ weight: TextWeight;italic?: boolean;lineHeight?: number;x?: number;diff --git a/site/src/cut/lib/textLooks.ts b/site/src/cut/lib/textLooks.tsindex 9b8700ab..2e461b42 100644--- a/site/src/cut/lib/textLooks.ts+++ b/site/src/cut/lib/textLooks.ts@@ -1,4 +1,4 @@-import type { OverlayAnimStyle, OverlayLoopStyle, WordEffectId } from "@donkeycut/effects-kit";+import type { OverlayAnimStyle, OverlayLoopStyle, TextWeight, WordEffectId } from "@donkeycut/effects-kit";import type { TextEnsemble, TextVariation } from "./textCompose";import type { CaptionStyleId, FontId } from "./types";@@ -36,7 +36,7 @@ export interface TextLook {text: {font: FontId;size: number;- weight: 400 | 700;+ weight: TextWeight;color: string;onCards?: string[];italic?: boolean;diff --git a/site/src/cut/lib/titleFont.test.ts b/site/src/cut/lib/titleFont.test.tsnew file mode 100644index 00000000..00825841--- /dev/null+++ b/site/src/cut/lib/titleFont.test.ts@@ -0,0 +1,22 @@+import { beforeEach, expect, test } from "bun:test";++// A title's font is checked against the registry: an id it does not know is+// refused, so the assistant learns its face did not land.++const { runAiTool } = await import("./aiTools");+const { useEditor } = await import("./store");++beforeEach(() => {+ useEditor.setState({ projectId: "p", loaded: true, aspect: "9:16", assets: [], clips: [], audioClips: [], overlays: [] } as never);+});++test("a known font lands on the title", async () => {+ const t = (await runAiTool("add_title", { text: "HI", start: 0, end: 1, font: "serif" })) as { id: string };+ const o = useEditor.getState().overlays.find((x) => x.id === t.id) as { font?: string };+ expect(o.font).toBe("serif");+});++test("an unknown font is refused, naming it", async () => {+ await expect(runAiTool("add_title", { text: "HI", start: 0, end: 1, font: "nosuchface" })).rejects.toThrow(/nosuchface/);+ expect(useEditor.getState().overlays.length).toBe(0);+});diff --git a/site/src/cut/lib/transitionShape.test.ts b/site/src/cut/lib/transitionShape.test.tsnew file mode 100644index 00000000..2c962f11--- /dev/null+++ b/site/src/cut/lib/transitionShape.test.ts@@ -0,0 +1,183 @@+import { beforeAll, describe, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { existsSync } from "node:fs";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { Canvas } from "skia-canvas";+import { FrameCompositor, type Frame } from "./composite";+import { setRasterFactory, type RasterSurface } from "./raster";+import { shapeEase, sliceSpeed } from "./transitionShape";+import { xfadeTransition } from "../server/transitionExpr";+import type { TransitionStyle, VideoClip } from "./types";++/**+ * The shaped transitions draw the same frames on the compositor's canvas+ * (preview and the tab's export) and in the ffmpeg export's xfade+ * expressions: a white outgoing shot handing over to a black incoming one,+ * compared pixel by pixel at the same moments.+ */++const W = 90;+const H = 160;+const FPS = 30;+const SECONDS = 0.5;+const FRAMES = Math.round(SECONDS * FPS);+const FFMPEG = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools/ffmpeg");++beforeAll(() => {+ setRasterFactory({+ createCanvas: (w, h) => new Canvas(w, h) as unknown as RasterSurface,+ decodeImage: async () => null,+ canvasToBlob: async () => new Blob(),+ snapshot: async (canvas) => canvas as unknown as ImageBitmap,+ });+});++/** A solid shot. */+function solid(color: string): Frame {+ const c = new Canvas(W, H);+ const ctx = c.getContext("2d");+ ctx.fillStyle = color;+ ctx.fillRect(0, 0, W, H);+ return { kind: "ready", image: c as unknown as CanvasImageSource, width: W, height: H };+}++const clipOf = (over: Partial<VideoClip> = {}): VideoClip => ({+ id: "c1",+ assetId: "a1",+ track: 0,+ start: 0,+ in: 0,+ out: 4,+ muted: false,+ transition: SECONDS,+ ...over,+});++/** The compositor's frame at progress `p`, as luma bytes. */+function canvasFrame(style: TransitionStyle, p: number, feather?: number): Uint8Array {+ const canvas = new Canvas(W, H);+ const comp = new FrameCompositor(canvas as unknown as HTMLCanvasElement);+ const master = clipOf({ transitionStyle: style, ...(feather ? { transitionFeather: feather } : {}) });+ comp.drawCrossJoin(+ style,+ p,+ {+ masterFrame: solid("#ffffff"),+ masterClip: master,+ masterAlpha: 1,+ masterZoom: 1,+ masterFx: { dx: 0, dy: 0 },+ incFrame: solid("#000000"),+ incClip: clipOf({ id: "c2" }),+ incAlpha: p,+ incZoom: 1,+ },+ 0+ );+ const rgba = canvas.getContext("2d").getImageData(0, 0, W, H).data;+ const out = new Uint8Array(W * H);+ for (let i = 0; i < W * H; i++) {+ out[i] = rgba[i * 4];+ }+ return out;+}++/** Every frame of the ffmpeg join, as luma bytes. Frame n stands at+ * progress n / FRAMES. */+function ffmpegFrames(style: TransitionStyle, feather?: number): Uint8Array[] {+ const opt = xfadeTransition(style, feather, SECONDS, W, H);+ const graph =+ `color=c=white:s=${W}x${H}:r=${FPS}:d=${SECONDS},format=yuv420p[a];` ++ `color=c=black:s=${W}x${H}:r=${FPS}:d=${SECONDS * 2},format=yuv420p[b];` ++ `[a][b]xfade=${opt}:duration=${SECONDS}:offset=0,format=gray[out]`;+ const run = spawnSync(FFMPEG, ["-v", "error", "-filter_complex", graph, "-map", "[out]", "-frames:v", String(FRAMES), "-f", "rawvideo", "-"]);+ expect(run.stderr.toString()).toBe("");+ expect(run.status).toBe(0);+ return [...Array(FRAMES).keys()].map((n) => new Uint8Array(run.stdout.subarray(n * W * H, (n + 1) * W * H)));+}++/** The mean luma difference between two frames, 0..255. */+const meanDiff = (a: Uint8Array, b: Uint8Array) => a.reduce((s, v, i) => s + Math.abs(v - b[i]), 0) / a.length;++/** Luma at (x, y). */+const at = (f: Uint8Array, x: number, y: number) => f[Math.round(y) * W + Math.round(x)];++describe("the shaped transitions", () => {+ test("the bundled ffmpeg is there", () => {+ expect(existsSync(FFMPEG)).toBe(true);+ });++ test("the clock wipe sweeps a wedge from the top edge down to the left one", () => {+ // A quarter of the way in, a point just under the top edge is uncovered+ // and one by the left edge is not.+ const f = canvasFrame("clockwipe", 0.25);+ expect(at(f, W - 2, 3)).toBeLessThan(40);+ expect(at(f, 2, H - 3)).toBeGreaterThan(215);+ const done = canvasFrame("clockwipe", 1);+ expect(at(done, 2, H - 3)).toBeLessThan(40);+ });++ test("a horizontal slice slides the top half left and the bottom half right", () => {+ // Midway the incoming shot shows at the right of the top half and the+ // left of the bottom half.+ const f = canvasFrame("sliceleft", 0.5);+ expect(at(f, W - 3, H / 4)).toBeLessThan(40);+ expect(at(f, 3, H / 4)).toBeGreaterThan(215);+ expect(at(f, 3, (3 * H) / 4)).toBeLessThan(40);+ expect(at(f, W - 3, (3 * H) / 4)).toBeGreaterThan(215);+ });++ test("a vertical slice brings the left column up from below and the right one down from above", () => {+ const f = canvasFrame("sliceup", 0.5);+ expect(at(f, W / 4, H - 3)).toBeLessThan(40);+ expect(at(f, W / 4, 3)).toBeGreaterThan(215);+ expect(at(f, (3 * W) / 4, 3)).toBeLessThan(40);+ expect(at(f, (3 * W) / 4, H - 3)).toBeGreaterThan(215);+ });++ test("a slice holds back, whips through the middle and settles long", () => {+ // The curve the reference's slice bars trace: a quarter of the move by+ // 0.385 of the way, three quarters by 0.6, the last tenth over the final+ // fifth.+ expect(shapeEase("sliceup", 0.3)).toBeLessThan(0.17);+ expect(shapeEase("sliceup", 0.385)).toBeCloseTo(0.25, 2);+ expect(shapeEase("sliceleft", 0.6)).toBeGreaterThan(0.75);+ expect(shapeEase("sliceleft", 0.8)).toBeLessThan(0.97);+ expect(shapeEase("sliceup", 0)).toBe(0);+ expect(shapeEase("sliceup", 1)).toBe(1);++ // The streak's speed is the slope of that curve.+ for (let p = 0.02; p < 1; p += 0.05) {+ const slope = (shapeEase("sliceup", p + 1e-5) - shapeEase("sliceup", p - 1e-5)) / 2e-5;+ expect(sliceSpeed(p)).toBeCloseTo(slope, 3);+ }+ });++ test("a feathered split opens as a soft band", () => {+ const f = canvasFrame("splitopen", 0.5, 0.2);+ // Black at the center, white at the edges, grays between.+ expect(at(f, W / 2, H / 2)).toBeLessThan(40);+ expect(at(f, 1, H / 2)).toBeGreaterThan(215);+ const row = [...Array(W).keys()].map((x) => at(f, x, H / 2));+ expect(row.filter((v) => v > 40 && v < 215).length).toBeGreaterThan(6);+ });++ const cases: [TransitionStyle, number | undefined][] = [+ ["clockwipe", undefined],+ ["sliceleft", undefined],+ ["sliceup", undefined],+ ["splitopen", 0.2],+ ["wipeleft", 0.15],+ ["circleclose", 0.1],+ ];+ for (const [style, feather] of cases) {+ test(`${style}${feather ? ` feathered ${feather}` : ""} renders the same frames in ffmpeg`, () => {+ const frames = ffmpegFrames(style, feather);+ for (let n = 1; n < FRAMES; n += 2) {+ const diff = meanDiff(canvasFrame(style, n / FRAMES, feather), frames[n]);+ expect(diff).toBeLessThan(4);+ }+ });+ }+});diff --git a/site/src/cut/lib/transitionShape.ts b/site/src/cut/lib/transitionShape.tsnew file mode 100644index 00000000..d03c26f1--- /dev/null+++ b/site/src/cut/lib/transitionShape.ts@@ -0,0 +1,132 @@+/**+ * The geometry of the shaped transitions, written once for both renderers:+ * the compositor draws it on a canvas (preview and the tab's export) and+ * the ffmpeg export spells the same numbers as an xfade expression. Keeping+ * the curves and constants here is what keeps the two in step.+ */++import type { TransitionStyle } from "./types";++/** The styles whose reveal edge can be softened, and the axis it runs on. */+export const FEATHER_STYLES: TransitionStyle[] = [+ "wipeleft",+ "wiperight",+ "wipeup",+ "wipedown",+ "circleopen",+ "circleclose",+ "splitopen",+ "splitclose",+];++/** The softest edge offered: a ramp half the frame wide. */+export const TRANSITION_FEATHER_MAX = 0.5;++/** The styles that cut the frame into two halves sliding opposite ways. */+export const SLICE_STYLES: TransitionStyle[] = ["sliceleft", "sliceup"];++/**+ * How long the virtual shutter stays open on a sliding half, seconds: half a+ * frame at 30fps, the 180° shutter a camera would have smeared the move+ * with. The halves streak by however far they travel in that time.+ */+export const SLICE_SHUTTER = 1 / 60;++/** How many copies average into a sliding half's streak. */+export const SLICE_TAPS = 5;++/** The clock wipe's sweep from the top edge down to the left one. */+export const CLOCK_SWEEP = Math.PI / 2;++/** Whether `style` takes a soft edge. */+export const takesFeather = (style: TransitionStyle | undefined): boolean =>+ !!style && FEATHER_STYLES.includes(style);++/** A clip's stored softness brought into range; 0 = a hard edge. */+export const clampFeather = (v: number | undefined): number =>+ Math.min(TRANSITION_FEATHER_MAX, Math.max(0, Number.isFinite(v) ? (v as number) : 0));++/**+ * The slices' whip, fitted to a reference's slice bars: the halves pull a+ * quarter of their move (`SLICE_KNEE_SHARE`) by `SLICE_KNEE` of the way+ * through, rising as a power `SLICE_PULL` of progress, then snap through the+ * middle and settle onto the frame as a power `SLICE_SETTLE` of what is left.+ */+export const SLICE_KNEE = 0.385;+export const SLICE_KNEE_SHARE = 0.25;+export const SLICE_PULL = 1.75;+export const SLICE_SETTLE = 2.7;++/**+ * The share of its move a shaped transition has made at linear progress `p`.+ * The slices whip (above); the clock wipe leaves fast and settles onto the+ * edge.+ */+export function shapeEase(style: TransitionStyle, p: number): number {+ const q = Math.min(1, Math.max(0, p));+ if (style === "clockwipe") {+ return 1 - (1 - q) * (1 - q);+ }+ if (q < SLICE_KNEE) {+ return SLICE_KNEE_SHARE * (q / SLICE_KNEE) ** SLICE_PULL;+ }+ return 1 - (1 - SLICE_KNEE_SHARE) * ((1 - q) / (1 - SLICE_KNEE)) ** SLICE_SETTLE;+}++/** How fast a slice moves at `p`, in eased share per unit of progress: the+ * slope of its whip. */+export function sliceSpeed(p: number): number {+ const q = Math.min(1, Math.max(0, p));+ if (q < SLICE_KNEE) {+ return ((SLICE_KNEE_SHARE * SLICE_PULL) / SLICE_KNEE) * (q / SLICE_KNEE) ** (SLICE_PULL - 1);+ }+ return (((1 - SLICE_KNEE_SHARE) * SLICE_SETTLE) / (1 - SLICE_KNEE)) * ((1 - q) / (1 - SLICE_KNEE)) ** (SLICE_SETTLE - 1);+}++/**+ * How far a sliding half streaks at `p`, as a share of the axis it slides+ * along, for a transition `seconds` long.+ */+export const sliceStreak = (p: number, seconds: number): number =>+ seconds > 0 ? sliceSpeed(p) * (SLICE_SHUTTER / seconds) : 0;++/** How far a soft edge's ramp runs for `style`, px: the feather is a share+ * of the distance the edge crosses — the width for a sideways wipe or split,+ * the height for an upward or downward one, the half-diagonal for a circle. */+export function featherWidth(style: TransitionStyle, feather: number, W: number, H: number): number {+ const across = style === "wipeup" || style === "wipedown" ? H : style.startsWith("circle") ? Math.hypot(W, H) / 2 : W;+ return clampFeather(feather) * across;+}++/**+ * How much of the incoming shot shows at pixel (x, y) of a W×H frame, 0..1,+ * for a soft-edged reveal at progress `p`. Each style measures a distance+ * from where its edge starts — the far side for a wipe, the center for an+ * opening split or circle — and the reveal reaches `p` of that distance plus+ * the ramp, so the shot is fully hidden at 0 and fully shown at 1.+ */+export function featherAlpha(style: TransitionStyle, p: number, x: number, y: number, W: number, H: number, feather: number): number {+ const F = Math.max(1e-6, featherWidth(style, feather, W, H));+ const ramp = (d: number, span: number, reach: number) => Math.min(1, Math.max(0, (reach * (span + F) - d) / F));+ const R = Math.hypot(W, H) / 2;+ switch (style) {+ case "wipeleft":+ return ramp(W - x, W, p);+ case "wiperight":+ return ramp(x, W, p);+ case "wipeup":+ return ramp(H - y, H, p);+ case "wipedown":+ return ramp(y, H, p);+ case "splitopen":+ return ramp(Math.abs(x - W / 2), W / 2, p);+ case "splitclose":+ return ramp(Math.min(x, W - x), W / 2, p);+ case "circleopen":+ return ramp(Math.hypot(x - W / 2, y - H / 2), R, p);+ case "circleclose":+ return 1 - ramp(Math.hypot(x - W / 2, y - H / 2), R, 1 - p);+ default:+ return p >= 0.5 ? 1 : 0;+ }+}diff --git a/site/src/cut/lib/typeInPlace.test.ts b/site/src/cut/lib/typeInPlace.test.tsnew file mode 100644index 00000000..3fc71c87--- /dev/null+++ b/site/src/cut/lib/typeInPlace.test.ts@@ -0,0 +1,92 @@+import { beforeAll, expect, test } from "bun:test";+import { installSkiaRaster } from "./headless/skiaRaster";+import { renderProjectFrame } from "./exportRender";+import { createRasterCanvas, decodeRasterImage } from "./raster";+import { renderElementFrames } from "./textRender";+import type { ExportDoc } from "./renderSnapshot";+import type { TextOverlay } from "./types";++/**+ * A typewriter types each letter where the whole title puts it: a centered+ * title half typed starts at the same left edge as the finished title.+ */++const size = { width: 540, height: 960 };++beforeAll(async () => {+ expect(await installSkiaRaster()).toBe(true);+});++const title: TextOverlay = {+ id: "t",+ text: "COMING SOON",+ start: 0,+ end: 3,+ x: 0.5,+ y: 0.5,+ size: 90,+ font: "archivo-black",+ weight: 400,+ color: "#ffffff",+ plate: false,+ shadow: false,+ anim: { in: { style: "typewriter", seconds: 1 } },+};++const doc: ExportDoc = {+ aspect: "9:16", assets: [], clips: [], audioClips: [], overlays: [title],+ subtitles: { cues: [], showOnVideo: false, showOnTimeline: false },+};++/** The leftmost and rightmost columns with ink on the title's middle band. */+function inkSpan(read: (x: number, y: number) => number): { left: number; right: number } {+ let left = size.width;+ let right = -1;+ for (let y = size.height / 2 - 20; y < size.height / 2 + 20; y++) {+ for (let x = 0; x < size.width; x++) {+ if (read(x, y) > 128) {+ left = Math.min(left, x);+ right = Math.max(right, x);+ }+ }+ }+ return { left, right };+}++/** The title's ink at `t` through the in-tab export frame. */+async function exported(t: number) {+ const frame = await renderProjectFrame(doc, t, size, () => "");+ const data = (frame.getContext("2d") as CanvasRenderingContext2D).getImageData(0, 0, size.width, size.height).data;+ frame.width = frame.height = 1;+ return inkSpan((x, y) => data[(y * size.width + x) * 4]);+}++/** The title's ink at `t` through the sampled frames the ffmpeg path overlays. */+async function sampled(t: number) {+ const frames = await renderElementFrames(title, size.width, size.height, 30);+ let at = 0;+ let index = frames.entries[0].image;+ for (const e of frames.entries) {+ if (at > t + 1e-6) {+ break;+ }+ index = e.image;+ at += e.duration;+ }+ const image = await decodeRasterImage(frames.images[index]);+ const sheet = createRasterCanvas(size.width, size.height);+ const ctx = sheet.getContext("2d") as CanvasRenderingContext2D;+ ctx.drawImage(image!.source, frames.x, frames.y);+ const data = ctx.getImageData(0, 0, size.width, size.height).data;+ sheet.width = sheet.height = 1;+ return inkSpan((x, y) => data[(y * size.width + x) * 4 + 3]);+}++test("a half-typed centered title starts where the finished title starts", async () => {+ for (const read of [exported, sampled]) {+ const done = await read(2);+ const half = await read(0.5);+ expect(half.right).toBeLessThan(done.right - 60);+ expect(Math.abs(half.left - done.left)).toBeLessThanOrEqual(2);+ }+});diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.tsindex 365a7236..51e0a14c 100644--- a/site/src/cut/lib/types.ts+++ b/site/src/cut/lib/types.ts@@ -5,6 +5,7 @@ import {CAMERA_WORLD_MIN,EFFECT_LABELS,behindSubjectMask,+ evalOverlayFrame,overlayKind,poseAt,retimeOf,@@ -17,6 +18,7 @@ import {type LookStyle,type Mask,type OverlayBase,+ type OverlayFrameState,type OverlayKey,type OverlayKind,type OverlayPose,@@ -27,7 +29,7 @@ import {type TextOverlay as KitTextOverlay,type WordEffectId,} from "@donkeycut/effects-kit";-import type { ClipEffect, CodeFormat, SourceProfile } from "@donkeycut/effects-kit";+import type { ClipEffect, CodeFormat, SourceProfile, TextWeight } from "@donkeycut/effects-kit";import type { CameraCard } from "./cameraCard";import { getBackend, type CutBackend } from "./backend";import type { VideoProject } from "./genvideo/types";@@ -45,6 +47,11 @@ export const isLinkedAssetType = (type: string): type is LinkedAssetType =>* an image has no intrinsic duration, so the clip carries this as its `out`. */export const IMAGE_CLIP_SECONDS = 8;+/** The shortest clip, seconds: one frame at 30 fps, a flash frame. Every+ * renderer floors a clip's footprint here, so a one-frame clip plays one+ * frame in the preview and in every export. */+export const CLIP_MIN_SECONDS = 1 / 30;+/** Project output frame ratio as "W:H". Presets cover common platforms;* any ratio that passes `parseRatio` (e.g. "9:5") is valid. */export type Aspect = `${number}:${number}`;@@ -528,39 +535,53 @@ export const clipPosed = (c: {}): boolean =>clipKeyed(c) || Math.abs(c.rotation ?? 0) > 0.001 || (c.opacity ?? 1) < 0.999;+/** The fields a clip's pose reads. */+type PosedClip = {+ frame?: FrameRect;+ in: number;+ out: number;+ speed?: number;+ speedCurve?: SpeedNode[];+ reverse?: boolean;+ rotation?: number;+ opacity?: number;+ kf?: OverlayKey[];+ motionBlur?: number;+};++/** The clip as the element evaluator sees it: an element over the clip's+ * window, anchored on its region center. */+function clipPosable(clip: PosedClip) {+ const rect = rectOf(clip);+ return {+ start: 0,+ end: Math.max(CLIP_MIN_SECONDS, retimeOf(clip).len),+ x: rect.x + rect.w / 2,+ y: rect.y + rect.h / 2,+ rotation: clip.rotation,+ opacity: clip.opacity,+ kf: clip.kf,+ motionBlur: clip.motionBlur,+ };+}+/*** The clip's pose at `tLocal` seconds into its window: resting at its region* center, or moving along its key track — the overlay evaluator over the* clip's own anchor, so clips and elements share one interpolation.*/-export function clipPoseAt(- clip: {- frame?: FrameRect;- in: number;- out: number;- speed?: number;- speedCurve?: SpeedNode[];- reverse?: boolean;- rotation?: number;- opacity?: number;- kf?: OverlayKey[];- },- tLocal: number-): OverlayPose {- const rect = rectOf(clip);- const len = Math.max(0.1, retimeOf(clip).len);- return poseAt(- {- start: 0,- end: len,- x: rect.x + rect.w / 2,- y: rect.y + rect.h / 2,- rotation: clip.rotation,- opacity: clip.opacity,- kf: clip.kf,- },- tLocal- );+export function clipPoseAt(clip: PosedClip, tLocal: number): OverlayPose {+ return poseAt(clipPosable(clip), tLocal);+}++/**+ * The clip's frame state at `tLocal`: its pose, its keyed blur, and the+ * motion blur streak its keyed movement draws (design px), from the element+ * evaluator, so a clip softens exactly as an element does. `aspect` is the+ * frame's width / height.+ */+export function clipFrameAt(clip: PosedClip, tLocal: number, aspect: number): OverlayFrameState {+ return evalOverlayFrame(clipPosable(clip), tLocal, aspect);}/** One-click layouts for arranging a video layer in the frame. `fit` is the@@ -670,6 +691,9 @@ export interface VideoClip {transition?: number;/** Look of that transition; absent = "crossfade". */transitionStyle?: TransitionStyle;+ /** Softness of that transition's reveal edge (the bar's `feather`);+ * absent = a hard edge. */+ transitionFeather?: number;/** Entrance animation on this clip's own head (absent = none). Unlike a* transition it belongs to one clip and never moves its neighbors. */animIn?: ClipAnim;@@ -701,6 +725,10 @@ export interface VideoClip {* multiplies its fitted size, rotation turns it about its center, opacity* fades it. Absent = the clip sits in its region untransformed. */kf?: OverlayKey[];+ /** Motion blur: the shutter, 0..1 of a 30fps frame, streaking the+ * picture's keyed movement the way an element's `motionBlur` does.+ * Absent = off. */+ motionBlur?: number;/** Rounded corners and a border stroke on the clip's box; absent = plain. */boxStyle?: BoxStyle;/** Background removal: keys the clip's picture to an AI matte baked to a@@ -793,6 +821,10 @@ export interface TimelineTransition {/** Blend length, 0.1..TRANSITION_MAX. */seconds: number;style: TransitionStyle;+ /** How soft the reveal edge is, as a share of the frame the edge crosses+ * (0..TRANSITION_FEATHER_MAX); absent = a hard edge. Wipes, circles and+ * splits take it. */+ feather?: number;/** Hidden bars stay on the row (grayed) and keep the boundary they line up* with, so nothing else claims it, but play nothing — the cut renders hard* in the preview and in every export. */@@ -838,6 +870,9 @@ export type TransitionStyle =| "circleclose"| "splitopen"| "splitclose"+ | "clockwipe"+ | "sliceleft"+ | "sliceup"| "audiocross";/** The ffmpeg xfade transition each style renders with on export. Doubles as@@ -862,6 +897,11 @@ export const TRANSITION_XFADE: Record<TransitionStyle, string> = {circleclose: "circleclose",splitopen: "vertopen",splitclose: "vertclose",+ // The shaped styles ffmpeg has no built-in for run as xfade expressions+ // (server/transitionExpr.ts), as does any edge with a feather.+ clockwipe: "custom",+ sliceleft: "custom",+ sliceup: "custom",// The cross dissolve never reaches xfade — the picture cuts and the renders// branch before this map. The entry keeps the record total.audiocross: "fade",@@ -915,8 +955,8 @@ export const transitionBarAt = (export const TRANSITION_STYLE_GROUPS: { label: string; ids: TransitionStyle[] }[] = [{ label: "Fade", ids: ["crossfade", "dipblack", "dipwhite", "blur"] },{ label: "Zoom", ids: ["crosszoom"] },- { label: "Push", ids: ["pushleft", "pushright", "pushup", "pushdown"] },- { label: "Wipe", ids: ["wipeleft", "wiperight", "wipeup", "wipedown"] },+ { label: "Push", ids: ["pushleft", "pushright", "pushup", "pushdown", "sliceleft", "sliceup"] },+ { label: "Wipe", ids: ["wipeleft", "wiperight", "wipeup", "wipedown", "clockwipe"] },{ label: "Shape", ids: ["circleopen", "circleclose", "splitopen", "splitclose"] },];@@ -944,11 +984,17 @@ export const TRANSITION_STYLE_LABELS: Record<TransitionStyle, string> = {circleclose: "Circle close",splitopen: "Split open",splitclose: "Split close",+ clockwipe: "Clock wipe",+ sliceleft: "Slice left",+ sliceup: "Slice up",audiocross: "Cross dissolve",};/** Peak scale the zoom transitions push into (preview and export). */export const TRANSITION_ZOOM = 1.18;+/** Peak defocus of the blur transition and the blur entrance/exit, as a share+ * of the frame's short side. */+export const TRANSITION_BLUR = 1 / 24;/** A clip's own entrance/exit animation. The same style id serves both sides:* directional names describe the motion (slideleft moves the picture@@ -957,6 +1003,7 @@ export const TRANSITION_ZOOM = 1.18;* animation they'd duplicate the same visual. */export type AnimStyle =| "fade"+ | "blur"| "zoom"| "pop"| "slideleft"@@ -972,6 +1019,7 @@ export interface ClipAnim {export const ANIM_STYLE_IDS: AnimStyle[] = ["fade",+ "blur","zoom","pop","slideleft",@@ -982,6 +1030,7 @@ export const ANIM_STYLE_IDS: AnimStyle[] = [export const ANIM_STYLE_LABELS: Record<AnimStyle, string> = {fade: "Fade",+ blur: "Blur",zoom: "Zoom",pop: "Pop",slideleft: "Slide left",@@ -1014,12 +1063,15 @@ const EDGE_ANIM: Record<TransitionStyle, AnimStyle | null> = {crossfade: "fade",dipblack: "fade",dipwhite: "fade",- blur: "fade",+ blur: "blur",crosszoom: "zoom",circleopen: "pop",circleclose: "pop",splitopen: "pop",splitclose: "pop",+ clockwipe: "pop",+ sliceleft: "slideleft",+ sliceup: "slideup",pushleft: "slideleft",pushright: "slideright",pushup: "slideup",@@ -1033,6 +1085,7 @@ const EDGE_ANIM: Record<TransitionStyle, AnimStyle | null> = {const ANIM_TRANSITION: Record<AnimStyle, TransitionStyle> = {fade: "crossfade",+ blur: "blur",zoom: "crosszoom",pop: "circleopen",slideleft: "pushleft",@@ -1443,6 +1496,7 @@ export const SHAPE_LABELS: Record<ShapeKind, string> = {hexagon: "Hexagon",line: "Line",arrow: "Arrow",+ doodle: "Doodle",};/** A patch that may touch any kind's fields (never the discriminant). The@@ -1568,7 +1622,7 @@ export interface SubtitlesBlock {emphasisFont?: FontId;emphasisColor?: string;emphasisItalic?: boolean;- emphasisWeight?: 400 | 700;+ emphasisWeight?: TextWeight;/** Size multiplier over the caption's own size. */emphasisScale?: number;}diff --git a/site/src/cut/server/ai/catalog.ts b/site/src/cut/server/ai/catalog.tsindex ae7d2fe3..d1535ae8 100644--- a/site/src/cut/server/ai/catalog.ts+++ b/site/src/cut/server/ai/catalog.ts@@ -35,7 +35,7 @@ import { AUDIO_TOOLS } from "@/cut/components/AudioPanel.tools";import { CAMERA_CARD_TOOLS } from "@/cut/components/CameraCardSection.tools";import { EDITOR_TOOLS } from "@/cut/components/Editor.tools";import { EFFECT_TREATMENTS, EFFECTS_TOOLS } from "@/cut/components/EffectsPanel.tools";-import { ELEMENTS_TOOLS } from "@/cut/components/ElementsPanel.tools";+import { ELEMENTS_TOOLS, SHAPE_KINDS } from "@/cut/components/ElementsPanel.tools";import { VIDEO_GEN_TOOLS } from "@/cut/components/GeneratePanel.tools";import { IMAGE_GEN_TOOLS } from "@/cut/components/ImageGenPanel.tools";import { INSPECTOR_TOOLS } from "@/cut/components/Inspector.tools";@@ -69,6 +69,7 @@ import {SOURCE_PROFILES,WHEEL_LABELS,WHEEL_ZONES,+ TEXT_WEIGHTS,} from "@donkeycut/effects-kit";import type { AiToolDef } from "../../lib/aiToolDef";import { GOOGLE_FONT_IDS } from "../../lib/fontCatalog";@@ -362,12 +363,12 @@ Someone points at a video — a file they imported, a link you brought in, a cli"transitions-and-fades": `# Transitions, animations, looks & fadesRoute the ask to the right feature:- set_transition: a styled join between one video clip and the next, blended across the cut. It never moves clips or shortens anything, and it plays only while the pair touches. One style, audiocross — the cross dissolve — blends the sound alone: the picture cuts while the two clips ramp equal-power past each other, half the length either side of the cut, each playing into the other's time out of the media its trim left behind, so the mix holds its loudness through the join (it plays at cuts only, so it does nothing on a clip's open head or tail; a clip trimmed to the end of its source has nothing to cross with and its side stops at the cut). In the UI it is picked from the Effects tab's Sound family, beside the treatments on the sound, and its bar sits across the cut rather than before it; every style that blends the picture is picked from the Transitions tab.-- set_animation: one clip's own entrance ("in") or exit ("out") — the clip fades/zooms/pops/slides on its own edge. Never moves neighbors. For "fade this clip in", "make it slide in", use this. A wipe is a transition, not an animation.+- set_animation: one clip's own entrance ("in") or exit ("out") — the clip fades/blurs/zooms/pops/slides on its own edge. Never moves neighbors. For "fade this clip in", "make it slide in", use this. A wipe is a transition, not an animation.- set_background: the color of the frame itself, behind everything, for the whole cut. For "make the background white", or any cut whose picture is titles and shapes rather than footage. Covered in the graphics skill.- update_audio fadeIn/fadeOut: audio-only ramps on one soundtrack clip ("fade the music out").- An ask beyond every preset here — a reference template, a grid or mosaic reveal, a multi-panel build — is a construction job: the graphics skill's composite-builds section shows how overlay video tracks, masks, and keyframes compose into it.-set_transition styles (clipId = the leading clip; 0 clears, max 2s): crossfade blends; crosszoom adds a zoom punch; dipblack/dipwhite dip through a solid color; blur defocuses through the cut; pushleft/right/up/down shove the old frame out; wipeleft/right/up/down reveal with a hard traveling edge; circleopen/close and splitopen/close are shape reveals; audiocross — the cross dissolve — cuts the picture and dissolves only the sound across the cut, for a handover the eye takes as a cut and the ear takes as a blend. Directional names describe the motion.+set_transition styles (clipId = the leading clip; 0 clears, max 2s): crossfade blends; crosszoom adds a zoom punch; dipblack/dipwhite dip through a solid color; blur defocuses through the cut; pushleft/right/up/down shove the old frame out; sliceleft cuts the frame into a top and a bottom half that whip apart (top left, bottom right) and sliceup into a left and a right column (left up, right down), motion-blurred; wipeleft/right/up/down reveal with a traveling edge; clockwipe sweeps the new shot in like a clock hand pinned at the top-left corner; circleopen/close and splitopen/close are shape reveals; feather softens a wipe, circle or split edge into a blurred band; audiocross — the cross dissolve — cuts the picture and dissolves only the sound across the cut, for a handover the eye takes as a cut and the ear takes as a blend. Directional names describe the motion.set_animation styles: fade (audio follows), zoom, pop, slideleft/right/up/down — the slide names are the motion direction (slideleft enters from the right edge moving left, or exits off the left). Each edge holds one effect and the last pick wins: set_animation on a transitioned edge replaces that transition, set_transition clears the animations adjacent to its joint.Bars and the debris they leave: a transition lives on the transitions row as its own object at an absolute time, listed in editor_state \`transitions\` with the boundary it \`plays\`. It plays whatever cut or clip edge its window lines up with; a bar lining up with nothing reads \`parked: true\` and plays nothing — the badge is on the row, the blend is gone. Moving, retiming, or replacing clips is what parks a bar, so every timeline mutation returns \`parkedTransitions\` when its edit left one. Own that debris in the same turn you make it: a bar you parked and know the intent of, reattach with set_transition/set_animation on the edge it belongs to, or clear with remove_transition. When you can't tell what a parked bar was for — it predates your edits, or the user's ask doesn't say where the blend should end up — do not guess and do not leave it silent: name what's there ("there's a 0.5s dip to black at 29.4s that isn't on a cut any more") and ask whether to remove it or move it to a specific joint. The same holds for anything else on the timeline that reads as leftover from your own edits: say what you see and confirm before deleting the user's work.@@ -383,14 +384,14 @@ Use copy_selection and paste_selection for any timeline type, including effectsTimeline items start ungrouped. Use group_items to group any selection of clips, audio, elements, captions and transitions, and ungroup_items to separate them. Selecting a member selects the whole group; moving the group preserves its relative timing.The frame's own color is set_background, not a full-frame rectangle: it fills the frame behind every clip and element, holds for the whole cut, letterboxes fitted footage, and shows through gaps on track 0. "Make the background white", a colored card behind a title sequence, a text-and-graphics video with no footage at all — all of them start with set_background. A rectangle element is for a panel or a band inside the frame, not for the frame itself.A whole run of text — lines timed to speech, a lyric video, a kinetic-typography passage, a quote sequence — is add_text_sequence, one call. Design is opt-in: called with no look it lays a plain run — one readable face, one place in the frame, a short fade, no cards, no moves, the frame color untouched — which is what a bare "add the text" asks for. Name a look when the user asked for a designed run, and then the look is the design of the whole run, not one line: the frame color, the cards, and the ensemble the run walks across its length — faces, entrances, where successive lines land, and what each holds. Read the text-videos skill for the looks and the flow, and text-creativity for the fifty devices that keep a long run from reading flat. A designed run is composed in the call, never laid identical and then repaired element by element. A video needs no footage. Titles, shapes and stickers over a background color are a complete cut on their own: it plays, exports, and shares like any other. Build one by setting the background, then placing elements on the rows the stack needs — reach for footage only when the user asks for it.-Titles (add_title / update_overlay): text, size (frame px; the design short side is 1080), font (${FONTS.map((f) => `${f.id}=${f.label}`).join(", ")}), weight (400/700), color, shadow, plate (translucent dark backdrop). Type never leaves the picture: a line too wide for the room its anchor leaves is broken onto more lines as it draws, in the preview and in the export alike, so write the words at the size the design wants and leave the fitting to the renderer.-Shapes (add_shape): rect, ellipse, line, arrow. w/h are frame fractions; rect/ellipse take fill + fill_opacity, corner radius (rect), and an outline (stroke_color/stroke_width); line/arrow draw in \`fill\` with h as their thickness, and rotation gives them their direction (0° points right, 90° points down).+Titles (add_title / update_overlay): text, size (frame px; the design short side is 1080), font (${FONTS.map((f) => `${f.id}=${f.label}`).join(", ")}), weight (${TEXT_WEIGHTS.join("/")}), color, shadow, plate (translucent dark backdrop). Type never leaves the picture: a line too wide for the room its anchor leaves is broken onto more lines as it draws, in the preview and in the export alike, so write the words at the size the design wants and leave the fitting to the renderer.+Shapes (add_shape): ${SHAPE_KINDS.join(", ")}. w/h are frame fractions; rect/ellipse take fill + fill_opacity, corner radius (rect), and an outline (stroke_color/stroke_width); line/arrow draw in \`fill\` with h as their thickness, and rotation gives them their direction (0° points right, 90° points down). A filled shape's \`pattern\` lays the fill as stripes clipped to the outline (pattern_width/pattern_gap/pattern_angle; a segmented loading bar is 2 px lines with 1 px gaps). Any shape takes a shadow (shadow_color/shadow_blur/shadow_opacity/shadow_y), the same one titles take; a bright shadow_color at shadow_y 0 is a glow. A doodle is animated graffiti: hand-drawn marks in its fill and \`inks\` that change every few frames inside its box, the graffiti-film overlay; its row decides which titles it paints over.Stickers (add_sticker): a project image asset (generated cutouts in the Elements panel carry origin "sticker"); w is a frame-width fraction and height follows the source's aspect unless h sets it. Lottie JSON uploads become animated stickers that loop for the element's duration.Custom stickers (create_sticker): one call generates the image, removes the background, adds the die-cut outline, and places it — use it when the user asks for a sticker of something they don't have (it spends credits like image generation).Effects (add_effect): time-ranged treatments over the finished cut. Three families, one list: the picture treatments (${EFFECT_TREATMENTS.join(", ")}), the graded looks (${LOOK_EFFECTS.join(", ")}), and the audio treatments (${AUDIO_EFFECT_IDS.join(", ")}). A picture effect treats the footage and everything laid over it, and set_clip_effects puts one on a single clip, inside its mask; an audio effect treats everything audible under its window at once — clip sound, upper-track sound and the soundtrack — so "make this bit sound like a phone call", "put an echo on that line", or "muffle the music here" is an effect element over that stretch, and a per-clip volume, fade or duck is the audio tools instead. A handover on the sound at a cut belongs to the transition on that cut — the audiocross style — not here. Placed like any element and tuned with \`amount\` (update_overlay), and trims like anything else: a picture effect dropped over a clip opens covering that clip, while an audio effect opens at the standard length wherever it was placed — a treatment on the sound is aimed at a moment, so give it the window you mean. One effect at a time reads best, amounts under ~0.5. Effects are stylized post treatments on a stretch of timeline; pure color — exposure, warmth, a cinematic or film palette on one clip — is the per-clip grading tools instead (color-grading skill), whose preset catalog is separate from the look effects.Advanced text (update_overlay): italic, align (multi-line), letter_spacing (em), line_height, wrap_width (frame-width fraction; side grips wrap text, height follows content; corners scale type proportionally), stroke_color/stroke_width (em outline behind the fill), and richer shadows. Bundled Google families join the system set — ${GOOGLE_FONT_IDS.join(", ")} — and so do the user's own: a .ttf/.otf/.woff/.woff2 file dropped on the Library is offered in every project under a font id like "font:<key>". Editor_state lists the ones this account has under \`fonts\` — use an id from there instead of guessing at a family name, and when someone asks for a typeface that is not in the list, tell them the Library takes font files.Animation (set_overlay_animation): every element takes preset In/Out ramps (${OVERLAY_ANIM_STYLE_IDS.join(", ")} — ${TEXT_ONLY_ANIM_STYLE_IDS.join(", ")} for titles only; the per-letter styles sweep a title character by character; ${DIVE_ANIM_STYLE_IDS.join(", ")} flies into the last letter until its ink fills the frame, the built-in zoom-through transition), a Loop (${OVERLAY_LOOP_STYLE_IDS.join(", ")}) that runs its whole duration, and a Move that says what it does WHILE it holds. A title takes one more: Words (${WORD_EFFECT_IDS.join(", ")}), which plays the line word by word — an emphasis travelling along a line that is fully up, or a build assembling the line as it is spoken. A typewriter entrance takes a caret (caret, caret_blink, caret_blinks, caret_hide), the blinking bar of a typed comment or search box. Every element takes a Hit (${OVERLAY_HIT_STYLE_IDS.join(", ")}) at hit_at seconds inside it: press is a finger tapping a button — the Post or Like button of a call-to-action card — and a click from stock_search kind "sound" placed at the same moment makes it heard. Entrances 0.3–0.6s read well; one loop per scene, sparingly; a move on the lines that matter. All of them are slots on the element, applied internally, and they compose over its keyframe track. A slot is the reach when the motion is per-letter or per-word — typewriter, the glyph sweeps, the word effects — which one pose track cannot express; what the whole element does is keyed.-Keyframes (set_overlay_keyframes): for motion no preset covers — a path across the frame, a slow push in, a drift that holds first. A key is a whole pose (position, scale, rotation, opacity, blur) at a time in seconds from the element's start; the pose moves between keys along each key's ease (sine.inOut, power2/power3 in/out, default linear) and holds outside them, and presets still compose on top. This is the default way to animate an element: a key names the exact pose at the exact second, where a named ramp only approximates it. Two or three keys carry almost everything. In the UI the diamond in the inspector adds a key at the playhead, and dragging the element then records into it. set_clip_keyframes is the same track on a video clip (any track): pans, push-ins, picture-in-picture flights, spins, and fades on the footage itself.+Keyframes (set_overlay_keyframes): for motion no preset covers — a path across the frame, a slow push in, a drift that holds first. A key is a whole pose (position, scale, rotation, opacity, blur) at a time in seconds from the element's start; the pose moves between keys along each key's ease (sine.inOut, power2/power3 in/out, default linear) and holds outside them, and presets still compose on top. This is the default way to animate an element: a key names the exact pose at the exact second, where a named ramp only approximates it. Two or three keys carry almost everything. In the UI the diamond in the inspector adds a key at the playhead, and dragging the element then records into it. set_clip_keyframes is the same track on a video clip (any track): pans, push-ins, picture-in-picture flights, spins, and fades on the footage itself; its blur keys and motion_blur bring a panel in as a smear that settles sharp.Camera over a board of cards (set_group_camera): lay the cards out across the frame, group them, and key one camera that steps from card to card, pushes onto the one being talked about, pulls back, or whips — the cards keep their own keys inside it. Depth of field is the neighbours' own keys: blur and dim them at the moments the camera pushes in. motion_blur on the camera (or on an element, through update_overlay) streaks fast moves and leaves still frames sharp.Masks (set_mask): trim any element or video clip to a shape — ${MASK_SHAPE_KINDS.join(", ")} — with feather, invert, rounded corners on a box, and its own keyframe track for reveals and wipes. The pen kind takes an outline as points, for any region the fixed shapes miss: a doorway, a screen — read the region's corners off a captured frame in frame fractions, and stack a masked copy of the shot, treated by set_clip_effects or a grade, so the treatment shows only there. A region that moves with a hand, the window between the hands, a head or a person is track_motion's: use "mask" keys a pen outline onto it on every frame, one item for the whole span; use "follow" carries an element along with it. The subject kind trims by the person in the shot (on-device segmentation; preview and export match): inverted sits the item behind the speaker, plain keeps it only on the speaker. Behind-speaker reads well when the speaker is clearly separated from the background; with no detectable person it degrades to the plain picture. Group and Ungroup are in the timeline toolbar (⌘/Ctrl+G and ⇧⌘/Ctrl+G).Composite builds: these primitives compose. Stacked overlay video copies, masks and their keys, pose keyframes, staggered starts, and shapes construct effects beyond any preset list. Given a reference (a screenshot or clip of an effect the user wants), reverse-engineer it:diff --git a/site/src/cut/server/cutFrames.e2e.test.ts b/site/src/cut/server/cutFrames.e2e.test.tsnew file mode 100644index 00000000..4faf8928--- /dev/null+++ b/site/src/cut/server/cutFrames.e2e.test.ts@@ -0,0 +1,232 @@+import { describe, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { existsSync } from "node:fs";+import { mkdtemp, rm } from "node:fs/promises";+import os from "node:os";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { effectPreviewState } from "@donkeycut/effects-kit";+import { runExport, type ExportSpec, type RenderHandle } from "./exportPipeline";++// The bundled LGPL ffmpeg the app ships, ahead of anything else on PATH.+const TOOLS = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools");+const W = 64;+const H = 64;+const FPS = 30;++/** A solid-color clip file, `secs` long. */+const solid = (dir: string, name: string, color: string, secs: number) => {+ const r = spawnSync(path.join(TOOLS, "ffmpeg"), [+ "-v", "error", "-y", "-f", "lavfi", "-i", `color=c=${color}:s=${W}x${H}:r=${FPS}:d=${secs}`,+ "-pix_fmt", "yuv420p", "-c:v", "mpeg4", "-q:v", "2", path.join(dir, name),+ ]);+ expect(r.status).toBe(0);+};++/** The center pixel of every frame of `file`, as [r, g, b]. */+const centers = (file: string): [number, number, number][] => {+ const r = spawnSync(path.join(TOOLS, "ffmpeg"), ["-v", "error", "-i", file, "-f", "rawvideo", "-pix_fmt", "rgb24", "-"], {+ maxBuffer: 1 << 28,+ });+ expect(r.status).toBe(0);+ const size = W * H * 3;+ const at = (H / 2) * W * 3 + (W / 2) * 3;+ return [...Array(Math.floor(r.stdout.length / size)).keys()].map((i) => {+ const o = i * size + at;+ return [r.stdout[o], r.stdout[o + 1], r.stdout[o + 2]];+ });+};++describe("cuts between frames", () => {+ test.skipIf(!existsSync(path.join(TOOLS, "ffmpeg")))(+ "every clip starts on the frame the preview starts it on, and its effects keep the preview's clock",+ async () => {+ const mediaDir = await mkdtemp(path.join(os.tmpdir(), "cut-frames-media-"));+ const tmpDir = await mkdtemp(path.join(os.tmpdir(), "cut-frames-job-"));+ const PATH = process.env.PATH;+ process.env.PATH = `${TOOLS}:${PATH}`;+ try {+ solid(mediaDir, "red.mp4", "red", 4);+ const still = spawnSync(path.join(TOOLS, "ffmpeg"), [+ "-v", "error", "-y", "-f", "lavfi", "-i", `color=c=green:s=${W}x${H}`, "-frames:v", "1", path.join(mediaDir, "green.png"),+ ]);+ expect(still.status).toBe(0);+ solid(mediaDir, "blue.mp4", "blue", 2);++ // Red to 3.5s, a green still flashing 0.0433s, then blue under a black+ // strobe. The flash covers frames 105 and 106; the cut after it falls+ // at frame 106.3, so blue starts on frame 107. The flash pops black+ // on its first frame and shows green on its second.+ const POP = 0.0433;+ const STROBE = { effect: "flash", amount: 1, tone: "black", rate: 5.5 } as const;+ const spec: ExportSpec = {+ projectId: "cut-frames",+ width: W,+ height: H,+ fps: FPS,+ crf: 20,+ preset: "ultrafast",+ duration: 4.5433,+ background: "#000000",+ clips: [+ { file: "red.mp4", in: 0, out: 3.5, muted: true, fit: "fill" },+ { file: "green.png", in: 0, out: POP, muted: true, fit: "fill", image: true, effects: [{ ...STROBE, rate: 15 }] },+ { file: "blue.mp4", in: 0, out: 1, muted: true, fit: "fill", effects: [STROBE] },+ ],+ // ffmpeg's own ProRes writer, so the encoder is the same in every+ // build whichever one an earlier export probed.+ codec: "prores",+ container: "mov",+ audio: [],+ overlays: [],+ effects: [],+ captions: [],+ };+ const job: RenderHandle = { tmpDir, outPath: path.join(tmpDir, "out.mov"), progress: 0, log: [] };+ await runExport(job, spec, (file) => path.join(mediaDir, file));++ const px = centers(job.outPath);+ const hue = ([r, g, b]: [number, number, number]) =>+ r + g + b < 60 ? "dark" : r > g && r > b ? "red" : g > b ? "green" : "blue";+ expect(px.slice(103, 107).map(hue)).toEqual(["red", "red", "dark", "green"]);++ // From frame 107 the strobe darkens exactly where the preview does,+ // reading each frame at its time since blue's start.+ const want = [...Array(20).keys()].map((n) => {+ const tLocal = (107 + n) / FPS - (3.5 + POP);+ return (effectPreviewState("flash", 1, tLocal, undefined, undefined, 1, STROBE).flash ?? 0) > 0.5 ? "dark" : "blue";+ });+ expect(px.slice(107, 127).map(hue)).toEqual(want);+ } finally {+ process.env.PATH = PATH;+ await rm(mediaDir, { recursive: true, force: true });+ await rm(tmpDir, { recursive: true, force: true });+ }+ },+ 120_000+ );++ test.skipIf(!existsSync(path.join(TOOLS, "ffmpeg")))(+ "footage at another rate shows the source frame the preview shows, from any in point",+ async () => {+ const mediaDir = await mkdtemp(path.join(os.tmpdir(), "cut-frames-media-"));+ const tmpDir = await mkdtemp(path.join(os.tmpdir(), "cut-frames-job-"));+ const PATH = process.env.PATH;+ process.env.PATH = `${TOOLS}:${PATH}`;+ try {+ // A 25 fps clip whose frame i is grey level 8i: the preview shows, at+ // each 30 fps frame, the last source frame that has started by then.+ const STEP = 8;+ const made = spawnSync(path.join(TOOLS, "ffmpeg"), [+ "-v", "error", "-y", "-f", "lavfi", "-i", `color=c=black:s=${W}x${H}:r=25:d=1.2`,+ "-vf", `geq=lum='16+N*${STEP}':cb=128:cr=128`, "-pix_fmt", "yuv420p", "-c:v", "mpeg4", "-q:v", "1",+ path.join(mediaDir, "count.mp4"),+ ]);+ expect(made.status).toBe(0);+ // In at 0.03s, between source frames 0 and 1: the clip opens on+ // frame 0, already on screen there.+ const IN = 0.03;+ const spec: ExportSpec = {+ projectId: "cut-frames",+ width: W,+ height: H,+ fps: FPS,+ crf: 20,+ preset: "ultrafast",+ duration: 1,+ background: "#000000",+ clips: [{ file: "count.mp4", in: IN, out: IN + 1, muted: true, fit: "fill" }],+ codec: "prores",+ container: "mov",+ audio: [],+ overlays: [],+ effects: [],+ captions: [],+ };+ const job: RenderHandle = { tmpDir, outPath: path.join(tmpDir, "out.mov"), progress: 0, log: [] };+ await runExport(job, spec, (file) => path.join(mediaDir, file));++ // Each frame's grey, read back as the source frame it came from.+ const shown = centers(job.outPath).slice(0, 30).map(([r]) => Math.round((r * 219) / 255 / STEP));+ const want = [...Array(30).keys()].map((k) => Math.floor((IN + k / FPS) * 25 + 1e-9));+ expect(shown).toEqual(want);+ } finally {+ process.env.PATH = PATH;+ await rm(mediaDir, { recursive: true, force: true });+ await rm(tmpDir, { recursive: true, force: true });+ }+ },+ 120_000+ );++ test.skipIf(!existsSync(path.join(TOOLS, "ffmpeg")))(+ "an animated element shows each of its pictures on its own frame",+ async () => {+ const mediaDir = await mkdtemp(path.join(os.tmpdir(), "cut-frames-media-"));+ const tmpDir = await mkdtemp(path.join(os.tmpdir(), "cut-frames-job-"));+ const PATH = process.env.PATH;+ process.env.PATH = `${TOOLS}:${PATH}`;+ try {+ solid(mediaDir, "black.mp4", "black", 3);+ // Thirty pictures one frame each from 1s, picture k grey level 16 + 7k.+ const N = 30;+ const STEP = 7;+ const png = (name: string, color: string) =>+ expect(+ spawnSync(path.join(TOOLS, "ffmpeg"), [+ "-v", "error", "-y", "-f", "lavfi", "-i", `color=c=${color}:s=${W}x${H}`, "-frames:v", "1", path.join(tmpDir, name),+ ]).status+ ).toBe(0);+ png("blank.png", "[email protected]");+ for (let k = 0; k < N; k++) {+ const v = (16 + STEP * k).toString(16).padStart(2, "0");+ png(`f${k}.png`, `0x${v}${v}${v}`);+ }+ const spec: ExportSpec = {+ projectId: "cut-frames",+ width: W,+ height: H,+ fps: FPS,+ crf: 20,+ preset: "ultrafast",+ duration: 3,+ background: "#000000",+ clips: [{ file: "black.mp4", in: 0, out: 3, muted: true, fit: "fill" }],+ codec: "prores",+ container: "mov",+ audio: [],+ overlays: [+ {+ x: 0,+ y: 0,+ start: 1,+ end: 2,+ blank: "blank.png",+ frames: [...Array(N).keys()].map((k) => ({ file: `f${k}.png`, duration: 1 / FPS })),+ },+ ],+ effects: [],+ captions: [],+ };+ const job: RenderHandle = { tmpDir, outPath: path.join(tmpDir, "out.mov"), progress: 0, log: [] };+ await runExport(job, spec, (file) => path.join(mediaDir, file));++ // Frame 30 + k shows picture k: from one frame to the next the grey+ // climbs one picture's step (a repeat would hold, a skip would climb+ // two), read through the round trip to video levels.+ const px = centers(job.outPath).map(([r]) => r);+ expect(px[29]).toBeLessThan(px[30] - STEP / 2);+ const climbs = px.slice(30, 30 + N).slice(1).map((r, k) => r - px[30 + k]);+ for (const c of climbs) {+ expect(c).toBeGreaterThan(STEP / 2);+ expect(c).toBeLessThan(STEP * 1.5);+ }+ } finally {+ process.env.PATH = PATH;+ await rm(mediaDir, { recursive: true, force: true });+ await rm(tmpDir, { recursive: true, force: true });+ }+ },+ 120_000+ );+});diff --git a/site/src/cut/server/exportPipeline.test.ts b/site/src/cut/server/exportPipeline.test.tsindex c5c715bb..261b1f58 100644--- a/site/src/cut/server/exportPipeline.test.ts+++ b/site/src/cut/server/exportPipeline.test.ts@@ -394,7 +394,7 @@ describe("a speed curve in the filtergraph", () => {const curved = clip("a.mp4", { speedCurve: [[0, 1], [4, 4]] });const g = await graphFor({ clips: [curved, clip("b.mp4")] });const video = g.find((f) => f.startsWith("[0:v]trim="))!;- expect(video).toContain("setpts='(clip(T-STARTT");+ expect(video).toContain("setpts='(clip(T-0,0,");expect(video).not.toContain("(PTS-STARTPTS)/");// The baked WAV joins the inputs after the two media files, and the// clip's sound is read from it in timeline seconds with no tempo.@@ -432,13 +432,27 @@ describe("smooth slow motion in the filtergraph", () => {expect(head).toContain("split=3[smic0_0][smic0_1][smic0_2]");const pieces = g.filter((f) => f.startsWith("[smic0_"));expect(pieces.length).toBe(3);- expect(pieces[0]).toContain("fps=30[smo");+ expect(pieces[0]).toContain("fps=30:round=up:start_time=0[smo");expect(pieces[0]).not.toContain("minterpolate");expect(pieces[1]).toContain("minterpolate=fps=30");expect(pieces[2]).not.toContain("minterpolate");expect(g.some((f) => f.includes("concat=n=3:v=1:a=0,fps=30[smoc0]"))).toBe(true);});+ test("a curve stamps the preroll before 0, so interpolation never sees it", async () => {+ // The 1 s preroll ahead of the in point must land before 0 for the+ // interpolation's trim=start=0 to drop it: 4.5 s is preroll, 5 s the in point.+ const curved = clip("a.mp4", { in: 5, out: 9, smoothSlow: true, speedCurve: [[0, 0.5], [4, 0.5]] });+ const head = (await graphFor({ clips: [curved] })).find((f) => f.startsWith("[0:v]trim="))!;+ expect(head).toContain("trim=start=0,");+ const expr = /setpts='([^']+)'/.exec(head)![1];+ const at = (T: number) =>+ new Function("T", "TB", "clip", "min", `return ${expr};`)(T, 1, (x: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, x)), Math.min) as number;+ expect(at(4.5)).toBeLessThan(0);+ expect(at(5)).toBeCloseTo(0, 6);+ expect(at(5.5)).toBeGreaterThan(0);+ });+test("a clip at 1× or faster keeps its plain chain with the flag on", async () => {const g = await graphFor({ clips: [clip("a.mp4", { speed: 2, smoothSlow: true })] });expect(g.join(";")).not.toContain("minterpolate");@@ -588,7 +602,7 @@ describe("clip masks in the filtergraph", () => {const joined = g.join(";");// Both effects gate on the segment's own clock, 0 to its 2s length, in// the order the clip lists them.- const negAt = g.findIndex((c) => c.includes("lutyuv=y='minval+maxval-val'") && c.includes("gte(t,0)*lt(t,2)"));+ const negAt = g.findIndex((c) => c.includes("lutyuv=y='minval+maxval-val'") && c.includes("gte(t,0.000000)*lt(t,2.000000)"));const hueAt = g.findIndex((c) => c.includes("hue=H='2*PI*0.8*(t-0)'"));const padAt = g.findIndex((c) => c.includes("pad=540:960") && c.includes("[email protected]"));expect(negAt).toBeGreaterThanOrEqual(0);@@ -722,6 +736,18 @@ describe("clip keyframes in the filtergraph", () => {expect(xfadeMismatches(g)).toEqual([]);});+ test("a keyed clip off the frame grid poses on its own clock", async () => {+ // b starts at 3.71 s; its first frame is 3.7333 s (frame 112 at 30 fps),+ // so its keys read 0.023 s into the clip on the segment's first frame,+ // as the clip's effects and the preview do.+ const g = await graphFor({ clips: [clip("a.mp4", { out: 3.71 }), clip("b.mp4", { kf: KF })] });+ const posed = g.filter((f) => f.includes("rotate=a=") || f.includes("-w/2"));+ expect(posed.length).toBeGreaterThan(0);+ for (const f of posed) {+ expect(f).toContain("(t+0.023)");+ }+ });+test("a keyed overlay clip positions by expression with its start folded in", async () => {const g = await graphFor({clips: [clip("a.mp4", { out: 8 })],@@ -768,6 +794,56 @@ describe("clip keyframes in the filtergraph", () => {});});+describe("keyed clip blur in the filtergraph", () => {+ const key = (t: number, x: number, blur?: number) => ({ t, x, y: 0.5, scale: 1, rotation: 0, opacity: 1, ...(blur !== undefined ? { blur } : {}) });++ test("pose-key blur ramps a Gaussian blur frame by frame on a track-0 clip", async () => {+ const g = await graphFor({ clips: [clip("a.mp4", { out: 2, kf: [key(0, 0.5, 30), key(0.3, 0.5, 0)] })] });+ const joined = g.join(";");+ expect(joined).toContain("premultiply=inplace=1");+ expect(joined).toMatch(/sendcmd=c='0\.000 gblur@\w+ sigma /);+ expect(joined).toContain("unpremultiply=inplace=1");+ expect(xfadeMismatches(g)).toEqual([]);+ });++ test("pose-key blur samples a clip off the frame grid on its own clock", async () => {+ // The blur falls from 30 over 0.3 s. b's first frame is 0.023 s into the+ // clip, so its first sigma sits below the one an on-grid clip opens on.+ const firstSigma = (g: string[]) => Number(/sendcmd=c='0\.000 gblur@\w+ sigma ([\d.]+)/.exec(g.join(";"))![1]);+ const keyed = { kf: [key(0, 0.5, 30), key(0.3, 0.5, 0)] };+ const onGrid = firstSigma(await graphFor({ clips: [clip("a.mp4", { out: 3.7 }), clip("b.mp4", keyed)] }));+ const offGrid = firstSigma(await graphFor({ clips: [clip("a.mp4", { out: 3.71 }), clip("b.mp4", keyed)] }));+ expect(offGrid).toBeLessThan(onGrid);+ });++ test("motion blur averages shifted copies of a moving overlay clip", async () => {+ const g = await graphFor({+ clips: [clip("a.mp4", { out: 4 })],+ overlayVideos: [+ { file: "ov.mp4", in: 0, out: 2, start: 1, track: 1, muted: true, frame: { x: 0, y: 0, w: 0.5, h: 0.5 }, kf: [key(0, 0.1), key(0.3, 0.25)], motionBlur: 1 },+ ],+ });+ const joined = g.join(";");+ expect(joined).toMatch(/mix=inputs=\d+/);+ expect(joined).toContain("crop=");+ expect(xfadeMismatches(g)).toEqual([]);+ });++ test("a blur entrance on an open edge blurs in from the frame, its sound with it", async () => {+ const g = await graphFor({ clips: [clip("a.mp4", { out: 2, animIn: { style: "blur", seconds: 0.5 } })] });+ const joined = g.join(";");+ expect(joined).toContain("xfade=transition=hblur:duration=0.500");+ expect(joined).toContain("afade=t=in:st=0:d=0.500");+ });++ test("a keyed clip with no blur and no shutter takes no softening pass", async () => {+ const g = await graphFor({ clips: [clip("a.mp4", { out: 2, kf: [key(0, 0.4), key(1, 0.6)] })] });+ const joined = g.join(";");+ expect(joined).not.toContain("premultiply");+ expect(joined).not.toContain("sendcmd");+ });+});+describe("the project background in the filtergraph", () => {test("a cut of nothing but elements renders the background for its whole length", async () => {const g = await graphFor({@@ -797,7 +873,7 @@ describe("the project background in the filtergraph", () => {const g = await graphFor({ clips: [clip("a.mp4", { fit: "fill", flipH: true })] });expect(g.join(";")).toContain(":(ih-oh)*0.500,hflip,setsar=1");const v = await graphFor({ clips: [clip("a.mp4", { flipV: true })] });- expect(v.join(";")).toContain(",vflip,setsar=1,format=yuv420p,pad=");+ expect(v.join(";")).toMatch(/,vflip,setsar=1,[^;]*format=yuv420p,pad=/);});test("no background named keeps the black frame every cut had before", async () => {@@ -902,15 +978,16 @@ describe("a reversed clip in the filtergraph", () => {const join = runs.find((a) => a.includes("concat"))!;expect(join).toContain("-c");expect(join[join.indexOf("-c") + 1]).toBe("copy");- // The graph reads the copy, never the source, and trims the mirrored- // span: source [2, 8] under a pivot of 9 is [1, 7] in the copy.+ // The graph reads the copy, never the source, and times the mirrored+ // span from its in point: source [2, 8] under a pivot of 9 is [1, 7] in+ // the copy.const inputs = graph.slice(0, graph.indexOf("-filter_complex"));expect(inputs).not.toContain("/media/a.mp4");expect(inputs.some((p) => p.endsWith("turned_clip_0.mov"))).toBe(true);const idx = inputs.filter((p, i) => inputs[i - 1] === "-i").findIndex((p) => p.endsWith("turned_clip_0.mov"));const g = graph[graph.indexOf("-filter_complex") + 1].split(";");const video = g.find((f) => f.startsWith(`[${idx}:v]trim=`))!;- expect(video).toContain("trim=1.000:7.000,setpts=(PTS-STARTPTS)/2");+ expect(video).toContain("trim=0.000:7.000,setpts=(PTS-1.000/TB)/2.000");expect(video).not.toContain("reverse");const audio = g.find((f) => f.startsWith(`[${idx}:a]atrim=`))!;expect(audio).toContain("atrim=1.000:7.000");@@ -930,7 +1007,7 @@ describe("a reversed clip in the filtergraph", () => {});const g = await graphFor({ clips: [curved, clip("b.mp4")] });const video = g.find((f) => f.startsWith("[1:v]trim="))!;- expect(video).toContain("trim=0.000:4.000,setpts='(clip(T-STARTT");+ expect(video).toContain("trim=0.000:4.000,setpts='(clip(T-0,0,");const first = g.find((f) => f.includes("apad=whole_dur="))!;expect(first).toContain(`apad=whole_dur=${retimeOf(curved).len.toFixed(3)}`);});@@ -1686,3 +1763,56 @@ describe("stems", () => {expect(runs.some((a) => a.includes("-filter_complex"))).toBe(true);});});++describe("an animated element's slideshow", () => {+ test("a long run of one-frame pictures stays on the timeline's frames", async () => {+ // 102 frames at 30 fps from 8.5333s, the doodle that drew a frame early+ // every few beats when each duration was rounded to the millisecond.+ const START = 8.5333;+ const N = 102;+ await graphFor({+ clips: [clip("a.mp4", { out: 12 })],+ overlays: [+ {+ start: START,+ end: START + N / 30,+ x: 0,+ y: 0,+ blank: "b.png",+ frames: [...Array(N).keys()].map((k) => ({ file: `d_f${k}.png`, duration: 1 / 30 })),+ },+ ],+ });+ const list = written.find((w) => w.file.endsWith("overlay_anim_0.ffconcat"))!.data;+ const durations = [...list.matchAll(/^duration (\S+)$/gm)].map((m) => Number(m[1]));+ // Each picture starts where the timeline puts it, to the microsecond.+ let at = durations[0];+ expect(at).toBeCloseTo(START, 6);+ for (let k = 1; k <= N; k++) {+ at += durations[k];+ expect(Math.abs(at - (START + k / 30))).toBeLessThan(2e-6);+ }+ });+});++describe("windows on floored times", () => {+ test("an element, an overlay clip and an effect starting a hair before a frame show on that frame", async () => {+ // 4.2666s sits just under frame 128 (4.266667s at 30 fps): every window+ // has to open by that frame's time.+ const AT = 4.2666;+ const g = (+ await graphFor({+ clips: [clip("a.mp4", { out: 8 })],+ overlays: [{ file: "el.png", x: 0, y: 0, start: AT, end: 6 }],+ overlayVideos: [{ file: "o.mp4", in: 0, out: 1, start: AT, track: 1, muted: true }],+ effects: [{ effect: "negative", start: AT, end: 6 }],+ })+ ).join(";");+ const opens = [...g.matchAll(/gte\(t,([0-9.]+)\)|between\(t,([0-9.]+),/g)].map((m) => Number(m[1] ?? m[2]));+ const near = opens.filter((t) => Math.abs(t - AT) < 0.01);+ expect(near.length).toBeGreaterThanOrEqual(3);+ for (const t of near) {+ expect(t).toBeLessThanOrEqual(128 / 30);+ }+ });+});diff --git a/site/src/cut/server/exportPipeline.ts b/site/src/cut/server/exportPipeline.tsindex 4960eaaf..d2c1dea2 100644--- a/site/src/cut/server/exportPipeline.ts+++ b/site/src/cut/server/exportPipeline.ts@@ -7,6 +7,7 @@ export type { SpecClipColor };import { readFile, stat, unlink, writeFile } from "node:fs/promises";import path from "node:path";import { atempoChain, audioChannels, hasStream, mediaDuration, num, videoDecodeCost, videoDimensions } from "./util";+import { xfadeTransition } from "./transitionExpr";import { assertGraphSafe, fexpr } from "./filterGraph";import { bakeRetimedAudio, setptsExpr, type BakedAudio } from "./retimeAudio";import { bakeTurnedMedia } from "./turnMedia";@@ -15,8 +16,8 @@ import { alignRecording } from "./soundAlign";import { shiftSpan, type SpecSound } from "../lib/soundSource";import { masterRawMix, packStems } from "./exportAudio";import { STEM_CHANNELS, STEM_RATE, type StemDef } from "../lib/stems";-import { CLIP_MAX_ZOOM, regionPx, TRANSITION_XFADE, TRANSITION_ZOOM, type ColorGrade, type TransitionStyle } from "../lib/types";-import { audioFxFilters, buildClipLut, buildTransferLut, CLARITY_EPS, compositeSpaceFor, detailActive, detailGain, detailRadius, effectFilterLines, graphicsToHlg, hexToHlgHex, hlgToPq, isAudioEffect, lookFilterLines, lutToCube, mirrorRetimable, parseLutFile, recipeKey, retimeOf, shortestTurn, slowRuns, srcSpan, sortedKeys, soundFilters, type ChainChroma, type ClipColorRecipe, type ClipEffect, type ClipSound, type CodeFormat, type OutputSpace, type OverlayKey, type ParsedLut, type Retime, type SpeedNode } from "@donkeycut/effects-kit";+import { CLIP_MAX_ZOOM, CLIP_MIN_SECONDS, clipFrameAt, regionPx, TRANSITION_ZOOM, type ColorGrade } from "../lib/types";+import { audioFxFilters, buildClipLut, elementLook, buildTransferLut, CLARITY_EPS, compositeSpaceFor, detailActive, detailGain, detailRadius, effectFilterLines, effectRecipe, type FlashRhythm, type FlashTone, type GlitchKind, type LeakCourse, graphicsToHlg, hexToHlgHex, hlgToPq, isAudioEffect, lookFilterLines, lutToCube, mirrorRetimable, parseLutFile, recipeKey, retimeOf, shortestTurn, slowRuns, srcSpan, sortedKeys, soundFilters, type ChainChroma, type ClipColorRecipe, type ClipEffect, type ClipSound, type CodeFormat, type OutputSpace, type OverlayKey, type ParsedLut, type Retime, type SpeedNode } from "@donkeycut/effects-kit";// The render pipeline itself: spec in, finished mp4 out. Shared by the local// engine's job registry (jobs.ts) and the cloud render worker, which stage@@ -175,6 +176,9 @@ export interface ExportSpec {* zoom renders as the fade plus zoom ramps on both segments' overlap* windows. */transitionStyle?: string;+ /** Softness of that transition's reveal edge (lib/transitionShape.ts);+ * absent = hard. */+ transitionFeather?: number;/** This clip's own entrance/exit animation, baked into the segment's* head/tail window: fade (audio follows), zoom, pop, or a slide* against black. Unknown styles render as a fade. */@@ -215,7 +219,11 @@ export interface ExportSpec {* the picture's center (frame fractions), scale multiplies it, rotation* turns it. Opacity keys ride the painted mask's luma, so the graph* never needs an animatable alpha filter. */- kf?: { t: number; x: number; y: number; scale: number; rotation: number; opacity: number }[];+ kf?: OverlayKey[];+ /** Motion blur shutter, 0..1 of a 30fps frame, streaking the keyed+ * movement; absent = off. Key blur and the streak soften the posed+ * segment frame by frame. */+ motionBlur?: number;/** Client-painted border ring (a stroked rounded rect along the clip's* box, transparent elsewhere), full-frame sized; overlaid onto the* segment before fades, masks and pose so it rides the clip. */@@ -309,7 +317,9 @@ export interface ExportSpec {subject?: { invert?: boolean; feather?: number };};/** Keyframed pose track, seconds from this overlay's start. */- kf?: { t: number; x: number; y: number; scale: number; rotation: number; opacity: number }[];+ kf?: OverlayKey[];+ /** Motion blur shutter, as on `clips`. */+ motionBlur?: number;/** Client-painted border ring, box-sized (a letterboxed segment pads out* to its box when bordered); overlaid onto the segment so it rides the* clip through fades, masks and pose. */@@ -383,6 +393,14 @@ export interface ExportSpec {focus?: { x: number; y: number };/** Seconds a zoom takes to reach its depth. */ramp?: number;+ /** A light leak's course. */+ leak?: LeakCourse;+ /** The kind a glitch is pinned to. */+ glitch?: GlitchKind;+ /** A flash's tone, strobe rate and rhythm. */+ tone?: FlashTone;+ rate?: number;+ rhythm?: FlashRhythm;/** Its row in the element stack; lane 0 is the top. */lane?: number;start: number;@@ -600,8 +618,23 @@ function ffColor(hex: string | undefined): string {/** The `setpts` that lays a trimmed span at its rate: the plain division for* one rate, the map's polyline for a curve (quoted: it carries commas). */-const retimedPts = (rt: Retime) =>- rt.uniform ? `(PTS-STARTPTS)/${num(rt.rate)}` : fexpr(setptsExpr(rt));+/** The longest a source frame may stay on screen: footage runs at 1 fps or+ * more, so a trim reaching this far before the in point keeps the frame+ * already showing there. */+const SOURCE_PREROLL = 1;++/** A footage clip's source span, timed from its in point: the frame on+ * screen at the in point comes along (stamped before 0) and the re-stamp to+ * the output rate shows it on the clip's first frame. */+const sourceSpan = (idx: number, c: { in: number; out: number }, rt: Retime) =>+ `[${idx}:v]trim=${num(Math.max(0, c.in - SOURCE_PREROLL))}:${num(c.out)},setpts=${retimedPts(rt, c.in)}`;++/** A curve clamps everything before its first knot to 0; the preroll keeps+ * its own seconds before 0, as one rate does, so a trim at 0 drops it. */+const retimedPts = (rt: Retime, from: number) =>+ rt.uniform+ ? `(PTS-${num(from)}/TB)/${num(rt.rate)}`+ : fexpr(`${setptsExpr(rt, from)}+min(T-${num(from)},0)/TB`);/** Timeline length of a media clip's span through its rate or curve. *//** ffmpeg's motion-compensated interpolation, laying `fps` frames over the@@ -610,6 +643,11 @@ const retimedPts = (rt: Retime) =>* inside the footage is never blended across. */const MOTION_INTERPOLATE = "mi_mode=mci:mc_mode=aobmc:me_mode=bidir:vsbmc=1:scd=fdiff";+/** Footage re-stamped to the output rate from 0: each output frame shows the+ * last source frame that has started by its time, as the preview's decoder+ * does. A 25 fps source at 30 fps holds its first frame twice. */+const sourceAt = (fps: number) => `fps=${fps}:round=up:start_time=0`;+/*** The head of a footage clip's picture chain: its retimed, framed source at* `fps`. A clip smoothing its slow motion runs ffmpeg's motion interpolation@@ -629,12 +667,13 @@ function framedTimebase(fps: number,filters: string[]): string {- const plain = `${timebase},fps=${fps},${framing},setsar=1`;+ const plain = `${timebase},${sourceAt(fps)},${framing},setsar=1`;if (c.image || !c.smoothSlow) return plain;const runs = slowRuns(rt, 1 / fps);if (runs.length === 0) return plain;const interp = `minterpolate=fps=${fps}:${MOTION_INTERPOLATE}`;- const framed = `${timebase},${framing},setsar=1`;+ // Interpolation starts on the first frame at or after the in point.+ const framed = `${timebase},trim=start=0,${framing},setsar=1`;if (runs.length === 1 && runs[0][0] <= 1e-6 && runs[0][1] >= rt.len - 1e-6) {return `${framed},${interp}`;}@@ -649,7 +688,7 @@ function framedTimebase(filters.push(`${framed},split=${pieces.length}${pieces.map((_, k) => `[smi${tag}_${k}]`).join("")}`);pieces.forEach((pc, k) => {filters.push(- `[smi${tag}_${k}]trim=${num(pc.from)}:${num(pc.to)},setpts=PTS-STARTPTS,${pc.slow ? interp : `fps=${fps}`}[smo${tag}_${k}]`+ `[smi${tag}_${k}]trim=${num(pc.from)}:${num(pc.to)},setpts=PTS-STARTPTS,${pc.slow ? interp : sourceAt(fps)}[smo${tag}_${k}]`);});filters.push(@@ -658,8 +697,50 @@ function framedTimebase(return `[smo${tag}]null`;}+/** Concat-list times are kept to the microsecond. */+const CONCAT_US = 1e6;++/** A window edge in seconds, to the microsecond: a time floored to the+ * tenth of a millisecond still opens on the frame it sits just under. */+const edge = (t: number) => t.toFixed(6);++/**+ * An ffconcat slideshow of pictures laid end to end from 0, each held until+ * its `to` second. Every duration is the gap between two microsecond-rounded+ * ends, so a long run of one-frame pictures never drifts off the timeline.+ * A picture whose end does not move past the one before is left out. Each+ * picture opens at the output rate: the demuxer stamps the slideshow in its+ * first picture's timebase, and a still's default of 25 fps would round+ * every start to 40 ms and drop one 30 fps picture in five.+ */+function concatList(holds: { file: string; to: number }[], fps: number): string {+ const lines = ["ffconcat version 1.0"];+ let at = 0;+ for (const h of holds) {+ const end = Math.round(h.to * CONCAT_US);+ if (end <= at) {+ continue;+ }+ lines.push(`file '${h.file}'`, `option framerate ${fps}`, `duration ${((end - at) / CONCAT_US).toFixed(6)}`);+ at = end;+ }+ return lines.join("\n") + "\n";+}++/** How far past a whole frame a time may sit, in frames, and still count as+ * on it: rounding in a sum of clip lengths never pushes a cut a frame late. */+const GRID_SLACK = 1e-3;++/** The first output frame at or after `t` seconds. */+const gridFrame = (t: number, fps: number) => Math.ceil(t * fps - GRID_SLACK);++/** Filters that hold a stream to exactly `count` frames at `fps`: a short+ * one repeats its last frame, a long one is cut. */+const gridHold = (count: number, fps: number) =>+ `tpad=stop_mode=clone:stop=${count},trim=end_frame=${count},setpts=PTS-STARTPTS,fps=${fps}`;+const spanLen = (c: { in: number; out: number; speed?: number; speedCurve?: SpeedNode[]; reverse?: boolean }) =>- Math.max(0.1, retimeOf(c).len);+ Math.max(CLIP_MIN_SECONDS, retimeOf(c).len);/** Seconds a range keeps on either side of itself beyond what a join or a* sound handle reaches, so a boundary never lands on a frame the neighbor@@ -1157,22 +1238,24 @@ export async function runExport(const o = spec.overlays[k];if (o.frames?.length && o.blank) {const blank = path.join(job.tmpDir, path.basename(o.blank));- const lines = ["ffconcat version 1.0"];- if (o.start > 1e-3) lines.push(`file '${blank}'`, `duration ${num(o.start)}`);+ // Blank up to the element, each picture to its end, blank after it.+ const holds: { file: string; to: number }[] = [];+ if (o.start > 1e-3) holds.push({ file: blank, to: o.start });let cursor = o.start;for (const f of o.frames) {- if (f.duration < 1e-4) continue;- lines.push(- `file '${path.join(job.tmpDir, path.basename(f.file))}'`,- `duration ${num(f.duration)}`- );cursor += f.duration;+ // A sliver no frame falls in stretches the hold before it.+ if (f.duration < 1e-4 && holds.length > 0) {+ holds[holds.length - 1].to = cursor;+ continue;+ }+ holds.push({ file: path.join(job.tmpDir, path.basename(f.file)), to: cursor });}if (spec.duration - cursor > 1e-3) {- lines.push(`file '${blank}'`, `duration ${num(spec.duration - cursor)}`);+ holds.push({ file: blank, to: spec.duration });}const list = path.join(job.tmpDir, `overlay_anim_${k}.ffconcat`);- await io.writeFile(list, lines.join("\n") + "\n");+ await io.writeFile(list, concatList(holds, fps));animOverlayInput.set(k, nInputs++);inputs.push("-f", "concat", "-safe", "0", "-i", list);} else if (o.file) {@@ -1219,16 +1302,19 @@ export async function runExport(tag: string): Promise<number | undefined> => {if (mk.frames?.length) {- const lines = ["ffconcat version 1.0"];+ const holds: { file: string; to: number }[] = [];+ let cursor = 0;for (const f of mk.frames) {- if (f.duration < 1e-4) continue;- lines.push(- `file '${path.join(job.tmpDir, path.basename(f.file))}'`,- `duration ${num(f.duration)}`- );+ cursor += f.duration;+ // A sliver no frame falls in stretches the hold before it.+ if (f.duration < 1e-4 && holds.length > 0) {+ holds[holds.length - 1].to = cursor;+ continue;+ }+ holds.push({ file: path.join(job.tmpDir, path.basename(f.file)), to: cursor });}const list = path.join(job.tmpDir, `${tag}.ffconcat`);- await io.writeFile(list, lines.join("\n") + "\n");+ await io.writeFile(list, concatList(holds, fps));const idx = nInputs++;inputs.push("-f", "concat", "-safe", "0", "-i", list);return idx;@@ -1328,23 +1414,20 @@ export async function runExport(for (const [lane, entries] of [...captionLanes.entries()].sort((a, b) => a[0] - b[0])) {entries.sort((a, b) => a.start - b.start);const blank = path.join(job.tmpDir, "sub_blank.png");- const lines = ["ffconcat version 1.0"];+ const holds: { file: string; to: number }[] = [];let cursor = 0;for (const c of entries) {const from = Math.max(c.start, cursor);if (c.end - from < 1e-3) continue;- if (from - cursor > 1e-3) lines.push(`file '${blank}'`, `duration ${num(from - cursor)}`);- lines.push(- `file '${path.join(job.tmpDir, path.basename(c.file))}'`,- `duration ${num(c.end - from)}`- );+ if (from - cursor > 1e-3) holds.push({ file: blank, to: from });+ holds.push({ file: path.join(job.tmpDir, path.basename(c.file)), to: c.end });cursor = c.end;}if (spec.duration - cursor > 1e-3) {- lines.push(`file '${blank}'`, `duration ${num(spec.duration - cursor)}`);+ holds.push({ file: blank, to: spec.duration });}const list = path.join(job.tmpDir, `captions_${lane}.ffconcat`);- await io.writeFile(list, lines.join("\n") + "\n");+ await io.writeFile(list, concatList(holds, fps));captionInputs.push(nInputs++);inputs.push("-f", "concat", "-safe", "0", "-i", list);}@@ -1354,12 +1437,13 @@ export async function runExport(// A clip's own effects run on its segment's clock over its framed// picture, after its look: chain fragment `core` in, the treated fragment// out. The clock starts `from` seconds in (a split's right half carries- // on), so the effects begin at -from on the segment's clock. The fades,+ // on), and `lead` more when the clip's first frame falls after its start,+ // so the effects begin at -(from + lead) on the segment's clock. The fades,// pad and mask that follow carry them, as in the preview.- const wearEffects = (core: string, c: { effects?: ClipEffect[]; effectsFrom?: number }, len: number, w: number, h: number, pixFmt: string, tag: string) => {- const from = c.effectsFrom ?? 0;+ const wearEffects = (core: string, c: { effects?: ClipEffect[]; effectsFrom?: number }, len: number, w: number, h: number, pixFmt: string, tag: string, lead: number) => {+ const from = (c.effectsFrom ?? 0) + lead;for (const [n, e] of (c.effects ?? []).entries()) {- const lines = effectFilterLines(`${tag}fi${n}`, `${tag}fo${n}`, e.effect, e.amount, -from, len, w, h, `${tag}x${n}`, undefined, undefined, { ...chroma, pixFmt });+ const lines = effectFilterLines(`${tag}fi${n}`, `${tag}fo${n}`, e.effect, e.amount, -from, len, w, h, `${tag}x${n}`, undefined, undefined, { ...chroma, pixFmt }, e);if (!lines) continue;filters.push(`${core}[${tag}fi${n}]`, ...lines);core = `[${tag}fo${n}]null`;@@ -1688,6 +1772,23 @@ export async function runExport(}}+ // The output frames each track-0 clip owns. The frame at time t shows the+ // clip that has started by t, as the preview does, so a clip runs from+ // the first frame at or after its start to the first one at or after its+ // end: a cut at frame 106.3 lands on frame 107. A clip that holds no frame+ // of its own still shows one, and the next clip takes it back.+ const segFrames: { first: number; count: number }[] = [];+ {+ let next = 0;+ spec.clips.forEach((c, j) => {+ const count = Math.max(1, gridFrame(clipStarts[j] + clipDur(c), fps) - next);+ segFrames.push({ first: next, count });+ next += count;+ });+ }+ /** Seconds from clip j's start to its first frame. */+ const segLead = (j: number) => segFrames[j].first / fps - clipStarts[j];+// The shared person matte, split once per subject-mask consumer: elements// trimmed to (or off) the person, and subject-masked clips on any track.// Each consumer takes one split, negates it when inverted, and blurs it by@@ -1747,35 +1848,155 @@ export async function runExport(kf.some((k) => Math.abs(k[field] - kf[0][field]) > 1e-6);/** The rotate/scale filters a keyed segment runs before positioning, on* its own local clock. `boxW`/`boxH` give the constant rotate canvas. */+ /** The key clock on a stream whose time 0 sits `startAt` seconds into the+ * item: `(t-1.500)` for an element starting at 1.5 s on the timeline,+ * `(t+0.023)` for a segment whose first frame lands 0.023 s in. */+ const keyClock = (startAt: number): string => {+ if (startAt > 1e-9) {+ return `(t-${num(startAt)})`;+ }+ return startAt < -1e-9 ? `(t+${num(-startAt)})` : "t";+ };const poseTransformFilters = (kf: OverlayKey[],boxW: number,- boxH: number+ boxH: number,+ lead: number): string => {const ks = sortedKeys(kf);+ const v = keyClock(-lead);let chain = "";if (varies(ks, "rotation") || Math.abs(ks[0].rotation) > 1e-6) {const diag = 2 * Math.ceil(Math.hypot(boxW, boxH) / 2);- const rot = piecewiseExpr(unwrappedRotation(ks), "t");+ const rot = piecewiseExpr(unwrappedRotation(ks), v);chain += `,rotate=a='${rot}*PI/180':ow=${diag}:oh=${diag}:[email protected]`;}if (varies(ks, "scale") || Math.abs(ks[0].scale - 1) > 1e-6) {- const sc = piecewiseExpr(ks.map((k) => ({ t: k.t, v: k.scale })), "t");+ const sc = piecewiseExpr(ks.map((k) => ({ t: k.t, v: k.scale })), v);chain += `,scale=w='trunc(iw*${sc}/2)*2':h=-2:eval=frame`;}return chain;};/** The overlay x/y expressions placing a keyed segment's center at its* pose, with the key clock offset to `startAt` on the consuming stream's- * timeline (0 for a local-clock base). */+ * timeline (minus the lead for a segment-clock base). */const posePositionExprs = (kf: OverlayKey[], startAt: number): { x: string; y: string } => {const ks = sortedKeys(kf);- const v = startAt > 1e-9 ? `(t-${num(startAt)})` : "t";+ const v = keyClock(startAt);return {x: `'${piecewiseExpr(ks.map((k) => ({ t: k.t, v: k.x })), v)}*${W}-w/2'`,y: `'${piecewiseExpr(ks.map((k) => ({ t: k.t, v: k.y })), v)}*${H}-h/2'`,};};+ // ---- Keyed blur and motion blur on a posed segment ----------------------+ // The kit evaluator samples the clip's key blur and motion streak on every+ // output frame, eases and all, so the graph softens what the preview does.+ // The segment pads onto a constant canvas with room for the smear and the+ // blur, premultiplies, averages copies shifted along the streak (the+ // preview's additive taps), and blurs with a Gaussian whose sigma sendcmd+ // sets frame by frame. Each stage runs only over the frames that soften,+ // so an 8-frame settle costs nothing for the rest of the clip.+ const softenPose = (+ cur: string,+ c: Parameters<typeof clipFrameAt>[0] & { kf?: OverlayKey[] },+ dur: number,+ boxW: number,+ boxH: number,+ toAlpha: string,+ tag: string,+ lead: number+ ): string => {+ if (!c.kf?.length || (!c.motionBlur && !c.kf.some((k) => k.blur))) return cur;+ const even = (n: number) => 2 * Math.ceil(n / 2);+ const q = (v: number) => Math.round(v * 20) / 20;++ // Per-frame blur sigma and streak, output px, on the segment's clock;+ // the kit samples the clip `lead` seconds further in, where it stands.+ const n = Math.max(1, Math.round(dur * fps));+ const blurs: { t: number; v: number }[] = [];+ const sx: { t: number; v: number }[] = [];+ const sy: { t: number; v: number }[] = [];+ let first = -1;+ let last = -1;+ let taps = 0;+ let maxBlur = 0;+ let reachX = 0;+ let reachY = 0;+ for (let i = 0; i < n; i++) {+ const t = i / fps;+ const look = elementLook(clipFrameAt(c, t + lead, W / H), outScale);+ const b = q(look?.blur ?? 0);+ const x = look && look.taps >= 2 ? q(look.streakX) : 0;+ const y = look && look.taps >= 2 ? q(look.streakY) : 0;+ blurs.push({ t, v: b });+ sx.push({ t, v: x });+ sy.push({ t, v: y });+ if (!b && !x && !y) {+ continue;+ }+ if (first < 0) {+ first = i;+ }+ last = i;+ taps = Math.max(taps, x || y ? (look?.taps ?? 0) : 0);+ maxBlur = Math.max(maxBlur, b);+ reachX = Math.max(reachX, Math.abs(x) / 2);+ reachY = Math.max(reachY, Math.abs(y) / 2);+ }+ if (first < 0) return cur;+ const on = `enable='between(t,${num(Math.max(0, (first - 0.5) / fps))},${num((last + 0.5) / fps)})'`;++ // The canvas holds the largest posed picture, its smear and its blur.+ const ks = c.kf;+ const turns = ks.some((k) => Math.abs(k.rotation) > 1e-6);+ const diag = 2 * Math.ceil(Math.hypot(boxW, boxH) / 2);+ const grow = Math.max(1, ...ks.map((k) => k.scale));+ const spread = Math.ceil(3 * maxBlur);+ const cw = even((turns ? diag : boxW) * grow + 2 * (Math.ceil(reachX) + spread));+ const ch = even((turns ? diag : boxH) * grow + 2 * (Math.ceil(reachY) + spread));+ const rx = taps >= 2 ? even(reachX + 1) : 0;+ const ry = taps >= 2 ? even(reachY + 1) : 0;+ filters.push(+ `[${cur}]${toAlpha},pad=w=${cw + 2 * rx}:h=${ch + 2 * ry}:x=(ow-iw)/2:y=(oh-ih)/2:[email protected]:eval=frame,` ++ `premultiply=inplace=1:${on}[${tag}p]`+ );+ let out = `${tag}p`;++ // The streak: copies at even steps along it, averaged. Outside the+ // window the first copy is the unshifted picture, which mix passes.+ if (taps >= 2) {+ const ex = piecewiseExpr(sx, "t");+ const ey = piecewiseExpr(sy, "t");+ const ins = Array.from({ length: taps }, (_, i) => `[${tag}s${i}]`);+ filters.push(`[${out}]split=${taps}${ins.join("")}`);+ for (let i = 0; i < taps; i++) {+ const f = num(i / (taps - 1) - 0.5);+ filters.push(+ `[${tag}s${i}]crop=${cw}:${ch}:x=${fexpr(`${rx}-(${f})*${ex}`)}:y=${fexpr(`${ry}-(${f})*${ey}`)}[${tag}c${i}]`+ );+ }+ filters.push(`${Array.from({ length: taps }, (_, i) => `[${tag}c${i}]`).join("")}mix=inputs=${taps}:${on}[${tag}m]`);+ out = `${tag}m`;+ }++ // The blur: sigma set on every frame it changes, half a frame early so+ // the command lands on its own frame whatever the rounding.+ let blur = "";+ if (maxBlur > 0) {+ const cmds: string[] = [];+ let prev = -1;+ for (let i = first; i <= last + 1 && i < n; i++) {+ if (blurs[i].v === prev) {+ continue;+ }+ prev = blurs[i].v;+ cmds.push(`${num(Math.max(0, (i - 0.5) / fps))} gblur@${tag} sigma ${num(prev)}`);+ }+ blur = `sendcmd=c='${cmds.join(";")}',gblur@${tag}=sigma=${num(blurs[first].v)}:${on},`;+ }+ filters.push(`[${out}]${blur}unpremultiply=inplace=1:${on}[${tag}o]`);+ return `${tag}o`;+ };/** One matte split, shaped for a consumer. Every counted consumer takes* exactly one, so the split's outputs all connect. */@@ -1925,6 +2146,7 @@ export async function runExport(const secs = Math.min(a.seconds, max);if (a.style === "zoom") return { kind: "zoom", secs };if (a.style === "pop") return { kind: "pop", secs };+ if (a.style === "blur") return { kind: "xfade", secs, xfade: "hblur" };if (/^slide(left|right|up|down)$/.test(a.style)) {return { kind: "xfade", secs, xfade: a.style };}@@ -2015,9 +2237,10 @@ export async function runExport(const tf = isFadeAnim(animOutNow) ? Math.min(animOutNow!.seconds, dur - hf) : 0;// The sound of a fade animation follows the picture whether the fade// renders inline (over black) or in the backdrop pass.- const ahf = animIn?.style === "fade" ? Math.min(animIn.seconds, dur) : 0;+ const soundFades = (s: string | undefined) => s === "fade" || s === "blur";+ const ahf = soundFades(animIn?.style) ? Math.min(animIn!.seconds, dur) : 0;const atf =- animOut?.style === "fade" ? Math.min(animOut.seconds, Math.max(0, dur - ahf)) : 0;+ soundFades(animOut?.style) ? Math.min(animOut!.seconds, Math.max(0, dur - ahf)) : 0;if (!headFx && hf <= 0.01 && czHead > 0.01) {headFx = { kind: "zoom", secs: Math.min(czHead, dur) };}@@ -2039,7 +2262,7 @@ export async function runExport(} else {timebase = c.image? `[${idx}:v]setpts=PTS-STARTPTS`- : `[${idx}:v]trim=${num(c.in)}:${num(c.out)},setpts=${retimedPts(rt)}`;+ : sourceSpan(idx, c, rt);}if ((c.image || rmIn || videoPresence.get(c.file)) && !c.hidden) {const region = regionPx(c.frame, W, H);@@ -2082,6 +2305,9 @@ export async function runExport(// a still just replays its looped input. The color runs on the framed// picture before the letterbox pad, so the bars stay the frame color.let core = framedTimebase(timebase, frame, `c${j}`, rmIn ? {} : c, rt, fps, filters);+ // The picture holds the frames the clip owns before anything paints+ // on it, so a held frame takes its own effects at its own time.+ core += `,${gridHold(segFrames[j].count, fps)}`;if (plan) core = plan.run(core, `c${j}`);core += `,format=${segFmt}`;// The look bakes in after grade + framing, before the edge effects, so@@ -2099,7 +2325,7 @@ export async function runExport(return `[lko${j}]null`;};const wears = !!c.effects?.length;- if (wears) core = wearEffects(lookAt(core), c, dur, W, H, segFmt, `cw${j}`);+ if (wears) core = wearEffects(lookAt(core), c, dur, W, H, segFmt, `cw${j}`, segLead(j));core += padding;if (!wears) core = lookAt(core);// The border ring lands after the look (true stroke color) and before@@ -2149,12 +2375,13 @@ export async function runExport(cur = `cmc${j}`;}if (keyed) {- const tf = poseTransformFilters(c.kf!, W, H);+ const tf = poseTransformFilters(c.kf!, W, H, segLead(j));if (tf) {filters.push(`[${cur}]null${tf}[ckt${j}]`);cur = `ckt${j}`;}- const pos = posePositionExprs(c.kf!, 0);+ cur = softenPose(cur, c, dur, W, H, videoToAlpha, `cso${j}`, segLead(j));+ const pos = posePositionExprs(c.kf!, -segLead(j));placeOnClear(cur, pos.x, pos.y, dur, `ckb${j}`, `ckp${j}`);cur = `ckp${j}`;}@@ -2242,7 +2469,8 @@ export async function runExport(// clone from the copies split off above, so a slide-in covers the previous// clip's last frame and a slide-out reveals the next clip's first frame.// Slides become cover/reveal (the backdrop stays put); pop alpha-blends- // over the frozen frame; fade — and any unknown stored style — crossfades.+ // over the frozen frame; blur defocuses across it; fade — and any unknown+ // stored style — crossfades.const segLabel = spec.clips.map((_, j) => `v${j}`);const backdropFx = (a: { style: string; seconds: number },@@ -2251,6 +2479,7 @@ export async function runExport(bg: string): EdgeFx => {if (a.style === "pop") return { kind: "pop", secs, bg };+ if (a.style === "blur") return { kind: "xfade", secs, xfade: "hblur", bg };if (/^slide(left|right|up|down)$/.test(a.style)) {const dir = a.style.slice(5);return { kind: "xfade", secs, xfade: `${side === "head" ? "cover" : "reveal"}${dir}`, bg };@@ -2373,9 +2602,19 @@ export async function runExport(// cross dissolve cuts the picture and crosses the sound instead — a tail// fade against the incoming clip's head fade. The rest hard-cut (concat).// Fold left so mixed sequences chain correctly.+ // Each segment is held to the frames it owns, so every cut lands on the+ // frame the preview cuts on and a short clip never shifts the rest.+ spec.clips.forEach((_, j) => {+ filters.push(+ `[${segLabel[j]}]${gridHold(segFrames[j].count, fps)}[vg${j}]`+ );+ segLabel[j] = `vg${j}`;+ });let vAcc = segLabel[0];let aAcc = "a0";- let acc = clipDur(spec.clips[0]); // running timeline length of the accumulator+ // Running timeline length of the accumulator, on the frame grid.+ const gridEnd = (j: number) => (segFrames[j].first + segFrames[j].count) / fps;+ let acc = gridEnd(0);for (let j = 1; j < spec.clips.length; j++) {const prev = spec.clips[j - 1];const durJ = clipDur(spec.clips[j]);@@ -2394,10 +2633,11 @@ export async function runExport(} else if (d > 0.01) {const offset = Math.max(0, acc - d);// The style id resolves through the allowlist map; anything unknown- // (or an old spec without a style) renders as a plain fade.- const kind = TRANSITION_XFADE[prev.transitionStyle as TransitionStyle] ?? "fade";+ // (or an old spec without a style) renders as a plain fade. The shaped+ // and softened styles come back as xfade expressions.+ const kind = xfadeTransition(prev.transitionStyle, prev.transitionFeather, d, W, H);filters.push(`[${segLabel[j]}]tpad=start_duration=${num(d)}:start_mode=clone[vh${j}]`);- filters.push(`[${vAcc}][vh${j}]xfade=transition=${kind}:duration=${num(d)}:offset=${num(offset)}[${vOut}]`);+ filters.push(`[${vAcc}][vh${j}]xfade=${kind}:duration=${num(d)}:offset=${num(offset)}[${vOut}]`);filters.push(`[${aAcc}]afade=t=out:st=${num(offset)}:d=${num(d)}[ah${j}]`);filters.push(`[ah${j}][a${j}]concat=n=2:v=0:a=1[${aOut}]`);} else {@@ -2407,7 +2647,7 @@ export async function runExport(filters.push(`[${vAcc}][${segLabel[j]}]concat=n=2:v=1:a=0,fps=${fps}[${vOut}]`);filters.push(`[${aAcc}][a${j}]concat=n=2:v=0:a=1[${aOut}]`);}- acc = acc + durJ;+ acc = gridEnd(j);vAcc = vOut;aAcc = aOut;}@@ -2499,7 +2739,7 @@ export async function runExport(} else {timebase = oc.image? `[${idx}:v]setpts=PTS-STARTPTS`- : `[${idx}:v]trim=${num(oc.in)}:${num(oc.out)},setpts=${retimedPts(ort)}`;+ : sourceSpan(idx, oc, ort);}let core = framedTimebase(timebase, framing, `o${k}`, orIn ? {} : oc, ort, fps, filters);if (plan) core = plan.run(core, `o${k}`);@@ -2515,7 +2755,9 @@ export async function runExport(core = `[olko${k}]null`;}}- core = wearEffects(core, oc, olen, boxW, boxH, lookFmt, `ow${k}`);+ // Seconds from the element's start to its first frame on the grid.+ const ovLead = gridFrame(oc.start, fps) / fps - oc.start;+ core = wearEffects(core, oc, olen, boxW, boxH, lookFmt, `ow${k}`, ovLead);if (boxPad) core += boxPad;// The border ring lands after the look and box pad, before the edge// ramps, so it fades and masks with the clip like the preview.@@ -2549,18 +2791,19 @@ export async function runExport(masked = `omc${k}`;}if (keyed) {- const tf = poseTransformFilters(oc.kf!, boxW, boxH);+ const tf = poseTransformFilters(oc.kf!, boxW, boxH, ovLead);if (tf) {filters.push(`[${masked}]${videoToAlpha}${tf},format=${alphaFmt}[okt${k}]`);masked = `okt${k}`;}+ masked = softenPose(masked, oc, olen, boxW, boxH, `format=${alphaFmt}`, `oso${k}`, ovLead);}- // The zoom slices' concat drops the stream's frame-rate metadata, and- // tpad converts start_duration to a frame count through it — without the- // fps re-stamp it pads zero frames and the overlay lands early.- filters.push(`[${masked}]fps=${fps},tpad=start_duration=${num(oc.start)}[${seg}]`);+ // The clip's first frame is the first one at or after its start, as on+ // track 0. The zoom slices' concat drops the stream's frame-rate+ // metadata, so the fps re-stamp keeps tpad's frames on the grid.+ filters.push(`[${masked}]fps=${fps},tpad=start=${gridFrame(oc.start, fps)}[${seg}]`);const next = `vovv${k}`;- const enable = `enable='between(t,${num(oc.start)},${num(end)})'`;+ const enable = `enable='between(t,${edge(oc.start)},${edge(end)})'`;// The shadow falls on whatever is already there, then the clip goes down// over it. Its silhouette is punched out, so the two never fight.const oshIdx = overlayShadowInput.get(oc);@@ -2693,7 +2936,7 @@ export async function runExport(} else if (o.file) {const idx = inputIndex.get(o.file)!;filters.push(- `[${onto}][${gfx(`${idx}:v`)}]overlay=0:0:enable='gte(t,${num(o.start)})*lt(t,${num(o.end)})'${ovl}[${next}]`+ `[${onto}][${gfx(`${idx}:v`)}]overlay=0:0:enable='gte(t,${edge(o.start)})*lt(t,${edge(o.end)})'${ovl}[${next}]`);} else {return onto;@@ -2727,7 +2970,7 @@ export async function runExport(const lines = effectFilterLines(vLabel,`vfx${i}`,- e.effect,+ effectRecipe(e),e.amount,Math.max(0, e.start),Math.min(e.end, spec.duration),@@ -2736,7 +2979,8 @@ export async function runExport(`fx${i}`,e.focus,e.ramp,- chroma+ chroma,+ e);if (!lines) continue;filters.push(...lines);diff --git a/site/src/cut/server/retimeAudio.ts b/site/src/cut/server/retimeAudio.tsindex 423fe0dd..d999030c 100644--- a/site/src/cut/server/retimeAudio.ts+++ b/site/src/cut/server/retimeAudio.ts@@ -121,10 +121,11 @@ export function wavFloat32(channels: Float32Array[], sampleRate: number): Uint8A/*** The `setpts` expression that lays a clip's frames along its map: the* reduced polyline of the retime as a sum of clipped ramps, so nothing nests- * however many knots the curve has. `T - STARTT` is the frame's seconds past- * the trim's first frame; the sum is its timeline second, over the timebase.+ * however many knots the curve has. `T - from` is the frame's seconds past+ * the clip's in point; the sum is its timeline second, over the timebase. A+ * frame from before the in point lands at 0.*/-export function setptsExpr(retime: Retime): string {+export function setptsExpr(retime: Retime, from: number): string {// Slopes carry six places: a rounded slope drifts by its error times the// knot's width, and three places would put a long ramp a frame off.const fnum = (n: number) => String(Math.round(n * 1e6) / 1e6);@@ -136,9 +137,9 @@ export function setptsExpr(retime: Retime): string {const w = s1 - s0;if (w <= 0) continue;const slope = (t1 - t0) / w;- const x = i === 0 ? "T-STARTT" : `T-STARTT-${fnum(s0)}`;+ const x = `T-${fnum(from + s0)}`;terms.push(`clip(${x},0,${fnum(w)})*${fnum(slope)}`);}- if (terms.length === 0) return "PTS-STARTPTS";+ if (terms.length === 0) return `PTS-${fnum(from)}/TB`;return `(${terms.join("+")})/TB`;}diff --git a/site/src/cut/server/transcribe.ts b/site/src/cut/server/transcribe.tsindex 522564c2..8527fb3f 100644--- a/site/src/cut/server/transcribe.ts+++ b/site/src/cut/server/transcribe.ts@@ -8,6 +8,7 @@ import { assertLocalRuntime } from "./local-only";import { createJobRegistry } from "./jobRegistry";import { mediaPath, readProject } from "./projects";import { retimeOf, type SpeedNode } from "@donkeycut/effects-kit";+import { CLIP_MIN_SECONDS } from "../lib/types";import { bakeRetimedAudio, type BakedAudio } from "./retimeAudio";import { atempoChain, findOnPath, hasStream, num, round, errorMessage } from "./util";@@ -352,10 +353,10 @@ async function runTranscribe(job: TranscribeJob, spec: TranscribeSpec) {const filters: string[] = [];// Per-clip timeline length: a sped-up clip is shorter and time-stretched,// so the transcript's cue times line up with what the user sees. A gap- // spacer (no file) keeps its exact length — flooring it at 0.1s would+ // spacer (no file) keeps its exact length — flooring it would// land every cue after the gap late.const clipDur = (c: TranscribeSpec["clips"][number]) =>- c.file ? Math.max(0.1, retimeOf(c).len) : Math.max(0, c.out - c.in);+ c.file ? Math.max(CLIP_MIN_SECONDS, retimeOf(c).len) : Math.max(0, c.out - c.in);// A clip whose rate changes through its footage is baked to a WAV in// timeline seconds first (the same stretch the export and the preview// use), so its words land where they play.@@ -435,7 +436,7 @@ async function runTranscribe(job: TranscribeJob, spec: TranscribeSpec) {spec.audio.forEach((a, k) => {if (!audible.get(a.file)) return;filters.push(- audioRead(a, Math.max(0.1, retimeOf(a).len)) ++ audioRead(a, Math.max(CLIP_MIN_SECONDS, retimeOf(a).len)) +`aresample=16000,aformat=sample_fmts=s16:channel_layouts=mono,` +`volume=${num(a.volume)},adelay=${Math.max(0, Math.round(a.start * 1000))}:all=1[snd${k}]`);diff --git a/site/src/cut/server/transitionExpr.ts b/site/src/cut/server/transitionExpr.tsnew file mode 100644index 00000000..8d5c555d--- /dev/null+++ b/site/src/cut/server/transitionExpr.ts@@ -0,0 +1,149 @@+/**+ * The xfade options a transition renders with on the ffmpeg export.+ *+ * Most styles are xfade built-ins. The shaped styles ffmpeg has no built-in+ * for — the clock wipe, the slices — and any reveal softened by a feather+ * run as `custom` expressions spelling the same geometry the compositor+ * draws (lib/transitionShape.ts). xfade hands an expression `P`, which runs+ * 1 → 0 across the transition, the two pictures' values at the pixel as `A`+ * and `B`, and `a0(x,y)`…`b3(x,y)` to read any pixel of either. The filter+ * evaluates one expression on several slice threads at once, so nothing here+ * stores into expression variables: every term is spelled inline.+ */++import { fexpr } from "./filterGraph";+import {+ CLOCK_SWEEP,+ clampFeather,+ FEATHER_STYLES,+ SLICE_KNEE,+ SLICE_KNEE_SHARE,+ SLICE_PULL,+ SLICE_SETTLE,+ SLICE_SHUTTER,+ SLICE_STYLES,+ SLICE_TAPS,+ takesFeather,+} from "../lib/transitionShape";+import { TRANSITION_XFADE, type TransitionStyle } from "../lib/types";++/** Linear progress, 0 → 1. */+const PROG = "(1-P)";++/** A number written short and without exponent notation. */+const n = (v: number) => (Math.abs(v) < 1e-9 ? "0" : Number(v.toFixed(6)).toString());++/** The value plane `PLANE` of picture `src` ("a" or "b") holds at (x, y). */+const sample = (src: "a" | "b", x: string, y: string) =>+ `if(eq(PLANE,0),${src}0(${x},${y}),if(eq(PLANE,1),${src}1(${x},${y}),if(eq(PLANE,2),${src}2(${x},${y}),${src}3(${x},${y}))))`;++/** The slices' eased share of their move: the whip `shapeEase` draws, a+ * power rise to the knee and a power settle after it. `P` is what is left. */+const SLICE_EASE =+ `if(lt(${PROG},${n(SLICE_KNEE)}),${n(SLICE_KNEE_SHARE)}*pow(${PROG}/${n(SLICE_KNEE)},${n(SLICE_PULL)}),` ++ `1-${n(1 - SLICE_KNEE_SHARE)}*pow(P/${n(1 - SLICE_KNEE)},${n(SLICE_SETTLE)}))`;++/** The slope of that whip, `sliceSpeed`. */+const SLICE_SPEED =+ `if(lt(${PROG},${n(SLICE_KNEE)}),${n((SLICE_KNEE_SHARE * SLICE_PULL) / SLICE_KNEE)}*pow(${PROG}/${n(SLICE_KNEE)},${n(SLICE_PULL - 1)}),` ++ `${n(((1 - SLICE_KNEE_SHARE) * SLICE_SETTLE) / (1 - SLICE_KNEE))}*pow(P/${n(1 - SLICE_KNEE)},${n(SLICE_SETTLE - 1)}))`;++/** The clock wipe's eased sweep: quadratic out. */+const CLOCK_EASE = "(1-P*P)";++/**+ * A soft reveal: the incoming picture's share at the pixel, the+ * `featherAlpha` profile with its distances in plane px. `F` is the ramp,+ * the feather's share of the distance the edge crosses.+ */+function featherShare(style: TransitionStyle, feather: number): string {+ const f = n(clampFeather(feather));+ const R = "(hypot(W,H)/2)";+ const ramp = (d: string, span: string, F: string, reach = PROG) => `clip((${reach}*(${span}+${F})-(${d}))/${F},0,1)`;+ const Fw = `(${f}*W)`;+ const Fh = `(${f}*H)`;+ const Fr = `(${f}*${R})`;+ switch (style) {+ case "wipeleft":+ return ramp("W-X", "W", Fw);+ case "wiperight":+ return ramp("X", "W", Fw);+ case "wipeup":+ return ramp("H-Y", "H", Fh);+ case "wipedown":+ return ramp("Y", "H", Fh);+ case "splitopen":+ return ramp("abs(X-W/2)", "W/2", Fw);+ case "splitclose":+ return ramp("min(X,W-X)", "W/2", Fw);+ case "circleopen":+ return ramp("hypot(X-W/2,Y-H/2)", R, Fr);+ default:+ // circleclose: the outgoing picture shrinks into a soft circle.+ return `(1-${ramp("hypot(X-W/2,Y-H/2)", R, Fr, "P")})`;+ }+}++/**+ * One half of a slice, averaged over the shutter's taps. `u` is where the+ * half's strip — outgoing then incoming — reads at this pixel along the+ * slide axis, before the tap offset.+ */+function sliceHalf(across: boolean, dir: -1 | 1, seconds: number): string {+ const len = across ? "W" : "H";+ const coord = across ? "X" : "Y";+ // The half moves `dir * e * len`; the pixel reads the strip that far back.+ const u = `(${coord}-(${dir})*${SLICE_EASE}*${len})`;+ const streak = `(${SLICE_SPEED}*${n(seconds > 0 ? SLICE_SHUTTER / seconds : 0)}*${len})`;+ const taps = [...Array(SLICE_TAPS).keys()].map((k) => {+ const off = SLICE_TAPS > 1 ? n(k / (SLICE_TAPS - 1) - 0.5) : "0";+ const at = `(${u}-${off}*${streak})`;+ // Leftward/upward halves pull the incoming picture in after the outgoing+ // one's far edge; rightward/downward ones before its near edge.+ const out = dir < 0 ? `lt(${at},${len})` : `gte(${at},0)`;+ const inc = dir < 0 ? `(${at}-${len})` : `(${at}+${len})`;+ const read = (src: "a" | "b", v: string) => (across ? sample(src, v, "Y") : sample(src, "X", v));+ return `if(${out},${read("a", at)},${read("b", inc)})`;+ });+ return `((${taps.join("+")})/${SLICE_TAPS})`;+}++/** A slice: the frame cut in two, each half sliding its own way. */+function sliceExpr(style: TransitionStyle, seconds: number): string {+ const across = style === "sliceleft";+ const first = across ? "lt(Y,H/2)" : "lt(X,W/2)";+ return `if(${first},${sliceHalf(across, -1, seconds)},${sliceHalf(across, 1, seconds)})`;+}++/** The clock wipe: the incoming picture behind a hand pinned at the top-left+ * corner. The angle is measured in frame px, so a plane subsampled on one+ * axis sweeps the same wedge. */+function clockExpr(W: number, H: number): string {+ return `if(lte(atan2(Y/H*${n(H)},X/W*${n(W)}),${CLOCK_EASE}*${n(CLOCK_SWEEP)}),B,A)`;+}++/**+ * The option string for one xfade join of a W×H frame lasting `seconds`:+ * `transition=<name>` for a built-in, or `transition=custom:expr='…'`.+ * Unknown styles render as a fade.+ */+export function xfadeTransition(+ style: string | undefined,+ feather: number | undefined,+ seconds: number,+ W: number,+ H: number+): string {+ const known = style as TransitionStyle;+ const kind = TRANSITION_XFADE[known] ?? "fade";+ if (SLICE_STYLES.includes(known)) {+ return `transition=custom:expr=${fexpr(sliceExpr(known, seconds))}`;+ }+ if (known === "clockwipe") {+ return `transition=custom:expr=${fexpr(clockExpr(W, H))}`;+ }+ if (takesFeather(known) && clampFeather(feather) > 0 && FEATHER_STYLES.includes(known)) {+ return `transition=custom:expr=${fexpr(`A+(B-A)*${featherShare(known, feather!)}`)}`;+ }+ return `transition=${kind}`;+}