Skip to content

Open source CapCut alternative.

TypeScript

fix(site): fill the frame on Dive [rebuild]

David Led564d2fb89f0
diff --git a/docs/guides/cut/README.md b/docs/guides/cut/README.md
index 4b0446a1..77f695bf 100644
--- a/docs/guides/cut/README.md
+++ b/docs/guides/cut/README.md
@@ -26,7 +26,7 @@ AI is hosted whatever the home: image, video, and voiceover generation and the a
## The preview
-The preview scrubs and plays from per-clip decoders held in memory. It shows a frame the moment it arrives and refines behind it, so the timeline always answers and a jump lands on a picture. Every cache is bounded by one memory budget sized to the machine. The performance guide holds the frame budget and how a change is measured.
+The preview shows ready frames immediately and refines them as decoding catches up. Dive paints inside the output frame, so a title's scale and rotation preserve full-frame coverage. The performance guide covers memory and measurement.
Background previews capture a project operation and a render document before asynchronous work starts. The operation carries the project, residency, document version, capabilities, and transport. The preview queue coalesces edits and drains the final edit when the editor closes. Cloud exports requested from a document store that document in the queued job, so a later edit cannot change the export's input.
diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.ts
index 42a8c264..231f1e7c 100644
--- a/site/packages/effects-kit/src/render.ts
+++ b/site/packages/effects-kit/src/render.ts
@@ -19,7 +19,7 @@ import {
} from "./anim";
import { diveView, slotReel, slotSeed, type DiveFocus, type DiveView } from "./dive";
import { presetExtent } from "./motion/evaluate";
-import { evalOverlayFrame, hasOverlayKeys, poseAt, poseExtent, sortedKeys } from "./keys";
+import { evalOverlayFrame, hasOverlayKeys, poseAt, poseExtent, sortedKeys, type OverlayFrameState } from "./keys";
import { applyMaskToCanvas, isMaskAnimated } from "./mask";
import { tracePolyShape } from "./shapePath";
import type { LottieHandle } from "./lottie";
@@ -131,15 +131,17 @@ export interface PaintFrame {
phase?: PaintPhase;
}
-/** What a pixel-changing animation contributes to one picture: how much of the
- * element is uncovered, and where its characters sit. The transform styles
- * never come through here — they ride the drawn picture instead. */
+/** What a pixel-changing animation contributes to one picture: its reveal,
+ * glyph motion, and the pose and view a dive paints inside the frame. */
export interface PaintPhase {
reveal?: number;
glyphs?: GlyphPhase;
glyphLoop?: GlyphLoopPhase;
/** The view a dive draws the whole picture under (see `diveView`). */
dive?: DiveView;
+ /** A dive's pose is painted before frame clipping, so a half-size title
+ * still fills the output frame when its flight lands. */
+ pose?: Pick<OverlayFrameState, "x" | "y" | "dx" | "dy" | "scale" | "rotation">;
}
/**
@@ -679,6 +681,19 @@ export function applyDiveView(
ctx.translate(-v.fx, -v.fy);
}
+// Place the element inside the output surface before the dive fills it.
+function applyPaintPose(
+ ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
+ pose: NonNullable<PaintPhase["pose"]>,
+ rest: { x: number; y: number },
+ frame: PaintFrame
+): void {
+ ctx.translate(pose.x * frame.width + pose.dx * frame.scale, pose.y * frame.height + pose.dy * frame.scale);
+ ctx.rotate((pose.rotation * Math.PI) / 180);
+ ctx.scale(pose.scale, pose.scale);
+ ctx.translate(-rest.x * frame.width, -rest.y * frame.height);
+}
+
/** Whether either edge of an element dives, so a renderer knows to measure
* the focus and give the flight the whole frame. */
export function divesAt(anim: OverlayAnim | undefined): boolean {
@@ -952,7 +967,9 @@ export async function paintElementInto(
ctx.clearRect(0, 0, width, height);
ctx.globalAlpha = overlay.opacity ?? 1;
- if (overlay.rotation) {
+ if (phase?.pose) {
+ applyPaintPose(ctx, phase.pose, overlay, frame);
+ } else if (overlay.rotation) {
const cx = overlay.x * width;
const cy = overlay.y * height;
ctx.translate(cx, cy);
@@ -1270,12 +1287,7 @@ export async function renderOverlayFrames(
ctx.globalAlpha = ev.opacity;
ctx.translate(-x0, -y0);
// The pose places the element; the preset's travel rides on top of it.
- ctx.translate(ev.x * width + ev.dx * scale, ev.y * height + ev.dy * scale);
- ctx.rotate((ev.rotation * Math.PI) / 180);
- ctx.scale(ev.scale, ev.scale);
- // Back to the origin the painters draw around, so they stay unaware of
- // any of this.
- ctx.translate(-overlay.x * width, -overlay.y * height);
+ applyPaintPose(ctx, ev, overlay, frame);
// A dive's view goes inside the pose, so the type is drawn as vectors at
// the size it is seen and stays sharp however far in the flight gets.
if (focus && ev.dive) applyDiveView(ctx, diveView(ev.dive, focus, ev, overlay, frame));
diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsx
index c821a200..6a201179 100644
--- a/site/src/cut/components/OverlayLayer.tsx
+++ b/site/src/cut/components/OverlayLayer.tsx
@@ -1123,6 +1123,9 @@ function OverlayItem({
return (
<>
+ {diving && live && (
+ <DiveCanvas o={o} live={live} stageWidth={stageWidth} stageHeight={stageHeight} />
+ )}
<div
ref={boxRef}
className={cn(
@@ -1174,9 +1177,6 @@ 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}
@@ -1347,9 +1347,8 @@ 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
+ * 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({
@@ -1377,7 +1376,7 @@ function DiveCanvas({
if (busy.current) return;
busy.current = true;
const env = cutRenderEnv(assets ?? []);
- // The element's own look, neutral: the box already carries its pose.
+ // The painter receives the pose separately from the element's look.
const el = { ...o, rotation: undefined, opacity: undefined } as Overlay;
void (async () => {
try {
@@ -1390,7 +1389,7 @@ function DiveCanvas({
height: h,
scale: Math.min(w, h) / 1080,
});
- await paintElementInto(canvas, el, env, { dive: view });
+ await paintElementInto(canvas, el, env, { dive: view, pose: next });
}
} finally {
busy.current = false;
@@ -1402,15 +1401,11 @@ function DiveCanvas({
ref={ref}
width={w}
height={h}
- className="pointer-events-none absolute"
+ className="pointer-events-none absolute inset-0"
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)`,
+ opacity: live.opacity,
}}
/>
);
diff --git a/site/src/cut/lib/exportDive.test.ts b/site/src/cut/lib/exportDive.test.ts
new file mode 100644
index 00000000..ee2e38c0
--- /dev/null
+++ b/site/src/cut/lib/exportDive.test.ts
@@ -0,0 +1,65 @@
+import { beforeAll, expect, test } from "bun:test";
+import { installSkiaRaster } from "./headless/skiaRaster";
+import { renderProjectFrame } from "./exportRender";
+import { decodeRasterImage } from "./raster";
+import { renderElementFrames } from "./textRender";
+import type { ExportDoc } from "./renderSnapshot";
+import type { TextOverlay } from "./types";
+
+const size = { width: 320, height: 180 };
+const color = "#c9a227";
+const corners = [[1, 1], [size.width - 2, 1], [1, size.height - 2], [size.width - 2, size.height - 2]];
+
+beforeAll(async () => {
+ expect(await installSkiaRaster()).toBe(true);
+});
+
+// A transformed title must still land its ink at all four frame corners.
+for (const pose of [
+ { x: 0.5, y: 0.5, scale: 0.5, rotation: 0 },
+ { x: 0.3, y: 0.7, scale: 0.5, rotation: 30 },
+ { x: 0.7, y: 0.3, scale: 2, rotation: -30 },
+]) {
+ test(`dive fills the frame at scale ${pose.scale} and rotation ${pose.rotation}`, async () => {
+ const title: TextOverlay = {
+ id: "title", text: "THE END", start: 0, end: 4.25,
+ x: 0.5, y: 0.5, size: 320, font: "sf", weight: 700,
+ color, plate: false, shadow: false,
+ kf: [{ t: 0, ...pose, opacity: 1 }],
+ anim: { in: { style: "slot", seconds: 2 }, out: { style: "dive", seconds: 0.75 } },
+ };
+ const doc: ExportDoc = {
+ aspect: "16:9", assets: [], clips: [], audioClips: [], overlays: [title],
+ subtitles: { cues: [], showOnVideo: false, showOnTimeline: false },
+ };
+ const canvas = await renderProjectFrame(doc, title.end - 0.001, size, () => "");
+ const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
+
+ try {
+ for (const [x, y] of corners) {
+ expect([...ctx.getImageData(x, y, 1, 1).data]).toEqual([201, 162, 39, 255]);
+ }
+ } finally {
+ canvas.width = 1;
+ canvas.height = 1;
+ }
+
+ // The engine's sampled PNG sequence must land on the same full frame.
+ const frames = await renderElementFrames(title, size.width, size.height, 30);
+ expect([frames.x, frames.y, frames.w, frames.h]).toEqual([0, 0, size.width, size.height]);
+ const image = await decodeRasterImage(frames.images.at(-1)!);
+ expect(image).not.toBeNull();
+ canvas.width = size.width;
+ canvas.height = size.height;
+
+ try {
+ ctx.drawImage(image!.source, 0, 0);
+ for (const [x, y] of corners) {
+ expect([...ctx.getImageData(x, y, 1, 1).data]).toEqual([201, 162, 39, 255]);
+ }
+ } finally {
+ canvas.width = 1;
+ canvas.height = 1;
+ }
+ });
+}
diff --git a/site/src/cut/lib/exportRender.ts b/site/src/cut/lib/exportRender.ts
index 88be6b5e..f3eb1108 100644
--- a/site/src/cut/lib/exportRender.ts
+++ b/site/src/cut/lib/exportRender.ts
@@ -1197,7 +1197,7 @@ async function drawStamps(
? {
...layer.phase,
...(ev?.glyphLoop ? { glyphLoop: ev.glyphLoop } : {}),
- ...(dive ? { dive } : {}),
+ ...(dive && ev ? { dive, pose: ev } : {}),
}
: undefined;
const bitmap =
@@ -1222,6 +1222,12 @@ async function drawStamps(
const cx = layer.overlay.x * canvas.width;
const cy = layer.overlay.y * canvas.height;
const posed = (c: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) => {
+ // A dive's pose is already inside its frame-sized picture.
+ if (dive) {
+ c.drawImage(bitmap, 0, 0, canvas.width, canvas.height);
+ return;
+ }
+
c.translate(ev.x * canvas.width + ev.dx * scale, ev.y * canvas.height + ev.dy * scale);
c.rotate((ev.rotation * Math.PI) / 180);
c.scale(ev.scale, ev.scale);