Skip to content

Open source CapCut alternative.

TypeScript

feat(site): play slot and dive in the preview, export and animation tiles

David Lea3b7b57d1cfb
diff --git a/site/src/cut/components/AnimationTiles.tsx b/site/src/cut/components/AnimationTiles.tsx
index 0724d756..9447601e 100644
--- a/site/src/cut/components/AnimationTiles.tsx
+++ b/site/src/cut/components/AnimationTiles.tsx
@@ -11,6 +11,8 @@ import {
MOTION,
presetExtent,
sampleProperties,
+ slotReel,
+ TEXT_ONLY_ANIM_STYLE_IDS,
OVERLAY_ANIM_STYLE_IDS,
OVERLAY_ANIM_STYLE_LABELS,
OVERLAY_LOOP_STYLE_IDS,
@@ -162,6 +164,19 @@ const wordStyle = () => ({ fontFamily: fontStack("montserrat"), fontWeight: 700,
const typesItsName = (slot: Slot, style: string) =>
slot === "in" || slot === "out" ? !!MOTION.edges[style]?.animate.typed : false;
+/** Whether the demo rolls each letter up a reel (slot). The reel's three
+ * windows come from React empty, for the reason a typed demo's do. */
+const rollsItsName = (slot: Slot, style: string) =>
+ slot === "in" || slot === "out" ? !!MOTION.edges[style]?.animate.roll : false;
+
+/** Whether the demo flies into its own last letter (dive). */
+const divesIntoName = (slot: Slot, style: string) =>
+ slot === "in" || slot === "out" ? !!MOTION.edges[style]?.animate.dive : false;
+
+/** How far a dive demo zooms: the last letter's ink fills a tile well before
+ * the frame-sized flight the element takes. */
+const DEMO_DIVE_MAX = 40;
+
/** Whether the demo has to lay the name out letter by letter: the per-glyph
* ramps and the per-glyph loops both move each character on its own delay. */
const splitsGlyphs = (slot: Slot, style: string) =>
@@ -204,6 +219,8 @@ function LiveName({
const ref = useRef<HTMLSpanElement>(null);
const label = labelOf(slot, style);
const typed = typesItsName(slot, style);
+ const rolls = isText && rollsItsName(slot, style);
+ const dives = divesIntoName(slot, style);
// Golden-ratio steps around the cycle: neighbouring tiles land far apart in
// it, and no run of them ever bunches up mid-motion together.
const phase = index * (slot === "loop" ? LOOP_SPREAD : demoCycle(seconds)) * PHASE_STEP;
@@ -214,16 +231,33 @@ function LiveName({
// effect's to write, starting with the whole name.
if (el.dataset.word) el.textContent = el.dataset.word;
const letters = () => Array.from(el.children) as HTMLElement[];
+ const chars = [...label];
+ /** A slot letter's reel, drawn into its empty windows. */
+ const drawReel = (kid: HTMLElement, i: number, roll: number) => {
+ const windows = kid.querySelectorAll<HTMLElement>("[data-reel]");
+ const reel = roll ? slotReel(chars[i] ?? "", i, roll) : [{ ch: chars[i] ?? "", y: 0, alpha: 1 }];
+ windows.forEach((w, k) => {
+ const r = reel[k];
+ w.textContent = r ? r.ch : "";
+ w.style.transform = r ? `translate(-50%, ${r.y * 100}%)` : "";
+ w.style.opacity = r ? String(r.alpha) : "0";
+ });
+ };
const rest = () => {
el.style.transform = "";
+ el.style.transformOrigin = "";
el.style.opacity = "";
el.style.clipPath = "";
- for (const kid of letters()) {
+ letters().forEach((kid, i) => {
kid.style.transform = "";
kid.style.opacity = "";
- }
+ if (rolls) drawReel(kid, i, 0);
+ });
if (el.dataset.word) el.textContent = el.dataset.word;
};
+ // The reel windows come from React empty: the name goes in before
+ // anything moves.
+ if (rolls) letters().forEach((kid, i) => drawReel(kid, i, 0));
if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return rest;
let start = 0;
let raf = 0;
@@ -242,17 +276,25 @@ function LiveName({
const g = glyphStateAt(st, i, kids.length);
kid.style.transform = `translate(${px(g.dx)}px, ${px(g.dy)}px) rotate(${g.rotate}deg) scale(${g.sx}, ${g.sy})`;
kid.style.opacity = String(g.alpha);
+ if (rolls) drawReel(kid, i, g.roll ?? 0);
});
glyphed = true;
} else {
if (glyphed) {
- for (const kid of letters()) {
+ letters().forEach((kid, i) => {
kid.style.transform = "";
kid.style.opacity = "";
- }
+ if (rolls) drawReel(kid, i, 0);
+ });
glyphed = false;
}
- el.style.transform = `translate(${px(st.dx)}px, ${px(st.dy)}px) rotate(${st.rotate}deg) scale(${st.scale})`;
+ // A dive zooms the name about its last letter.
+ const last = dives ? letters().at(-1) : undefined;
+ el.style.transformOrigin = last
+ ? `${last.offsetLeft + last.offsetWidth / 2}px ${last.offsetTop + last.offsetHeight / 2}px`
+ : "";
+ const zoom = st.dive ? Math.pow(DEMO_DIVE_MAX, st.dive) : 1;
+ el.style.transform = `translate(${px(st.dx)}px, ${px(st.dy)}px) rotate(${st.rotate}deg) scale(${st.scale * zoom})`;
el.style.opacity = String(st.alpha);
}
el.style.clipPath =
@@ -269,7 +311,7 @@ function LiveName({
cancelAnimationFrame(raf);
rest();
};
- }, [slot, style, isText, seconds, speed, phase]);
+ }, [slot, style, isText, seconds, speed, phase, label, rolls, dives]);
return (
<span
@@ -280,7 +322,16 @@ function LiveName({
>
{typed
? null
- : isText && splitsGlyphs(slot, style)
+ : rolls
+ ? [...label].map((ch, i) => (
+ <span key={i} className="relative inline-block align-top" style={{ overflowY: "clip" }}>
+ <span className="invisible">{ch}</span>
+ <span data-reel className="absolute top-0 left-1/2" />
+ <span data-reel className="absolute top-0 left-1/2" />
+ <span data-reel className="absolute top-0 left-1/2" />
+ </span>
+ ))
+ : (isText && splitsGlyphs(slot, style)) || dives
? [...label].map((ch, i) => (
<span key={i} className="inline-block">
{ch}
@@ -524,7 +575,7 @@ export function AnimationTiles({
/** The slot plays an animation the project carries itself, so none of these
* tiles is what is running — including None. */
custom?: boolean;
- /** Typewriter is offered on titles only. */
+ /** Typing and rolling reels are offered on titles only. */
isText: boolean;
/** The In/Out length the panel has set — the hover demo's ramp. */
seconds: number;
@@ -593,7 +644,7 @@ export function AnimationTiles({
? HOLD_IDS
: slot === "loop"
? OVERLAY_LOOP_STYLE_IDS
- : OVERLAY_ANIM_STYLE_IDS.filter((s) => s !== "typewriter" || isText);
+ : OVERLAY_ANIM_STYLE_IDS.filter((s) => isText || !TEXT_ONLY_ANIM_STYLE_IDS.includes(s));
return (
<div className="grid grid-cols-2 gap-[9px]">
diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsx
index fd8351a2..114d9f98 100644
--- a/site/src/cut/components/OverlayLayer.tsx
+++ b/site/src/cut/components/OverlayLayer.tsx
@@ -18,12 +18,13 @@ import {
subtitleLaneCount,
trackPos,
} from "@/cut/lib/subtitles";
-import { evalOverlayFrame, glyphStateAt, 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 MaskPoint, type OverlayFrameState, type WordDraw } from "@donkeycut/effects-kit";
+import { diveView, evalOverlayFrame, glyphStateAt, measureDiveFocus, paintElementInto, slotReel, 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,
@@ -675,6 +676,12 @@ function OverlayItem({
// still works.
const behindHidden = behindSubjectOverlay(o) && !editing;
+ // 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;
+
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.
@@ -752,6 +759,7 @@ function OverlayItem({
})()
: {}),
...(maskCss ?? {}),
+ ...(diving ? { visibility: "hidden" as const } : {}),
...(reveal !== undefined
? { clipPath: `inset(0 ${(1 - Math.min(1, Math.max(0, reveal))) * 100}% 0 0)` }
: {}),
@@ -1166,6 +1174,9 @@ function OverlayItem({
<StickerView sticker={o} stageWidth={stageWidth} stageHeight={stageHeight} t={t} />
) : null}
</div>
+ {diving && live && (
+ <DiveCanvas o={o} live={live} stageWidth={stageWidth} stageHeight={stageHeight} />
+ )}
{/* 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}
@@ -1281,6 +1292,9 @@ function GlyphText({
const chars = [...line].map((ch) => {
const i = gi++;
const g = glyphStateAt(phase, i, total);
+ // A slot character shows its reel through its own line band; the
+ // band clips top and bottom only, so a wide filler is not cut.
+ const reel = g.roll ? slotReel(ch, i, g.roll) : null;
return (
<span
key={i}
@@ -1291,9 +1305,32 @@ function GlyphText({
transform:
`translate(${g.dx * scale}px, ${g.dy * scale}px)` +
` rotate(${g.rotate}deg) scale(${g.sx}, ${g.sy})`,
+ ...(reel
+ ? { position: "relative", overflowX: "visible", overflowY: "clip", verticalAlign: "top" }
+ : {}),
}}
>
- {ch}
+ {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>
+ ))}
+ </>
+ ) : (
+ ch
+ )}
</span>
);
});
@@ -1308,6 +1345,76 @@ function GlyphText({
);
}
+/**
+ * A diving element, painted by the kit over a stage-sized canvas that rides
+ * inside the element's box: the box carries the pose, the canvas the dive's
+ * view, and the painter is the one the export draws with. One paint is in
+ * flight at a time; a frame that arrives during it is painted next.
+ */
+function DiveCanvas({
+ o,
+ live,
+ stageWidth,
+ stageHeight,
+}: {
+ o: Overlay;
+ live: OverlayFrameState;
+ 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 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 element's own look, neutral: the box already carries its pose.
+ 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 });
+ }
+ } finally {
+ busy.current = false;
+ }
+ })();
+ }, [o, live, w, h, assets]);
+ return (
+ <canvas
+ ref={ref}
+ width={w}
+ height={h}
+ className="pointer-events-none absolute"
+ style={{
+ left: "50%",
+ top: "50%",
+ width: stageWidth,
+ height: stageHeight,
+ // The canvas spans the stage in the element's unposed frame: its
+ // origin sits the element's resting position back from the box center.
+ transform: `translate(${-o.x * stageWidth}px, ${-o.y * stageHeight}px)`,
+ }}
+ />
+ );
+}
+
function useMaskCss(
o: Overlay,
boxRef: React.RefObject<HTMLDivElement | null>,
diff --git a/site/src/cut/lib/behindPass.ts b/site/src/cut/lib/behindPass.ts
index 0df3ac7d..a3e0ecf4 100644
--- a/site/src/cut/lib/behindPass.ts
+++ b/site/src/cut/lib/behindPass.ts
@@ -11,7 +11,11 @@
import {
applyWordDraw,
+ diveView,
+ divesAt,
evalOverlayFrame,
+ measureDiveFocus,
+ peekDiveFocus,
glyphStateAt,
hasGlyphMotion,
maskComposite,
@@ -21,7 +25,7 @@ import {
import { personSegmenter, segmentSubjectAlpha } from "./cutout";
import { allowance, holdMemory } from "./memoryBudget";
import { createRasterCanvas } from "./raster";
-import { renderElementPng } from "./textRender";
+import { cutRenderEnv, renderElementPng } from "./textRender";
import {
behindSubjectOverlay,
frontSubjectOverlay,
@@ -297,6 +301,11 @@ export class SubjectMaskCompositor {
this.segmenter = s;
});
const behind = overlays.filter((o) => behindSubjectOverlay(o) && drawable(o));
+ await Promise.all(
+ behind.filter((o) => divesAt(o.anim)).map((o) =>
+ measureDiveFocus(o, w / h, cutRenderEnv(assets)).catch(() => {})
+ )
+ );
await Promise.all(
behind.flatMap((o) =>
// An element that lights its words needs one picture per word, all of
@@ -511,6 +520,15 @@ export class SubjectMaskCompositor {
ctx.rotate(((ev.rotation + (g?.rotate ?? 0)) * Math.PI) / 180);
ctx.scale(ev.scale * (g?.sx ?? 1), ev.scale * (g?.sy ?? 1));
ctx.translate(-cx, -cy);
+ // A dive flies the same one picture in, under the view the other
+ // renderers draw their type under.
+ const focus = ev.dive && divesAt(o.anim) ? peekDiveFocus(o, W / H, cutRenderEnv(assets)) : undefined;
+ if (focus) {
+ const v = diveView(ev.dive!, focus, ev, o, { width: W, height: H, scale });
+ ctx.translate(v.tx, v.ty);
+ ctx.scale(v.s, v.s);
+ ctx.translate(-v.fx, -v.fy);
+ }
ctx.drawImage(bmp, 0, 0, W, H);
ctx.restore();
}
diff --git a/site/src/cut/lib/exportRender.ts b/site/src/cut/lib/exportRender.ts
index 4a273d7d..88be6b5e 100644
--- a/site/src/cut/lib/exportRender.ts
+++ b/site/src/cut/lib/exportRender.ts
@@ -42,12 +42,12 @@ import type { InputVideoTrack, WrappedCanvas } from "mediabunny";
import { allowance, canvasBytes, holdMemory } from "./memoryBudget";
import { getClipSpans, overlayLayers, projectDuration, spanSequence } from "./store";
import { captionStyle, cueOverlay, cueWordFrames, laneCues, laneHidden, subtitleLaneCount, trackPos } from "./subtitles";
-import { applyEffectToCanvas, evalOverlayFrame, retimeOf, grainTile, smoothsAt, isAudioEffect, isMaskAnimated, isOverlayAnimated, maskFrameAt, MATTE_FPS, matteLumaToAlpha, planAnimatedLayers, type LottieHandle, type OverlayAnim, type PaintPhase } from "@donkeycut/effects-kit";
+import { applyEffectToCanvas, diveView, divesAt, evalOverlayFrame, measureDiveFocus, retimeOf, grainTile, smoothsAt, isAudioEffect, isMaskAnimated, isOverlayAnimated, maskFrameAt, MATTE_FPS, matteLumaToAlpha, planAnimatedLayers, type DiveFocus, type LottieHandle, type OverlayAnim, type PaintPhase } from "@donkeycut/effects-kit";
import { backdropStill, loadBackdropStill } from "./backdropStills";
import { hasSubjectOverlays, SubjectMaskCompositor } from "./behindPass";
import { createRasterCanvas, type RasterSurface } from "./raster";
import { exportFrameSynth, SYNTH_EDGE, synthWeight, type FrameSynth } from "./frameSynth";
-import { renderElementPng } from "./textRender";
+import { cutRenderEnv, renderElementCanvas, renderElementPng } from "./textRender";
import { assetIsSilent, behindSubjectOverlay, clipCovers, frameOf, frontSubjectOverlay, isEffectOverlay, isTextOverlay, laneOf, overlayAnimStyle, projectBackground, rectOf, removalActive } from "./types";
import type { ClipAnim, ClipSpan, EffectOverlay, MediaAsset, Overlay, StickerOverlay } from "./types";
import type { ExportDoc } from "./renderSnapshot";
@@ -761,13 +761,20 @@ class StampCache {
const el = isMaskAnimated(m)
? { ...layer.overlay, mask: { ...m!, ...maskFrameAt(m!, tLocal), kf: undefined } }
: layer.overlay;
- const png = await renderElementPng(el, this.width, this.height, this.assets, phase);
- const bitmap = await createImageBitmap(png);
+ // Drawn once and shown once, so it goes straight from the canvas.
+ const bitmap = await createImageBitmap(
+ await renderElementCanvas(el, this.width, this.height, this.assets, phase)
+ );
hit?.bitmap.close();
this.maskFrames.set(layer, { t: tLocal, bitmap });
return bitmap;
}
+ /** Where a diving element flies into, measured once per look. */
+ diveFocus(o: Overlay): Promise<DiveFocus> {
+ return measureDiveFocus(o, this.width / this.height, cutRenderEnv(this.assets));
+ }
+
/** Release the pictures of layers that have finished by `t`. */
retire(t: number) {
for (const [layer, bitmap] of this.drawn) {
@@ -1175,7 +1182,24 @@ async function drawStamps(
: null;
// A per-glyph loop moves the characters inside the picture, so its window
// is drawn afresh each frame with the loop folded into the layer's phase.
- const livePhase = ev?.glyphLoop ? { ...layer.phase, glyphLoop: ev.glyphLoop } : undefined;
+ // So is a dive: a cached picture scaled up fifty times would be mush, so
+ // each frame draws the type at the size it is seen.
+ const dive =
+ ev?.dive && divesAt(layer.anim)
+ ? diveView(ev.dive, await stamps.diveFocus(layer.source ?? layer.overlay), ev, layer.overlay, {
+ width: canvas.width,
+ height: canvas.height,
+ scale: Math.min(canvas.width, canvas.height) / 1080,
+ })
+ : undefined;
+ const livePhase =
+ ev?.glyphLoop || dive
+ ? {
+ ...layer.phase,
+ ...(ev?.glyphLoop ? { glyphLoop: ev.glyphLoop } : {}),
+ ...(dive ? { dive } : {}),
+ }
+ : undefined;
const bitmap =
livePhase || isMaskAnimated(layer.overlay.mask)
? await stamps.bitmapAt(layer, tLocal, livePhase ?? layer.phase)
diff --git a/site/src/cut/lib/textRender.ts b/site/src/cut/lib/textRender.ts
index b90ef7b9..751ceab9 100644
--- a/site/src/cut/lib/textRender.ts
+++ b/site/src/cut/lib/textRender.ts
@@ -3,6 +3,7 @@
import {
hexAlpha,
WORD_BASELINE_DROP,
+ renderElementCanvas as kitRenderElementCanvas,
renderElementPng as kitRenderElementPng,
renderOverlayFrames as kitRenderOverlayFrames,
LINE_HEIGHT,
@@ -178,6 +179,18 @@ export function renderElementPng(
return kitRenderElementPng(overlay, width, height, cutRenderEnv(assets), phase);
}
+/** The element drawn on a canvas at the export resolution, for a picture
+ * drawn straight away. */
+export function renderElementCanvas(
+ overlay: Overlay,
+ width: number,
+ height: number,
+ assets: MediaAsset[] = [],
+ phase?: PaintPhase
+): Promise<HTMLCanvasElement> {
+ return kitRenderElementCanvas(overlay, width, height, cutRenderEnv(assets), phase);
+}
+
/** Rasterize an animated element into its region-cropped frame set (the
* export's ffconcat slideshow food), bound to this app's render env. */
export function renderElementFrames(