Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Add pinch-to-zoom on timeline

JonnyBurgerb965dddd28eb
diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsx
index 0aa6464716..884bf979d2 100644
--- a/packages/studio/src/components/Timeline/Timeline.tsx
+++ b/packages/studio/src/components/Timeline/Timeline.tsx
@@ -23,6 +23,7 @@ import {timelineVerticalScroll} from './timeline-refs';
import {TimelineDragHandler} from './TimelineDragHandler';
import {TimelineInOutPointer} from './TimelineInOutPointer';
import {TimelineList} from './TimelineList';
+import {TimelinePinchZoom} from './TimelinePinchZoom';
import {TimelinePlayCursorSyncer} from './TimelinePlayCursorSyncer';
import {TimelineScrollable} from './TimelineScrollable';
import {TimelineSlider} from './TimelineSlider';
@@ -113,6 +114,7 @@ const TimelineInner: React.FC = () => {
className={'css-reset ' + VERTICAL_SCROLLBAR_CLASSNAME}
>
<TimelineWidthProvider>
+ <TimelinePinchZoom />
<div style={inner}>
<SplitterContainer
orientation="vertical"
diff --git a/packages/studio/src/components/Timeline/TimelinePinchZoom.tsx b/packages/studio/src/components/Timeline/TimelinePinchZoom.tsx
new file mode 100644
index 0000000000..4c0a931622
--- /dev/null
+++ b/packages/studio/src/components/Timeline/TimelinePinchZoom.tsx
@@ -0,0 +1,84 @@
+import {useCallback, useContext, useEffect, type FC} from 'react';
+import {Internals} from 'remotion';
+import {useIsVideoComposition} from '../../helpers/is-current-selected-still';
+import {TimelineZoomCtx} from '../../state/timeline-zoom';
+import {scrollableRef, timelineVerticalScroll} from './timeline-refs';
+import {getFrameFromX} from './timeline-scroll-logic';
+
+const ZOOM_WHEEL_DELTA = 0.005;
+
+export const TimelinePinchZoom: FC = () => {
+ const isVideoComposition = useIsVideoComposition();
+ const videoConfig = Internals.useUnsafeVideoConfig();
+ const {canvasContent} = useContext(Internals.CompositionManager);
+ const {setZoom} = useContext(TimelineZoomCtx);
+
+ const onWheel = useCallback(
+ (e: WheelEvent) => {
+ if (!isVideoComposition) {
+ return;
+ }
+
+ const {ctrlKey, metaKey, clientX} = e;
+ if (!(ctrlKey || metaKey)) {
+ return;
+ }
+
+ if (!videoConfig || videoConfig.durationInFrames < 2) {
+ return;
+ }
+
+ if (!canvasContent || canvasContent.type !== 'composition') {
+ return;
+ }
+
+ const scrollEl = scrollableRef.current;
+ if (!scrollEl) {
+ return;
+ }
+
+ e.preventDefault();
+
+ const rect = scrollEl.getBoundingClientRect();
+ const clampedClientX = Math.min(Math.max(clientX, rect.left), rect.right);
+ const clientXInContent = clampedClientX + scrollEl.scrollLeft - rect.left;
+
+ const width = scrollEl.scrollWidth;
+ const anchorFrame = getFrameFromX({
+ clientX: clientXInContent,
+ durationInFrames: videoConfig.durationInFrames,
+ width,
+ extrapolate: 'clamp',
+ });
+
+ const {deltaMode} = e;
+ let {deltaY} = e;
+ if (deltaMode === WheelEvent.DOM_DELTA_LINE) {
+ deltaY *= 16;
+ } else if (deltaMode === WheelEvent.DOM_DELTA_PAGE) {
+ deltaY *= scrollEl.clientHeight;
+ }
+
+ setZoom(
+ canvasContent.compositionId,
+ (z) => z - deltaY * ZOOM_WHEEL_DELTA,
+ {anchorFrame},
+ );
+ },
+ [isVideoComposition, videoConfig, canvasContent, setZoom],
+ );
+
+ useEffect(() => {
+ const el = timelineVerticalScroll.current;
+ if (!el) {
+ return;
+ }
+
+ el.addEventListener('wheel', onWheel, {passive: false});
+ return () => {
+ el.removeEventListener('wheel', onWheel);
+ };
+ }, [onWheel]);
+
+ return null;
+};
diff --git a/packages/studio/src/components/Timeline/timeline-scroll-logic.ts b/packages/studio/src/components/Timeline/timeline-scroll-logic.ts
index 9e3be8ea79..9df768b198 100644
--- a/packages/studio/src/components/Timeline/timeline-scroll-logic.ts
+++ b/packages/studio/src/components/Timeline/timeline-scroll-logic.ts
@@ -286,11 +286,13 @@ export const zoomAndPreserveCursor = ({
newZoom,
currentFrame,
currentDurationInFrames,
+ anchorFrame,
}: {
oldZoom: number;
newZoom: number;
currentFrame: number;
currentDurationInFrames: number;
+ anchorFrame?: number;
}) => {
const ratio = newZoom / oldZoom;
if (ratio === 1) {
@@ -304,7 +306,8 @@ export const zoomAndPreserveCursor = ({
}
const frameIncrement = getFrameIncrement(currentDurationInFrames);
- const prevCursorPosition = frameIncrement * currentFrame + TIMELINE_PADDING;
+ const frameForScroll = anchorFrame ?? currentFrame;
+ const prevCursorPosition = frameIncrement * frameForScroll + TIMELINE_PADDING;
const newCursorPosition =
ratio * (prevCursorPosition - TIMELINE_PADDING) + TIMELINE_PADDING;
diff --git a/packages/studio/src/state/timeline-zoom.tsx b/packages/studio/src/state/timeline-zoom.tsx
index a5dc95a39a..ec82308a27 100644
--- a/packages/studio/src/state/timeline-zoom.tsx
+++ b/packages/studio/src/state/timeline-zoom.tsx
@@ -1,4 +1,4 @@
-import React, {createContext, useMemo, useState} from 'react';
+import React, {createContext, useCallback, useMemo, useState} from 'react';
import {
getCurrentDuration,
getCurrentFrame,
@@ -11,7 +11,11 @@ export const TIMELINE_MAX_ZOOM = 5;
export const TimelineZoomCtx = createContext<{
zoom: Record<string, number>;
- setZoom: (compositionId: string, prev: (prevZoom: number) => number) => void;
+ setZoom: (
+ compositionId: string,
+ prev: (prevZoom: number) => number,
+ options?: {anchorFrame?: number},
+ ) => void;
}>({
zoom: {},
setZoom: () => {
@@ -22,38 +26,45 @@ export const TimelineZoomCtx = createContext<{
export const TimelineZoomContext: React.FC<{
readonly children: React.ReactNode;
}> = ({children}) => {
- const [zoom, setZoom] = useState<Record<string, number>>(() =>
+ const [zoom, setZoomState] = useState<Record<string, number>>(() =>
getZoomFromLocalStorage(),
);
+ const setZoom = useCallback(
+ (
+ compositionId: string,
+ callback: (prevZoomLevel: number) => number,
+ options?: {anchorFrame?: number},
+ ) => {
+ setZoomState((prevZoomMap) => {
+ const newZoomWithFloatingPointErrors = Math.min(
+ TIMELINE_MAX_ZOOM,
+ Math.max(
+ TIMELINE_MIN_ZOOM,
+ callback(prevZoomMap[compositionId] ?? TIMELINE_MIN_ZOOM),
+ ),
+ );
+ const newZoom = Math.round(newZoomWithFloatingPointErrors * 10) / 10;
+
+ zoomAndPreserveCursor({
+ oldZoom: prevZoomMap[compositionId] ?? TIMELINE_MIN_ZOOM,
+ newZoom,
+ currentDurationInFrames: getCurrentDuration(),
+ currentFrame: getCurrentFrame(),
+ anchorFrame: options?.anchorFrame,
+ });
+ return {...prevZoomMap, [compositionId]: newZoom};
+ });
+ },
+ [],
+ );
+
const value = useMemo(() => {
return {
zoom,
- setZoom: (
- compositionId: string,
- callback: (prevZoomLevel: number) => number,
- ) => {
- setZoom((prevZoomMap) => {
- const newZoomWithFloatingPointErrors = Math.min(
- TIMELINE_MAX_ZOOM,
- Math.max(
- TIMELINE_MIN_ZOOM,
- callback(prevZoomMap[compositionId] ?? TIMELINE_MIN_ZOOM),
- ),
- );
- const newZoom = Math.round(newZoomWithFloatingPointErrors * 10) / 10;
-
- zoomAndPreserveCursor({
- oldZoom: prevZoomMap[compositionId] ?? TIMELINE_MIN_ZOOM,
- newZoom,
- currentDurationInFrames: getCurrentDuration(),
- currentFrame: getCurrentFrame(),
- });
- return {...prevZoomMap, [compositionId]: newZoom};
- });
- },
+ setZoom,
};
- }, [zoom]);
+ }, [zoom, setZoom]);
return (
<TimelineZoomCtx.Provider value={value}>