Skip to content

🎥 Make videos programmatically with React

preserve position when zooming

Jonny Burger4f301e1ae077
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsx
index 21aee36b16..d1b57a0c4b 100644
--- a/packages/cli/src/editor/components/PlayPause.tsx
+++ b/packages/cli/src/editor/components/PlayPause.tsx
@@ -93,17 +93,25 @@ export const PlayPause: React.FC<{
if (e.altKey) {
seek(0);
- ensureFrameIsInViewport('fit-left', durationInFrames, 0);
+ ensureFrameIsInViewport({
+ direction: 'fit-left',
+ durationInFrames,
+ frame: 0,
+ });
} else if (e.shiftKey) {
frameBack(videoFps);
- ensureFrameIsInViewport(
- 'fit-left',
+ ensureFrameIsInViewport({
+ direction: 'fit-left',
durationInFrames,
- Math.max(0, frame - videoFps)
- );
+ frame: Math.max(0, frame - videoFps),
+ });
} else {
frameBack(1);
- ensureFrameIsInViewport('fit-left', durationInFrames, frame - 1);
+ ensureFrameIsInViewport({
+ direction: 'fit-left',
+ durationInFrames,
+ frame: frame - 1,
+ });
}
},
[durationInFrames, frame, frameBack, seek, videoFps]
@@ -117,21 +125,25 @@ export const PlayPause: React.FC<{
if (e.altKey) {
seek(video.durationInFrames - 1);
- ensureFrameIsInViewport(
- 'fit-right',
- video.durationInFrames - 1,
- video.durationInFrames - 1
- );
+ ensureFrameIsInViewport({
+ direction: 'fit-right',
+ durationInFrames: video.durationInFrames - 1,
+ frame: video.durationInFrames - 1,
+ });
} else if (e.shiftKey) {
frameForward(video.fps);
- ensureFrameIsInViewport(
- 'fit-right',
- video.durationInFrames,
- Math.min(video.durationInFrames - 1, frame + video.fps)
- );
+ ensureFrameIsInViewport({
+ direction: 'fit-right',
+ durationInFrames: video.durationInFrames,
+ frame: Math.min(video.durationInFrames - 1, frame + video.fps),
+ });
} else {
frameForward(1);
- ensureFrameIsInViewport('fit-right', video.durationInFrames, frame + 1);
+ ensureFrameIsInViewport({
+ direction: 'fit-right',
+ durationInFrames: video.durationInFrames,
+ frame: frame + 1,
+ });
}
e.preventDefault();
diff --git a/packages/cli/src/editor/components/PreviewToolbar.tsx b/packages/cli/src/editor/components/PreviewToolbar.tsx
index 0c929a5407..273b0da5ce 100644
--- a/packages/cli/src/editor/components/PreviewToolbar.tsx
+++ b/packages/cli/src/editor/components/PreviewToolbar.tsx
@@ -76,7 +76,6 @@ export const PreviewToolbar: React.FC = () => {
<Flex />
<FpsCounter playbackSpeed={playbackRate} />
<TimelineZoomControls />
-
<div style={padding} />
</div>
<PlaybackKeyboardShortcutsManager setPlaybackRate={setPlaybackRate} />
diff --git a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
index a849149dde..2e41c46bcb 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
@@ -136,12 +136,12 @@ export const TimelineDragHandler: React.FC = () => {
return;
}
- const frame = getFrameFromX(
- getClientXWithScroll(e.clientX) - left,
- videoConfig.durationInFrames,
+ const frame = getFrameFromX({
+ clientX: getClientXWithScroll(e.clientX) - left,
+ durationInFrames: videoConfig.durationInFrames,
width,
- 'clamp'
- );
+ extrapolate: 'clamp',
+ });
seek(frame);
setDragging({
dragging: true,
@@ -170,12 +170,12 @@ export const TimelineDragHandler: React.FC = () => {
const isLeftOfArea = e.clientX <= left;
- const frame = getFrameFromX(
- getClientXWithScroll(e.clientX) - left,
- videoConfig.durationInFrames,
+ const frame = getFrameFromX({
+ clientX: getClientXWithScroll(e.clientX) - left,
+ durationInFrames: videoConfig.durationInFrames,
width,
- 'clamp'
- );
+ extrapolate: 'clamp',
+ });
if (isLeftOfArea && canScrollTimelineIntoDirection().canScrollLeft) {
if (scroller.current) {
@@ -188,10 +188,10 @@ export const TimelineDragHandler: React.FC = () => {
return;
}
- const nextFrame = getFrameWhileScrollingLeft(
- videoConfig.durationInFrames,
- width
- );
+ const nextFrame = getFrameWhileScrollingLeft({
+ durationInFrames: videoConfig.durationInFrames,
+ width,
+ });
const scrollPos = getScrollPositionForCursorOnLeftEdge({
nextFrame,
@@ -327,12 +327,12 @@ export const TimelineDragHandler: React.FC = () => {
dragging: false,
});
- const frame = getFrameFromX(
- getClientXWithScroll(e.clientX) - left,
- videoConfig.durationInFrames,
+ const frame = getFrameFromX({
+ clientX: getClientXWithScroll(e.clientX) - left,
+ durationInFrames: videoConfig.durationInFrames,
width,
- 'clamp'
- );
+ extrapolate: 'clamp',
+ });
persistCurrentFrame(frame);
@@ -357,12 +357,12 @@ export const TimelineDragHandler: React.FC = () => {
dragging: false,
});
- const frame = getFrameFromX(
- getClientXWithScroll(e.clientX) - left,
- videoConfig.durationInFrames,
+ const frame = getFrameFromX({
+ clientX: getClientXWithScroll(e.clientX) - left,
+ durationInFrames: videoConfig.durationInFrames,
width,
- 'extend'
- );
+ extrapolate: 'extend',
+ });
if (inOutDragging.dragging === 'in') {
if (frame < 1) {
return setInAndOutFrames((prev) => ({
diff --git a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
index 068403c0b6..f86f349694 100644
--- a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
@@ -1,7 +1,15 @@
import type React from 'react';
-import {useContext, useEffect, useRef} from 'react';
+import {useContext, useEffect} from 'react';
import {Internals} from 'remotion';
import {TimelineZoomCtx} from '../../state/timeline-zoom';
+import {
+ getCurrentDuration,
+ getCurrentFrame,
+ getCurrentZoom,
+ setCurrentDuration,
+ setCurrentFrame,
+ setCurrentZoom,
+} from './imperative-state';
import {scrollableRef} from './timeline-refs';
import {
ensureFrameIsInViewport,
@@ -23,13 +31,10 @@ export const TimelinePlayCursorSyncer: React.FC = () => {
const video = Internals.useVideo();
const timelineContext = useContext(Internals.Timeline.TimelineContext);
const {zoom} = useContext(TimelineZoomCtx);
- const currentFrame = useRef<number>(timelineContext.frame);
- const currentZoom = useRef<number>(zoom);
- const currentDuration = useRef<number>(video?.durationInFrames ?? 1);
- currentFrame.current = timelineContext.frame;
- currentZoom.current = zoom;
- currentDuration.current = video?.durationInFrames ?? 1;
+ setCurrentFrame(timelineContext.frame);
+ setCurrentZoom(zoom);
+ setCurrentDuration(video?.durationInFrames ?? 1);
const playing = timelineContext.playing ?? false;
@@ -45,11 +50,11 @@ export const TimelinePlayCursorSyncer: React.FC = () => {
return;
}
- ensureFrameIsInViewport(
- timelineContext.playbackRate > 0 ? 'page-right' : 'page-left',
- video.durationInFrames,
- timelineContext.frame
- );
+ ensureFrameIsInViewport({
+ direction: timelineContext.playbackRate > 0 ? 'page-right' : 'page-left',
+ durationInFrames: video.durationInFrames,
+ frame: timelineContext.frame,
+ });
}, [playing, timelineContext, video]);
/**
@@ -66,27 +71,32 @@ export const TimelinePlayCursorSyncer: React.FC = () => {
if (playing) {
lastTimelinePositionWhileScrolling = {
scrollLeft: current.scrollLeft,
- frame: currentFrame.current,
- zoomLevel: currentZoom.current,
- durationInFrames: currentDuration.current,
+ frame: getCurrentFrame(),
+ zoomLevel: getCurrentZoom(),
+ durationInFrames: getCurrentDuration(),
};
} else if (lastTimelinePositionWhileScrolling !== null) {
- if (isCursorInViewport(currentFrame.current, currentDuration.current)) {
+ if (
+ isCursorInViewport({
+ frame: getCurrentFrame(),
+ durationInFrames: getCurrentDuration(),
+ })
+ ) {
return;
}
if (
- lastTimelinePositionWhileScrolling.zoomLevel === currentZoom.current &&
+ lastTimelinePositionWhileScrolling.zoomLevel === getCurrentZoom() &&
lastTimelinePositionWhileScrolling.durationInFrames ===
- currentDuration.current
+ getCurrentDuration()
) {
current.scrollLeft = lastTimelinePositionWhileScrolling.scrollLeft;
} else {
- ensureFrameIsInViewport(
- 'center',
- currentDuration.current,
- lastTimelinePositionWhileScrolling.frame
- );
+ ensureFrameIsInViewport({
+ direction: 'center',
+ durationInFrames: getCurrentDuration(),
+ frame: lastTimelinePositionWhileScrolling.frame,
+ });
}
}
}, [playing]);
diff --git a/packages/cli/src/editor/components/Timeline/TimelineSlider.tsx b/packages/cli/src/editor/components/Timeline/TimelineSlider.tsx
index 69208ceb9f..8962cab88d 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineSlider.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineSlider.tsx
@@ -1,6 +1,11 @@
import React, {createRef, useImperativeHandle, useMemo, useRef} from 'react';
import {Internals} from 'remotion';
-import {useGetXPositionOfItemInTimeline} from '../../helpers/get-left-of-timeline-slider';
+import {
+ getXPositionOfItemInTimelineImperatively,
+ useGetXPositionOfItemInTimeline,
+} from '../../helpers/get-left-of-timeline-slider';
+import {getCurrentDuration} from './imperative-state';
+import {sliderAreaRef} from './timeline-refs';
import {TimelineSliderHandle} from './TimelineSliderHandle';
const container: React.CSSProperties = {
@@ -18,7 +23,7 @@ const line: React.CSSProperties = {
};
export const redrawTimelineSliderFast = createRef<{
- draw: (frame: number) => void;
+ draw: (frame: number, width?: number) => void;
}>();
export const TimelineSlider: React.FC = () => {
@@ -36,13 +41,17 @@ export const TimelineSlider: React.FC = () => {
useImperativeHandle(redrawTimelineSliderFast, () => {
return {
- draw: (frame) => {
+ draw: (frame, width?: number) => {
const {current} = ref;
if (!current) {
throw new Error('unexpectedly did not have ref to timelineslider');
}
- current.style.transform = `translateX(${get(frame)}px)`;
+ current.style.transform = `translateX(${getXPositionOfItemInTimelineImperatively(
+ frame,
+ getCurrentDuration(),
+ width ?? (sliderAreaRef.current?.clientWidth as number) ?? 0
+ )}px)`;
},
};
});
diff --git a/packages/cli/src/editor/components/Timeline/TimelineZoomControls.tsx b/packages/cli/src/editor/components/Timeline/TimelineZoomControls.tsx
index 38541b646a..3746a7ec05 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineZoomControls.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineZoomControls.tsx
@@ -1,4 +1,5 @@
import React, {useCallback, useContext} from 'react';
+import {useIsStill} from '../../helpers/is-current-selected-still';
import {
TimelineZoomCtx,
TIMELINE_MAX_ZOOM,
@@ -29,10 +30,15 @@ export const TimelineZoomControls: React.FC = () => {
const onChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
(e) => {
- setZoom(Number(e.target.value));
+ setZoom(() => Number(e.target.value));
},
[setZoom]
);
+ const isStill = useIsStill();
+
+ if (isStill) {
+ return null;
+ }
return (
<div style={container}>
diff --git a/packages/cli/src/editor/components/Timeline/imperative-state.ts b/packages/cli/src/editor/components/Timeline/imperative-state.ts
new file mode 100644
index 0000000000..d0dbff280d
--- /dev/null
+++ b/packages/cli/src/editor/components/Timeline/imperative-state.ts
@@ -0,0 +1,27 @@
+export let currentFrame = 0;
+export let currentZoom = 1;
+export let currentDuration = 1;
+
+export const getCurrentZoom = () => {
+ return currentZoom;
+};
+
+export const setCurrentZoom = (z: number) => {
+ currentZoom = z;
+};
+
+export const getCurrentFrame = () => {
+ return currentFrame;
+};
+
+export const setCurrentFrame = (f: number) => {
+ currentFrame = f;
+};
+
+export const getCurrentDuration = () => {
+ return currentDuration;
+};
+
+export const setCurrentDuration = (d: number) => {
+ currentDuration = d;
+};
diff --git a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts
index fc289d823e..68191c9cd7 100644
--- a/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts
+++ b/packages/cli/src/editor/components/Timeline/timeline-scroll-logic.ts
@@ -24,7 +24,12 @@ const calculateFrameWhileScrollingRight = ({
scrollLeft: number;
}) => {
return (
- getFrameFromX(scrollLeft, durationInFrames, width, 'clamp') +
+ getFrameFromX({
+ clientX: scrollLeft,
+ durationInFrames,
+ width,
+ extrapolate: 'clamp',
+ }) +
Math.ceil(
((scrollableRef.current?.clientWidth as number) - TIMELINE_PADDING) /
getFrameIncrement(durationInFrames)
@@ -32,28 +37,37 @@ const calculateFrameWhileScrollingRight = ({
);
};
-export const getFrameWhileScrollingLeft = (
- durationInFrames: number,
- width: number
-) => {
- const nextFrame = getFrameFromX(
- (scrollableRef.current?.scrollLeft as number) - SCROLL_INCREMENT,
+export const getFrameWhileScrollingLeft = ({
+ durationInFrames,
+ width,
+}: {
+ durationInFrames: number;
+ width: number;
+}) => {
+ const nextFrame = getFrameFromX({
+ clientX: (scrollableRef.current?.scrollLeft as number) - SCROLL_INCREMENT,
durationInFrames,
width,
- 'clamp'
- );
- const currentFrame = getFrameFromX(
- scrollableRef.current?.scrollLeft as number,
+ extrapolate: 'clamp',
+ });
+ const currentFrame = getFrameFromX({
+ clientX: scrollableRef.current?.scrollLeft as number,
durationInFrames,
width,
- 'clamp'
- );
+ extrapolate: 'clamp',
+ });
// Should go back at least 1 frame, but not less than 0
return Math.max(0, Math.min(currentFrame - 1, nextFrame));
};
-export const isCursorInViewport = (frame: number, durationInFrames: number) => {
+export const isCursorInViewport = ({
+ frame,
+ durationInFrames,
+}: {
+ frame: number;
+ durationInFrames: number;
+}) => {
const width = scrollableRef.current?.scrollWidth ?? 0;
const scrollLeft = scrollableRef.current?.scrollLeft ?? 0;
@@ -81,21 +95,25 @@ export const isCursorInViewport = (frame: number, durationInFrames: number) => {
);
};
-export const ensureFrameIsInViewport = (
- direction: 'fit-left' | 'fit-right' | 'page-right' | 'page-left' | 'center',
- durationInFrames: number,
- frame: number
-) => {
+export const ensureFrameIsInViewport = ({
+ direction,
+ durationInFrames,
+ frame,
+}: {
+ direction: 'fit-left' | 'fit-right' | 'page-right' | 'page-left' | 'center';
+ durationInFrames: number;
+ frame: number;
+}) => {
redrawTimelineSliderFast.current?.draw(frame);
const width = scrollableRef.current?.scrollWidth ?? 0;
const scrollLeft = scrollableRef.current?.scrollLeft ?? 0;
if (direction === 'fit-left') {
- const currentFrameLeft = getFrameFromX(
- scrollLeft,
+ const currentFrameLeft = getFrameFromX({
+ clientX: scrollLeft,
durationInFrames,
width,
- 'clamp'
- );
+ extrapolate: 'clamp',
+ });
const scrollPos = getScrollPositionForCursorOnLeftEdge({
nextFrame: frame,
durationInFrames,
@@ -134,7 +152,7 @@ export const ensureFrameIsInViewport = (
}
if (direction === 'page-right' || direction === 'page-left') {
- if (!isCursorInViewport(frame, durationInFrames)) {
+ if (!isCursorInViewport({frame, durationInFrames})) {
scrollToTimelineXOffset(
direction === 'page-left'
? getScrollPositionForCursorOnRightEdge({
@@ -234,12 +252,17 @@ export const getFrameWhileScrollingRight = ({
return Math.min(durationInFrames - 1, Math.max(nextFrame, currentFrame + 1));
};
-export const getFrameFromX = (
- clientX: number,
- durationInFrames: number,
- width: number,
- extrapolate: 'clamp' | 'extend'
-) => {
+export const getFrameFromX = ({
+ clientX,
+ durationInFrames,
+ width,
+ extrapolate,
+}: {
+ clientX: number;
+ durationInFrames: number;
+ width: number;
+ extrapolate: 'clamp' | 'extend';
+}) => {
const pos = clientX - TIMELINE_PADDING;
const frame = Math.round(
interpolate(
@@ -254,3 +277,38 @@ export const getFrameFromX = (
);
return frame;
};
+
+export const zoomAndPreserveCursor = ({
+ oldZoom,
+ newZoom,
+ currentFrame,
+ currentDurationInFrames,
+}: {
+ oldZoom: number;
+ newZoom: number;
+ currentFrame: number;
+ currentDurationInFrames: number;
+}) => {
+ const ratio = newZoom / oldZoom;
+ if (ratio === 1) {
+ return;
+ }
+
+ const {current} = scrollableRef;
+
+ if (!current) {
+ return;
+ }
+
+ const frameIncrement = getFrameIncrement(currentDurationInFrames);
+ const prevCursorPosition = frameIncrement * currentFrame + TIMELINE_PADDING;
+
+ const newCursorPosition =
+ ratio * (prevCursorPosition - TIMELINE_PADDING) + TIMELINE_PADDING;
+
+ current.scrollLeft += newCursorPosition - prevCursorPosition;
+ redrawTimelineSliderFast.current?.draw(
+ currentFrame,
+ (scrollableRef.current?.clientWidth ?? 0) * ratio
+ );
+};
diff --git a/packages/cli/src/editor/helpers/get-left-of-timeline-slider.ts b/packages/cli/src/editor/helpers/get-left-of-timeline-slider.ts
index e4df2f54ac..14b5043543 100644
--- a/packages/cli/src/editor/helpers/get-left-of-timeline-slider.ts
+++ b/packages/cli/src/editor/helpers/get-left-of-timeline-slider.ts
@@ -20,9 +20,11 @@ export const useGetXPositionOfItemInTimeline = () => {
return 0;
}
- const proportion = frame / (videoConfig.durationInFrames - 1);
-
- return proportion * (width - TIMELINE_PADDING * 2) + TIMELINE_PADDING;
+ return getXPositionOfItemInTimelineImperatively(
+ frame,
+ videoConfig.durationInFrames,
+ width
+ );
},
[videoConfig, width]
);
@@ -31,3 +33,13 @@ export const useGetXPositionOfItemInTimeline = () => {
return {get, width};
}, [get, width]);
};
+
+export const getXPositionOfItemInTimelineImperatively = (
+ frame: number,
+ duration: number,
+ width: number
+) => {
+ const proportion = frame / (duration - 1);
+
+ return proportion * (width - TIMELINE_PADDING * 2) + TIMELINE_PADDING;
+};
diff --git a/packages/cli/src/editor/state/timeline-zoom.tsx b/packages/cli/src/editor/state/timeline-zoom.tsx
index 0ba8058de0..0df8e77472 100644
--- a/packages/cli/src/editor/state/timeline-zoom.tsx
+++ b/packages/cli/src/editor/state/timeline-zoom.tsx
@@ -1,11 +1,16 @@
import React, {createContext, useMemo, useState} from 'react';
+import {
+ getCurrentDuration,
+ getCurrentFrame,
+} from '../components/Timeline/imperative-state';
+import {zoomAndPreserveCursor} from '../components/Timeline/timeline-scroll-logic';
export const TIMELINE_MIN_ZOOM = 1;
export const TIMELINE_MAX_ZOOM = 5;
export const TimelineZoomCtx = createContext<{
zoom: number;
- setZoom: React.Dispatch<React.SetStateAction<number>>;
+ setZoom: (prev: (prevZoom: number) => number) => void;
}>({
zoom: -1,
setZoom: () => {
@@ -21,7 +26,21 @@ export const TimelineZoomContext: React.FC<{
const value = useMemo(() => {
return {
zoom,
- setZoom,
+ setZoom: (callback: (prevZoom: number) => number) => {
+ setZoom((prevZoom) => {
+ const newZoom = Math.min(
+ TIMELINE_MAX_ZOOM,
+ Math.max(TIMELINE_MIN_ZOOM, callback(prevZoom))
+ );
+ zoomAndPreserveCursor({
+ oldZoom: prevZoom,
+ newZoom,
+ currentDurationInFrames: getCurrentDuration(),
+ currentFrame: getCurrentFrame(),
+ });
+ return newZoom;
+ });
+ },
};
}, [zoom]);