Skip to content

Open source CapCut alternative.

TypeScript

fix(site): the preview keeps its picture through a resize

David Leb8773b0ef025
diff --git a/site/src/cut/components/Preview.tsx b/site/src/cut/components/Preview.tsx
index 049cd438..11587d40 100644
--- a/site/src/cut/components/Preview.tsx
+++ b/site/src/cut/components/Preview.tsx
@@ -19,7 +19,7 @@ import {
paintPoster,
readPoster,
} from "@/cut/lib/posterCache";
-import { setPreviewCanvas } from "@/cut/lib/previewCanvas";
+import { resizePreviewSurface, setPreviewCanvas } from "@/cut/lib/previewCanvas";
import { CLIP_MAX_ZOOM, clipCovers, clipKeyed, clipPoseAt, clipZoom, contentRect, frameOf, isFullRect, rectOf, REGION_MAX_SCALE, type Aspect, type ClipSpan, type FrameRect, type MediaAsset, type VideoClip } from "@/cut/lib/types";
import { hasMaskKeys, type MaskKey } from "@donkeycut/effects-kit";
import { cn } from "@/lib/utils";
@@ -146,6 +146,9 @@ const MAX_ZOOM = 32;
const ZOOM_STEPS = [0.05, 0.1, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 3, 4, 6, 8, 12, 16, 24, 32];
/** How long the zoom control lingers after the last camera move. */
const HUD_LINGER_MS = 2600;
+/** How still the camera has to be before the backing store re-resolves to the
+ * size on screen. */
+const SURFACE_SETTLE_MS = 220;
/** The stage's corner rounding: tiny, and gone once the picture is small on
* screen. */
@@ -307,12 +310,52 @@ export function Preview() {
// a few hundred pixels wide put every grade, look and mask pass through
// millions of pixels nobody could see; the export renders at full size on its
// own surface, so nothing about the file changes.
- const surface = useMemo(() => {
+ const wantSurface = useMemo(() => {
const dpr = typeof window === "undefined" ? 1 : Math.min(2, window.devicePixelRatio || 1);
const w = Math.max(2, Math.min(frame.w, Math.round(stage.w * dpr)));
return { w, h: Math.max(2, Math.round((w * frame.h) / frame.w)) };
}, [stage.w, frame.w, frame.h]);
+ /**
+ * The resolution the store actually holds, which is not the same thing as
+ * the size on screen.
+ *
+ * Re-resolving costs the picture: the store is erased, and the decoders are
+ * keyed to their decode height, so a new height opens sources that have yet
+ * to produce a frame. Doing that on every step of a zoom is the flicker —
+ * the resize empties the canvas and the replacement frame is not there yet.
+ * So the resolution follows the screen only once the picture is standing
+ * still: never mid-playback, and only after the camera has settled. In
+ * between, the pixels already on the canvas are scaled to the box, which
+ * costs sharpness and nothing else.
+ */
+ const playing = useEditor((s) => s.playing);
+ const [surface, setSurface] = useState(wantSurface);
+ // What the store was last resolved for. The wait below is there to absorb a
+ // camera in motion, so everything else lands at once: the opening fit, and a
+ // change of project aspect, which the store has to match or the picture is
+ // squashed into the wrong shape.
+ const resolved = useRef({ frameW: 0, fitted: false });
+ useEffect(() => {
+ if (wantSurface.w === surface.w && wantSurface.h === surface.h) return;
+ const camera = resolved.current.fitted && resolved.current.frameW === frame.w;
+ resolved.current = { frameW: frame.w, fitted: true };
+ if (!camera) {
+ setSurface(wantSurface);
+ return;
+ }
+ if (playing) return;
+ const id = window.setTimeout(() => setSurface(wantSurface), SURFACE_SETTLE_MS);
+ return () => window.clearTimeout(id);
+ }, [wantSurface, surface, playing, frame.w]);
+
+ // The store is written here rather than as a rendered attribute, because
+ // assigning one erases the canvas and the picture has to come across.
+ useLayoutEffect(() => {
+ const canvas = canvasRef.current;
+ if (canvas) resizePreviewSurface(canvas, surface.w, surface.h);
+ }, [surface]);
+
useEffect(() => {
const wrap = wrapRef.current;
if (!wrap) return;
@@ -538,8 +581,9 @@ export function Preview() {
<StagePictureFx>
<canvas
ref={canvasRef}
- width={surface.w}
- height={surface.h}
+ // The backing store is sized in a layout effect, which carries the
+ // picture across the resize; rendering it as an attribute would
+ // blank the canvas on every change.
className="block size-full"
// Drag the viewport to reference what's on screen: the clip under
// the playhead travels as a media drag (timeline placement, chat
diff --git a/site/src/cut/lib/previewCanvas.ts b/site/src/cut/lib/previewCanvas.ts
index 1e53b69d..7e739294 100644
--- a/site/src/cut/lib/previewCanvas.ts
+++ b/site/src/cut/lib/previewCanvas.ts
@@ -1,5 +1,7 @@
"use client";
+import { createRasterCanvas, type RasterSurface } from "./raster";
+
/**
* The live preview <canvas>, registered by Preview while it is mounted, so
* panels can sample the composited frame (the color panel's histogram reads
@@ -16,6 +18,50 @@ export function getPreviewCanvas(): HTMLCanvasElement | null {
return el;
}
+/** Where the picture waits while the backing store is replaced under it. Kept
+ * for the page's life: a resize is a moment where allocating is the last thing
+ * to be doing. */
+let carry: RasterSurface | null = null;
+
+/**
+ * Resize the preview's backing store, keeping the picture.
+ *
+ * Assigning a canvas's width or height erases it, and the engine repaints on
+ * its own schedule — with a decoder still opening it deliberately leaves what
+ * is on screen alone, which is the whole reason the preview holds a frame
+ * instead of strobing. Those two together are a black flash: the store is
+ * cleared, and the frame that would refill it is not ready yet. So the picture
+ * comes across the resize — copied out, then laid back down scaled to the new
+ * store — and the canvas is never empty at any instant. The next real frame
+ * simply paints over it.
+ */
+export function resizePreviewSurface(canvas: HTMLCanvasElement, w: number, h: number): void {
+ if (canvas.width === w && canvas.height === h) return;
+ const held = holdPicture(canvas);
+ canvas.width = w;
+ canvas.height = h;
+ if (!held) return;
+ const ctx = canvas.getContext("2d");
+ ctx?.drawImage(held, 0, 0, w, h);
+}
+
+/** The canvas's current pixels, on a surface of their own. Null when there is
+ * nothing to keep or nowhere to put it. */
+function holdPicture(canvas: HTMLCanvasElement): RasterSurface | null {
+ const { width: w, height: h } = canvas;
+ if (w < 1 || h < 1) return null;
+ if (!carry) carry = createRasterCanvas(w, h);
+ if (carry.width !== w) carry.width = w;
+ if (carry.height !== h) carry.height = h;
+ const ctx = carry.getContext("2d") as CanvasRenderingContext2D | null;
+ if (!ctx) return null;
+ ctx.clearRect(0, 0, w, h);
+ // Cross-origin media taints the canvas; drawing one into another is still
+ // allowed, and nothing here reads pixels back.
+ ctx.drawImage(canvas, 0, 0);
+ return carry;
+}
+
/**
* A clip's raw decoded frame, straight from the playback engine's decoder
* element — before any color grade — so analysis (the color panel's Auto)