Skip to content

Open source CapCut alternative.

TypeScript

feat(site): a circle mask is a perfect circle

David Le5a067b35dd9d
diff --git a/site/src/cut/components/Inspector.tools.ts b/site/src/cut/components/Inspector.tools.ts
index 6fd957b8..751d0d91 100644
--- a/site/src/cut/components/Inspector.tools.ts
+++ b/site/src/cut/components/Inspector.tools.ts
@@ -46,7 +46,7 @@ export const INSPECTOR_TOOLS = [
{
name: "set_mask",
description:
- "Mask an overlay element or a video clip — trim its picture to a shape or to the person in the shot. Kinds: rect (rounded box), square (rounded square, side = w of the frame width), circle (ellipse), linear (half-plane: at rotation 0 the top side stays), mirror (band across the item), subject (the person matte: plain keeps the picture only on the person; invert keeps it off the person — behind the speaker). Position is the mask center's offset from the item's own center (a clip's region center), in frame fractions; w/h size it in frame fractions; feather softens the edge; invert keeps what the shape leaves out. Subject masks use only feather and invert. Pass kind \"none\" to remove the mask. `keys` animates the geometry over time — each key is the full geometry at a moment, moving linearly between keys; pass an empty list to clear the keys and keep the mask still.",
+ "Mask an overlay element or a video clip — trim its picture to a shape or to the person in the shot. Kinds: rect (rounded box), square (rounded square, side = w of the frame width), circle (ellipse; w and h are frame fractions, so a perfect circle needs w × frame width = h × frame height — omitting h gives you that circle), linear (half-plane: at rotation 0 the top side stays), mirror (band across the item), subject (the person matte: plain keeps the picture only on the person; invert keeps it off the person — behind the speaker). Position is the mask center's offset from the item's own center (a clip's region center), in frame fractions; w/h size it in frame fractions; feather softens the edge; invert keeps what the shape leaves out. Subject masks use only feather and invert. Pass kind \"none\" to remove the mask. `keys` animates the geometry over time — each key is the full geometry at a moment, moving linearly between keys; pass an empty list to clear the keys and keep the mask still.",
inputSchema: obj({
id: str("Overlay element id or video clip id"),
kind: {
diff --git a/site/src/cut/components/Inspector.tsx b/site/src/cut/components/Inspector.tsx
index 9a115a64..430871c4 100644
--- a/site/src/cut/components/Inspector.tsx
+++ b/site/src/cut/components/Inspector.tsx
@@ -2880,7 +2880,20 @@ function MaskSection({ target }: { target: MaskTarget }) {
<Row label="Shape">
<Select
value={m.kind}
- onValueChange={(kind) => target.set({ ...m, kind: kind as MaskKind })}
+ onValueChange={(kind) => {
+ // A fresh circle starts perfectly round: w and h are frame
+ // fractions, so equal pixels means unequal fractions. ⇧-drag
+ // on the stage keeps it round through resizes.
+ const patch =
+ kind === "circle" && !hasMaskKeys(m)
+ ? (() => {
+ const fr = frameOf(useEditor.getState().aspect);
+ const w = m.w ?? 0.5;
+ return { w, h: (w * fr.w) / fr.h };
+ })()
+ : {};
+ target.set({ ...m, ...patch, kind: kind as MaskKind });
+ }}
>
<SelectTrigger className="h-8 w-36 text-[12px]">
<SelectValue />
diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsx
index 67bfeb2a..f2504599 100644
--- a/site/src/cut/components/OverlayLayer.tsx
+++ b/site/src/cut/components/OverlayLayer.tsx
@@ -1312,13 +1312,33 @@ export function MaskGizmoCore({
const a = HANDLE_AXIS[handle];
const g0 = f;
startDrag(e, {
- onMove: (dx, dy) => {
+ 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;
- const nw = clampSize(snapTo(g0.w + (grow * mx * a.x) / stageWidth, 1));
- const nh = clampSize(snapTo(g0.h + (grow * my * a.y) / stageHeight, 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);
@@ -1339,11 +1359,11 @@ export function MaskGizmoCore({
? { w: nw, ...shift }
: m.kind === "mirror"
? { h: nh }
- : a.y === 0
- ? { w: nw, ...shift }
- : a.x === 0
- ? { h: nh, ...shift }
- : { w: nw, h: nh, ...shift }
+ : constrained || (a.x !== 0 && a.y !== 0)
+ ? { w: nw, h: nh, ...shift }
+ : a.y === 0
+ ? { w: nw, ...shift }
+ : { h: nh, ...shift }
);
},
onUp: () => setGuide(null),
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index 5e673a70..4f63cf73 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -506,6 +506,12 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {
...(typeof input.invert === "boolean" ? { invert: input.invert || undefined } : {}),
...(isNum(input.radius) ? { radius: clamp(input.radius, 0, 400) || undefined } : {}),
};
+ // A circle with no explicit h is a perfect circle: w and h are fractions
+ // of different frame edges, so round means unequal fractions.
+ if (mask.kind === "circle" && !isNum(input.h)) {
+ const fr = frameOf(s.aspect);
+ mask.h = clamp(((mask.w ?? 0.5) * fr.w) / fr.h, 0.01, 2);
+ }
setMask(mask);
// Keys layer on after the mask lands, so each one captures against the
// geometry the call just set.