Open source CapCut alternative.
TypeScript
feat(site): mask outline, clip gizmo, and drag-anywhere move
David Leeccb6f14543a
diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsxindex a92c8232..7a3f6e5c 100644--- a/site/src/cut/components/OverlayLayer.tsx+++ b/site/src/cut/components/OverlayLayer.tsx@@ -14,7 +14,7 @@ import {subtitleLaneCount,trackPos,} from "@/cut/lib/subtitles";-import { evalOverlayFrame, hasMaskKeys, hasOverlayKeys, isOverlayAnimated, lineLikeShape, maskFrameAt, paintMaskCoverage, resolveShadow, shapeMetrics, shapePathD, type LottieHandle, type MaskKey } from "@donkeycut/effects-kit";+import { evalOverlayFrame, hasMaskKeys, hasOverlayKeys, isOverlayAnimated, lineLikeShape, maskFrameAt, paintMaskCoverage, resolveShadow, shapeMetrics, shapePathD, type LottieHandle, type Mask, type MaskKey } from "@donkeycut/effects-kit";import {LINE_HEIGHT,PLATE_PAD_X,@@ -964,37 +964,35 @@ const MASK_GRIP ="absolute z-10 rounded-full border-[2.5px] border-[#ff9f0a] bg-white shadow-[0_1px_4px_rgba(0,0,0,0.4)]";/**- * On-canvas mask editing: a center grip that moves the mask and a corner grip- * that resizes it (the band height, for mirror). The grips are children of- * the element box, so they ride its transform; screen deltas are folded back- * through the element's live rotation and scale, then through the mask's own- * angle for the resize. With mask keys in play a drag writes the key at the- * playhead, the same contract as dragging a keyframed element.+ * On-canvas mask editing, shared by elements and video clips: an amber+ * outline tracing the mask's edge, a center grip that moves it and a corner+ * grip that resizes it (the band height, for mirror). Coordinates are local+ * px around the layer's anchor — the mount point carries the layer's own+ * transform — and screen deltas are folded back through that transform via+ * `rotation`/`poseScale`, then through the mask's own angle for the resize.+ * With mask keys in play a drag writes the key at the playhead, the same+ * contract as dragging a keyframed element.*/-function MaskGizmo({- overlay: o,+export function MaskGizmoCore({+ mask: m,stageWidth,stageHeight,tLocal,rotation,poseScale,+ writeGeom,+ begin,}: {- overlay: Overlay;+ mask: Mask;stageWidth: number;stageHeight: number;tLocal: number;rotation: number;poseScale: number;+ writeGeom: (patch: Partial<Omit<MaskKey, "t">>) => void;+ begin: () => void;}) {- const m = o.mask!;const f = maskFrameAt(m, tLocal);- const st = () => useEditor.getState();- const writeGeom = (patch: Partial<Omit<MaskKey, "t">>) => {- const cur = st().overlays.find((x) => x.id === o.id)?.mask;- if (!cur) return;- if (hasMaskKeys(cur)) return st().setOverlayMaskKey(o.id, tLocal, patch, { transient: true });- st().updateOverlayTransient(o.id, { mask: { ...cur, ...patch } });- };// Screen deltas → the box's local space (undo the element transform).const toLocal = (dx: number, dy: number) => {const r = (-rotation * Math.PI) / 180;@@ -1005,16 +1003,90 @@ function MaskGizmo({};};const theta = (f.rotation * Math.PI) / 180;- const sizable = m.kind === "rect" || m.kind === "circle" || m.kind === "mirror";+ const sizable =+ m.kind === "rect" || m.kind === "square" || m.kind === "circle" || m.kind === "mirror";+ // The outline traces the mask's hard edge in local px; a square's side is+ // `w` of the frame width on both axes (the painter's rule), and+ // linear/mirror edges run wider than any frame so they always cross it.+ const cx = f.x * stageWidth;+ const cy = f.y * stageHeight;+ const w = f.w * stageWidth;+ const h = m.kind === "square" ? w : f.h * stageHeight;+ const span = stageWidth + stageHeight;// The resize grip sits on the mask's corner (mirror: its lower edge),// turned by the mask's own angle.- const gx = m.kind === "mirror" ? 0 : (f.w * stageWidth) / 2;- const gy = (f.h * stageHeight) / 2;+ const gx = m.kind === "mirror" ? 0 : w / 2;+ const gy = h / 2;const rx = gx * Math.cos(theta) - gy * Math.sin(theta);const ry = gx * Math.sin(theta) + gy * Math.cos(theta);const clampSize = (v: number) => Math.min(2, Math.max(0.01, v));+ const designScale = Math.min(stageWidth, stageHeight) / 1080;+ const edge = {+ className: "pointer-events-none",+ fill: "none" as const,+ stroke: "#ff9f0a",+ strokeWidth: 1.5,+ strokeDasharray: "6 4",+ vectorEffect: "non-scaling-stroke" as const,+ };+ const beginMove = (e: React.PointerEvent) => {+ e.stopPropagation();+ begin();+ const g0 = f;+ startDrag(e, {+ onMove: (dx, dy) => {+ const l = toLocal(dx, dy);+ writeGeom({ x: g0.x + l.x / stageWidth, y: g0.y + l.y / stageHeight });+ },+ });+ };+ // The whole interior moves the mask, so a drag can start anywhere inside+ // the outline; linear/mirror get a grabbable strip along their edge line.+ const grabW = m.kind === "linear" || m.kind === "mirror" ? span * 2 : w;+ const grabH = m.kind === "linear" ? 28 : m.kind === "mirror" ? Math.max(h, 28) : h;return (<>+ <div+ className="absolute cursor-move"+ style={{+ left: `calc(50% + ${cx}px)`,+ top: `calc(50% + ${cy}px)`,+ width: grabW,+ height: grabH,+ transform: `translate(-50%, -50%) rotate(${f.rotation}deg)`,+ borderRadius:+ m.kind === "circle" ? "50%" : (m.radius ?? 0) * designScale,+ }}+ onPointerDown={beginMove}+ />+ <svg+ className="pointer-events-none absolute overflow-visible"+ width={1}+ height={1}+ style={{ left: `calc(50% + ${cx}px)`, top: `calc(50% + ${cy}px)` }}+ >+ <g transform={`rotate(${f.rotation})`}>+ {m.kind === "rect" || m.kind === "square" ? (+ <rect+ x={-w / 2}+ y={-h / 2}+ width={w}+ height={h}+ rx={(m.radius ?? 0) * designScale}+ {...edge}+ />+ ) : m.kind === "circle" ? (+ <ellipse cx={0} cy={0} rx={w / 2} ry={h / 2} {...edge} />+ ) : m.kind === "linear" ? (+ <line x1={-span} y1={0} x2={span} y2={0} {...edge} />+ ) : m.kind === "mirror" ? (+ <>+ <line x1={-span} y1={-h / 2} x2={span} y2={-h / 2} {...edge} />+ <line x1={-span} y1={h / 2} x2={span} y2={h / 2} {...edge} />+ </>+ ) : null}+ </g>+ </svg><spantitle="Drag to move the mask"className={cn(MASK_GRIP, "size-[13px] cursor-move")}@@ -1023,17 +1095,7 @@ function MaskGizmo({top: `calc(50% + ${f.y * stageHeight}px)`,transform: "translate(-50%, -50%)",}}- onPointerDown={(e) => {- e.stopPropagation();- st().pushHistory();- const g0 = f;- startDrag(e, {- onMove: (dx, dy) => {- const l = toLocal(dx, dy);- writeGeom({ x: g0.x + l.x / stageWidth, y: g0.y + l.y / stageHeight });- },- });- }}+ onPointerDown={beginMove}/>{sizable && (<span@@ -1046,20 +1108,25 @@ function MaskGizmo({}}onPointerDown={(e) => {e.stopPropagation();- st().pushHistory();+ begin();const g0 = f;startDrag(e, {onMove: (dx, dy) => {const l = toLocal(dx, dy);- // Into the mask's own axes for width/height growth.+ // Into the mask's own axes for width/height growth; a square+ // has one side and a mirror band one height.const mx = l.x * Math.cos(theta) + l.y * Math.sin(theta);const my = -l.x * Math.sin(theta) + l.y * Math.cos(theta);- writeGeom({- ...(m.kind !== "mirror"+ writeGeom(+ m.kind === "square"? { w: clampSize(g0.w + (2 * mx) / stageWidth) }- : {}),- h: clampSize(g0.h + (2 * my) / stageHeight),- });+ : m.kind === "mirror"+ ? { h: clampSize(g0.h + (2 * my) / stageHeight) }+ : {+ w: clampSize(g0.w + (2 * mx) / stageWidth),+ h: clampSize(g0.h + (2 * my) / stageHeight),+ }+ );},});}}@@ -1069,6 +1136,45 @@ function MaskGizmo({);}+/** The element mount: grips are children of the element box, so they ride+ * its transform; writes land on the overlay's mask (or its key at the+ * playhead). */+function MaskGizmo({+ overlay: o,+ stageWidth,+ stageHeight,+ tLocal,+ rotation,+ poseScale,+}: {+ overlay: Overlay;+ stageWidth: number;+ stageHeight: number;+ tLocal: number;+ rotation: number;+ poseScale: number;+}) {+ const st = () => useEditor.getState();+ const writeGeom = (patch: Partial<Omit<MaskKey, "t">>) => {+ const cur = st().overlays.find((x) => x.id === o.id)?.mask;+ if (!cur) return;+ if (hasMaskKeys(cur)) return st().setOverlayMaskKey(o.id, tLocal, patch, { transient: true });+ st().updateOverlayTransient(o.id, { mask: { ...cur, ...patch } });+ };+ return (+ <MaskGizmoCore+ mask={o.mask!}+ stageWidth={stageWidth}+ stageHeight={stageHeight}+ tLocal={tLocal}+ rotation={rotation}+ poseScale={poseScale}+ writeGeom={writeGeom}+ begin={() => st().pushHistory()}+ />+ );+}+/** A shape drawn as inline SVG with the painter's own pixel geometry* (`shapeMetrics` in stage space), so preview and export burn-in match. */function ShapeView({diff --git a/site/src/cut/components/Preview.tsx b/site/src/cut/components/Preview.tsxindex 963f8ca6..4c21febe 100644--- a/site/src/cut/components/Preview.tsx+++ b/site/src/cut/components/Preview.tsx@@ -12,9 +12,10 @@ import {readPoster,} from "@/cut/lib/posterCache";import { setPreviewCanvas } from "@/cut/lib/previewCanvas";-import { frameOf, isFullRect, rectOf, type Aspect, type ClipSpan, type FrameRect, type MediaAsset, type VideoClip } from "@/cut/lib/types";+import { clipKeyed, clipPoseAt, frameOf, isFullRect, rectOf, 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 { OverlayLayer } from "./OverlayLayer";+import { MaskGizmoCore, OverlayLayer } from "./OverlayLayer";import { StageEffectPaint, stageSlices, useEffectLanes, useStageEffects } from "./StageEffects";/** The clip under the playhead, when it overflows the frame in fill mode. */@@ -219,6 +220,7 @@ export function Preview() {onDragEnd={clearAssetDrag}/><OverlayPipHandle stage={stage} />+ <ClipMaskGizmo stage={stage} />{slices.map((slice) =>slice.kind === "elements" ? (<OverlayLayer@@ -316,3 +318,60 @@ function OverlayPipHandle({ stage }: { stage: { w: number; h: number } }) {</div>);}++/**+ * The selected video clip's mask on the stage: the shared gizmo mounted at+ * the mask's anchor — the clip rect's center, carried through the clip's+ * pose so the outline sits where the compositor draws. Shows while the clip+ * is live under the playhead; the anchor point is a zero-size box, so the+ * gizmo's center-relative coordinates measure from it directly.+ */+function ClipMaskGizmo({ stage }: { stage: { w: number; h: number } }) {+ const selection = useEditor((s) => s.selection);+ const clips = useEditor((s) => s.clips);+ const currentTime = useEditor((s) => s.currentTime);+ const skimTime = useEditor((s) => s.skimTime);+ const playing = useEditor((s) => s.playing);+ if (!playing && skimTime !== null) return null;+ if (selection?.kind !== "clip") return null;+ const clip = clips.find((c) => c.id === selection.id);+ if (!clip?.mask || clip.mask.kind === "subject" || clip.hidden) return null;+ const speed = clip.speed && clip.speed > 0 ? clip.speed : 1;+ const len = Math.max(0.1, (clip.out - clip.in) / speed);+ if (currentTime < clip.start || currentTime >= clip.start + len) return null;+ const tLocal = currentTime - clip.start;+ const rect = rectOf(clip);+ const pose = clipKeyed(clip) ? clipPoseAt(clip, tLocal) : null;+ const ax = pose ? pose.x : rect.x + rect.w / 2;+ const ay = pose ? pose.y : rect.y + rect.h / 2;+ const writeGeom = (patch: Partial<Omit<MaskKey, "t">>) => {+ const st = useEditor.getState();+ const cur = st.clips.find((c) => c.id === clip.id)?.mask;+ if (!cur) return;+ if (hasMaskKeys(cur)) return st.setClipMaskKey(clip.id, tLocal, patch, { transient: true });+ st.updateClipTransient(clip.id, { mask: { ...cur, ...patch } });+ };+ return (+ <div+ className="absolute"+ style={{+ left: ax * stage.w,+ top: ay * stage.h,+ width: 0,+ height: 0,+ transform: pose ? `rotate(${pose.rotation}deg) scale(${pose.scale})` : undefined,+ }}+ >+ <MaskGizmoCore+ mask={clip.mask}+ stageWidth={stage.w}+ stageHeight={stage.h}+ tLocal={tLocal}+ rotation={pose?.rotation ?? 0}+ poseScale={pose?.scale ?? 1}+ writeGeom={writeGeom}+ begin={() => useEditor.getState().pushHistory()}+ />+ </div>+ );+}