Open source CapCut alternative.
TypeScript
"use client";
import { useTranslations } from "next-intl";
import { creatorNames } from "@/cut/lib/defaultNames";
import { guideSnapLines } from "@/cut/lib/guides";
import { createContext, Fragment, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, type CSSProperties } from "react";
import { createPortal } from "react-dom";
import { releaseAnimRest, useAnimPreview } from "@/cut/lib/animPreview";
import { startDrag } from "@/cut/lib/drag";
import { startSelectionDrag, togglePreviewSelection } from "@/cut/components/previewSelectionDrag";
import { pictureGroupSelected } from "@/cut/lib/previewSelection";
import { useSkim, usePreviewTime } from "@/cut/lib/playhead";
import { projectDuration, useEditor } from "@/cut/lib/store";
import { overlayLiveAt, stageTimeAt } from "@/cut/lib/overlayLive";
import {
captionStyle,
cueAt,
cueOverlay,
laneCues,
laneHidden,
subtitleLaneCount,
trackPos,
} from "@/cut/lib/subtitles";
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 } 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, reelBand, 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 {
LINE_HEIGHT,
PLATE_PAD_X,
PLATE_PAD_Y,
PLATE_RADIUS,
cutRenderEnv,
plateFill,
SHADOW,
wordDrawCss,
} from "@/cut/lib/textRender";
import { cycleFaceAt, fitTextToFrame, textBoxSize, textFontOf, textLineLift } from "@/cut/lib/textFit";
import {
behindSubjectOverlay,
clampKeyPos,
clampOverlayPos,
frameOf,
fontStack,
isTextOverlay,
laneOf,
type Overlay,
type OverlayPatch,
type ShapeOverlay,
type StickerOverlay,
} from "@/cut/lib/types";
import { subjectMatteSnapshot } from "@/cut/lib/behindPass";
import {
BOX_HANDLES,
CORNER_HANDLES,
FRAME_HANDLES,
HANDLE_AXIS,
TransformHandles,
type ResizeHandle,
} from "./TransformHandles";
import { stageEffectFilter, stageEffectTransform } from "@/cut/lib/effectStack";
import { rgbSplitFilter } from "./rgbSplit";
import { useLiveEffects } from "./StageEffects";
import { cn } from "@/lib/utils";
// Plate geometry as CSS, kept in lockstep with the export burn-in metrics.
const PLATE_PADDING = `${PLATE_PAD_Y}em ${PLATE_PAD_X}em`;
const PLATE_RADIUS_EM = `${PLATE_RADIUS}em`;
/** The unclipped layer the selected element's chrome mounts into. The stage
* clips its content at the frame; the chrome — outline, grips, mask gizmo —
* portals here so the box of an element hanging past the edge stays visible
* and grabbable, the way the video clip gizmo already does. */
export const OverlayChromeHost = createContext<HTMLElement | null>(null);
/** How the stage takes a press that started on something the user has not
* selected. Selection comes first: an unselected title, shape, sticker or
* caption is part of the picture, so pressing one pans the picture the way
* pressing bare footage does, and a press that never travels selects it.
* Grabbing to move is what a selected element does. */
export const StagePress = createContext<
((e: React.PointerEvent, onTap: () => void) => void) | null
>(null);
/**
* The rotate handle's cursor. CSS has no rotate cursor, so the affordance is
* drawn here — and because the glyph is an arc with a head at each end rather
* than a full circle, which way it faces carries meaning: it is turned to sit
* tangent to the element, so the heads always point the way a drag will take
* it. White over a dark rim keeps it legible on any footage.
*/
const ROTATE_GLYPH_W = 59;
const ROTATE_GLYPH_H = 90;
const ROTATE_GLYPH =
"M58.202 82.4995C57.967 81.6525 57.086 81.1635 56.239 81.3945L40.985 85.6485C46.122 77.9215 48.929 68.7815 48.929 59.4355C48.929 34.8625 30.107 14.6195 6.125 12.3295L18.995 2.88146C19.705 2.35846 19.857 1.36246 19.336 0.652464C18.815 -0.0575359 17.82 -0.213536 17.108 0.310464L0.65 12.3935C0.601 12.4295 0.576001 12.4825 0.533001 12.5225C0.480001 12.5705 0.437001 12.6245 0.391001 12.6795C0.306001 12.7815 0.230999 12.8825 0.174999 12.9995C0.138999 13.0715 0.118 13.1475 0.0939999 13.2255C0.0589999 13.3415 0.0320002 13.4545 0.0240002 13.5755C0.0210002 13.6165 0 13.6525 0 13.6945C0 13.7585 0.0289993 13.8125 0.0359993 13.8735C0.0479993 13.9695 0.0630004 14.0605 0.0930004 14.1545C0.133 14.2855 0.19 14.4015 0.261 14.5155C0.282 14.5495 0.285999 14.5885 0.309999 14.6215L12.391 31.0785C12.702 31.5055 13.186 31.7295 13.677 31.7295C14.004 31.7295 14.334 31.6295 14.619 31.4215C15.329 30.8985 15.481 29.9025 14.96 29.1925L4.873 15.4545C27.685 17.1435 45.741 36.1995 45.741 59.4355C45.741 68.5495 42.879 77.4515 37.663 84.8545L33.051 68.3205C32.814 67.4735 31.932 66.9815 31.088 67.2155C30.24 67.4525 29.745 68.3305 29.981 69.1805L35.467 88.8445C35.474 88.8695 35.494 88.8895 35.502 88.9135C35.551 89.0595 35.618 89.1925 35.709 89.3215C35.745 89.3735 35.785 89.4185 35.827 89.4665C35.88 89.5255 35.912 89.5965 35.975 89.6485C36.03 89.6935 36.096 89.7135 36.155 89.7495C36.179 89.7645 36.194 89.7895 36.218 89.8035C36.237 89.8145 36.261 89.8095 36.281 89.8205C36.505 89.9325 36.742 90.0095 36.984 90.0095C36.987 90.0095 36.99 90.0075 36.993 90.0075C36.996 90.0075 36.998 90.0095 37.001 90.0095C37.144 90.0095 37.289 89.9905 37.429 89.9505L57.095 84.4645C57.944 84.2275 58.439 83.3495 58.202 82.4995Z";
/** A square the glyph fits inside at any angle, so turning it never clips. */
const ROTATE_BOX = Math.ceil(Math.hypot(ROTATE_GLYPH_W, ROTATE_GLYPH_H));
/** The glyph's own arrows run top-to-bottom, so a quarter turn puts them
* left-to-right — the way an unrotated element's top handle drags. */
const ROTATE_BASE_DEG = -90;
/** Degrees around a quarter turn where a rotate drag locks on. Wide enough to
* feel as a detent at drag speed — a rotate drag covers several degrees per
* pointer event, so a narrow window slips by between frames. */
const ROTATE_SNAP_DEG = 6;
/** Land an angle on the quarter turn within reach: the angle to use and the
* detent it locked to (null when free). */
/** Degrees from a box's center to a pointer. `offset` orients the reading:
* an element's lollipop sits above its top edge (+90), a mask's on its line. */
const angleFrom = (cx: number, cy: number, offset = 0) =>
(ev: { clientX: number; clientY: number }) =>
(Math.atan2(ev.clientY - cy, ev.clientX - cx) * 180) / Math.PI + offset;
/** Into (-180, 180], the range every rotation is stored and detented in. */
const normDeg = (deg: number) => ((((deg + 180) % 360) + 360) % 360) - 180;
function snapQuarter(deg: number): { deg: number; locked: number | null } {
for (const q of [-180, -90, 0, 90, 180]) {
if (Math.abs(deg - q) < ROTATE_SNAP_DEG) return { deg: q, locked: q };
}
return { deg, locked: null };
}
const rotateCursorCache = new Map<number, string>();
function rotateCursor(rotationDeg: number): string {
// Quantized: a drag would otherwise mint a new cursor image every frame, and
// five degrees is below what the eye reads on a glyph this size.
const deg = Math.round((rotationDeg + ROTATE_BASE_DEG) / 5) * 5;
const hit = rotateCursorCache.get(deg);
if (hit) return hit;
const c = ROTATE_BOX / 2;
const dx = (ROTATE_BOX - ROTATE_GLYPH_W) / 2;
const dy = (ROTATE_BOX - ROTATE_GLYPH_H) / 2;
const layer = (attrs: string) =>
`<g transform='rotate(${deg} ${c} ${c}) translate(${dx} ${dy})'><path d='${ROTATE_GLYPH}' ${attrs}/></g>`;
const svg =
`<svg xmlns='http://www.w3.org/2000/svg' width='32' height='32' viewBox='0 0 ${ROTATE_BOX} ${ROTATE_BOX}'>` +
`${layer("fill='none' stroke='rgba(0,0,0,0.55)' stroke-width='10' stroke-linejoin='round'")}` +
`${layer("fill='%23ffffff'")}</svg>`;
const css = `url("data:image/svg+xml;utf8,${svg}") 16 16, grab`;
rotateCursorCache.set(deg, css);
return css;
}
/** Snap when a title edge/center lands within this many stage px of a line. */
const SNAP_PX = 6;
/** Safe-area inset (fraction of the frame) offered as margin snap lines. */
const CANVAS_MARGIN = 0.05;
/** Active alignment guides, as stage-pixel positions. */
interface Guides {
v: number[]; // vertical lines (x)
h: number[]; // horizontal lines (y)
}
/** A subtitle track's key in the shared snap-box registry. Overlay ids are
* uids, so these can't collide. */
const subtitleBoxId = (lane: number) => `subtitle-caption-${lane}`;
export function OverlayLayer({
stageWidth,
gradeAbove = null,
from = 0,
to = Infinity,
captions = true,
}: {
stageWidth: number;
/** Effects on a lane shallower than this one grade and move these elements.
* An effect filters what plays under it, so the ones below it wear its look
* and the ones above it do not (see StageEffects). Null grades nothing. */
gradeAbove?: number | null;
/** The lanes this slice of the stack draws: [from, to). */
from?: number;
to?: number;
/** Captions ride the topmost slice; nothing grades them. */
captions?: boolean;
}) {
const allOverlays = useEditor((s) => s.overlays);
// Lane 0 is the top row, so the deepest lane paints first and lane 0 lands
// over it. These boxes stack by DOM order alone; the document's array order
// is the order elements were made, which is not the stack the rows show and
// not the one the export draws.
const overlays = useMemo(
() =>
allOverlays
.filter((o) => laneOf(o) >= from && laneOf(o) < to)
.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);
const guidesHidden = useEditor((s) => s.guidesHidden);
// Titles preview under the skimmer too (paused only), matching the canvas.
const t = usePreviewTime();
const skimTime = useSkim();
const total = useEditor(projectDuration);
// The moment the elements are on screen for: the canvas's own, which holds
// the last frame with the playhead on the cut's end.
const at = stageTimeAt(t, total);
const live = useLiveEffects();
// The grade and frame motion of every effect above this slice.
const graded = useMemo(
() => (gradeAbove === null ? [] : live.filter((e) => e.lane < gradeAbove).map((e) => e.state)),
[live, gradeAbove]
);
const transform = stageEffectTransform(graded);
const rootRef = useRef<HTMLDivElement>(null);
// Live box elements per on-screen item (titles and the subtitle caption), so
// a dragged one can align to the others that are on screen at the same time.
const boxes = useRef<Map<string, HTMLElement>>(new Map());
const [guides, setGuides] = useState<Guides>({ v: [], h: [] });
const registerBox = useCallback((id: string, el: HTMLElement | null) => {
if (el) boxes.current.set(id, el);
else boxes.current.delete(id);
}, []);
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
// and centers of the other on-screen items — independently per axis — and
// paint the matched guide lines. Hold ⌘/Ctrl to bypass.
const snap = useCallback(
(id: string, px: number, py: number, ev: PointerEvent): { x: number; y: number } => {
const el = boxes.current.get(id);
const root = rootRef.current;
if (!el || !root || ev.metaKey || ev.ctrlKey) {
setGuides({ v: [], h: [] });
return { x: px, y: py };
}
const r = el.getBoundingClientRect();
const cx = px * stageWidth;
const cy = py * stageHeight;
// Frame lines: edges, safe margins, center, and every guide showing —
// a keep-out box offers its inner edge, so a title pulls flush to it.
const vt = [0, CANVAS_MARGIN * stageWidth, stageWidth / 2, (1 - CANVAS_MARGIN) * stageWidth, stageWidth];
const ht = [0, CANVAS_MARGIN * stageHeight, stageHeight / 2, (1 - CANVAS_MARGIN) * stageHeight, stageHeight];
if (!guidesHidden) {
const lines = guideSnapLines(shownGuides, aspect, guideLines);
vt.push(...lines.v.map((f) => f * stageWidth));
ht.push(...lines.h.map((f) => f * stageHeight));
}
// Plus every other on-screen box's edges and center (titles and the
// subtitle caption alike), read from its rect in stage space.
const rootRect = root.getBoundingClientRect();
for (const [bid, e] of boxes.current) {
if (bid === id) continue;
const rr = e.getBoundingClientRect();
const left = rr.left - rootRect.left;
const top = rr.top - rootRect.top;
vt.push(left, left + rr.width / 2, left + rr.width);
ht.push(top, top + rr.height / 2, top + rr.height);
}
// For one axis, snap the closest of {near edge, center, far edge} to the
// closest target line, returning the shifted center and the matched line.
const pick = (anchors: number[], offsets: number[], targets: number[]) => {
let best = { d: SNAP_PX + 1, center: NaN, line: NaN };
anchors.forEach((a, i) => {
for (const T of targets) {
const d = Math.abs(a - T);
if (d < best.d) best = { d, center: T - offsets[i], line: T };
}
});
return best;
};
const bx = pick([cx - r.width / 2, cx, cx + r.width / 2], [-r.width / 2, 0, r.width / 2], vt);
const by = pick([cy - r.height / 2, cy, cy + r.height / 2], [-r.height / 2, 0, r.height / 2], ht);
const v: number[] = [];
const h: number[] = [];
let outX = px;
let outY = py;
if (!Number.isNaN(bx.center)) {
outX = bx.center / stageWidth;
v.push(bx.line);
}
if (!Number.isNaN(by.center)) {
outY = by.center / stageHeight;
h.push(by.line);
}
setGuides({ v, h });
return { x: outX, y: outY };
},
[stageWidth, stageHeight, shownGuides, guideLines, guidesHidden, aspect]
);
const clearGuides = useCallback(() => setGuides({ v: [], h: [] }), []);
// A rotate drag that locks onto a quarter turn paints a guide through the
// element's center: the horizontal line at 0°/±180°, the vertical one at
// ±90°, matching the axis the element's baseline runs along.
const rotationGuide = useCallback(
(p: { clientX: number; clientY: number; quarter: number } | null) => {
const root = rootRef.current;
if (!p || !root) {
setGuides({ v: [], h: [] });
return;
}
const r = root.getBoundingClientRect();
if (Math.abs(p.quarter) === 90) setGuides({ v: [p.clientX - r.left], h: [] });
else setGuides({ v: [], h: [p.clientY - r.top] });
},
[]
);
// The selected title and whether the playhead sits inside it. Selecting a
// title off the playhead (e.g. focusing its text in the panel) edits it in
// isolation: it shows alone so it never stacks over whatever title is live.
// Not while scrubbing — the skimmer must still show the exact frame's titles.
const scrubbing = skimTime !== null;
const sel =
selection?.kind === "overlay" ? overlays.find((o) => o.id === selection.id) : undefined;
const multiple = multiSelection.length > 1;
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 (
<>
<div
ref={rootRef}
className="pointer-events-none absolute inset-0"
style={{ transform, filter }}
>
{overlays.map((o) => {
if (o.hidden) return null;
// An effect has no place in the frame — it grades what plays under it
// (see StageEffects), so it draws nothing here and takes no box,
// handles or drag. The timeline bar is the thing you grab.
if (o.kind === "effect") return null;
const selected = selectedIds.has(o.id) || sel?.id === o.id;
const inRange = overlayLiveAt(o, at);
// While hover-scrubbing (paused, skimmer active) the preview must show the
// exact frame under the skimmer — a selected but out-of-frame title can't
// leak into a frame it isn't part of. Off the skimmer, a selected title
// that sits off the playhead is shown alone (isolate) for editing.
if (isolate ? !selected : !inRange && (scrubbing || !selected || multiple)) return null;
return (
<OverlayItem
key={o.id}
// 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) ? cycleFaceAt(o, t - o.start, frame.w, frame.h) : o}
// The skimmer paints the bare frame: the item still renders, its
// selection chrome (outline, resize handle) does not.
selected={selected && !scrubbing}
armed={selected && !scrubbing}
grouped={grouped}
ghost={!inRange && !selected}
t={t}
stageWidth={stageWidth}
bandTransform={transform}
registerBox={registerBox}
snap={snap}
onSnapEnd={clearGuides}
rotationGuide={rotationGuide}
/>
);
})}
{captions && (
<SubtitleCaptions
stageWidth={stageWidth}
stageHeight={stageHeight}
registerBox={registerBox}
snap={snap}
onSnapEnd={clearGuides}
/>
)}
{guides.v.map((x, i) => (
<div
key={`v${i}`}
className="pointer-events-none absolute top-0 bottom-0 z-10 w-px bg-[#ff2d55]"
style={{ left: x }}
/>
))}
{guides.h.map((y, i) => (
<div
key={`h${i}`}
className="pointer-events-none absolute right-0 left-0 z-10 h-px bg-[#ff2d55]"
style={{ top: y }}
/>
))}
</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" || !overlayLiveAt(o, at)) return null;
return (
<OverlayItem
key={o.id}
overlay={cycleFaceAt(o, t - o.start, frame.w, frame.h)}
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 = () => {};
/** Re-read a face's metrics when the page finishes loading one. */
const subscribeFaces = (changed: () => void) => {
const fonts = typeof document === "undefined" ? undefined : document.fonts;
fonts?.addEventListener?.("loadingdone", changed);
return () => fonts?.removeEventListener?.("loadingdone", changed);
};
/** A title's lines set where the export's painter sets them. A CSS line box
* centres the face's ascent and descent; the painter draws from the em box's
* middle, a tenth of an em higher on a tall-ascent face (`textLineLift`). The
* lift is relative, so the box, its plate and its chrome stay put and only the
* type moves. */
function PainterLines({ font, children }: { font: string; children: React.ReactNode }) {
const lift = useSyncExternalStore(subscribeFaces, () => textLineLift(font), () => 0);
return <div style={lift ? { position: "relative", top: -lift } : undefined}>{children}</div>;
}
/** 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
* the burn-in lights. */
function WordText({
text,
draws,
lineHeight,
tail,
texture,
typed,
}: {
text: string;
draws: WordDraw[];
lineHeight?: number;
/** 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) => {
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={
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>
);
})}
</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;
stageHeight: number;
registerBox: (id: string, el: HTMLElement | null) => void;
snap: (id: string, x: number, y: number, ev: PointerEvent) => { x: number; y: number };
onSnapEnd: () => void;
}) {
const subtitles = useEditor((s) => s.subtitles);
if (!subtitles.showOnVideo) return null;
return (
<>
{Array.from({ length: subtitleLaneCount(subtitles) }, (_, lane) =>
laneHidden(subtitles, lane) ? null : (
<SubtitleCaption key={lane} lane={lane} {...props} />
)
)}
</>
);
}
/** One track's active cue, rendered exactly like the export burn-in.
* Nothing renders when there is no cue at the playhead (no speech). Dragging
* the caption moves the whole track's anchor; an Option-drag moves only the
* caption on screen, and a caption that has its own spot moves alone on any
* drag. Both ride the same smart snapping and guide lines as titles. */
function SubtitleCaption({
lane,
stageWidth,
stageHeight,
registerBox,
snap,
onSnapEnd,
}: {
lane: number;
stageWidth: number;
stageHeight: number;
registerBox: (id: string, el: HTMLElement | null) => void;
snap: (id: string, x: number, y: number, ev: PointerEvent) => { x: number; y: number };
onSnapEnd: () => void;
}) {
const subtitles = useEditor((s) => s.subtitles);
const frame = frameOf(useEditor((s) => s.aspect));
const selection = useEditor((s) => s.selection);
const multiSelection = useEditor((s) => s.multiSelection);
const stagePress = useContext(StagePress);
const t = usePreviewTime();
const total = useEditor(projectDuration);
// The cue on screen, with the last one held when the playhead sits on the
// cut's end, the way the canvas holds the last frame.
const cues = laneCues(subtitles, lane);
const cue = cueAt(cues, stageTimeAt(t, total));
if (!cue || !cue.text.trim()) return null;
// The caption moves its whole track, so it takes the same rule the elements
// take: the cue on screen has to be the selection before a drag moves it.
const armed = (selection?.kind === "cue" && selection.id === cue.id) ||
multiSelection.some((item) => item?.kind === "cue" && item.id === cue.id);
// Captions ride the same style/opener/anchor logic as the export burn-in,
// so the preview and the rendered file match exactly.
const style = captionStyle(subtitles.style);
// The track's word effect plays on the playhead: the cue resolves its words
// at this moment exactly as the export burn-in resolves them at that frame.
const ov = cueOverlay(
cue,
style,
cue.id === cues[0]?.id,
trackPos(subtitles, style, lane),
subtitles.wordHighlight ? t : undefined,
frame.w
);
const scale = stageWidth / frame.w;
return (
<div
ref={(el) => registerBox(subtitleBoxId(lane), el)}
data-preview-kind="cue"
data-preview-id={cue.id}
className={cn(
"sub-caption pointer-events-auto absolute -translate-x-1/2 -translate-y-1/2 cursor-grab text-center whitespace-pre active:cursor-grabbing",
armed && "outline-[1.5px] outline-offset-[3px] outline-[#0a84ff]"
)}
onPointerDown={(e) => {
if (e.button !== 0) return;
const s = useEditor.getState();
if (togglePreviewSelection(e, { kind: "cue", id: cue.id })) return;
if (armed && startSelectionDrag(e, stageWidth, stageHeight)) return;
if (!armed && stagePress) {
stagePress(e, () => s.select({ kind: "cue", id: cue.id }));
return;
}
s.pushHistory();
const { x: x0, y: y0 } = ov;
const alone = e.altKey || (cue.x !== undefined && cue.y !== undefined);
startDrag(e, {
onMove: (dx, dy, ev) => {
const p = snap(subtitleBoxId(lane), x0 + dx / stageWidth, y0 + dy / stageHeight, ev);
const at = {
x: Math.min(0.98, Math.max(0.02, p.x)),
y: Math.min(0.98, Math.max(0.02, p.y)),
};
if (alone) useEditor.getState().setCuePosition(cue.id, at);
else useEditor.getState().setSubtitleTrackMeta(lane, at);
},
onUp: onSnapEnd,
});
}}
style={{
left: `${ov.x * 100}%`,
top: `${ov.y * 100}%`,
// The caption draws the lines `cueOverlay` already broke it into,
// measured against the room the export gives it. An absolutely
// positioned box otherwise shrinks to whatever is left of the stage
// beside its own anchor — half of it for a centered caption — and
// re-breaks lines the rendered file keeps whole.
width: "max-content",
fontSize: ov.size * scale,
fontFamily: fontStack(ov.font),
fontWeight: ov.weight,
lineHeight: LINE_HEIGHT,
color: ov.color,
textShadow: ov.shadow
? `0 ${SHADOW.offsetY * scale}px ${SHADOW.blur * scale}px ${SHADOW.color}`
: undefined,
background: ov.plate ? plateFill(ov) : undefined,
padding: ov.plate ? PLATE_PADDING : undefined,
borderRadius: ov.plate ? PLATE_RADIUS_EM : undefined,
}}
>
<PainterLines font={textFontOf(ov, ov.size * scale)}>
{ov.wordDraw ? <WordText text={ov.text} draws={ov.wordDraw} /> : ov.text}
</PainterLines>
</div>
);
}
function OverlayItem({
overlay: o,
selected,
armed,
grouped,
ghost,
t,
stageWidth,
bandTransform,
registerBox,
snap,
onSnapEnd,
rotationGuide,
}: {
overlay: Overlay;
selected: boolean;
/** Whether a press on this element moves it. Only the selection (or the
* group holding it) drags; everything else hands the press to the stage. */
armed: boolean;
/** One of several selected items: it wears its outline alone, and the
* selection frame carries the grips for the whole set. */
grouped: boolean;
ghost: boolean;
/** The previewed timeline moment (playhead or paused skimmer). */
t: number;
stageWidth: number;
/** The frame motion of the effects above this element's slice, so lifted
* chrome moves with the picture it outlines. */
bandTransform?: string;
registerBox: (id: string, el: HTMLElement | null) => void;
snap: (id: string, x: number, y: number, ev: PointerEvent) => { x: number; y: number };
onSnapEnd: () => void;
/** Paint (or clear) the guide line while a rotate drag sits on a quarter
* turn; the point is the rotating element's center in client coords. */
rotationGuide: (p: { clientX: number; clientY: number; quarter: number } | null) => void;
}) {
const [editing, setEditing] = useState(false);
/** The angle the readout shows while a rotate drag runs; null when idle. */
const [spin, setSpin] = useState<number | null>(null);
const editRef = useRef<HTMLDivElement>(null);
const boxRef = useRef<HTMLDivElement>(null);
const frame = frameOf(useEditor((s) => s.aspect));
const scale = stageWidth / frame.w;
const stageHeight = (stageWidth * frame.h) / frame.w;
const isText = isTextOverlay(o);
// What the element actually reads as: its written lines, broken wherever a
// line is wider than the room the frame leaves it. The export's painter
// breaks the same lines in the same places; editing shows the raw text, so
// the box holds what the author typed.
const drawnText = !isTextOverlay(o) ? "" : editing ? o.text : fitTextToFrame(o, frame.w, frame.h);
// Publish this element's box so a sibling drag can align to it.
useEffect(() => {
registerBox(o.id, boxRef.current);
return () => registerBox(o.id, null);
}, [o.id, registerBox]);
useEffect(() => {
if (editing && editRef.current) {
editRef.current.focus();
const range = document.createRange();
range.selectNodeContents(editRef.current);
const sel = window.getSelection();
sel?.removeAllRanges();
sel?.addRange(range);
}
}, [editing]);
// The element's animation state at this exact moment — the same evaluator
// the export samples, applied here as a live CSS transform. Ghosts (shown
// out of range for editing) render at rest. A rehearsal from the inspector
// outranks the clock: the element plays its animation where it stands, and
// after the run it holds its resting pose until the clock moves.
const rehearsal = useAnimPreview(o.id);
const restedAt = useRef<number | null>(null);
useEffect(() => {
if (rehearsal !== "rest") {
restedAt.current = null;
return;
}
if (restedAt.current === null) restedAt.current = t;
else if (t !== restedAt.current) releaseAnimRest(o.id);
}, [rehearsal, t, o.id]);
// A rehearsal draws the slot it is testing on its own — the run carries that
// one animation, so an In or a loop never rides along with it.
const running = rehearsal !== null && rehearsal !== "rest" ? rehearsal : null;
const live =
isOverlayAnimated(o) && rehearsal !== "rest" && (running !== null || !ghost)
? evalOverlayFrame(
running ? { ...o, anim: running.anim } : o,
running ? running.tLocal : Math.max(0, t - o.start),
frame.w / frame.h
)
: null;
// The pose's own zoom is kept apart from the rest of the chain: the element
// 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 || 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.
const words = isText && !editing ? overlayWords(o) : undefined;
// A rehearsal from the inspector sweeps the effect along the line on its own
// clock; otherwise it follows the playhead, like everything else here.
const wordLocal = running ? running.tLocal : Math.max(0, t - o.start);
const wordDraw =
words && !glyphs && isTextOverlay(o)
? wordDrawsAt(
drawnText,
words,
o.color ?? WORD_ACCENT_DEFAULT,
Math.max(0.1, o.end - o.start),
wordLocal
)
: null;
// 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 && typed === undefined && live?.countProgress !== undefined && !editing
? countText(drawnText, live.countProgress)
: isText
? drawnText
: "";
// Behind-the-speaker elements draw inside the canvas compositor; the DOM
// keeps an invisible hit target (and the selection chrome) so editing
// still works.
const behindHidden = behindSubjectOverlay(o) && !editing;
// The typing bar, lit or dark by the evaluator's blink. It takes no room
// in the line — its negative margin gives back its width — so the text
// sits exactly where the painter, which draws the bar over nothing, puts it.
const caretWidth = `max(${2 * scale}px, ${CARET_WIDTH_EM}em)`;
// After a typed letter the bar hugs its ink, giving back the tracking the
// line lays after that letter.
const typedHead = [...shownText].slice(0, typed);
const hug = typedHead.length > 0 && typedHead[typedHead.length - 1] !== "\n";
const caretGap = `(${CARET_GAP_EM}em - ${hug && isTextOverlay(o) ? (o.letterSpacing ?? 0) : 0}em)`;
const caretBar =
isText && !editing && live?.caret ? (
<span
aria-hidden
style={{
display: "inline-block",
width: caretWidth,
height: `${CARET_HEIGHT_EM}em`,
marginLeft: `calc${caretGap}`,
marginRight: `calc(-1 * (${caretGap} + ${caretWidth}))`,
verticalAlign: "middle",
background: typeCaret(o.anim)?.color ?? o.color,
}}
/>
) : null;
// A dive flies the view into the element's deepest ink. Zoomed fifty times,
// the few pixels the DOM and the canvas disagree on where a line's middle
// 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;
// A hit that darkens dims the element's own pixels, the same multiply the
// painters run; blur softens them. The DOM content, the dive canvas and the
// streak canvas all carry the one filter.
const dim = live?.brightness !== undefined ? `brightness(${live.brightness})` : "";
const filterOf = (px: number) => [px >= 0.1 ? `blur(${px.toFixed(2)}px)` : "", dim].filter(Boolean).join(" ") || undefined;
const blurFilter = filterOf(blurPx);
// The dive canvas sits on the stage outside the box, so its blur takes the
// radius undivided.
const diveFilter = filterOf(blurPx * poseZoom);
// Motion blur: while the element moves, the kit paints it and smears the
// picture along its streak; at rest the element is the plain DOM box.
const streakable =
!!(o.motionBlur || o.camera?.motionBlur) && !editing && !behindHidden && !diving && !ghost;
const [streakReady, setStreakReady] = useState(false);
const streaking = streakable && !!live?.streak && streakReady;
const tLocal = Math.max(0, t - o.start);
// The mask clips the content wrapper only, never the box itself — the
// selection chrome and the mask's own grips must stay visible outside it.
const maskCss = useMaskCss(o, boxRef, stageWidth, stageHeight, scale, tLocal, editing);
// The selection chrome lifts out of the stage's clipping into the chrome
// host, so a box hanging past the frame edge keeps its outline and grips.
// The twin copies the box's placement and transform chain; its laid-out
// size is read off the real box, which keeps auto-sized text honest.
const chromeHost = useContext(OverlayChromeHost);
const stagePress = useContext(StagePress);
const liftChrome = !!chromeHost && selected && !editing && !grouped;
const [chromeSize, setChromeSize] = useState<{ w: number; h: number } | null>(null);
// Which mount the chrome lands in. The twin folds the pose's zoom into its
// size, so anything inside it that rides the element has to re-apply it;
// the element box carries the zoom in its transform already.
const chromeLifted = liftChrome && !!chromeSize;
useLayoutEffect(() => {
if (!liftChrome) return;
const el = boxRef.current;
if (!el) return;
const read = () => setChromeSize({ w: el.offsetWidth, h: el.offsetHeight });
read();
const ro = new ResizeObserver(read);
ro.observe(el);
return () => {
ro.disconnect();
setChromeSize(null);
};
}, [liftChrome]);
// The box carries position, rotation, and opacity; the content wrapper
// inside it carries a title's type styles (the edit caret inherits them)
// and the mask.
const baseTransform = `translate(-50%, -50%)${animTransform}`;
const chromeTransform = `translate(-50%, -50%)${animMove}`;
const style: CSSProperties = {
// A keyframed element is placed by its pose, not by its resting x/y.
left: `${(live?.x ?? o.x) * 100}%`,
top: `${(live?.y ?? o.y) * 100}%`,
transform: baseTransform,
opacity: (live ? live.opacity : (o.opacity ?? 1)) * (ghost ? 0.35 : 1),
};
const contentStyle: CSSProperties = {
...(isText
? (() => {
const shadow = resolveShadow(o.shadow);
return {
width: o.wrapWidth ? o.wrapWidth * stageWidth + (o.plate ? 2 * PLATE_PAD_X * o.size * scale : 0) : undefined,
fontSize: o.size * scale,
fontFamily: fontStack(o.font),
fontWeight: o.weight,
fontStyle: o.italic ? "italic" : undefined,
lineHeight: o.lineHeight ?? LINE_HEIGHT,
letterSpacing: o.letterSpacing ? `${o.letterSpacing}em` : undefined,
textAlign: o.align ?? "center",
color: o.color,
// The canvas pair strokes before filling; paint-order keeps the
// DOM stroke behind the fill the same way. `width` is the visible
// outline outside the glyph, so the centered CSS stroke doubles it
// (the canvas painter doubles its lineWidth identically).
WebkitTextStroke: o.stroke
? `${o.stroke.width * 2}em ${o.stroke.color}`
: undefined,
paintOrder: o.stroke ? ("stroke fill" as const) : undefined,
textShadow: shadow
? `0 ${shadow.offsetY * scale}px ${shadow.blur * scale}px ${shadow.color}`
: undefined,
// A behind-speaker title paints on the canvas; the DOM box keeps
// its footprint but no visible plate.
background: o.plate && !behindHidden ? plateFill(o) : undefined,
padding: o.plate ? PLATE_PADDING : undefined,
borderRadius: o.plate ? `${o.plateRadius ?? PLATE_RADIUS}em` : undefined,
};
})()
: {}),
...(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)` }
: {}),
};
const commitText = () => {
if (!isText) return;
const text = (editRef.current?.innerText ?? "").replace(/\n+$/, "");
setEditing(false);
if (text !== o.text) {
const s = useEditor.getState();
s.pushHistory();
// An emptied title falls back to the placeholder in the page's language,
// read here in the handler so the per-frame item holds no translator.
s.updateOverlayTransient(o.id, { text: text || creatorNames().text });
}
};
// A single click anywhere outside the editable box commits and dismisses it,
// so text doesn't stay "stuck" in edit mode when the click misses focus.
useEffect(() => {
if (!editing) return;
const onDown = (e: PointerEvent) => {
if (!editRef.current?.contains(e.target as Node)) commitText();
};
document.addEventListener("pointerdown", onDown, true);
return () => document.removeEventListener("pointerdown", onDown, true);
// commitText closes over the current overlay; re-bind when editing toggles.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [editing]);
/** The whole group's live members (or just this element), snapshotted at a
* gesture's start so deltas apply member-relative. */
const groupSnapshot = () => {
const all = useEditor.getState().overlays;
return o.groupId ? all.filter((x) => x.groupId === o.groupId) : [o];
};
const clampPos = (v: number) => (hasOverlayKeys(o) ? clampKeyPos(v, o) : clampOverlayPos(v, o));
/**
* Commit a live transform. Position and rotation are pose: on a keyframed
* element they land in the key at the playhead — which is what makes
* dragging in the preview the way you author motion — while everything else
* (a font size, a shape's box) is the element's own and always patches it.
*/
const writeTransform = (
patches: { id: string; patch: { x?: number; y?: number; rotation?: number } }[]
) => {
const s = useEditor.getState();
const direct: { id: string; patch: OverlayPatch }[] = [];
for (const { id, patch } of patches) {
const el = s.overlays.find((x) => x.id === id);
const { x, y, rotation, ...rest } = patch;
const posed = x !== undefined || y !== undefined || "rotation" in patch;
if (el && posed && hasOverlayKeys(el)) {
s.setOverlayKey(
id,
Math.max(0, Math.min(t - el.start, Math.max(0.1, el.end - el.start))),
{
...(x !== undefined ? { x } : {}),
...(y !== undefined ? { y } : {}),
// A key stores its angle outright; absence means zero only on the
// element itself.
...("rotation" in patch ? { rotation: rotation ?? 0 } : {}),
},
{ transient: true }
);
if (Object.keys(rest).length) direct.push({ id, patch: rest as OverlayPatch });
continue;
}
direct.push({ id, patch: patch as OverlayPatch });
}
if (direct.length) s.updateOverlaysTransient(direct);
};
// A grip resizes what the kind actually stores: a corner takes a title's
// font size, a shape's box, a sticker's width; a side grip takes one axis
// alone — a shape's width or height, a title's wrapping width. The grip's
// opposite corner or side stays planted: a corner takes both axes by how
// far it travels from the far corner, a side grip its own axis measured
// from the far side, and the center walks out so the planted edge holds
// still on screen. A group scales as one about the same planted point.
const resizeFrom = (handle: ResizeHandle, e: React.PointerEvent) => {
const s = useEditor.getState();
s.pushHistory();
const el = boxRef.current!;
const box = el.getBoundingClientRect();
const cx = box.left + box.width / 2;
const cy = box.top + box.height / 2;
const axis = HANDLE_AXIS[handle];
const rad = (-(live?.rotation ?? o.rotation ?? 0) * Math.PI) / 180;
// The box's own size on screen: the bounding rect is the turned box's
// envelope, so take the layout size under the pose's scale.
const ow = el.offsetWidth * (live?.scale ?? 1);
const oh = el.offsetHeight * (live?.scale ?? 1);
// The planted point, in the element's own space relative to its center.
const ax = (-axis.x * ow) / 2;
const ay = (-axis.y * oh) / 2;
// The grip's reach from the planted point, in the element's own space: a
// corner measures the full diagonal, a side grip only its own axis.
const reach = (px: number, py: number) => {
const dx = px - cx;
const dy = py - cy;
const lx = dx * Math.cos(rad) - dy * Math.sin(rad);
const ly = dx * Math.sin(rad) + dy * Math.cos(rad);
if (axis.y === 0) return Math.abs(lx - ax);
if (axis.x === 0) return Math.abs(ly - ay);
return Math.hypot(lx - ax, ly - ay);
};
const d0 = Math.max(8, reach(e.clientX, e.clientY));
const members = groupSnapshot();
const self = members.find((m) => m.id === o.id) ?? members[0];
// Under a group camera the center's walk is on screen; the members move
// by that much of their world.
const cam = elementCameraAt(o, tLocal);
const aspect = frame.w / frame.h;
const seen = cam ? cameraPoint(cam, self.x, self.y, aspect) : null;
const worldWalk = (mx: number, my: number) => {
if (!cam || !seen) return { x: mx, y: my };
const w = worldPoint(cam, seen.x + mx, seen.y + my, aspect);
return { x: w.x - self.x, y: w.y - self.y };
};
const textSizes = new Map(members.filter(isTextOverlay).map((m) => [m.id, textBoxSize(m, frame.w, frame.h)]));
const stickerSize = (v: number) => Math.min(1.5, Math.max(0.02, v));
// A sticker's resting height as a frame fraction: the one it stores, or
// the grabbed box laid out under the source's own aspect. A side grip
// pins it so the other axis holds while one stretches.
const stickerH = (m: Overlay): number | undefined => {
if (m.kind !== "sticker") return undefined;
if (m.h) return m.h;
const laid = m.id === o.id ? el.offsetHeight / stageHeight : 0;
return laid > 0 ? laid : undefined;
};
const scaled = (m: Overlay, k: number): Partial<Overlay> => {
if (isTextOverlay(m)) {
if (axis.y === 0) {
const width = Math.max(frame.w * 0.01, textSizes.get(m.id)!.width * k - (m.plate ? 2 * PLATE_PAD_X * m.size : 0));
return { wrapWidth: Math.min(2, width / frame.w) };
}
const size = Math.min(320, Math.max(16, m.size * k));
return { size, ...(m.wrapWidth ? { wrapWidth: m.wrapWidth * size / m.size } : {}) };
}
if (m.kind === "shape") {
const w = { w: Math.min(2, Math.max(0.01, m.w * k)) };
const h = { h: Math.min(2, Math.max(0.002, m.h * k)) };
// A side grip pulls its own axis alone: left/right take the width,
// top/bottom the height.
if (axis.y === 0) return w;
if (axis.x === 0) return h;
return { ...w, ...h };
}
if (m.kind === "sticker") {
// A side grip stretches its own axis and pins the other; a corner
// scales the whole picture, the stored height riding along.
const h = stickerH(m);
if (axis.y === 0) return { w: stickerSize(m.w * k), ...(h ? { h } : {}) };
if (axis.x === 0) return h ? { h: stickerSize(h * k) } : { w: stickerSize(m.w * k) };
return { w: stickerSize(m.w * k), ...(m.h ? { h: stickerSize(m.h * k) } : {}) };
}
return {};
};
// How much the grabbed element actually grew per axis once its size
// clamps, so the planted point holds through the clamp too.
const grew = (patch: Partial<Overlay>): { kx: number; ky: number } => {
const p = patch as {
size?: number;
w?: number;
h?: number;
wrapWidth?: number;
};
if (isTextOverlay(self)) {
const before = textSizes.get(self.id)!;
const after = textBoxSize({ ...self, size: p.size ?? self.size, wrapWidth: p.wrapWidth ?? self.wrapWidth }, frame.w, frame.h);
return { kx: after.width / before.width, ky: after.height / before.height };
}
if (self.kind === "shape")
return { kx: (p.w ?? self.w) / self.w, ky: (p.h ?? self.h) / self.h };
if (self.kind === "sticker") {
const kx = (p.w ?? self.w) / self.w;
const h0 = stickerH(self);
// Height follows the width whenever it is not stored on its own.
const ky = p.h !== undefined && h0 ? p.h / h0 : axis.x === 0 ? 1 : kx;
return { kx, ky };
}
return { kx: 1, ky: 1 };
};
startDrag(e, {
onMove: (_dx, _dy, ev) => {
const k = reach(ev.clientX, ev.clientY) / d0;
const { kx, ky } = grew(scaled(self, k));
// The center's walk that keeps the planted point on its screen spot,
// rotated back to the screen and into frame fractions.
const wx = axis.x ? ((kx - 1) * axis.x * ow) / 2 : 0;
const wy = axis.y ? ((ky - 1) * axis.y * oh) / 2 : isTextOverlay(self) ? ((ky - 1) * oh) / 2 : 0;
const walk = worldWalk(
(wx * Math.cos(rad) + wy * Math.sin(rad)) / stageWidth,
(-wx * Math.sin(rad) + wy * Math.cos(rad)) / stageHeight
);
writeTransform(
members.map((m) => ({
id: m.id,
patch: {
...scaled(m, k),
x: clampPos(self.x + kx * (m.x - self.x) + walk.x),
y: clampPos(self.y + ky * (m.y - self.y) + walk.y),
},
}))
);
},
});
};
// The lollipop above the box rotates around the center; angles within reach
// of a quarter turn snap to it, and 0 stores as absence. A group orbits
// its shared center: positions revolve, each member's rotation shifts by
// the same delta, and the grabbed element's angle snaps the same way.
const rotateFrom = (e: React.PointerEvent) => {
const s = useEditor.getState();
s.pushHistory();
const box = boxRef.current!.getBoundingClientRect();
const cx = box.left + box.width / 2;
const cy = box.top + box.height / 2;
const members = groupSnapshot();
const gx = members.reduce((sum, m) => sum + m.x, 0) / members.length;
const gy = members.reduce((sum, m) => sum + m.y, 0) / members.length;
const angleAt = angleFrom(cx, cy, 90);
const start0 = angleAt(e);
// The grabbed member's start position, for placing the guide while the
// group orbit carries its center away from the box rect measured above.
const grabbed = members.find((m) => m.id === o.id) ?? o;
// The cursor turns with the element for the whole drag, so its heads keep
// pointing the way the next bit of travel will take it.
let liveRotation = o.rotation ?? 0;
setSpin(liveRotation);
startDrag(e, {
cursor: () => rotateCursor(liveRotation),
onMove: (_dx, _dy, ev) => {
if (members.length === 1) {
const { deg, locked } = snapQuarter(normDeg(angleAt(ev)));
const rotation = Math.round(deg);
liveRotation = rotation;
setSpin(rotation);
rotationGuide(locked === null ? null : { clientX: cx, clientY: cy, quarter: locked });
// The handle reads the angle on screen; a group camera's roll is
// the world's, so the element keeps the rest.
const own = Math.round(normDeg(rotation - (elementCameraAt(o, tLocal)?.rotation ?? 0)));
writeTransform([{ id: o.id, patch: { rotation: own === 0 ? undefined : own } }]);
return;
}
let delta = normDeg(angleAt(ev) - start0);
const lead = normDeg((o.rotation ?? 0) + delta);
const { locked } = snapQuarter(lead);
if (locked !== null) delta = normDeg(delta + locked - lead);
liveRotation = normDeg((o.rotation ?? 0) + delta);
setSpin(Math.round(liveRotation));
const rad = (delta * Math.PI) / 180;
// Positions are frame fractions with unequal axes; orbit in a square
// space keyed to width so the group turns rigidly on screen.
const ax = 1;
const ay = stageHeight / stageWidth;
writeTransform(
members.map((m) => {
const ox = (m.x - gx) * ax;
const oy = (m.y - gy) * ay;
const rotation = Math.round(normDeg((m.rotation ?? 0) + delta));
return {
id: m.id,
patch: {
x: clampPos(gx + (ox * Math.cos(rad) - oy * Math.sin(rad)) / ax),
y: clampPos(gy + (ox * Math.sin(rad) + oy * Math.cos(rad)) / ay),
rotation: rotation === 0 ? undefined : rotation,
},
};
})
);
const ox0 = (grabbed.x - gx) * ax;
const oy0 = (grabbed.y - gy) * ay;
rotationGuide(
locked === null
? null
: {
clientX:
cx + (gx + (ox0 * Math.cos(rad) - oy0 * Math.sin(rad)) / ax - grabbed.x) * stageWidth,
clientY:
cy + (gy + (ox0 * Math.sin(rad) + oy0 * Math.cos(rad)) / ay - grabbed.y) * stageHeight,
quarter: locked,
}
);
},
onUp: () => {
rotationGuide(null);
setSpin(null);
},
});
};
const beginMove = (e: React.PointerEvent) => {
if (editing || e.button !== 0) return;
const s = useEditor.getState();
if (togglePreviewSelection(e, { kind: "overlay", id: o.id })) return;
if (armed && startSelectionDrag(e, stageWidth, stageHeight, { kind: "overlay", id: o.id })) return;
// Nothing moves until it is the selection: an unselected element is part
// of the picture, so the stage pans under it and a stationary press picks
// it up for the next drag.
if (!armed && stagePress) {
stagePress(e, () => s.select({ kind: "overlay", id: o.id }));
return;
}
s.select({ kind: "overlay", id: o.id });
s.pushHistory();
// A grouped element drags its whole group: the grabbed one snaps, the
// rest follow by the same delta.
const members = groupSnapshot().map((m) => ({ id: m.id, x: m.x, y: m.y }));
const self = members.find((m) => m.id === o.id) ?? { id: o.id, x: o.x, y: o.y };
// Under a group camera the pointer moves the element on screen; the
// members move by that much of their world.
const cam = elementCameraAt(o, tLocal);
const aspect = frame.w / frame.h;
const seen = cam ? cameraPoint(cam, self.x, self.y, aspect) : null;
startDrag(e, {
onMove: (dx, dy, ev) => {
if (cam && seen) {
const w = worldPoint(cam, seen.x + dx / stageWidth, seen.y + dy / stageHeight, aspect);
writeTransform(members.map((m) => ({ id: m.id, patch: { x: clampPos(m.x + w.x - self.x), y: clampPos(m.y + w.y - self.y) } })));
return;
}
const p = snap(o.id, self.x + dx / stageWidth, self.y + dy / stageHeight, ev);
const ddx = clampPos(p.x) - self.x;
const ddy = clampPos(p.y) - self.y;
writeTransform(
members.map((m) => ({
id: m.id,
patch: { x: clampPos(m.x + ddx), y: clampPos(m.y + ddy) },
}))
);
},
onUp: onSnapEnd,
});
};
const chrome = selected && !editing && !grouped && (
<>
{/* The rotate button clears the element's own top edge, so moving is
never caught by it. Its cursor rides the element's rotation, so
the glyph's heads keep pointing along the drag. */}
<TransformHandles
color="#0a84ff"
// The selection line draws 3px outside the box (plus half its
// stroke); the grips sit centered on it.
className="-inset-[3.75px]"
rotation={live?.rotation ?? o.rotation ?? 0}
angle={spin}
rotateCursor={rotateCursor(live?.rotation ?? o.rotation ?? 0)}
// Every element wears the whole frame; a line has only its own
// length to pull on.
handles={isText || (o.kind === "shape" && lineLikeShape(o.shape)) ? BOX_HANDLES : FRAME_HANDLES}
onResize={resizeFrom}
onRotate={rotateFrom}
/>
{o.mask && o.mask.kind !== "subject" && (
// The gizmo works in the element box's own pixels and folds screen
// deltas back through `poseScale`, so its mount has to sit in a space
// the zoom has been applied to. In the twin that means re-applying it
// here, over the box's stretched size; in the element box it is
// already in the transform overhead, and this is the identity.
<div
className="pointer-events-none absolute"
style={{
left: "50%",
top: "50%",
width: chromeLifted ? chromeSize.w : "100%",
height: chromeLifted ? chromeSize.h : "100%",
transform: `translate(-50%, -50%)${chromeLifted ? ` scale(${poseZoom})` : ""}`,
}}
>
<MaskGizmo
overlay={o}
stageWidth={stageWidth}
stageHeight={stageHeight}
tLocal={tLocal}
rotation={live?.rotation ?? o.rotation ?? 0}
poseScale={poseZoom}
/>
</div>
)}
</>
);
return (
<>
{diving && live && (
<DiveCanvas o={o} live={live} stageWidth={stageWidth} stageHeight={stageHeight} filter={diveFilter} />
)}
<div
ref={boxRef}
className={cn(
"overlay-item pointer-events-auto absolute cursor-grab rounded-xs text-center whitespace-pre active:cursor-grabbing",
selected && !chromeLifted && "outline-[1.5px] outline-offset-[3px] outline-[#0a84ff]",
editing && "cursor-text"
)}
style={style}
data-preview-kind="overlay"
data-preview-id={o.id}
onPointerDown={beginMove}
onDoubleClick={isText ? () => setEditing(true) : undefined}
>
{/* The filter sits outside the mask and the wipe, so the element blurs
after them — the order every renderer draws in. */}
<div style={blurFilter ? { filter: blurFilter } : undefined}>
<div style={contentStyle}>
{behindHidden ? (
<span className="opacity-0">{shownText}</span>
) : isTextOverlay(o) ? (
<PainterLines font={textFontOf(o, o.size * scale)}>
{editing ? (
<div
ref={editRef}
className="min-w-2 whitespace-pre-wrap break-words outline-none select-text"
contentEditable
suppressContentEditableWarning
onBlur={commitText}
onKeyDown={(e) => {
if (e.key === "Escape") {
e.preventDefault();
commitText();
}
e.stopPropagation();
}}
>
{o.text}
</div>
) : glyphs || (texture && !wordDraw) ? (
<GlyphText
text={shownText}
phase={glyphs ?? {}}
scale={scale}
zoom={poseZoom}
ink={isTextOverlay(o) ? o.color : undefined}
tail={caretBar}
texture={texture}
typed={typed}
em={1 / ((isTextOverlay(o) ? o.lineHeight : undefined) ?? LINE_HEIGHT)}
/>
) : wordDraw ? (
<WordText
text={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>
)}
</PainterLines>
) : 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" ? (
<StickerView sticker={o} stageWidth={stageWidth} stageHeight={stageHeight} t={t} />
) : null}
</div>
</div>
{streakable && live && (
<StreakCanvas
o={o}
live={live}
tLocal={running ? running.tLocal : tLocal}
stageWidth={stageWidth}
stageHeight={stageHeight}
poseZoom={poseZoom}
filter={blurFilter}
mask={o.mask?.kind === "subject" ? maskCss : undefined}
onReady={setStreakReady}
/>
)}
{/* The twin can't mount until its size is read, so the in-box chrome
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(
(() => {
// The twin: same placement, the base transform chain, the box's
// laid-out size — so the
// chrome lands exactly on the element while escaping the frame's
// clipping, and the grips it carries stay circular.
const ringStyle: CSSProperties = { inset: -4.5, borderRadius: 5, borderWidth: 1.5 };
const twinStyle: CSSProperties = {
left: style.left,
top: style.top,
// The zoom lands in the size, never in the transform: translating
// by half of the grown box centers it exactly where scaling the
// small one did, and every line and grip inside stays screen-sized.
width: chromeSize.w * poseZoom,
height: chromeSize.h * poseZoom,
transform: chromeTransform,
};
return (
<>
{/* The dashed ring draws the box's full extent; the twin carries
the move drag and the text-edit double click, standing in for
the box it covers. */}
<div
className="pointer-events-none absolute inset-0"
style={{ transform: bandTransform || undefined }}
>
<div
className="pointer-events-auto absolute cursor-grab active:cursor-grabbing"
style={twinStyle}
data-preview-kind="overlay"
data-preview-id={o.id}
onPointerDown={beginMove}
onDoubleClick={isText ? () => setEditing(true) : undefined}
>
<div className="pointer-events-none absolute border-[#0a84ff]" style={{ ...ringStyle, borderStyle: "dashed" }} />
</div>
</div>
{/* The frame-clipped solid ring paints over it, so dashes show
only where the box leaves the frame — the clip gizmo's own
language. The clip stays frame-aligned; the ring inside it
rides the band's motion like the twin does. */}
<div
className="pointer-events-none absolute inset-0 overflow-hidden"
style={{ borderRadius: "var(--stage-radius)" }}
>
<div className="absolute inset-0" style={{ transform: bandTransform || undefined }}>
<div className="absolute" style={twinStyle}>
<div className="absolute border-[#0a84ff]" style={{ ...ringStyle, borderStyle: "solid" }} />
</div>
</div>
</div>
{/* The grips and the rotate button ride their own top layer, so
the solid ring never draws across their white faces. */}
<div
className="pointer-events-none absolute inset-0"
style={{ transform: bandTransform || undefined }}
>
<div className="absolute" style={twinStyle}>
{chrome}
</div>
</div>
</>
);
})(),
chromeHost
)}
</>
);
}
/**
* The element's mask as CSS: the kit painter draws its coverage on a small
* box-sized canvas, and the data URL becomes the content's mask-image. The
* mask lives in the element's own space — the box's CSS transform carries it
* through pose, rotation and scale — so it clips exactly where the exported
* raster clips. Regenerated when the geometry, the box, or (keyed) the
* playhead moves; disabled while the text is being edited.
*/
/** A keyed mask's CSS repaints on the matte cadence — enough for a soft
* moving edge, a quarter of the per-frame PNG encodes. */
const MASK_CSS_FPS = 15;
/** The mask canvas's size cap; the CSS mask stretches it over the box, so a
* big element pays a small encode. */
const MASK_CSS_MAX = 512;
/** A streaking letter's taps, in its span's own px: the on-screen streak
* turned back through the letter's turn and stretch, and the share of the
* streak each copy sits at. */
function glyphStreak(g: ReturnType<typeof glyphStateAt>, scale: number): { x: number; y: number; taps: number[] } | null {
const s = g.streak!;
const n = streakTaps(Math.hypot(s.x, s.y));
if (n < 2) return null;
const r = (-g.rotate * Math.PI) / 180;
const x = s.x * scale;
const y = s.y * scale;
const ux = (x * Math.cos(r) - y * Math.sin(r)) / (g.sx || 1);
const uy = (x * Math.sin(r) + y * Math.cos(r)) / (g.sy || 1);
return { x: ux, y: uy, taps: [...Array(n).keys()].map((i) => i / (n - 1) - 0.5) };
}
/** Empty coverage: hides the element while a computed matte has no person. */
const NO_COVERAGE_MASK = "linear-gradient(transparent, transparent)";
/**
* Text laid out character by character, each one placed by the shared glyph
* evaluator. A ramp and a loop both land here, folded together by
* `glyphStateAt`. The canvas painter walks the same characters in the same
* order — line breaks take a turn too — so the preview and the export agree.
*/
function GlyphText({
text,
phase,
scale,
zoom,
ink,
tail,
texture,
typed,
em = 1 / LINE_HEIGHT,
}: {
text: string;
phase: Parameters<typeof glyphStateAt>[0];
scale: number;
/** 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;
/** One em of the type in line heights: a slot reel's pitch and band are
* set in em. */
em?: number;
}) {
const total = Math.max(1, [...text].length);
const lines = text.split("\n");
const at = typingAt(lines, typed);
const seed = slotSeed(text);
let gi = 0;
return (
<>
{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, or the
// band its edge sets; the band clips top and bottom only, so a wide
// filler is not cut.
const reel = g.roll ? slotReel(ch, i, g.roll, seed, g.reel, em) : null;
const bandInset = reel && g.reel?.band !== undefined ? (0.5 - reelBand(g.reel, em)) * 100 : 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;
// A letter streaking under motion blur: its streak in the span's
// own space, under the letter's turn and stretch.
const streak = g.streak && ch !== " " && !reel ? glyphStreak(g, scale) : null;
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 (
<span
key={i}
style={{
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
? bandInset !== null
? { position: "relative", clipPath: `inset(${bandInset.toFixed(2)}% -1000%)`, verticalAlign: "top" }
: { position: "relative", overflowX: "visible", overflowY: "clip", verticalAlign: "top" }
: {}),
...(texture && ch !== " " ? textureMask(texture) : {}),
}}
>
{reel ? (
<>
<span style={{ visibility: "hidden" }}>{ch}</span>
{reel.map((r, k) => (
<span
key={k}
style={{
position: "absolute",
left: "50%",
top: 0,
transform: `translate(-50%, ${r.y * 100}%)`,
opacity: r.alpha,
}}
>
{r.ch}
</span>
))}
</>
) : streak ? (
// A letter in flight under motion blur: copies along its
// streak that add up into one average, as the painter smears.
<span style={{ position: "relative", display: "inline-block", isolation: "isolate" }}>
<span style={{ visibility: "hidden" }}>{ch}</span>
{streak.taps.map((f, k) => (
<span
key={k}
aria-hidden
style={{
position: "absolute",
left: 0,
top: 0,
opacity: 1 / streak.taps.length,
mixBlendMode: "plus-lighter",
transform: `translate(${(streak.x * f).toFixed(2)}px, ${(streak.y * f).toFixed(2)}px)`,
}}
>
{split ? (
<>
{copy(SPLIT_RED, -split)}
{copy(SPLIT_CYAN, split)}
<span style={{ position: "relative" }}>{ch}</span>
</>
) : (
ch
)}
</span>
))}
</span>
) : split ? (
<>
{copy(SPLIT_RED, -split)}
{copy(SPLIT_CYAN, split)}
<span style={{ position: "relative" }}>{ch}</span>
</>
) : (
ch
)}
</span>
);
});
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" }}>
{withBar}
</span>
);
})}
</>
);
}
/**
* 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
* flight at a time; a frame that arrives during it is painted next.
*/
function DiveCanvas({
o,
live,
stageWidth,
stageHeight,
filter,
}: {
o: Overlay;
live: OverlayFrameState;
stageWidth: number;
stageHeight: number;
filter?: string;
}) {
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 pending = useRef<{ live: OverlayFrameState } | null>(null);
const busy = useRef(false);
useEffect(() => {
const canvas = ref.current;
if (!canvas) return;
pending.current = { live };
if (busy.current) return;
busy.current = true;
const env = cutRenderEnv(assets ?? []);
// The painter receives the pose separately from the element's look.
const el = { ...o, rotation: undefined, opacity: undefined } as Overlay;
void (async () => {
try {
while (pending.current) {
const next = pending.current.live;
pending.current = null;
const focus: DiveFocus = await measureDiveFocus(o, w / h, env);
const view = diveView(next.dive ?? 0, focus, next, o, {
width: w,
height: h,
scale: Math.min(w, h) / 1080,
});
await paintElementInto(canvas, el, env, { dive: view, pose: next, ...(next.caret ? { caret: true } : {}) });
}
} finally {
busy.current = false;
}
})();
}, [o, live, 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,
filter,
}}
/>
);
}
/**
* A moving element with motion blur, painted by the kit and smeared along its
* streak: the picture every export draws. It rides inside the element's box,
* which carries the pose, so the on-screen streak is turned back into the
* box's own space (unrotated, unzoomed) before the smear. The element's
* picture is painted once, cropped to its ink, and repainted only while its
* pixels change on their own (typing, a wipe, a per-glyph move, words, a
* Lottie); each moving frame then costs a few element-sized draws. At rest it
* draws nothing and the box shows the element itself.
*/
/** How far behind the playhead a self-moving element's streak picture may
* run, seconds: a few frames of paint latency. */
const STREAK_STALE_S = 0.1;
function StreakCanvas({
o,
live,
tLocal,
stageWidth,
stageHeight,
poseZoom,
filter,
mask,
onReady,
}: {
o: Overlay;
live: OverlayFrameState;
tLocal: number;
stageWidth: number;
stageHeight: number;
poseZoom: number;
filter?: string;
/** A front subject mask's CSS, laid over the stage-sized frame. */
mask?: CSSProperties;
onReady: (ready: boolean) => void;
}) {
const ref = useRef<HTMLCanvasElement>(null);
const raster = useRef<{ canvas: HTMLCanvasElement; scratch: HTMLCanvasElement; x: number; y: number; at: number } | null>(null);
const [painted, setPainted] = useState(0);
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));
// Pixels that change on their own repaint every frame while the element
// streaks; at rest the DOM box shows them and nothing paints. The rest
// paint once.
const selfMoving =
!!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;
const paintAt = selfMoving ? tLocal : 0;
const pending = useRef<number | null>(null);
const busy = useRef(false);
// The loop reads the newest element and size at each paint, so a change
// that lands while a paint is in flight is the next one painted.
const latest = useRef({ o, w, h, assets });
useEffect(() => {
latest.current = { o, w, h, assets };
if (selfMoving && !moving && raster.current) return;
pending.current = paintAt;
if (busy.current) return;
busy.current = true;
void (async () => {
try {
while (pending.current !== null) {
const at = pending.current;
pending.current = null;
const cur = latest.current;
const ev = evalOverlayFrame(cur.o, at, cur.w / cur.h);
const { el, phase } = elementAtMoment(cur.o, at, ev);
const canvas = raster.current?.canvas ?? document.createElement("canvas");
const scratch = raster.current?.scratch ?? document.createElement("canvas");
const origin = await paintElementCrop(canvas, el, cur.w, cur.h, cutRenderEnv(cur.assets ?? []), { t: at, phase, maskScratch: scratch });
raster.current = { canvas, scratch, ...origin, at };
setPainted((n) => n + 1);
}
} finally {
busy.current = false;
}
})();
}, [o, paintAt, w, h, assets, selfMoving, moving]);
// The picture, its mask scratch and the smear canvas count toward the
// memory report.
useEffect(() => {
const release = holdMemory("overlayRasters", () => {
const r = raster.current;
const c = ref.current;
const px = (x: { width: number; height: number } | null | undefined) => (x ? x.width * x.height * 4 : 0);
return px(r?.canvas) + px(r?.scratch) + px(c);
});
return () => {
release();
const r = raster.current;
if (r) r.canvas.width = r.canvas.height = r.scratch.width = r.scratch.height = 1;
raster.current = null;
onReady(false);
};
}, [onReady]);
const streak = live.streak;
useLayoutEffect(() => {
const canvas = ref.current;
const pic = raster.current;
if (!canvas || !pic) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
// A self-moving picture from another moment shows the DOM box until the
// paint for this one lands.
if (!streak || (selfMoving && Math.abs(pic.at - tLocal) > STREAK_STALE_S)) {
// At rest the box shows the element; the canvas empties once.
if (canvas.style.display !== "none") {
canvas.style.display = "none";
ctx.clearRect(0, 0, canvas.width, canvas.height);
onReady(false);
}
return;
}
// The on-screen streak in device px, turned into the box's own space.
const px = Math.min(w, h) / 1080;
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 * (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);
const ch = Math.max(canvas.height, Math.ceil((pic.canvas.height + Math.abs(ly) + 4) / 64) * 64);
if (canvas.width !== cw) canvas.width = cw;
if (canvas.height !== ch) canvas.height = ch;
const ox = Math.floor((cw - pic.canvas.width) / 2);
const oy = Math.floor((ch - pic.canvas.height) / 2);
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = "source-over";
ctx.clearRect(0, 0, cw, ch);
drawStreak(ctx, pic.canvas, ox, oy, {
streakX: lx,
streakY: ly,
taps: streakTaps(Math.hypot(streak.x, streak.y)),
});
canvas.style.display = "";
canvas.style.left = `${(pic.x - ox) / dpr}px`;
canvas.style.top = `${(pic.y - oy) / dpr}px`;
canvas.style.width = `${cw / dpr}px`;
canvas.style.height = `${ch / dpr}px`;
onReady(true);
}, [streak, live.rotation, live.sx, live.sy, poseZoom, painted, w, h, dpr, onReady, selfMoving, tLocal]);
return (
<div
className="pointer-events-none absolute"
style={{
left: "50%",
top: "50%",
width: stageWidth,
height: stageHeight,
// The stage in the element's unposed frame, as the dive canvas sits.
transform: `translate(${-o.x * stageWidth}px, ${-o.y * stageHeight}px)`,
filter,
}}
>
{/* The mask clips inside the filter, so the streak blurs after it. */}
<div
className="absolute inset-0"
style={mask ? { ...mask, maskPosition: "0px 0px", WebkitMaskPosition: "0px 0px" } : undefined}
>
<canvas ref={ref} width={1} height={1} className="absolute" style={{ display: "none" }} />
</div>
</div>
);
}
function useMaskCss(
o: Overlay,
boxRef: React.RefObject<HTMLDivElement | null>,
stageWidth: number,
stageHeight: number,
scale: number,
tLocal: number,
disabled: boolean
): CSSProperties | undefined {
const m = o.mask;
// A behind (inverted subject) element renders in the canvas pass; the DOM
// masks shapes and front subject elements.
const subjectFront = !!m && m.kind === "subject" && !m.invert;
const active = !!m && (m.kind !== "subject" || subjectFront) && !disabled;
const keyed = !!m && hasMaskKeys(m);
// The published matte refreshes at the segmentation cadence; its stamp is
// the memo key, so the CSS mask follows the person as the video plays.
const snap = subjectFront && active ? subjectMatteSnapshot() : null;
// The repaint keys: a keyed mask ticks at the matte cadence, and the
// subject branch follows the published matte's stamp.
const keyTick = keyed ? Math.round(tLocal * MASK_CSS_FPS) : 0;
const snapTick = subjectFront ? (snap?.at ?? -1) : 0;
// One canvas per element, reused across repaints.
const canvasRef = useRef<HTMLCanvasElement | null>(null);
const [box, setBox] = useState<{ w: number; h: number } | null>(null);
useEffect(() => {
if (!active) return;
const el = boxRef.current;
if (!el) return;
const measure = () => setBox({ w: el.offsetWidth, h: el.offsetHeight });
measure();
const ro = new ResizeObserver(measure);
ro.observe(el);
return () => ro.disconnect();
}, [active, boxRef]);
return useMemo(() => {
if (!active || !m || !box || box.w < 1 || box.h < 1) return undefined;
const canvas = (canvasRef.current ??= document.createElement("canvas"));
if (subjectFront) {
// Trim to the person: the published matte becomes the element's
// mask-image, positioned so it stays anchored to the frame while the
// element sits inside it. A computed matte with no person is empty
// coverage (the element hides, like every renderer); before the first
// matte lands the element stays whole.
if (!snap) return undefined;
if (!snap.canvas) {
return {
maskImage: NO_COVERAGE_MASK,
WebkitMaskImage: NO_COVERAGE_MASK,
};
}
if (canvas.width !== snap.canvas.width) canvas.width = snap.canvas.width;
if (canvas.height !== snap.canvas.height) canvas.height = snap.canvas.height;
const ctx = canvas.getContext("2d");
if (!ctx) return undefined;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.globalCompositeOperation = "source-over";
ctx.clearRect(0, 0, canvas.width, canvas.height);
const featherMatte =
(m.feather ?? 0) * scale * (snap.canvas.width / Math.max(1, stageWidth));
if (featherMatte > 0 && "filter" in ctx) ctx.filter = `blur(${featherMatte / 2}px)`;
ctx.drawImage(snap.canvas, 0, 0);
ctx.filter = "none";
const url = canvas.toDataURL();
const posX = box.w / 2 - (o.x * stageWidth);
const posY = box.h / 2 - (o.y * stageHeight);
return {
maskImage: `url(${url})`,
WebkitMaskImage: `url(${url})`,
maskRepeat: "no-repeat",
WebkitMaskRepeat: "no-repeat",
maskSize: `${stageWidth}px ${stageHeight}px`,
WebkitMaskSize: `${stageWidth}px ${stageHeight}px`,
maskPosition: `${posX}px ${posY}px`,
WebkitMaskPosition: `${posX}px ${posY}px`,
};
}
// The canvas caps below the box size and the CSS mask stretches it back:
// the soft coverage survives the scale and the PNG encode stays small.
const s = Math.min(1, MASK_CSS_MAX / Math.max(box.w, box.h));
const cw = Math.max(1, Math.round(box.w * s));
const ch = Math.max(1, Math.round(box.h * s));
if (canvas.width !== cw) canvas.width = cw;
if (canvas.height !== ch) canvas.height = ch;
const ctx = canvas.getContext("2d");
if (!ctx) return undefined;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.globalCompositeOperation = "source-over";
ctx.clearRect(0, 0, cw, ch);
if (maskInverts(m)) {
// Keep-outside: full coverage with the shape cut out of it, because a
// CSS mask has no invert of its own.
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, cw, ch);
ctx.globalCompositeOperation = "destination-out";
}
// Map frame coordinates onto the box: the element's anchor sits at the
// box center, which is where the painter anchors the mask.
ctx.scale(s, s);
ctx.translate(box.w / 2, box.h / 2);
ctx.translate(-o.x * stageWidth, -o.y * stageHeight);
paintMaskCoverage(
ctx,
{ ...m, invert: undefined },
tLocal,
{ width: stageWidth, height: stageHeight, scale },
{ x: o.x, y: o.y }
);
const url = canvas.toDataURL();
return {
maskImage: `url(${url})`,
WebkitMaskImage: `url(${url})`,
maskRepeat: "no-repeat",
WebkitMaskRepeat: "no-repeat",
maskSize: "100% 100%",
WebkitMaskSize: "100% 100%",
};
// The keyed tick folds a 15fps-quantized tLocal in only when the mask
// actually animates; the matte stamp refreshes the subject branch.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [active, m, box, o.x, o.y, stageWidth, stageHeight, scale, keyTick, snapTick]);
}
/**
* On-canvas mask editing, shared by elements and video clips: an amber
* outline tracing the mask's edge — its interior moves the mask, a lollipop
* above rotates it, a corner grip resizes it (the band height, for mirror),
* a chevron grip under the shape pulls the feather out and a grip inside the
* top-left corner rounds a box's corners — with the same detents and red
* guide lines as the element's own handles. A translucent band along the
* edge shows how far the feather reaches. Coordinates are local
* px around the layer's anchor — the mount point carries the layer's own
* transform — and screen deltas are folded back through that transform via
* `rotation`/`poseScale`, then through the mask's own angle for the resize.
* With mask keys in play a drag writes the key at the playhead, the same
* contract as dragging a keyframed element.
*
* A pen mask adds its own two states. Unclosed, the stage becomes a drawing
* surface: a click places a corner, clicking the first corner (or a
* double-click, or Enter) closes the outline, Backspace takes the last corner
* back and Escape clears the draft. Closed, every corner wears a grip that
* drags it, a hollow grip on each edge adds a corner there, and a
* double-click on a corner removes it.
*/
export function MaskGizmoCore({
mask: m,
stageWidth,
stageHeight,
tLocal,
rotation,
poseScale,
writeGeom,
writePoints,
begin,
}: {
mask: Mask;
stageWidth: number;
stageHeight: number;
tLocal: number;
rotation: number;
poseScale: number;
/** Keyed geometry: lands on the key at the playhead when keys exist. */
writeGeom: (patch: Partial<Omit<MaskKey, "t">>) => void;
/** A pen mask's corners, on the mask itself (a tracked outline keys its own
* and shows no corner grips). */
writePoints: (points: MaskPoint[]) => void;
begin: () => void;
}) {
const t = useTranslations("editor.maskGizmo");
// The grips' names, resolved once per locale: the gizmo redraws with the
// playhead, and a redraw takes no lookups.
const titles = useMemo(
() => ({
moveCorner: t("moveCorner"),
addCorner: t("addCorner"),
feather: t("feather"),
round: t("round"),
rotate: t("rotate"),
resize: t("resize"),
}),
[t]
);
const f = maskFrameAt(m, tLocal);
const pen = m.kind === "pen";
const drawing = pen && !penClosed(m);
// A tracked outline keys its corners: the guides trace the outline at the
// playhead, and its corners belong to the tracker.
const shown = f.points ? { ...m, points: f.points } : m;
// Guide lines while a drag sits on a detent: `rot` is the locked quarter
// turn, `x`/`y` mark the mask centered on an axis, `w`/`h` a size locked at
// exactly full frame. Local space, so on an element they ride its transform
// the way the mask itself does.
const [guide, setGuide] = useState<{
rot?: number;
x?: boolean;
y?: boolean;
w?: boolean;
h?: boolean;
} | null>(null);
/** The angle the readout shows while a rotate drag runs; null when idle. */
const [spin, setSpin] = useState<number | null>(null);
/** The feather value shown while its grip drags; null when idle. */
const [soft, setSoft] = useState<number | null>(null);
/** The corner radius shown while its grip drags; null when idle. */
const [round, setRound] = useState<number | null>(null);
const interiorRef = useRef<HTMLDivElement>(null);
/** The pen corners placed so far, local px around the anchor, while the
* outline is open; the pointer's current spot trails the last one. */
const [draft, setDraft] = useState<{ x: number; y: number }[]>([]);
const [trail, setTrail] = useState<{ x: number; y: number } | null>(null);
const drawRef = useRef<SVGSVGElement>(null);
// Screen deltas → the box's local space (undo the element transform).
const toLocal = (dx: number, dy: number) => {
const r = (-rotation * Math.PI) / 180;
const s = poseScale || 1;
return {
x: (dx * Math.cos(r) - dy * Math.sin(r)) / s,
y: (dx * Math.sin(r) + dy * Math.cos(r)) / s,
};
};
const theta = (f.rotation * Math.PI) / 180;
const sizable = maskSizeAxes(m.kind).length > 0;
// A screen point in the gizmo's local px: the drawing surface's own matrix
// folds in every transform above it, the layer's pose included.
const localPoint = (clientX: number, clientY: number) => {
const ctm = drawRef.current?.getScreenCTM();
if (!ctm) return { x: 0, y: 0 };
const pt = new DOMPoint(clientX, clientY).matrixTransform(ctm.inverse());
return { x: pt.x, y: pt.y };
};
// Local px → a pen corner in the mask's box: back through the mask's
// center and angle, then in fractions of its size.
const toPoint = (lx: number, ly: number): MaskPoint => {
const ox = lx - f.x * stageWidth;
const oy = ly - f.y * stageHeight;
const mx = ox * Math.cos(theta) + oy * Math.sin(theta);
const my = -ox * Math.sin(theta) + oy * Math.cos(theta);
return {
x: +(mx / Math.max(1e-6, f.w * stageWidth)).toFixed(4),
y: +(my / Math.max(1e-6, f.h * stageHeight)).toFixed(4),
};
};
const closeDraft = (pts: { x: number; y: number }[]) => {
if (pts.length < PEN_MIN_POINTS) return;
begin();
writePoints(pts.map((p) => toPoint(p.x, p.y)));
setDraft([]);
setTrail(null);
};
// Two corners within a grip's reach of each other are one corner: a
// double-click's second press, or a click back on the first corner.
const near = (a: { x: number; y: number }, b: { x: number; y: number }) =>
Math.hypot(a.x - b.x, a.y - b.y) < 10 / (poseScale || 1);
const placeCorner = (e: React.PointerEvent) => {
if (e.button !== 0) return;
e.stopPropagation();
e.preventDefault();
const at = localPoint(e.clientX, e.clientY);
if (draft.length >= PEN_MIN_POINTS && near(at, draft[0]!)) return closeDraft(draft);
if (draft.length > 0 && near(at, draft[draft.length - 1]!)) return;
setDraft([...draft, at]);
};
// Keys while drawing: Enter closes, Backspace takes a corner back, Escape
// clears the draft. Captured on the window ahead of the editor's own
// shortcuts, so Escape never reaches the selection.
useEffect(() => {
if (!drawing) return;
const onKey = (e: KeyboardEvent) => {
if (e.key === "Enter") closeDraft(draft);
else if (e.key === "Backspace") setDraft(draft.slice(0, -1));
else if (e.key === "Escape") setDraft([]);
else return;
e.preventDefault();
e.stopImmediatePropagation();
};
window.addEventListener("keydown", onKey, true);
return () => window.removeEventListener("keydown", onKey, true);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [drawing, draft]);
// A corner grip drags its corner; a hollow edge grip adds one where it
// sits and drags it from there. A double-click on a corner removes it.
const dragCorner = (e: React.PointerEvent, points: MaskPoint[], index: number) => {
e.stopPropagation();
begin();
const p0 = points[index]!;
startDrag(e, {
onMove: (dx, dy) => {
const l = toLocal(dx, dy);
const mx = l.x * Math.cos(theta) + l.y * Math.sin(theta);
const my = -l.x * Math.sin(theta) + l.y * Math.cos(theta);
const next = points.slice();
next[index] = {
x: +(p0.x + mx / Math.max(1e-6, f.w * stageWidth)).toFixed(4),
y: +(p0.y + my / Math.max(1e-6, f.h * stageHeight)).toFixed(4),
};
writePoints(next);
},
});
};
const addCorner = (e: React.PointerEvent, points: MaskPoint[], after: number) => {
const a = points[after]!;
const b = points[(after + 1) % points.length]!;
const next = points.slice();
next.splice(after + 1, 0, { x: +((a.x + b.x) / 2).toFixed(4), y: +((a.y + b.y) / 2).toFixed(4) });
dragCorner(e, next, after + 1);
};
const removeCorner = (points: MaskPoint[], index: number) => {
if (points.length <= PEN_MIN_POINTS) return;
begin();
writePoints(points.filter((_, i) => i !== index));
};
// The outline traces the mask's hard edge in local px; a square's side is
// `w` of the frame width on both axes (the painter's rule), and
// linear/mirror edges run wider than any frame so they always cross it.
const cx = f.x * stageWidth;
const cy = f.y * stageHeight;
const w = f.w * stageWidth;
const h = m.kind === "square" ? w : f.h * stageHeight;
const span = stageWidth + stageHeight;
// Which grips the kind can honor: a square's axes move together, a mirror
// band only opens and closes, and a linear edge has no size at all.
const maskHandles: ResizeHandle[] = drawing
? []
: m.kind === "square"
? CORNER_HANDLES
: m.kind === "mirror"
? ["n", "s"]
: sizable
? FRAME_HANDLES
: [];
const clampSize = (v: number) => Math.min(2, Math.max(0.01, v));
// Detents matching the inspector's: size locks onto exactly full frame,
// the center onto dead center, within a couple percent of the frame.
const snapTo = (v: number, target: number) => (Math.abs(v - target) < 0.02 ? target : v);
const designScale = Math.min(stageWidth, stageHeight) / 1080;
const radiusPx = f.radius * designScale;
// The feather's reach on stage: the painter blurs the hard edge with
// σ = feather / 2, so the transition spans about the feather width centered
// on the edge; a mirror band's feather never exceeds the band.
const featherPx = Math.min(f.feather * designScale, m.kind === "mirror" ? h : Infinity);
const outlineD = maskOutlinePathD(shown, w, h, span, radiusPx);
// Where the shape's bottom edge sits in its own frame, for the feather grip.
const bottom = m.kind === "linear" ? 0 : m.kind === "square" ? w / 2 : h / 2;
const edge = {
className: "pointer-events-none",
fill: "none" as const,
stroke: "#ff9f0a",
strokeWidth: 1.5,
strokeDasharray: "6 4",
vectorEffect: "non-scaling-stroke" as const,
};
const beginMove = (e: React.PointerEvent) => {
e.stopPropagation();
begin();
const g0 = f;
startDrag(e, {
onMove: (dx, dy) => {
const l = toLocal(dx, dy);
const x = snapTo(g0.x + l.x / stageWidth, 0);
const y = snapTo(g0.y + l.y / stageHeight, 0);
setGuide(x === 0 || y === 0 ? { x: x === 0, y: y === 0 } : null);
writeGeom({ x, y });
},
onUp: () => setGuide(null),
});
};
// The lollipop turns the mask about its center; quarter turns detent the
// same way the element's own handle does. Angles are mask-local — the
// mount's transform carries the element's rotation, and a screen-angle
// delta equals a local delta under rotate + uniform scale.
const rotateMask = (e: React.PointerEvent) => {
e.stopPropagation();
begin();
const box = interiorRef.current?.getBoundingClientRect();
const mcx = box ? box.left + box.width / 2 : e.clientX;
const mcy = box ? box.top + box.height / 2 : e.clientY;
const angleAt = angleFrom(mcx, mcy);
const a0 = angleAt(e);
const g0 = f;
let liveRot = rotation + g0.rotation;
setSpin(Math.round(g0.rotation));
startDrag(e, {
cursor: () => rotateCursor(liveRot),
onMove: (_dx, _dy, ev) => {
const { deg, locked } = snapQuarter(normDeg(g0.rotation + (angleAt(ev) - a0)));
const rot = Math.round(deg);
liveRot = rotation + rot;
setSpin(rot);
setGuide(locked === null ? null : { rot: locked });
writeGeom({ rotation: rot });
},
onUp: () => {
setGuide(null);
setSpin(null);
},
});
};
// A grip pulls its own edge and leaves the opposite one planted: the size
// takes the pull and the center walks half of it, so the far edge holds
// still. A mirror band is the exception — it opens about its line, which is
// the thing it draws.
const sizeMask = (handle: ResizeHandle, e: React.PointerEvent) => {
e.stopPropagation();
begin();
const a = HANDLE_AXIS[handle];
const g0 = f;
startDrag(e, {
onMove: (dx, dy, ev) => {
const l = toLocal(dx, dy);
const mx = l.x * Math.cos(theta) + l.y * Math.sin(theta);
const my = -l.x * Math.sin(theta) + l.y * Math.cos(theta);
const grow = m.kind === "mirror" ? 2 : 1;
let nw = clampSize(snapTo(g0.w + (grow * mx * a.x) / stageWidth, 1));
let nh = clampSize(snapTo(g0.h + (grow * my * a.y) / stageHeight, 1));
// ⇧ holds the shape true — equal pixel sides, a perfect circle or
// square — with the pulled axis driving; a circle also settles onto
// true round on its own when a drag lands within a hair of it. The
// fractions differ because w is measured against the frame's width
// and h against its height.
const round = (m.kind === "circle" || m.kind === "rect") && (a.x !== 0 || a.y !== 0);
if (round && (ev.shiftKey || m.kind === "circle")) {
const toH = (w: number) => (w * stageWidth) / stageHeight;
const toW = (h: number) => (h * stageHeight) / stageWidth;
if (ev.shiftKey) {
// The pulled axis drives; a corner drives from the wider pull.
const driveW = a.y === 0 || (a.x !== 0 && Math.abs(mx) >= Math.abs(my));
if (driveW) nh = clampSize(toH(nw));
else nw = clampSize(toW(nh));
if (nh !== toH(nw)) nw = clampSize(toW(nh));
} else if (a.x !== 0 && a.y !== 0 && Math.abs(nh - toH(nw)) < 0.02) {
nh = clampSize(toH(nw));
}
}
const constrained = round && ev.shiftKey;
const gw = a.x !== 0 && nw === 1;
const gh = a.y !== 0 && nh === 1;
setGuide(gw || gh ? { w: gw, h: gh } : null);
// The center's walk, in the mask's own axes in px: half of what each
// edge actually grew after the clamp. A square's vertical side is `w`
// of the frame width too, so its corner shifts both axes by the same
// px. Turned by the mask's angle back into the element's space.
const dw = (nw - g0.w) * stageWidth;
const dh = m.kind === "square" ? dw : (nh - g0.h) * stageHeight;
const hx = m.kind === "mirror" ? 0 : (a.x * dw) / 2;
const hy = m.kind === "mirror" ? 0 : (a.y * dh) / 2;
const ex = hx * Math.cos(theta) - hy * Math.sin(theta);
const ey = hx * Math.sin(theta) + hy * Math.cos(theta);
const shift =
hx || hy ? { x: g0.x + ex / stageWidth, y: g0.y + ey / stageHeight } : {};
writeGeom(
m.kind === "square"
? { w: nw, ...shift }
: m.kind === "mirror"
? { h: nh }
: constrained || (a.x !== 0 && a.y !== 0)
? { w: nw, h: nh, ...shift }
: a.y === 0
? { w: nw, ...shift }
: { h: nh, ...shift }
);
},
onUp: () => setGuide(null),
});
};
// The chevron grip pulls the feather: dragging away from the shape along
// its own down axis softens the edge, back in hardens it. Feather is design
// px, so the stage delta divides by the design scale.
const featherMask = (e: React.PointerEvent) => {
e.stopPropagation();
begin();
const g0 = f;
setSoft(Math.round(g0.feather));
startDrag(e, {
cursor: () => "ns-resize",
onMove: (dx, dy) => {
const l = toLocal(dx, dy);
const my = -l.x * Math.sin(theta) + l.y * Math.cos(theta);
const feather = Math.round(
Math.min(MASK_FEATHER_MAX, Math.max(0, g0.feather + my / designScale))
);
setSoft(feather);
writeGeom({ feather });
},
onUp: () => setSoft(null),
});
};
// The corner grip slides along the box's diagonal: inward rounds the
// corners, outward squares them.
const roundMask = (e: React.PointerEvent) => {
e.stopPropagation();
begin();
const r0 = f.radius;
const side = m.kind === "square" ? w : h;
const cap = Math.min(MASK_RADIUS_MAX, Math.min(w, side) / 2 / designScale);
setRound(Math.round(r0));
startDrag(e, {
cursor: () => "nwse-resize",
onMove: (dx, dy) => {
const l = toLocal(dx, dy);
const mx = l.x * Math.cos(theta) + l.y * Math.sin(theta);
const my = -l.x * Math.sin(theta) + l.y * Math.cos(theta);
const radius = Math.round(Math.min(cap, Math.max(0, r0 + (mx + my) / 2 / designScale)));
setRound(radius);
writeGeom({ radius });
},
onUp: () => setRound(null),
});
};
// The whole interior moves the mask, so a drag can start anywhere inside
// the outline; linear/mirror get a grabbable strip along their edge line.
const grabW = m.kind === "linear" || m.kind === "mirror" ? span * 2 : w;
const grabH = m.kind === "linear" ? 28 : m.kind === "mirror" ? Math.max(h, 28) : h;
const points = pen && !drawing && !f.points ? m.points! : null;
const grip = {
borderColor: "#ff9f0a",
boxShadow: "0 1px 4px rgba(0,0,0,0.4)",
};
if (drawing) {
const outline = draft.map((p) => `${p.x},${p.y}`).join(" ");
return (
<svg
ref={drawRef}
className="absolute overflow-visible"
width={1}
height={1}
style={{ left: "50%", top: "50%", cursor: "crosshair" }}
onPointerDown={placeCorner}
onDoubleClick={(e) => {
e.stopPropagation();
closeDraft(draft);
}}
onPointerMove={(e) => setTrail(localPoint(e.clientX, e.clientY))}
onPointerLeave={() => setTrail(null)}
>
<rect x={-span} y={-span} width={span * 2} height={span * 2} fill="transparent" style={{ pointerEvents: "all" }} />
{draft.length > 0 && (
<>
<polyline points={outline} {...edge} />
{trail && (
<line
x1={draft[draft.length - 1]!.x}
y1={draft[draft.length - 1]!.y}
x2={trail.x}
y2={trail.y}
stroke="#ff9f0a"
strokeOpacity={0.6}
strokeWidth={1.5}
strokeDasharray="6 4"
vectorEffect="non-scaling-stroke"
className="pointer-events-none"
/>
)}
{draft.map((p, i) => (
<circle
key={i}
cx={p.x}
cy={p.y}
r={i === 0 && draft.length >= PEN_MIN_POINTS ? 7 : 5}
fill="#ffffff"
stroke="#ff9f0a"
strokeWidth={2.5}
vectorEffect="non-scaling-stroke"
className="pointer-events-none"
/>
))}
</>
)}
</svg>
);
}
return (
<>
<div
ref={interiorRef}
className="absolute cursor-move"
style={{
left: `calc(50% + ${cx}px)`,
top: `calc(50% + ${cy}px)`,
width: grabW,
height: grabH,
transform: `translate(-50%, -50%) rotate(${f.rotation}deg)`,
// The grab area is the shape itself: a box's rounded corners, an
// ellipse, or the polygon's own outline.
...(m.kind === "linear" || m.kind === "mirror"
? {}
: { clipPath: `path("${maskOutlinePathD(shown, w, h, span, radiusPx, grabW / 2, grabH / 2)}")` }),
}}
onPointerDown={beginMove}
/>
<svg
className="pointer-events-none absolute overflow-visible"
width={1}
height={1}
style={{ left: `calc(50% + ${cx}px)`, top: `calc(50% + ${cy}px)` }}
>
<g transform={`rotate(${f.rotation})`}>
{featherPx > 0 && (
<path
d={outlineD}
fill="none"
stroke="#ff9f0a"
strokeOpacity={0.22}
strokeWidth={featherPx}
strokeLinejoin="round"
/>
)}
<path d={outlineD} {...edge} />
</g>
</svg>
<div
className="pointer-events-none absolute"
style={{
left: `calc(50% + ${cx}px)`,
top: `calc(50% + ${cy}px)`,
width: 0,
height: 0,
transform: `rotate(${f.rotation}deg)`,
}}
>
{points &&
points.map((p, i) => {
const q = points[(i + 1) % points.length]!;
return (
<Fragment key={i}>
<span
title={titles.moveCorner}
onPointerDown={(e) => dragCorner(e, points, i)}
onDoubleClick={(e) => {
e.stopPropagation();
removeCorner(points, i);
}}
className="pointer-events-auto absolute block size-[11px] cursor-move rounded-full border-[2.5px] bg-white"
style={{ left: p.x * w, top: p.y * h, transform: "translate(-50%, -50%)", ...grip }}
/>
<span
title={titles.addCorner}
onPointerDown={(e) => addCorner(e, points, i)}
className="pointer-events-auto absolute block size-[7px] cursor-copy rounded-full border-[1.5px] bg-white/70"
style={{
left: ((p.x + q.x) / 2) * w,
top: ((p.y + q.y) / 2) * h,
transform: "translate(-50%, -50%)",
...grip,
}}
/>
</Fragment>
);
})}
<div
className="absolute left-0"
style={{ top: bottom + featherPx / 2 + 22, transform: "translate(-50%, -50%)" }}
>
{soft !== null && (
<span
className="absolute left-1/2 top-full mt-1.5 rounded-full bg-black/55 px-2 py-0.5 text-[11px] font-medium tabular-nums text-white"
style={{ transform: `translateX(-50%) rotate(${-(rotation + f.rotation)}deg)` }}
>
{soft}
</span>
)}
{/* The grip wears the softness it controls: a ring that blurs
outward, in the mask's amber like every other mask grip. */}
<span
title={titles.feather}
onPointerDown={featherMask}
className="pointer-events-auto block size-[13px] cursor-ns-resize rounded-full border-[2.5px] bg-white"
style={{
borderColor: "#ff9f0a",
boxShadow: "0 0 0 3px rgba(255,159,10,0.35), 0 0 8px 4px rgba(255,159,10,0.3), 0 1px 4px rgba(0,0,0,0.4)",
}}
/>
</div>
{maskHasRadius(m.kind) && (
<div
className="absolute"
style={{
left: -w / 2 + radiusPx * 0.3 + 14,
top: -(m.kind === "square" ? w : h) / 2 + radiusPx * 0.3 + 14,
transform: "translate(-50%, -50%)",
}}
>
{round !== null && (
<span
className="absolute bottom-full left-1/2 mb-1.5 rounded-full bg-black/55 px-2 py-0.5 text-[11px] font-medium tabular-nums text-white"
style={{ transform: `translateX(-50%) rotate(${-(rotation + f.rotation)}deg)` }}
>
{round}
</span>
)}
<span
title={titles.round}
onPointerDown={roundMask}
className="pointer-events-auto grid size-[16px] cursor-nwse-resize place-items-center rounded-full border-[2.5px] bg-white shadow-[0_1px_4px_rgba(0,0,0,0.4)]"
style={{ borderColor: "#ff9f0a" }}
>
<svg viewBox="0 0 10 10" className="size-[7px]" fill="none" stroke="#ff9f0a" strokeWidth={2}>
<path d="M1 9 V5 A4 4 0 0 1 5 1 H9" />
</svg>
</span>
</div>
)}
</div>
{guide && (
<svg
className="pointer-events-none absolute z-10 overflow-visible"
width={1}
height={1}
style={{ left: "50%", top: "50%" }}
>
{guide.rot !== undefined && (
<g transform={`translate(${cx} ${cy}) rotate(${guide.rot})`}>
<line x1={-span} y1={0} x2={span} y2={0} stroke="#ff2d55" strokeWidth={1} vectorEffect="non-scaling-stroke" />
</g>
)}
{(guide.w || guide.h) && (
<g transform={`translate(${cx} ${cy}) rotate(${f.rotation})`}>
{guide.w && (
<>
<line x1={-stageWidth / 2} y1={-span} x2={-stageWidth / 2} y2={span} stroke="#ff2d55" strokeWidth={1} vectorEffect="non-scaling-stroke" />
<line x1={stageWidth / 2} y1={-span} x2={stageWidth / 2} y2={span} stroke="#ff2d55" strokeWidth={1} vectorEffect="non-scaling-stroke" />
</>
)}
{guide.h && (
<>
<line x1={-span} y1={-stageHeight / 2} x2={span} y2={-stageHeight / 2} stroke="#ff2d55" strokeWidth={1} vectorEffect="non-scaling-stroke" />
<line x1={-span} y1={stageHeight / 2} x2={span} y2={stageHeight / 2} stroke="#ff2d55" strokeWidth={1} vectorEffect="non-scaling-stroke" />
</>
)}
</g>
)}
{guide.x && (
<line x1={0} y1={-span} x2={0} y2={span} stroke="#ff2d55" strokeWidth={1} vectorEffect="non-scaling-stroke" />
)}
{guide.y && (
<line x1={-span} y1={0} x2={span} y2={0} stroke="#ff2d55" strokeWidth={1} vectorEffect="non-scaling-stroke" />
)}
</svg>
)}
<TransformHandles
className="z-10"
color="#ff9f0a"
handles={maskHandles}
rotation={rotation + f.rotation}
angle={spin}
rotateCursor={rotateCursor(rotation + f.rotation)}
rotateTitle={titles.rotate}
resizeTitle={titles.resize}
frame={{
left: `calc(50% + ${cx}px)`,
top: `calc(50% + ${cy}px)`,
width: Math.max(1, w),
// A linear mask is a line: its frame has no height to lift over.
height: m.kind === "linear" ? 1 : Math.max(1, h),
transform: `translate(-50%, -50%) rotate(${f.rotation}deg)`,
}}
onRotate={rotateMask}
onResize={sizeMask}
/>
</>
);
}
/** The element mount: grips are children of the element box, so they ride
* its transform; writes land on the overlay's mask (or its key at the
* playhead). */
function MaskGizmo({
overlay: o,
stageWidth,
stageHeight,
tLocal,
rotation,
poseScale,
}: {
overlay: Overlay;
stageWidth: number;
stageHeight: number;
tLocal: number;
rotation: number;
poseScale: number;
}) {
const st = () => useEditor.getState();
const writeGeom = (patch: Partial<Omit<MaskKey, "t">>) => {
const cur = st().overlays.find((x) => x.id === o.id)?.mask;
if (!cur) return;
if (hasMaskKeys(cur)) return st().setOverlayMaskKey(o.id, tLocal, patch, { transient: true });
st().updateOverlayTransient(o.id, { mask: { ...cur, ...patch } });
};
const writePoints = (points: MaskPoint[]) => {
const cur = st().overlays.find((x) => x.id === o.id)?.mask;
if (cur) st().updateOverlayTransient(o.id, { mask: { ...cur, points } });
};
return (
<MaskGizmoCore
mask={o.mask!}
stageWidth={stageWidth}
stageHeight={stageHeight}
tLocal={tLocal}
rotation={rotation}
poseScale={poseScale}
writeGeom={writeGeom}
writePoints={writePoints}
begin={() => st().pushHistory()}
/>
);
}
/** A shape drawn as inline SVG with the painter's own pixel geometry
* (`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,
stageHeight,
scale,
}: {
shape: ShapeOverlay;
stageWidth: number;
stageHeight: number;
scale: number;
}) {
const uid = useId().replace(/[^\w-]/g, "");
const m = shapeMetrics(o, { width: stageWidth, height: stageHeight, scale });
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"
/>
{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={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({
sticker: o,
stageWidth,
stageHeight,
t,
}: {
sticker: StickerOverlay;
stageWidth: number;
stageHeight: number;
t: number;
}) {
const asset = useEditor((s) =>
o.assetId ? s.assets.find((a) => a.id === o.assetId) : undefined
);
if (!asset) return <span className="block size-8 rounded bg-white/20" />;
// A stretched sticker draws at its stored height; otherwise the source's
// own aspect sets it.
const height = o.h ? o.h * stageHeight : undefined;
if (o.lottie) {
return (
<LottieView
asset={asset}
width={o.w * stageWidth}
height={height}
tLocal={Math.max(0, t - o.start)}
/>
);
}
return (
// eslint-disable-next-line @next/next/no-img-element -- project media blob/engine URL
<img
src={asset.url}
alt={asset.name}
draggable={false}
className="block max-w-none select-none"
style={{ width: o.w * stageWidth, height: height ?? "auto" }}
/>
);
}
/** A Lottie sticker in the preview: its own player instance, hard-seeked to
* the timeline moment (goToAndStop — never free-running), destroyed on
* unmount. Out-of-range elements don't mount, which caps live instances. */
function LottieView({
asset,
width,
height,
tLocal,
}: {
asset: { id: string; url: string; fileName: string; name: string };
width: number;
/** An explicit height; absent draws under the animation's own aspect. */
height?: number;
tLocal: number;
}) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const handleRef = useRef<LottieHandle | null>(null);
const [ready, setReady] = useState(0);
const [aspect, setAspect] = useState(1);
useEffect(() => {
let live = true;
void import("@/cut/lib/lottieAssets").then(async (m) => {
const full = useEditor.getState().assets.find((a) => a.id === asset.id);
const handle = full ? await m.newLottieInstance(full) : null;
if (!live) {
handle?.destroy();
return;
}
handleRef.current = handle;
if (handle) setAspect(handle.width / Math.max(1, handle.height));
setReady((n) => n + 1);
});
return () => {
live = false;
handleRef.current?.destroy();
handleRef.current = null;
};
}, [asset.id]);
useEffect(() => {
const handle = handleRef.current;
const canvas = canvasRef.current;
if (!handle || !canvas) return;
const frame = handle.seek(tLocal);
if (canvas.width !== frame.width || canvas.height !== frame.height) {
canvas.width = frame.width;
canvas.height = frame.height;
}
const ctx = canvas.getContext("2d");
if (!ctx) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(frame, 0, 0);
}, [tLocal, ready]);
return (
<canvas
ref={canvasRef}
className="block max-w-none select-none"
style={{ width, height: height ?? width / aspect }}
/>
);
}