Skip to content

The VS Code of video editing, built for agents.

TypeScript

Implement Object Mask Tool Integration and Refactor Logic

konstantin-paulus95b953171932
diff --git a/apps/web/src/components/canvas/object-mask-bar.tsx b/apps/web/src/components/canvas/object-mask-bar.tsx
index 8ae83798..ccd030e0 100644
--- a/apps/web/src/components/canvas/object-mask-bar.tsx
+++ b/apps/web/src/components/canvas/object-mask-bar.tsx
@@ -21,22 +21,16 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { cx } from "@/lib/cva";
-import { useDerived } from "@/engine/hooks";
+import { useDerived, useObjectMaskTool } from "@/engine/hooks";
import {
- brushRadius,
cancelObjectMask,
getMaskRestore,
getObjectTrack,
heldObjectMaskOp,
- objectMaskMode,
objectMaskModel,
objectMaskModelLoad,
- objectMaskOp,
pickObjectMaskModel,
preloadObjectMaskModel,
- setBrushRadius,
- setObjectMaskMode,
- setObjectMaskOp,
trackObjectMask,
} from "@/engine/object-mask";
@@ -86,6 +80,7 @@ type SessionState = {
*/
export function ObjectMaskBar() {
const world = useWorld();
+ const tool = useObjectMaskTool();
// The model is fetched as the tool opens, so it is ready by the first click.
onMount(preloadObjectMaskModel);
@@ -122,7 +117,7 @@ export function ObjectMaskBar() {
<ModelMenu disabled={busy()} />
<Separator orientation="vertical" class="min-h-5" />
<ModePopover />
- <OpMenu disabled={objectMaskMode() === "brush"} />
+ <OpMenu disabled={tool.mode() === "brush"} />
<Separator orientation="vertical" class="min-h-5" />
<Tooltip>
<TooltipTrigger as={Button} variant="ghost" class="text-muted-foreground" onClick={() => cancelObjectMask(world)}>
@@ -247,7 +242,8 @@ function ModelHint(props: { row: HTMLElement; children: JSX.Element }) {
/** Points or the brush, and the brush's size. */
function ModePopover() {
- const mode = () => MODES.find((entry) => entry.value === objectMaskMode()) ?? MODES[0];
+ const tool = useObjectMaskTool();
+ const mode = () => MODES.find((entry) => entry.value === tool.mode()) ?? MODES[0];
return (
<Popover placement="top-start" gutter={8}>
@@ -274,14 +270,14 @@ function ModePopover() {
onOpenAutoFocus={(event) => event.preventDefault()}
>
<MenuHeader>Tool</MenuHeader>
- <SegmentedIconTabs value={objectMaskMode} onChange={setObjectMaskMode} items={MODE_TABS} class="my-0.5" />
- <Show when={objectMaskMode() === "brush"}>
+ <SegmentedIconTabs value={tool.mode} onChange={(mode) => tool.set({ mode })} items={MODE_TABS} class="my-0.5" />
+ <Show when={mode().value === "brush"}>
<div class="pt-2 pb-0.5">
<SliderInput
- value={Math.round(brushRadius() * 200)}
+ value={Math.round(tool.brushRadius() * 200)}
min={BRUSH_SIZE.min}
max={BRUSH_SIZE.max}
- onChange={(value) => setBrushRadius(Math.min(BRUSH_SIZE.max, Math.max(BRUSH_SIZE.min, value)) / 200)}
+ onChange={(value) => tool.set({ brushRadius: Math.min(BRUSH_SIZE.max, Math.max(BRUSH_SIZE.min, value)) / 200 })}
format={(value) => `${value}%`}
/>
</div>
@@ -295,6 +291,7 @@ function ModePopover() {
/** Whether clicks add to the object or subtract from it; the button shows the op alt swaps in while held. */
function OpMenu(props: { disabled: boolean }) {
const world = useWorld();
+ const tool = useObjectMaskTool();
const held = useDerived(() => heldObjectMaskOp(world));
const op = () => (props.disabled ? OPS[0] : OPS.find((entry) => entry.value === held()) ?? OPS[0]);
@@ -323,7 +320,7 @@ function OpMenu(props: { disabled: boolean }) {
<div class="flex flex-col gap-1 py-0.5">
<For each={OPS}>
{(entry) => (
- <CheckedItem checked={entry.value === objectMaskOp()} onSelect={() => setObjectMaskOp(entry.value)}>
+ <CheckedItem checked={entry.value === tool.op()} onSelect={() => tool.set({ op: entry.value })}>
{entry.label}
</CheckedItem>
)}
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.ts
index 4ca9c2aa..40c28b0a 100644
--- a/apps/web/src/engine/create-engine.ts
+++ b/apps/web/src/engine/create-engine.ts
@@ -5,7 +5,7 @@
import { assetSystem, renderSystem, transformSystem, playbackSystem, motionSystem, AudioEngine, createRuntimeWorld, Geometry, Mode, RenderSurface, Time, ChildOf, syncInteractiveState } from '@diffusionstudio/runtime';
import { hudSystem } from './hud';
import { createSignal, type Accessor, type Setter } from 'solid-js';
-import { AssetSelection, Hud, Keys, MODIFIER_KEYS, Pointer, PointerEvents, ProjectConfig, SnapLines } from './traits';
+import { AssetSelection, Hud, Keys, MODIFIER_KEYS, ObjectMaskTool, Pointer, PointerEvents, ProjectConfig, SnapLines } from './traits';
import { inputSystem } from './input/input-system';
import { clearClipFrames, clearClipPeaks, clearMedia, clearPeaks, timelineSystem, TimelineSurface } from './timeline';
import { clearObjectTrackOf, clearObjectTracks } from './object-mask';
@@ -49,7 +49,7 @@ class Engine {
public constructor(projectId: string, options: EngineOptions = {}) {
this.world = createRuntimeWorld(projectId);
- this.world.add(Pointer, Keys, SnapLines, Hud, PointerEvents, AssetSelection, ProjectConfig, TimelineSurface);
+ this.world.add(Pointer, Keys, SnapLines, Hud, ObjectMaskTool, PointerEvents, AssetSelection, ProjectConfig, TimelineSurface);
this.unsubscribe.push(
this.world.onAdd(ChildOf('*'), () => (this.interactiveDirty = true)),
diff --git a/apps/web/src/engine/hooks/index.ts b/apps/web/src/engine/hooks/index.ts
index b8c0d6a7..88b394f8 100644
--- a/apps/web/src/engine/hooks/index.ts
+++ b/apps/web/src/engine/hooks/index.ts
@@ -10,3 +10,4 @@ export * from './use-selection';
export * from './use-asset-selection';
export * from './use-derived';
export * from './use-timeline-index';
+export * from './use-object-mask-tool';
diff --git a/apps/web/src/engine/hooks/use-object-mask-tool.ts b/apps/web/src/engine/hooks/use-object-mask-tool.ts
new file mode 100644
index 00000000..092dbeb7
--- /dev/null
+++ b/apps/web/src/engine/hooks/use-object-mask-tool.ts
@@ -0,0 +1,25 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { useTrait, useWorld } from '@diffusionstudio/koota-solid';
+
+import { ObjectMaskTool } from '../traits';
+
+import type { ObjectMaskMode, ObjectMaskOp } from '../object-mask/store';
+
+type ObjectMaskToolSettings = { mode: ObjectMaskMode; op: ObjectMaskOp; brushRadius: number };
+
+/** Reactive read and write of the ObjectMaskTool trait: how the object mask tool prompts, and the brush's size. */
+export function useObjectMaskTool() {
+ const world = useWorld();
+ const tool = useTrait(world, ObjectMaskTool);
+
+ const mode = (): ObjectMaskMode => tool()?.mode ?? 'points';
+ const op = (): ObjectMaskOp => tool()?.op ?? 'add';
+ const brushRadius = () => tool()?.brushRadius ?? 0;
+
+ const set = (next: Partial<ObjectMaskToolSettings>) => world.set(ObjectMaskTool, next);
+
+ return { mode, op, brushRadius, set };
+}
diff --git a/apps/web/src/engine/input/cursor.ts b/apps/web/src/engine/input/cursor.ts
index a909260d..577de4da 100644
--- a/apps/web/src/engine/input/cursor.ts
+++ b/apps/web/src/engine/input/cursor.ts
@@ -15,13 +15,15 @@
import { RenderSurface, Tool, ToolType } from '@diffusionstudio/runtime';
+import { ObjectMaskTool } from '../traits';
+
import type { World } from 'koota';
const CORNER_SVG = `<path d='m19.7432 17.0869-4.072 4.068 2.829 2.828-8.473-.013-.013-8.47 2.841 2.842 4.075-4.068 1.414-1.415-2.844-2.842h8.486v8.484l-2.83-2.827z' fill='%23fff'/><path d='m18.6826 16.7334-4.427 4.424 1.828 1.828-5.056-.016-.014-5.054 1.842 1.841 4.428-4.422 2.474-2.475-1.844-1.843h5.073v5.071l-1.83-1.828z' fill='%23000'/>`;
const EDGE_SVG = `<path d='m9 17.9907v.005l5.997 5.996.001-3.999h1.999 2.02v4l5.98-6.001-5.98-5.999.001 4.019-2.021.002h-2l.001-4.022zm1.411.003 3.587-3.588-.001 2.587h3.5 2.521v-2.585l3.565 3.586-3.564 3.585-.001-2.585h-2.521l-3.499-.001-.001 2.586z' fill='%23fff'/><path d='m17.4971 18.9932h2.521v2.586l3.565-3.586-3.565-3.585v2.605h-2.521-3.5v-2.607l-3.586 3.587 3.586 3.586v-2.587z' fill='%23000'/>`;
const ROTATE_SVG = `<path d="M22.4789 9.45728L25.9935 12.9942L22.4789 16.5283V14.1032C18.126 14.1502 14.6071 17.6737 14.5675 22.0283H17.05L13.513 25.543L9.97889 22.0283H12.5674C12.6071 16.5691 17.0214 12.1503 22.4789 12.1031L22.4789 9.45728Z" fill="black"/><path fill-rule="evenodd" clip-rule="evenodd" d="M21.4789 7.03223L27.4035 12.9945L21.4789 18.9521V15.1868C18.4798 15.6549 16.1113 18.0273 15.649 21.0284H19.475L13.5128 26.953L7.55519 21.0284H11.6189C12.1243 15.8155 16.2679 11.6677 21.4789 11.1559L21.4789 7.03223ZM22.4789 12.1031C17.0214 12.1503 12.6071 16.5691 12.5674 22.0284H9.97889L13.513 25.543L17.05 22.0284H14.5675C14.5705 21.6896 14.5947 21.3558 14.6386 21.0284C15.1157 17.4741 17.9266 14.6592 21.4789 14.1761C21.8063 14.1316 22.1401 14.1069 22.4789 14.1032V16.5284L25.9935 12.9942L22.4789 9.45729L22.4789 12.1031Z" fill="white"/>`;
-const STATIC_CURSORS = new Set(['default', 'pointer', 'crosshair', 'move', 'grab', 'grabbing', 'text', 'zoom-in', 'zoom-out']);
+const STATIC_CURSORS = new Set(['default', 'pointer', 'crosshair', 'move', 'grab', 'grabbing', 'text', 'zoom-in', 'zoom-out', 'none']);
function getCursorCss(svg: string, rotation: number, turn: number, color: string, hx = 16, hy = 16): string {
// Keep the drop shadow pointing the same way whatever the cursor's own
@@ -46,7 +48,7 @@ const CURSORS: Record<string, (rotation: number, color: string) => string> = {
export type CursorType =
| 'default' | 'pointer' | 'crosshair' | 'grab' | 'grabbing' | 'move' | 'text'
- | 'zoom-in' | 'zoom-out'
+ | 'zoom-in' | 'zoom-out' | 'none'
| 'ew-resize' | 'ns-resize' | 'nesw-resize' | 'nwse-resize'
| 'nesw-rotate' | 'nwse-rotate' | 'swne-rotate' | 'senw-rotate';
@@ -92,8 +94,10 @@ const TOOL_CURSORS: Record<ToolType, { idle: CursorType; pressed?: CursorType }>
[ToolType.OBJECT_MASK]: { idle: 'crosshair' },
};
-/** The cursor the armed tool asks for, pressed or at rest. */
+/** The cursor the armed tool asks for, pressed or at rest. The object mask's brush draws its own ring, so it hides the cursor. */
export function getToolCursor(world: World, pressed = false): CursorType {
- const cursor = TOOL_CURSORS[world.get(Tool)?.value ?? ToolType.MOVE];
+ const tool = world.get(Tool)?.value ?? ToolType.MOVE;
+ if (tool === ToolType.OBJECT_MASK && world.get(ObjectMaskTool)?.brushShown) return 'none';
+ const cursor = TOOL_CURSORS[tool];
return pressed ? cursor.pressed ?? cursor.idle : cursor.idle;
}
diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.ts
index fe03e4d7..4f878ac9 100644
--- a/apps/web/src/engine/input/shortcuts.ts
+++ b/apps/web/src/engine/input/shortcuts.ts
@@ -37,9 +37,9 @@ import { zoomBy, zoomTo, zoomToFit, zoomToSelection } from '../camera';
import { getDocumentEditor } from '../editor';
import { groupSelection, ungroupSelection, unwrapSequenceSelection, wrapSelectionInScene, wrapSelectionInSequence } from '../group';
import { getEditHistory } from '../history';
-import { cancelObjectMask, getObjectTrack, setObjectMaskMode, trackObjectMask, undoMaskStroke } from '../object-mask';
+import { cancelObjectMask, getObjectTrack, trackObjectMask, undoMaskStroke } from '../object-mask';
import { splitAtPlayhead } from '../split';
-import { Keys, MODIFIER_KEYS, Pointer } from '../traits';
+import { Keys, MODIFIER_KEYS, ObjectMaskTool, Pointer } from '../traits';
import { editTransform } from './interactions';
import type { TransformWrite } from './interactions';
@@ -508,8 +508,8 @@ const PRESSED_SHORTCUTS: readonly Shortcut[] = [
{ keys: ['arrowdown', 'shift'], action: nudge(0, NUDGE_FAST) },
{ keys: [' '], action: onSpacePressed },
// Object mask tool shortcuts
- { keys: ['p', '!mod'], action: () => setObjectMaskMode('points'), active: objectMaskTool },
- { keys: ['b', '!mod'], action: () => setObjectMaskMode('brush'), active: objectMaskTool },
+ { keys: ['p', '!mod'], action: (world) => world.set(ObjectMaskTool, { mode: 'points' }), active: objectMaskTool },
+ { keys: ['b', '!mod'], action: (world) => world.set(ObjectMaskTool, { mode: 'brush' }), active: objectMaskTool },
{ keys: ['escape'], action: cancelObjectMask, active: objectMaskTool },
{ keys: ['enter', 'mod', '!shift', '!alt'], action: trackObjectMask, active: objectMaskTool },
{ keys: ['z', 'mod', '!shift'], action: undoMaskStroke, active: maskStrokeToUndo },
diff --git a/apps/web/src/engine/object-mask/brush.ts b/apps/web/src/engine/object-mask/brush.ts
index b735612c..15f37850 100644
--- a/apps/web/src/engine/object-mask/brush.ts
+++ b/apps/web/src/engine/object-mask/brush.ts
@@ -5,7 +5,8 @@
import { paintMask, paintStroke } from '@diffusionstudio/sam2/mask';
import { currentSourceFrame, getVideoRect, pointOnVideo, videoPointAt } from './media';
-import { brushRadius, getObjectTrack } from './store';
+import { ObjectMaskTool } from '../traits';
+import { getObjectTrack } from './store';
import type { Entity, World } from 'koota';
import type { MaskStroke, ObjectTrack } from './store';
@@ -42,7 +43,7 @@ export function beginMaskStroke(world: World, clip: Entity, deviceX: number, dev
const point = rect && pointOnVideo(rect, deviceX, deviceY);
if (!track || !rect || !point) return;
- const stroke: MaskStroke = { label, radius: round(brushRadius()), points: [roundPoint(point)] };
+ const stroke: MaskStroke = { label, radius: round(world.get(ObjectMaskTool)!.brushRadius), points: [roundPoint(point)] };
track.strokes = [...track.strokes, stroke];
painting = { track, stroke };
paint(aspectOf(world, clip), 0);
diff --git a/apps/web/src/engine/object-mask/interaction.ts b/apps/web/src/engine/object-mask/interaction.ts
index 80113321..ac5e1464 100644
--- a/apps/web/src/engine/object-mask/interaction.ts
+++ b/apps/web/src/engine/object-mask/interaction.ts
@@ -4,10 +4,9 @@
import { RenderSurface } from '@diffusionstudio/runtime';
-import { Keys, Pointer } from '../traits';
+import { Keys, ObjectMaskTool, Pointer } from '../traits';
import { beginMaskStroke, endMaskStroke, extendMaskStroke } from './brush';
import { getVideoRect, pointOnVideo } from './media';
-import { objectMaskMode, objectMaskOp } from './store';
import { promptObjectMask } from './tracking';
import type { World } from 'koota';
@@ -25,7 +24,7 @@ const CLICK_DISTANCE = 4;
* adds: taking away is left to points.
*/
export function handleObjectMaskInteraction(world: World, event: DispatchedPointerEvent): void {
- if (objectMaskMode() === 'brush') {
+ if (world.get(ObjectMaskTool)!.mode === 'brush') {
handleBrush(world, event);
return;
}
@@ -65,7 +64,7 @@ function handleBrush(world: World, event: DispatchedPointerEvent): void {
export function heldObjectMaskOp(world: World): ObjectMaskOp {
const held = world.get(Keys)?.held;
const swapped = !!held && (held.has('alt') || held.has('shift'));
- const op = objectMaskOp();
+ const op = world.get(ObjectMaskTool)!.op;
if (!swapped) return op;
return op === 'add' ? 'subtract' : 'add';
}
diff --git a/apps/web/src/engine/object-mask/overlay.ts b/apps/web/src/engine/object-mask/overlay.ts
index af7bddd5..fbbc3618 100644
--- a/apps/web/src/engine/object-mask/overlay.ts
+++ b/apps/web/src/engine/object-mask/overlay.ts
@@ -6,21 +6,12 @@ import { Culled, Hidden, HitRegions, Tool, ToolType, entityQuad, isPointerInEnti
import { traceMask } from '@diffusionstudio/assets';
import { geometryOf } from '@diffusionstudio/sam2/models';
-import { Pointer } from '../traits';
+import { ObjectMaskTool, Pointer } from '../traits';
import { endMaskStroke, paintableObjectTrack } from './brush';
import { maskFrame } from './frame';
import { handleObjectMaskInteraction, heldObjectMaskLabel } from './interaction';
import { currentSourceFrame, getVideoRect, pointOnVideo, videoPointAt, videoPointToDevice } from './media';
-import {
- brushRadius,
- clearTargetEffect,
- getObjectHover,
- getObjectMask,
- getObjectTrack,
- getTargetClip,
- objectMaskMode,
- resetObjectMaskTools,
-} from './store';
+import { clearTargetEffect, getObjectHover, getObjectMask, getObjectTrack, getTargetClip } from './store';
import { clearObjectHover, hoverObjectMask } from './tracking';
import type { Entity, World } from 'koota';
@@ -66,14 +57,14 @@ export function drawObjectMasks(world: World, ctx: Ctx2D, resolution: number): v
clearObjectHover();
clearTargetEffect();
endMaskStroke();
- resetObjectMaskTools();
+ world.set(ObjectMaskTool, { mode: 'points', op: 'add', brushShown: false });
}
return;
}
active = true;
const target = toolTarget(world);
- const brush = objectMaskMode() === 'brush';
+ const brush = world.get(ObjectMaskTool)!.mode === 'brush';
updateHover(world, brush ? null : target);
const track = getObjectTrack();
@@ -95,7 +86,10 @@ export function drawObjectMasks(world: World, ctx: Ctx2D, resolution: number): v
if (rect && hover.frame === currentSourceFrame(world, hover.clip)) drawMask(ctx, rect, hover.mask, hover.size, HOVER_ALPHA, HOVER_OUTLINE_ALPHA, resolution);
}
- if (brush && target) drawBrush(world, ctx, target, resolution);
+ const shown = !!(brush && target && drawBrush(world, ctx, target, resolution));
+ if (world.get(ObjectMaskTool)!.brushShown !== shown) {
+ world.set(ObjectMaskTool, { brushShown: shown });
+ }
if (target) {
world.get(HitRegions)!.list.push({
@@ -196,19 +190,19 @@ function drawMask(ctx: Ctx2D, rect: VideoRect, mask: Sam2Mask | MaskFrame, size:
/**
* The brush's outline under the pointer, round on the footage, while there is
- * a mask on the clip to correct.
+ * a mask on the clip to correct. Says whether it drew.
*/
-function drawBrush(world: World, ctx: Ctx2D, target: { clip: Entity; rect: VideoRect }, resolution: number): void {
+function drawBrush(world: World, ctx: Ctx2D, target: { clip: Entity; rect: VideoRect }, resolution: number): boolean {
const pointer = world.get(Pointer);
- if (!pointer?.over || !paintableObjectTrack(world, target.clip)) return;
+ if (!pointer?.over || !paintableObjectTrack(world, target.clip)) return false;
const { rect } = target;
const pressed = pointer.phase === 'pressed';
const point = pressed ? videoPointAt(rect, pointer.clientX, pointer.clientY) : pointOnVideo(rect, pointer.clientX, pointer.clientY);
- if (!point) return;
+ if (!point) return false;
const { mat } = rect;
- const radius = brushRadius() * rect.h;
+ const radius = world.get(ObjectMaskTool)!.brushRadius * rect.h;
ctx.save();
// The path is laid down in box space and stroked in device pixels, so the line keeps its width at any zoom.
ctx.setTransform(mat.a, mat.b, mat.c, mat.d, mat.e, mat.f);
@@ -222,6 +216,8 @@ function drawBrush(world: World, ctx: Ctx2D, target: { clip: Entity; rect: Video
ctx.strokeStyle = '#FFFFFF';
ctx.stroke();
ctx.restore();
+
+ return true;
}
function drawPoints(ctx: Ctx2D, rect: VideoRect, points: MaskPoint[], resolution: number): void {
diff --git a/apps/web/src/engine/object-mask/store.ts b/apps/web/src/engine/object-mask/store.ts
index 3ad2db9d..51a88054 100644
--- a/apps/web/src/engine/object-mask/store.ts
+++ b/apps/web/src/engine/object-mask/store.ts
@@ -136,21 +136,7 @@ export type ObjectMaskMode = 'points' | 'brush';
*/
export type ObjectMaskOp = 'add' | 'subtract';
-/** The brush's radius when the tool is first picked, in 0..1 of the frame's height. */
-export const DEFAULT_BRUSH_RADIUS = 0.03;
-
-const [objectMaskMode, setObjectMaskMode] = createSignal<ObjectMaskMode>('points');
-const [objectMaskOp, setObjectMaskOp] = createSignal<ObjectMaskOp>('add');
-const [brushRadius, setBrushRadius] = createSignal(DEFAULT_BRUSH_RADIUS);
-
-/** The tool's mode, op and brush size, reactive; the brush size is kept while the tool is put down. */
-export { objectMaskMode, setObjectMaskMode, objectMaskOp, setObjectMaskOp, brushRadius, setBrushRadius };
-
-/** Back to points that add, as the tool is put down: it comes up again as it first did. */
-export function resetObjectMaskTools(): void {
- setObjectMaskMode('points');
- setObjectMaskOp('add');
-}
+// Which of them the tool is on, and the brush's size, are the world's `ObjectMaskTool`.
// ── The model ────────────────────────────────────────────────
diff --git a/apps/web/src/engine/object-mask/tracking.ts b/apps/web/src/engine/object-mask/tracking.ts
index 77a7b68b..31345752 100644
--- a/apps/web/src/engine/object-mask/tracking.ts
+++ b/apps/web/src/engine/object-mask/tracking.ts
@@ -162,6 +162,7 @@ export function cancelObjectMask(world: World): void {
/** The latest point the pointer asked about while a preview was running; served next. */
let pendingHover: { clip: Entity; point: MaskPoint } | null = null;
let hovering = false;
+let hoverEpoch = 0;
/**
* Previews the segment a click at `point` on `clip` would pick, on the
@@ -180,6 +181,7 @@ export function hoverObjectMask(world: World, clip: Entity, point: MaskPoint): v
while (pendingHover && !busy) {
const { clip, point: at } = pendingHover;
pendingHover = null;
+ const epoch = hoverEpoch;
if (!clip.isAlive()) continue;
const track = getObjectTrack();
@@ -193,6 +195,7 @@ export function hoverObjectMask(world: World, clip: Entity, point: MaskPoint): v
const preview = refining
? paintMask(await session.model.preview([...track.points, at]), track.strokes, session.aspect)
: await session.model.preview([at]);
+ if (epoch !== hoverEpoch) continue;
setObjectHover({ clip, frame, point: at, mask: maskFrame(preview), size: preview.size });
}
} catch {
@@ -205,6 +208,7 @@ export function hoverObjectMask(world: World, clip: Entity, point: MaskPoint): v
/** Forgets the hovered segment, when the pointer leaves the video. */
export function clearObjectHover(): void {
+ hoverEpoch++;
pendingHover = null;
setObjectHover(null);
}
diff --git a/apps/web/src/engine/traits.ts b/apps/web/src/engine/traits.ts
index fc7bcef8..7aec44a3 100644
--- a/apps/web/src/engine/traits.ts
+++ b/apps/web/src/engine/traits.ts
@@ -12,6 +12,7 @@
import { trait } from 'koota';
import type { CanvasPointerEvent, Point } from '@diffusionstudio/runtime';
+import type { ObjectMaskMode, ObjectMaskOp } from './object-mask/store';
import type { ProjectConfig as ProjectConfigStore } from './project-config';
export type PointerPhase = 'pressed' | 'lifted';
@@ -72,6 +73,21 @@ export type HudMode = 'idle' | 'moving' | 'marquee';
*/
export const Hud = trait({ mode: 'idle' as HudMode });
+/**
+ * How the object mask tool prompts: with points or the brush (`mode`),
+ * adding to the object or subtracting from it (`op`), and the brush's radius
+ * in 0..1 of the frame's height. `brushShown` is whether the brush's ring is
+ * drawn under the pointer, standing in for the cursor, which the stage hides
+ * meanwhile. Putting the tool down brings mode and op back to points that
+ * add; the radius is kept.
+ */
+export const ObjectMaskTool = trait({
+ mode: 'points' as ObjectMaskMode,
+ op: 'add' as ObjectMaskOp,
+ brushRadius: 0.03,
+ brushShown: false,
+});
+
/**
* Queue of pointer events to be processed by the input system.
*/