Skip to content

Open source CapCut alternative.

TypeScript

feat(site): mask keyframes on the timeline bars

David Lecb901abfbd4e
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index 823b1280..49b09f21 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -3137,8 +3137,8 @@ function ClipView({
</DropdownMenuItem>
) : null}
</ClipMenu>
- {/* Keys sit on the bar where they fall, so a pose track is visible
- without opening the inspector. */}
+ {/* Keys sit on the bar where they fall — pose and mask tracks both —
+ so the animation is visible without opening the inspector. */}
{(clip.kf ?? []).map((k) => (
<KeyMarker
key={k.t}
@@ -3149,6 +3149,17 @@ function ClipView({
width={w}
/>
))}
+ {(clip.mask?.kf ?? []).map((k) => (
+ <KeyMarker
+ key={`m${k.t}`}
+ item={{ id: clip.id, start: span.start, end: span.start + span.len }}
+ kind="clip"
+ track="mask"
+ t={k.t}
+ pps={pps}
+ width={w}
+ />
+ ))}
<span
className={cn(trimHandle, "tl-trim-l left-0")}
onPointerDown={(e) => startLaneTrim(e, "clip", clip.id, "l", ui)}
@@ -3869,6 +3880,17 @@ function OverlayClipView({
width={w}
/>
))}
+ {(clip.mask?.kf ?? []).map((k) => (
+ <KeyMarker
+ key={`m${k.t}`}
+ item={{ id: clip.id, start: clip.start, end: clip.start + overlayLen(clip) }}
+ kind="clip"
+ track="mask"
+ t={k.t}
+ pps={pps}
+ width={w}
+ />
+ ))}
<span
className={cn(trimHandle, "tl-trim-l left-0")}
onPointerDown={(e) => startLaneTrim(e, "overlayClip", clip.id, "l", ui)}
@@ -3886,14 +3908,16 @@ function OverlayClipView({
const KEY_HIT = 14;
/**
- * One keyframe on an item's bar — an element's or a video clip's: a diamond
- * where the key falls, dragged left and right to retime it. The grab is kept
- * off the bar underneath, so moving a key never moves the item it belongs
- * to. `kind` picks which store track the actions write.
+ * One keyframe on an item's bar — an element's or a video clip's, on its
+ * pose track (white diamond) or its mask's (amber): a diamond where the key
+ * falls, dragged left and right to retime it. The grab is kept off the bar
+ * underneath, so moving a key never moves the item it belongs to. `kind`
+ * and `track` pick which store actions the gestures write.
*/
function KeyMarker({
item,
kind,
+ track = "pose",
t,
pps,
width,
@@ -3901,6 +3925,7 @@ function KeyMarker({
}: {
item: { id: string; start: number; end: number };
kind: "overlay" | "clip";
+ track?: "pose" | "mask";
t: number;
pps: number;
width: number;
@@ -3913,6 +3938,7 @@ function KeyMarker({
(s) =>
!!s.selectedKey &&
s.selectedKey.kind === kind &&
+ s.selectedKey.track === track &&
s.selectedKey.id === item.id &&
s.selectedKey.t === t
);
@@ -3939,8 +3965,23 @@ function KeyMarker({
}
onPointerDown={(e) => {
const s = useEditor.getState();
- if (kind === "overlay") s.selectOverlayKey(item.id, t);
- else s.selectClipKey(item.id, t);
+ const select =
+ kind === "overlay"
+ ? track === "mask"
+ ? s.selectOverlayMaskKey
+ : s.selectOverlayKey
+ : track === "mask"
+ ? s.selectClipMaskKey
+ : s.selectClipKey;
+ const move =
+ kind === "overlay"
+ ? track === "mask"
+ ? s.moveOverlayMaskKey
+ : s.moveOverlayKey
+ : track === "mask"
+ ? s.moveClipMaskKey
+ : s.moveClipKey;
+ select(item.id, t);
let live = t;
// The undo step opens on the first movement, so a click that only
// seeks does not leave one behind.
@@ -3952,8 +3993,7 @@ function KeyMarker({
s.pushHistory();
}
const next = Math.max(0, t + dx / pps);
- if (kind === "overlay") s.moveOverlayKey(item.id, live, next, { transient: true });
- else s.moveClipKey(item.id, live, next, { transient: true });
+ move(item.id, live, next, { transient: true });
live = Math.max(0, Math.min(next, Math.max(0.1, item.end - item.start)));
},
// A click that never moved reads as "show me this key": the playhead
@@ -3966,7 +4006,8 @@ function KeyMarker({
>
<span
className={cn(
- "pointer-events-none rotate-45 bg-white shadow-[0_0_0_1px_rgba(0,0,0,0.35)] transition-[width,height]",
+ "pointer-events-none rotate-45 shadow-[0_0_0_1px_rgba(0,0,0,0.35)] transition-[width,height]",
+ track === "mask" ? "bg-[#ff9f0a]" : "bg-white",
picked ? "size-[9px] ring-[1.5px] ring-[#0a84ff]" : "size-[6px]"
)}
/>
@@ -4094,6 +4135,9 @@ function TextBar({
{(o.kf ?? []).map((k) => (
<KeyMarker key={k.t} item={o} kind="overlay" t={k.t} pps={pps} width={w} onMenu={onMenu} />
))}
+ {(o.mask?.kf ?? []).map((k) => (
+ <KeyMarker key={`m${k.t}`} item={o} kind="overlay" track="mask" t={k.t} pps={pps} width={w} />
+ ))}
<HideChip
hidden={!!o.hidden}
small
diff --git a/site/src/cut/lib/store.test.ts b/site/src/cut/lib/store.test.ts
index 2577fa74..b240ca46 100644
--- a/site/src/cut/lib/store.test.ts
+++ b/site/src/cut/lib/store.test.ts
@@ -453,11 +453,25 @@ describe("title lanes", () => {
s().setOverlayKey(t1.id, 1);
s().selectOverlayKey(t1.id, 1);
s().moveOverlayKey(t1.id, 1, 2.5, { transient: true });
- expect(s().selectedKey).toEqual({ kind: "overlay", id: t1.id, t: 2.5 });
+ expect(s().selectedKey).toEqual({ kind: "overlay", id: t1.id, t: 2.5, track: "pose" });
s().deleteSelection();
expect(overlayById(t1.id).kf).toBe(undefined);
});
+ test("a picked mask key deletes from the mask track and leaves the pose", () => {
+ const t1 = title({ start: 0, end: 4, mask: { kind: "rect" as const } });
+ useEditor.setState({ overlays: [t1] });
+ s().setOverlayKey(t1.id, 1);
+ s().setOverlayMaskKey(t1.id, 1);
+ s().setOverlayMaskKey(t1.id, 3, { w: 0.8 });
+ s().selectOverlayMaskKey(t1.id, 3);
+ s().moveOverlayMaskKey(t1.id, 3, 2, { transient: true });
+ expect(s().selectedKey).toEqual({ kind: "overlay", id: t1.id, t: 2, track: "mask" });
+ s().deleteSelection();
+ expect(overlayById(t1.id).mask!.kf!.map((k) => k.t)).toEqual([1]);
+ expect(overlayById(t1.id).kf!.length).toBe(1); // the pose track is untouched
+ });
+
test("selecting anything else drops the pick", () => {
const t1 = title({ start: 0, end: 4 });
useEditor.setState({ overlays: [t1] });
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 0877a2f0..d30da501 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -168,10 +168,15 @@ export interface EditorState {
* inspector). Bulk actions — delete, copy — act on this whole set. */
multiSelection: Selection[];
/** The keyframe picked on a timeline bar — an element's or a video
- * clip's — if any. It rides alongside the item selection: the item is
- * still what the inspector edits, but Delete takes the key. Any other
- * selection clears it. */
- selectedKey: { kind: "overlay" | "clip"; id: string; t: number } | null;
+ * clip's, on its pose track or its mask's — if any. It rides alongside
+ * the item selection: the item is still what the inspector edits, but
+ * Delete takes the key. Any other selection clears it. */
+ selectedKey: {
+ kind: "overlay" | "clip";
+ id: string;
+ t: number;
+ track: "pose" | "mask";
+ } | null;
currentTime: number;
playing: boolean;
/** While playing a scoped effect preview, the time playback auto-pauses at;
@@ -358,6 +363,13 @@ export interface EditorState {
opts?: { transient?: boolean }
) => void;
removeOverlayMaskKey: (id: string, tLocal: number) => void;
+ selectOverlayMaskKey: (id: string, tLocal: number) => void;
+ moveOverlayMaskKey: (
+ id: string,
+ fromT: number,
+ toT: number,
+ opts?: { transient?: boolean }
+ ) => void;
clearOverlayMaskKeys: (id: string) => void;
/** The same mask-key track on a video clip; `tLocal` is seconds from the
* clip's timeline start. */
@@ -368,6 +380,13 @@ export interface EditorState {
opts?: { transient?: boolean }
) => void;
removeClipMaskKey: (id: string, tLocal: number) => void;
+ selectClipMaskKey: (id: string, tLocal: number) => void;
+ moveClipMaskKey: (
+ id: string,
+ fromT: number,
+ toT: number,
+ opts?: { transient?: boolean }
+ ) => void;
clearClipMaskKeys: (id: string) => void;
/** The pose-key track on a video clip, the overlay contract over the
* clip's anchor: adding a key captures the clip's pose at that moment,
@@ -2156,7 +2175,7 @@ export const useEditor = create<EditorState>((baseSet, get) => {
const o = get().overlays.find((x) => x.id === id);
if (!o) return;
get().select({ kind: "overlay", id });
- set({ selectedKey: { kind: "overlay", id, t: tLocal } });
+ set({ selectedKey: { kind: "overlay", id, t: tLocal, track: "pose" } });
},
moveOverlayKey: (id, fromT, toT, opts) => {
@@ -2174,10 +2193,11 @@ export const useEditor = create<EditorState>((baseSet, get) => {
if (
picked &&
picked.kind === "overlay" &&
+ picked.track === "pose" &&
picked.id === id &&
Math.abs(picked.t - fromT) <= KEY_EPSILON
) {
- set({ selectedKey: { kind: "overlay", id, t } });
+ set({ selectedKey: { kind: "overlay", id, t, track: "pose" } });
}
},
@@ -2211,6 +2231,36 @@ export const useEditor = create<EditorState>((baseSet, get) => {
get().updateOverlayTransient(id, { mask: { ...o.mask, kf: kf.length ? kf : undefined } });
},
+ selectOverlayMaskKey: (id, tLocal) => {
+ const o = get().overlays.find((x) => x.id === id);
+ if (!o) return;
+ get().select({ kind: "overlay", id });
+ set({ selectedKey: { kind: "overlay", id, t: tLocal, track: "mask" } });
+ },
+
+ moveOverlayMaskKey: (id, fromT, toT, opts) => {
+ const o = get().overlays.find((x) => x.id === id);
+ if (!o?.mask?.kf?.length) return;
+ const key = o.mask.kf.find((k) => Math.abs(k.t - fromT) <= KEY_EPSILON);
+ if (!key) return;
+ const t = Math.max(0, Math.min(toT, Math.max(0.1, o.end - o.start)));
+ if (!opts?.transient) push();
+ get().updateOverlayTransient(id, {
+ mask: { ...o.mask, kf: upsertKey(removeKeyAt(o.mask.kf, fromT), { ...key, t }) },
+ });
+ // The pick follows the key it is on, so dragging never drops it.
+ const picked = get().selectedKey;
+ if (
+ picked &&
+ picked.kind === "overlay" &&
+ picked.track === "mask" &&
+ picked.id === id &&
+ Math.abs(picked.t - fromT) <= KEY_EPSILON
+ ) {
+ set({ selectedKey: { kind: "overlay", id, t, track: "mask" } });
+ }
+ },
+
clearOverlayMaskKeys: (id) => {
const o = get().overlays.find((x) => x.id === id);
if (!o?.mask?.kf?.length) return;
@@ -2238,6 +2288,36 @@ export const useEditor = create<EditorState>((baseSet, get) => {
get().updateClipTransient(id, { mask: { ...c.mask, kf: kf.length ? kf : undefined } });
},
+ selectClipMaskKey: (id, tLocal) => {
+ const c = get().clips.find((x) => x.id === id);
+ if (!c) return;
+ get().select({ kind: "clip", id });
+ set({ selectedKey: { kind: "clip", id, t: tLocal, track: "mask" } });
+ },
+
+ moveClipMaskKey: (id, fromT, toT, opts) => {
+ const c = get().clips.find((x) => x.id === id);
+ if (!c?.mask?.kf?.length) return;
+ const key = c.mask.kf.find((k) => Math.abs(k.t - fromT) <= KEY_EPSILON);
+ if (!key) return;
+ const t = Math.max(0, Math.min(toT, clipLen(c)));
+ if (!opts?.transient) push();
+ get().updateClipTransient(id, {
+ mask: { ...c.mask, kf: upsertKey(removeKeyAt(c.mask.kf, fromT), { ...key, t }) },
+ });
+ // The pick follows the key it is on, so dragging never drops it.
+ const picked = get().selectedKey;
+ if (
+ picked &&
+ picked.kind === "clip" &&
+ picked.track === "mask" &&
+ picked.id === id &&
+ Math.abs(picked.t - fromT) <= KEY_EPSILON
+ ) {
+ set({ selectedKey: { kind: "clip", id, t, track: "mask" } });
+ }
+ },
+
clearClipMaskKeys: (id) => {
const c = get().clips.find((x) => x.id === id);
if (!c?.mask?.kf?.length) return;
@@ -2272,7 +2352,7 @@ export const useEditor = create<EditorState>((baseSet, get) => {
const c = get().clips.find((x) => x.id === id);
if (!c) return;
get().select({ kind: "clip", id });
- set({ selectedKey: { kind: "clip", id, t: tLocal } });
+ set({ selectedKey: { kind: "clip", id, t: tLocal, track: "pose" } });
},
moveClipKey: (id, fromT, toT, opts) => {
@@ -2288,10 +2368,11 @@ export const useEditor = create<EditorState>((baseSet, get) => {
if (
picked &&
picked.kind === "clip" &&
+ picked.track === "pose" &&
picked.id === id &&
Math.abs(picked.t - fromT) <= KEY_EPSILON
) {
- set({ selectedKey: { kind: "clip", id, t } });
+ set({ selectedKey: { kind: "clip", id, t, track: "pose" } });
}
},
@@ -2654,8 +2735,12 @@ export const useEditor = create<EditorState>((baseSet, get) => {
key.kind === "overlay"
? st.overlays.find((x) => x.id === key.id)
: st.clips.find((x) => x.id === key.id);
- if (item?.kf?.some((k) => Math.abs(k.t - key.t) <= KEY_EPSILON)) {
- if (key.kind === "overlay") st.removeOverlayKey(key.id, key.t);
+ const keys = key.track === "mask" ? item?.mask?.kf : item?.kf;
+ if (keys?.some((k) => Math.abs(k.t - key.t) <= KEY_EPSILON)) {
+ if (key.kind === "overlay") {
+ if (key.track === "mask") st.removeOverlayMaskKey(key.id, key.t);
+ else st.removeOverlayKey(key.id, key.t);
+ } else if (key.track === "mask") st.removeClipMaskKey(key.id, key.t);
else st.removeClipKey(key.id, key.t);
return;
}