🎥 Make videos programmatically with React
`@remotion/studio`: Keep sequences visible when moving and resizing
Jonny Burgerd10dd4e40dc8
diff --git a/packages/example/e2e/studio.test.mts b/packages/example/e2e/studio.test.mtsindex a503312e23..4a831e4946 100644--- a/packages/example/e2e/studio.test.mts+++ b/packages/example/e2e/studio.test.mts@@ -596,6 +596,108 @@ test.describe('visual mode', () => {);});+ test('should keep one frame visible when dragging past either end of the timeline', async ({+ page,+ }) => {+ const sourceFile = path.join(+ exampleDir,+ 'src',+ 'TimelineNegativeFromResize',+ 'index.tsx',+ );+ const originalSource = fs.readFileSync(sourceFile, 'utf-8');+ const duration = Number(+ originalSource.match(+ /name="Negative start"[\s\S]*?durationInFrames=\{(\d+)\}/,+ )![1],+ );+ try {+ await page.goto(`${STUDIO_URL}/timeline-negative-start`);+ const sequence = page.locator(+ '[data-timeline-marquee-item][title="Negative start"]',+ );+ const zeroSequence = page.locator(+ '[data-timeline-marquee-item][title="Zero start"]',+ );+ await expect(sequence).toBeVisible();+ await expect(zeroSequence).toBeVisible();+ await expect(+ sequence.getByTitle('Drag to change duration'),+ ).toBeVisible();+ const box = (await sequence.boundingBox())!;+ const zeroBox = (await zeroSequence.boundingBox())!;+ const pixelsPerFrame = zeroBox.width / 36;+ const startX = box.x + box.width / 2;+ const y = box.y + box.height / 2;+ await page.mouse.move(startX, y);+ await page.mouse.down();+ try {+ await page.mouse.move(startX - pixelsPerFrame * 20, y, {steps: 10});+ await expect(sequence).toBeVisible();+ // Further movement must not push the final frame out of the timeline.+ await page.mouse.move(startX - pixelsPerFrame * 30, y, {steps: 10});+ } finally {+ await page.mouse.up();+ }+ await expect+ .poll(() => fs.readFileSync(sourceFile, 'utf-8'))+ .toContain(`from={${1 - duration}}`);+ await page.reload();+ await expect(zeroSequence).toBeVisible();+ await expect(sequence).toBeVisible();+ await expect+ .poll(async () => (await sequence.boundingBox())!.width)+ .toBeLessThanOrEqual(pixelsPerFrame + 17);++ // Shrink past the minimum, then grow again without releasing the pointer.+ const edge = sequence.getByTitle('Drag to change duration');+ await expect(edge).toBeVisible();+ const edgeBox = (await edge.boundingBox())!;+ const edgeX = edgeBox.x + edgeBox.width / 2;+ const edgeY = edgeBox.y + edgeBox.height / 2;+ await page.mouse.move(edgeX, edgeY);+ await page.mouse.down();+ try {+ await page.mouse.move(edgeX - pixelsPerFrame * 20, edgeY, {steps: 10});+ await expect(sequence).toBeVisible();+ await page.mouse.move(edgeX + pixelsPerFrame * 10, edgeY, {steps: 10});+ await expect+ .poll(async () => (await sequence.boundingBox())!.width)+ .toBeGreaterThan(pixelsPerFrame * 10);+ } finally {+ await page.mouse.up();+ }+ await expect+ .poll(() => fs.readFileSync(sourceFile, 'utf-8'))+ .toContain(`durationInFrames={${duration + 10}}`);+ await page.reload();+ await expect(sequence).toBeVisible();++ const rightBox = (await zeroSequence.boundingBox())!;+ const rightStart = rightBox.x + rightBox.width / 2;+ const rightY = rightBox.y + rightBox.height / 2;+ await page.mouse.move(rightStart, rightY);+ await page.mouse.down();+ try {+ await page.mouse.move(rightStart + pixelsPerFrame * 100, rightY, {+ steps: 10,+ });+ } finally {+ await page.mouse.up();+ }+ await expect+ .poll(() => fs.readFileSync(sourceFile, 'utf-8'))+ .toMatch(/name="Zero start"[^>]*from=\{89\}/);+ await page.reload();+ await expect(zeroSequence).toBeVisible();+ await expect+ .poll(async () => (await zeroSequence.boundingBox())!.width)+ .toBeLessThanOrEqual(pixelsPerFrame + 1);+ } finally {+ fs.writeFileSync(sourceFile, originalSource);+ }+ });+test('should commit a color drag before the picker closes', async ({page,}) => {diff --git a/packages/example/src/TimelineNegativeFromResize/index.tsx b/packages/example/src/TimelineNegativeFromResize/index.tsxindex de9b1bcf2d..8ebad9df13 100644--- a/packages/example/src/TimelineNegativeFromResize/index.tsx+++ b/packages/example/src/TimelineNegativeFromResize/index.tsx@@ -5,23 +5,16 @@ import {AbsoluteFill, Sequence} from 'remotion';export const TimelineNegativeFromResize: React.FC = () => {return (<>- <AbsoluteFill style={{backgroundColor: '#202020'}}>+ <AbsoluteFill style={{backgroundColor: '#202020'}} from={-8}><Sequencename="Negative start"- from={-20}- durationInFrames={21}+ durationInFrames={43}freeze={20}style={{backgroundColor: 'black'}}+ from={-42}><Starburst rays={16} colors={['#ffdd00', '#ff8800']} /></Sequence>- <Sequence- name="Negative start without freeze"- from={-20}- durationInFrames={21}- >- <div />- </Sequence></AbsoluteFill><Sequence name="Zero start" durationInFrames={36}><div />diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsxindex db674dbec0..c31125cad1 100644--- a/packages/studio/src/components/Timeline/Timeline.tsx+++ b/packages/studio/src/components/Timeline/Timeline.tsx@@ -435,11 +435,25 @@ const TimelineInner: React.FC = () => {const durationInFrames = videoConfig?.durationInFrames ?? 0;+ const {getDragOverrides} = useContext(+ Internals.VisualModeDragOverridesContext,+ );const filtered = useMemo(() => {- return timeline.filter((t) =>- shouldShowTrackInTimeline(t, durationInFrames),- );- }, [durationInFrames, timeline]);+ return timeline.filter((t) => {+ // Moving outside the composition can reduce the displayed duration to+ // zero. Keep the drag owner mounted until its pending edit is saved.+ if (+ t.sequence.showInTimeline &&+ t.nodePathInfo !== null &&+ getDragOverrides(t.nodePathInfo.sequenceSubscriptionKey).from !==+ undefined+ ) {+ return true;+ }++ return shouldShowTrackInTimeline(t, durationInFrames);+ });+ }, [durationInFrames, getDragOverrides, timeline]);// Keep `filtered` complete so a future toggle can show every programmatic// instance without recalculating the timeline or losing its instance index.diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsxindex d701ab8b2a..4a0db2ba82 100644--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx@@ -83,6 +83,8 @@ export type TimelineSequenceLeftEdgeDragTarget = {export type TimelineSequenceFromDragTarget = {readonly canSnapToTimelineStart: boolean;+ readonly minimumDeltaFrames: number;+ readonly initialTimelineStart: number;readonly effectKeyframes: TimelineSequenceEffectKeyframeDragTarget[];readonly fileName: string;readonly initialFrom: number;@@ -534,19 +536,17 @@ export const getTimelineSequenceFromDragValue = ({export const getTimelineSequenceFromDragDelta = ({deltaFrames,+ timelineDurationInFrames,pxPerFrame,snappingEnabled,targets,}: {readonly deltaFrames: number;+ readonly timelineDurationInFrames: number;readonly pxPerFrame: number;readonly snappingEnabled: boolean;readonly targets: readonly TimelineSequenceFromDragTarget[];}) => {- if (!snappingEnabled) {- return deltaFrames;- }-let closestSnap:| {readonly deltaFrames: number;@@ -554,7 +554,7 @@ export const getTimelineSequenceFromDragDelta = ({}| undefined;for (const target of targets) {- if (!target.canSnapToTimelineStart) {+ if (!snappingEnabled || !target.canSnapToTimelineStart) {continue;}@@ -576,7 +576,19 @@ export const getTimelineSequenceFromDragDelta = ({};}- return closestSnap?.deltaFrames ?? deltaFrames;+ const minimumDelta = Math.max(+ ...targets.map((target) => target.minimumDeltaFrames),+ );+ const maximumDelta = Math.min(+ ...targets.map(+ (target) => timelineDurationInFrames - 1 - target.initialTimelineStart,+ ),+ );+ // Clamp the shared delta after snapping so every selected track retains a frame.+ return Math.max(+ minimumDelta,+ Math.min(maximumDelta, closestSnap?.deltaFrames ?? deltaFrames),+ );};export const getTimelineSequenceFromDragChanges = ({@@ -759,10 +771,11 @@ export const getTimelineSequenceDurationDragTargets = ({targets.set(key, {fileName: nodePath.absolutePath,initialDuration: originalSequence.duration,- minimumDuration: getMinimumSequenceDuration({- sequence: originalSequence,- sequences,- }),+ // A negative start needs enough duration to retain one visible frame.+ minimumDuration: Math.max(+ 1 - originalSequence.from,+ getMinimumSequenceDuration({sequence: originalSequence, sequences}),+ ),nodePath,schema: controls.schema,});@@ -920,6 +933,7 @@ export const getTimelineSequenceFromDragTargets = ({{nodePath: track.nodePathInfo.sequenceSubscriptionKey,originalSequence,+ initialTimelineStart: track.cascadedStart,},];});@@ -950,6 +964,7 @@ export const getTimelineSequenceFromDragTargets = ({for (const {nodePath,originalSequence,+ initialTimelineStart,} of sequencesWithoutSelectedAncestors) {if (!isFromDraggableSequence(originalSequence)) {return null;@@ -1006,6 +1021,9 @@ export const getTimelineSequenceFromDragTargets = ({);targets.set(key, {canSnapToTimelineStart: originalSequence.parent === null,+ minimumDeltaFrames:+ 1 - originalSequence.duration - originalSequence.from,+ initialTimelineStart,effectKeyframes,fileName: nodePath.absolutePath,initialFrom: originalSequence.from,@@ -1555,7 +1573,9 @@ export const useTimelineSequenceFromDrag = ({// the global session listeners exist.stopPointerSessionRef.current = startCapturedPointerSession({event: e.nativeEvent,- captureTarget: e.currentTarget,+ // The bar is removed when it leaves the visible timeline. Capture on+ // a stable element so the move continues until the pointer is released.+ captureTarget: e.currentTarget.ownerDocument.body,onMove: (moveEvent) => {const dragState = dragStateRef.current;if (!dragState) {@@ -1564,6 +1584,7 @@ export const useTimelineSequenceFromDrag = ({const dx = moveEvent.clientX - dragState.initialClientX;const deltaFrames = getTimelineSequenceFromDragDelta({+ timelineDurationInFrames,deltaFrames: Math.round(dx / dragState.pxPerFrame),pxPerFrame: dragState.pxPerFrame,snappingEnabled: latestRef.current.editorSnapping,diff --git a/packages/studio/src/test/timeline-selection.test.ts b/packages/studio/src/test/timeline-selection.test.tsindex cd30c06e07..f855f635a8 100644--- a/packages/studio/src/test/timeline-selection.test.ts+++ b/packages/studio/src/test/timeline-selection.test.ts@@ -2594,6 +2594,22 @@ test('Timeline from drag applies the same delta to selected sequences', () => {});expect(targets?.map((target) => target.initialFrom)).toEqual([0, 10]);++ for (const [deltaFrames, expected] of [+ [-100, -24],+ [100, 79],+ ]) {+ expect(+ getTimelineSequenceFromDragDelta({+ deltaFrames,+ timelineDurationInFrames: 90,+ pxPerFrame: 10,+ snappingEnabled: true,+ targets: targets!,+ }),+ ).toBe(expected);+ }+expect(getTimelineSequenceFromDragChanges({targets: targets ?? [],@@ -2925,6 +2941,8 @@ test('Timeline from drag snaps a root sequence to frame 0', () => {const nodePath = makeNodePathInfo(['body', 0], []).sequenceSubscriptionKey;const target = {canSnapToTimelineStart: true,+ minimumDeltaFrames: -100,+ initialTimelineStart: 8,effectKeyframes: [],fileName: nodePath.absolutePath,initialFrom: 8,@@ -2935,6 +2953,7 @@ test('Timeline from drag snaps a root sequence to frame 0', () => {expect(getTimelineSequenceFromDragDelta({+ timelineDurationInFrames: 90,deltaFrames: -6,pxPerFrame,snappingEnabled: true,@@ -2943,6 +2962,7 @@ test('Timeline from drag snaps a root sequence to frame 0', () => {).toBe(-8);expect(getTimelineSequenceFromDragDelta({+ timelineDurationInFrames: 90,deltaFrames: -6,pxPerFrame,snappingEnabled: false,@@ -2951,6 +2971,7 @@ test('Timeline from drag snaps a root sequence to frame 0', () => {).toBe(-6);expect(getTimelineSequenceFromDragDelta({+ timelineDurationInFrames: 90,deltaFrames: -5,pxPerFrame,snappingEnabled: true,@@ -2964,12 +2985,15 @@ test('Timeline from drag does not snap nested sequences to the timeline start',expect(getTimelineSequenceFromDragDelta({+ timelineDurationInFrames: 90,deltaFrames: -6,pxPerFrame: timelineSequenceFromDragSnapThresholdPx / 2,snappingEnabled: true,targets: [{canSnapToTimelineStart: false,+ minimumDeltaFrames: -100,+ initialTimelineStart: 8,effectKeyframes: [],fileName: nodePath.absolutePath,initialFrom: 8,@@ -3076,6 +3100,8 @@ test('Timeline from drag removes the prop at the default value', () => {targets: [{canSnapToTimelineStart: true,+ minimumDeltaFrames: -100,+ initialTimelineStart: 8,effectKeyframes: [],fileName: nodePathInfo.sequenceSubscriptionKey.absolutePath,initialFrom: 5,