Skip to content

🎥 Make videos programmatically with React

Prevent keyframe drags outside timeline

Cursor Agent18565203a675
diff --git a/packages/studio/src/components/Timeline/get-bounded-keyframe-drag-delta.ts b/packages/studio/src/components/Timeline/get-bounded-keyframe-drag-delta.ts
new file mode 100644
index 0000000000..963dfc1df3
--- /dev/null
+++ b/packages/studio/src/components/Timeline/get-bounded-keyframe-drag-delta.ts
@@ -0,0 +1,24 @@
+export type TimelineKeyframeDragBoundsTarget = {
+ readonly displayFrame: number;
+};
+
+export const getBoundedKeyframeDragDelta = ({
+ delta,
+ durationInFrames,
+ targets,
+}: {
+ readonly delta: number;
+ readonly durationInFrames: number;
+ readonly targets: readonly TimelineKeyframeDragBoundsTarget[];
+}) => {
+ if (targets.length === 0 || durationInFrames <= 0) {
+ return 0;
+ }
+
+ const minDelta = Math.max(...targets.map((target) => -target.displayFrame));
+ const maxDelta = Math.min(
+ ...targets.map((target) => durationInFrames - 1 - target.displayFrame),
+ );
+
+ return Math.min(Math.max(delta, minDelta), maxDelta);
+};
diff --git a/packages/studio/src/components/Timeline/use-timeline-keyframe-drag.ts b/packages/studio/src/components/Timeline/use-timeline-keyframe-drag.ts
index a4555f4cf7..df092b09d7 100644
--- a/packages/studio/src/components/Timeline/use-timeline-keyframe-drag.ts
+++ b/packages/studio/src/components/Timeline/use-timeline-keyframe-drag.ts
@@ -16,6 +16,7 @@ import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sor
import {TIMELINE_PADDING} from '../../helpers/timeline-layout';
import {callMoveKeyframes} from './call-move-keyframe';
import {findTrackForNodePathInfo} from './find-track-for-node-path-info';
+import {getBoundedKeyframeDragDelta} from './get-bounded-keyframe-drag-delta';
import {parseKeyframeFieldFromNodePath} from './parse-keyframe-field-from-node-path';
import {useTimelineKeyframeDragState} from './TimelineKeyframeDragState';
import {
@@ -469,7 +470,11 @@ export const useTimelineKeyframeDrag = ({
durationInFrames: videoConfig.durationInFrames,
timelineWidth,
});
- const delta = rawDelta;
+ const delta = getBoundedKeyframeDragDelta({
+ delta: rawDelta,
+ durationInFrames: videoConfig.durationInFrames,
+ targets,
+ });
if (hasDragged && delta === lastDelta) {
return;
diff --git a/packages/studio/src/test/timeline-keyframes.test.ts b/packages/studio/src/test/timeline-keyframes.test.ts
index 2e5928f2d9..2bb2860b10 100644
--- a/packages/studio/src/test/timeline-keyframes.test.ts
+++ b/packages/studio/src/test/timeline-keyframes.test.ts
@@ -5,6 +5,7 @@ import type {
TSequence,
} from 'remotion';
import {Internals, type CodeValues} from 'remotion';
+import {getBoundedKeyframeDragDelta} from '../components/Timeline/get-bounded-keyframe-drag-delta';
import {getNodeKeyframes} from '../components/Timeline/get-node-keyframes';
import {getTimelineKeyframes} from '../components/Timeline/get-timeline-keyframes';
import {getTimelineKeyframeDragKey} from '../components/Timeline/TimelineKeyframeDragState';
@@ -230,6 +231,54 @@ test('keyframe display offsets follow the parent sequence context', () => {
]);
});
+test('bounded keyframe drag delta stays inside the composition timeline', () => {
+ expect(
+ getBoundedKeyframeDragDelta({
+ targets: [{displayFrame: 10}],
+ delta: -20,
+ durationInFrames: 100,
+ }),
+ ).toBe(-10);
+
+ expect(
+ getBoundedKeyframeDragDelta({
+ targets: [{displayFrame: 90}],
+ delta: 20,
+ durationInFrames: 100,
+ }),
+ ).toBe(9);
+});
+
+test('bounded keyframe drag delta allows negative source frames when display frames stay in range', () => {
+ expect(
+ getBoundedKeyframeDragDelta({
+ targets: [{displayFrame: 30}],
+ delta: -30,
+ durationInFrames: 100,
+ }),
+ ).toBe(-30);
+});
+
+test('bounded keyframe drag delta clamps multi-selection at the first timeline edge', () => {
+ const targets = [{displayFrame: 20}, {displayFrame: 95}];
+
+ expect(
+ getBoundedKeyframeDragDelta({
+ targets,
+ delta: -30,
+ durationInFrames: 100,
+ }),
+ ).toBe(-20);
+
+ expect(
+ getBoundedKeyframeDragDelta({
+ targets,
+ delta: 10,
+ durationInFrames: 100,
+ }),
+ ).toBe(4);
+});
+
test('getNodeKeyframes shows a temporary sequence keyframe from drag overrides', () => {
const nodePath = makeNodePath('sequence');