Skip to content

Open source CapCut alternative.

TypeScript

fix(site): oversized region boxes export safely and clamp to a ceiling [rebuild]

David Lef357fd17d05b
diff --git a/site/src/cut/components/Preview.tsx b/site/src/cut/components/Preview.tsx
index 4c21febe..09bdbd46 100644
--- a/site/src/cut/components/Preview.tsx
+++ b/site/src/cut/components/Preview.tsx
@@ -12,7 +12,7 @@ import {
readPoster,
} from "@/cut/lib/posterCache";
import { setPreviewCanvas } from "@/cut/lib/previewCanvas";
-import { clipKeyed, clipPoseAt, frameOf, isFullRect, rectOf, type Aspect, type ClipSpan, type FrameRect, type MediaAsset, type VideoClip } from "@/cut/lib/types";
+import { clipKeyed, clipPoseAt, 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";
import { MaskGizmoCore, OverlayLayer } from "./OverlayLayer";
@@ -171,12 +171,14 @@ export function Preview() {
if (e.target === e.currentTarget) useEditor.getState().select(null);
}}
>
+ {/* The selection handle mounts beside the stage, outside its clipping,
+ so a box dragged past the frame edge stays visible and grabbable. */}
+ <div className="relative" style={{ width: stage.w, height: stage.h }}>
<div
className={cn(
- "stage relative overflow-hidden rounded-xl bg-black shadow-[0_0_0_1px_rgba(0,0,0,0.08),0_12px_36px_rgba(0,0,0,0.18)]",
+ "stage absolute inset-0 overflow-hidden rounded-xl bg-black shadow-[0_0_0_1px_rgba(0,0,0,0.08),0_12px_36px_rgba(0,0,0,0.18)]",
pannable && "cursor-grab active:cursor-grabbing"
)}
- style={{ width: stage.w, height: stage.h }}
onPointerDown={(e) => {
if (
e.target === e.currentTarget ||
@@ -219,7 +221,6 @@ export function Preview() {
}}
onDragEnd={clearAssetDrag}
/>
- <OverlayPipHandle stage={stage} />
<ClipMaskGizmo stage={stage} />
{slices.map((slice) =>
slice.kind === "elements" ? (
@@ -237,6 +238,8 @@ export function Preview() {
)
)}
</div>
+ <OverlayPipHandle stage={stage} />
+ </div>
</div>
</section>
);
@@ -279,6 +282,8 @@ function OverlayPipHandle({ stage }: { stage: { w: number; h: number } }) {
const r = rect;
const patch = apply;
+ // The box may leave the frame — oversize it to focus on an area, or park it
+ // partly off screen — as long as a sliver stays inside to grab.
const onMove = (e: React.PointerEvent) => {
e.stopPropagation();
useEditor.getState().pushHistory();
@@ -286,8 +291,8 @@ function OverlayPipHandle({ stage }: { stage: { w: number; h: number } }) {
onMove: (dx, dy) =>
patch({
...r,
- x: Math.max(0, Math.min(1 - r.w, r.x + dx / stage.w)),
- y: Math.max(0, Math.min(1 - r.h, r.y + dy / stage.h)),
+ x: Math.max(0.05 - r.w, Math.min(0.95, r.x + dx / stage.w)),
+ y: Math.max(0.05 - r.h, Math.min(0.95, r.y + dy / stage.h)),
}),
});
};
@@ -299,23 +304,39 @@ function OverlayPipHandle({ stage }: { stage: { w: number; h: number } }) {
onMove: (dx, dy) =>
patch({
...r,
- w: Math.max(0.1, Math.min(1 - r.x, r.w + dx / stage.w)),
- h: Math.max(0.1, Math.min(1 - r.y, r.h + dy / stage.h)),
+ w: Math.max(0.1, Math.min(REGION_MAX_SCALE, r.w + dx / stage.w)),
+ h: Math.max(0.1, Math.min(REGION_MAX_SCALE, r.h + dy / stage.h)),
}),
});
};
+ const box = {
+ left: r.x * stage.w,
+ top: r.y * stage.h,
+ width: r.w * stage.w,
+ height: r.h * stage.h,
+ };
+ // The dashed box draws the full extent; the frame-clipped solid ring paints
+ // over it, so dashes show only where the box leaves the frame.
return (
- <div
- className="absolute cursor-move rounded-[3px] shadow-[inset_0_0_0_2px_#a855f7]"
- style={{ left: r.x * stage.w, top: r.y * stage.h, width: r.w * stage.w, height: r.h * stage.h }}
- onPointerDown={onMove}
- >
- <span
- className="absolute -right-1.5 -bottom-1.5 size-3 cursor-nwse-resize rounded-full bg-violet-500 shadow-[0_0_0_2px_white]"
- onPointerDown={onResize}
- />
- </div>
+ <>
+ <div
+ className="absolute cursor-move rounded-[3px] border-2 border-dashed border-[#0a84ff]"
+ style={box}
+ onPointerDown={onMove}
+ >
+ <span
+ className="absolute -right-1.5 -bottom-1.5 z-20 size-3 cursor-nwse-resize rounded-full bg-[#0a84ff] shadow-[0_0_0_2px_white]"
+ onPointerDown={onResize}
+ />
+ </div>
+ <div className="pointer-events-none absolute inset-0 z-10 overflow-hidden rounded-xl">
+ <div
+ className="absolute rounded-[3px] shadow-[inset_0_0_0_2px_#0a84ff]"
+ style={box}
+ />
+ </div>
+ </>
);
}
diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.ts
index fd50db1e..a3497124 100644
--- a/site/src/cut/lib/types.ts
+++ b/site/src/cut/lib/types.ts
@@ -214,37 +214,48 @@ export interface FrameRect {
export const FULL_FRAME: FrameRect = { x: 0, y: 0, w: 1, h: 1 };
+/** How far a region box may oversize the frame on each axis. Oversizing is
+ * the zoom-into-an-area move; the ceiling keeps export scale targets and
+ * mask canvases within what ffmpeg and browser canvases handle. */
+export const REGION_MAX_SCALE = 3;
+
/** A clip's effective region: its own `frame`, or the full frame if unset. */
export function rectOf(clip: { frame?: FrameRect }): FrameRect {
return clip.frame ?? FULL_FRAME;
}
-/** Whether a region covers the whole frame (so it needs no special layout). */
+/** Whether a region matches the whole frame (so it needs no special layout).
+ * An oversized or shifted region counts as regioned — it crops to the frame. */
export function isFullRect(r: FrameRect): boolean {
- return r.x <= 0.001 && r.y <= 0.001 && r.w >= 0.999 && r.h >= 0.999;
+ return (
+ Math.abs(r.x) <= 0.001 &&
+ Math.abs(r.y) <= 0.001 &&
+ Math.abs(r.w - 1) <= 0.001 &&
+ Math.abs(r.h - 1) <= 0.001
+ );
}
-/** A region's pixel box at an output size, even-rounded and clamped inside
- * the frame — the one rounding that decides where a regioned clip's segment
- * sits. The ffmpeg graph frames segments with it and the export client
- * paints mask coverage with it, so the two land on identical pixels. Null
- * for the full frame. */
+/** A region's pixel box at an output size, even-rounded — the one rounding
+ * that decides where a regioned clip's segment sits. The ffmpeg graph frames
+ * segments with it and the export client paints mask coverage with it, so
+ * the two land on identical pixels. The box may reach past the frame edges
+ * (up to REGION_MAX_SCALE per axis — the same ceiling the resize handle
+ * enforces, applied again here so a stored oversize can never blow up the
+ * export); the graph crops the frame window back out. Null for the full
+ * frame, judged with isFullRect's tolerance so a nudged-by-a-hair rect lays
+ * out exactly like the full frame it visually is. */
export function regionPx(
frame: { x: number; y: number; w: number; h: number } | undefined,
W: number,
H: number
): { rx: number; ry: number; rw: number; rh: number } | null {
- if (!frame) return null;
+ if (!frame || isFullRect(frame)) return null;
const even = (n: number) => 2 * Math.round(n / 2);
- const rw = Math.min(W, Math.max(2, even(frame.w * W)));
- const rh = Math.min(H, Math.max(2, even(frame.h * H)));
- // Clamp the origin so rx+rw ≤ W and ry+rh ≤ H — independent even-rounding
- // can otherwise push an edge-touching region a pixel past the frame, which
- // makes the pad filter reject the input ("not within the padded area") and
- // aborts the whole export.
- const rx = Math.max(0, Math.min(even(frame.x * W), W - rw));
- const ry = Math.max(0, Math.min(even(frame.y * H), H - rh));
- if (rx <= 0 && ry <= 0 && rw >= W && rh >= H) return null;
+ const rw = Math.min(even(REGION_MAX_SCALE * W), Math.max(2, even(frame.w * W)));
+ const rh = Math.min(even(REGION_MAX_SCALE * H), Math.max(2, even(frame.h * H)));
+ const rx = even(frame.x * W);
+ const ry = even(frame.y * H);
+ if (rx === 0 && ry === 0 && rw === W && rh === H) return null;
return { rx, ry, rw, rh };
}
diff --git a/site/src/cut/server/exportPipeline.ts b/site/src/cut/server/exportPipeline.ts
index 28f15098..d0c72acd 100644
--- a/site/src/cut/server/exportPipeline.ts
+++ b/site/src/cut/server/exportPipeline.ts
@@ -898,14 +898,22 @@ export async function runExport(
let frame: string;
if (region) {
// A regioned track-0 clip (split-screen half) scales into its rect,
- // then pads out to the full frame with black around it.
+ // then pads out to the full frame with black around it. The rect may
+ // reach past the frame (an oversized focus box), and pad rejects
+ // placement outside its area — so pad to the box holding both, then
+ // crop the frame window back out.
const { rx, ry, rw, rh } = region;
+ const bx = Math.min(0, rx);
+ const by = Math.min(0, ry);
+ const bw = Math.max(W, rx + rw) - bx;
+ const bh = Math.max(H, ry + rh) - by;
+ const win = bw > W || bh > H ? `,crop=${W}:${H}:${-bx}:${-by}` : "";
frame =
c.fit === "fill"
? `scale=${rw}:${rh}:force_original_aspect_ratio=increase,crop=${rw}:${rh},` +
- `pad=${W}:${H}:${rx}:${ry}:color=${padColor}`
+ `pad=${bw}:${bh}:${rx - bx}:${ry - by}:color=${padColor}${win}`
: `scale=${rw}:${rh}:force_original_aspect_ratio=decrease:force_divisible_by=2,` +
- `pad=${W}:${H}:${rx}+(${rw}-iw)/2:${ry}+(${rh}-ih)/2:color=${padColor}`;
+ `pad=${bw}:${bh}:${rx - bx}+(${rw}-iw)/2:${ry - by}+(${rh}-ih)/2:color=${padColor}${win}`;
} else {
frame =
c.fit === "fill"
@@ -1278,10 +1286,18 @@ export async function runExport(
`[osb${k}][${seg}]overlay=x=${kpos.x}:y=${kpos.y}:eof_action=pass[osp${k}]`
);
} else {
+ // The box may reach past the frame; pad to the box holding both and
+ // crop the frame window back out (pad rejects placement outside its
+ // area).
const padX = region ? region.rx : 0;
const padY = region ? region.ry : 0;
+ const bx = Math.min(0, padX);
+ const by = Math.min(0, padY);
+ const bw = Math.max(W, padX + boxW) - bx;
+ const bh = Math.max(H, padY + boxH) - by;
+ const win = bw > W || bh > H ? `,crop=${W}:${H}:${-bx}:${-by}` : "";
filters.push(
- `[${seg}]format=rgba,pad=${W}:${H}:${padX}:${padY}:[email protected][osp${k}]`
+ `[${seg}]format=rgba,pad=${bw}:${bh}:${padX - bx}:${padY - by}:[email protected]${win}[osp${k}]`
);
}
filters.push(`[osp${k}]format=rgba,split[os0${k}][os1${k}]`);