Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance keyframe drag functionality and update drag gesture handling

konstantin-paulusfc13c899f966
diff --git a/apps/web/src/engine/timeline/drag.ts b/apps/web/src/engine/timeline/drag.ts
index 36561b77..86d57754 100644
--- a/apps/web/src/engine/timeline/drag.ts
+++ b/apps/web/src/engine/timeline/drag.ts
@@ -18,19 +18,23 @@ import {
Computed,
Geometry,
Group,
+ Keyframe,
KeyframeDragOrigin,
Selected,
TrimDragOrigin,
findAssetDuration,
+ findClosestParentGeometry,
+ framesToSeconds,
store,
} from '@diffusionstudio/runtime';
import { Not, Or } from 'koota';
import { clamp } from '@/utils';
+import { getDocumentEditor } from '../editor';
import { resolveSequentialOverlaps } from '../overlap';
import { authoredTime, moveEntityTo, trimIn, trimOut } from '../timing';
import { findSnapDelta, findSnapFrame } from './snapping';
-import { framesToPixels, pixelsToFrames } from './view';
+import { framesToPixels, getFrameRate, getResolution, pixelsToFrames } from './view';
import type { Entity, World } from 'koota';
import type { TimelineSurfaceState } from './surface';
@@ -48,7 +52,7 @@ export type TrimEdge = 'in' | 'out';
* a gesture ends when the pointer is let go, which is not an event any clip
* receives — the clip only ever hears that it is still being dragged.
*/
-export function updateDragGestures(world: World, surface: TimelineSurfaceState): void {
+export function updateDragGestures(world: World, scene: Entity, surface: TimelineSurfaceState): void {
const position = surface.pointer?.position;
const dragging = !!position && position.state !== 'idle';
@@ -69,6 +73,18 @@ export function updateDragGestures(world: World, surface: TimelineSurfaceState):
beginClipDrag(world, entity);
}
}
+
+ // Keyframes likewise, and moved here rather than where they are drawn: a
+ // selected keyframe scrolled out of view, or on a collapsed track, still
+ // has to go with the rest.
+ if (world.query(KeyframeDragOrigin, Selected).length > 0) {
+ for (const keyframe of world.query(Keyframe, Selected, Not(KeyframeDragOrigin))) {
+ beginKeyframeDrag(keyframe);
+ }
+ }
+ for (const keyframe of world.query(KeyframeDragOrigin)) {
+ applyKeyframeDrag(world, scene, surface, keyframe);
+ }
}
/**
@@ -132,6 +148,32 @@ function earliestDraggedStart(world: World): number {
return Number.isFinite(earliest) ? earliest : 0;
}
+/** Notes where `keyframe` is, in its clip's own time. */
+export function beginKeyframeDrag(keyframe: Entity): void {
+ keyframe.add(KeyframeDragOrigin);
+ keyframe.set(KeyframeDragOrigin, { time: keyframe.get(Keyframe)?.time ?? 0 });
+}
+
+/**
+ * Places `keyframe` at where it started plus how far the pointer has come. A
+ * keyframe's time is a prop of its element, so a drag of it is an edit like
+ * any other.
+ */
+function applyKeyframeDrag(world: World, scene: Entity, surface: TimelineSurfaceState, keyframe: Entity): void {
+ const origin = keyframe.get(KeyframeDragOrigin)!;
+
+ // The pointer moves in scene frames; the keyframe lives in the clip's,
+ // which run `rate` times as fast.
+ const rate = findClosestParentGeometry(keyframe)?.get(Computed)?.playbackRate || 1;
+ const moved = pixelsToFrames(draggedPixels(surface), getResolution(world, scene)) * rate;
+
+ getDocumentEditor(world).editProperty(
+ keyframe,
+ 'time',
+ framesToSeconds(Math.max(0, origin.time + moved), getFrameRate(world)),
+ );
+}
+
/** Notes where `entity`'s edges are, so a trim can be measured from them. */
export function beginTrim(world: World, entity: Entity): void {
const computed = store(world, Computed);
diff --git a/apps/web/src/engine/timeline/render/keyframes.ts b/apps/web/src/engine/timeline/render/keyframes.ts
index 03449660..eb8687b0 100644
--- a/apps/web/src/engine/timeline/render/keyframes.ts
+++ b/apps/web/src/engine/timeline/render/keyframes.ts
@@ -10,14 +10,14 @@ import {
KeyframeDragOrigin,
Selected,
findClosestParentGeometry,
- framesToSeconds,
store,
} from '@diffusionstudio/runtime';
import { getDocumentEditor } from '../../editor';
+import { beginKeyframeDrag } from '../drag';
import { KEYFRAME_TRACK_HEIGHT } from '../config';
import { getRowTransform } from '../layout';
-import { framesToPixels, getFrameRate, getResolution, getViewport, pixelsToFrames } from '../view';
+import { framesToPixels, getResolution, getViewport } from '../view';
import type { Entity, World } from 'koota';
import type { RowCursor } from '../layout';
@@ -88,7 +88,7 @@ export function renderKeyframeTrack(
if (x + HITBOX_HALF < viewportLeft || x - HITBOX_HALF > viewportRight) continue;
- handleKeyframe(world, surface, keyframe, time, x, centerY, rate, resolution);
+ handleKeyframe(world, surface, keyframe, x, centerY);
const selected = keyframe.has(Selected);
ctx.strokeStyle = selected ? surface.colors.border.ring : COLOR;
@@ -117,20 +117,13 @@ export function renderKeyframeTrack(
ctx.restore();
}
-/**
- * Selecting and moving one keyframe. A keyframe's time is a prop of its
- * element, so a drag of it is an edit like any other; the drag is measured
- * from where it started, in the clip's own time.
- */
+/** Selecting one keyframe, and starting a drag of it. */
function handleKeyframe(
world: World,
surface: TimelineSurfaceState,
keyframe: Entity,
- time: number,
x: number,
centerY: number,
- rate: number,
- resolution: number,
): void {
const pointer = surface.pointer!;
const editor = getDocumentEditor(world);
@@ -153,18 +146,8 @@ function handleKeyframe(
}
if (dragging && !keyframe.has(KeyframeDragOrigin)) {
- keyframe.add(KeyframeDragOrigin);
- keyframe.set(KeyframeDragOrigin, { time });
+ beginKeyframeDrag(keyframe);
}
-
- const origin = keyframe.get(KeyframeDragOrigin);
- const position = pointer.position;
- if (!origin || !position || position.state === 'idle') return;
-
- // The pointer moves in scene frames; the keyframe lives in the clip's,
- // which run `rate` times as fast.
- const moved = pixelsToFrames(position.deltaX, resolution) * rate;
- editor.editProperty(keyframe, 'time', framesToSeconds(Math.max(0, origin.time + moved), getFrameRate(world)));
}
function diamond(ctx: CanvasRenderingContext2D, x: number, y: number): void {
diff --git a/apps/web/src/engine/timeline/timeline.ts b/apps/web/src/engine/timeline/timeline.ts
index a87603e4..72bd46b1 100644
--- a/apps/web/src/engine/timeline/timeline.ts
+++ b/apps/web/src/engine/timeline/timeline.ts
@@ -51,7 +51,7 @@ export function timelineSystem(world: World): void {
// Before anything is drawn, and whether or not the rows are: a gesture
// that ended has to be closed before the clips it moved are laid out
// again, and collapsing the timeline mid-drag must not strand one.
- updateDragGestures(world, surface);
+ updateDragGestures(world, scene, surface);
// The rows are clipped to below the ruler, which is then painted over
// the top of them: a clip scrolled up under the ruler disappears