Skip to content

🎥 Make videos programmatically with React

seems right :thinking_face:

Jonny Burgerfc18d80b2a31
diff --git a/packages/cli/src/editor/components/Canvas.tsx b/packages/cli/src/editor/components/Canvas.tsx
index 859f64ec9d..e5c7769375 100644
--- a/packages/cli/src/editor/components/Canvas.tsx
+++ b/packages/cli/src/editor/components/Canvas.tsx
@@ -3,7 +3,10 @@ import {calculateScale} from '@remotion/player/src/calculate-scale';
import React, {useCallback, useContext, useEffect, useRef} from 'react';
import {smoothenZoom, unsmoothenZoom} from '../../smooth-zoom';
import {BACKGROUND} from '../helpers/colors';
-import {getEffectiveTranslation} from '../helpers/get-effective-translation';
+import {
+ getCenterPointWhileScrolling,
+ getEffectiveTranslation,
+} from '../helpers/get-effective-translation';
import {useDimensions, useIsStill} from '../helpers/is-current-selected-still';
import {PreviewSizeContext} from '../state/preview-size';
import {StillPreview, VideoPreview} from './Preview';
@@ -54,6 +57,7 @@ export const Canvas: React.FC = () => {
compositionWidth: dimensions.width,
previewSize: prevSize.size,
});
+
const effectiveTranslation = getEffectiveTranslation({
translation: prevSize.translation,
canvasSize: size,
@@ -61,29 +65,46 @@ export const Canvas: React.FC = () => {
compositionWidth: dimensions.width,
scale,
});
+
+ // Zoom in/out
if (e.ctrlKey || e.metaKey) {
- const oldSize =
- prevSize.size === 'auto'
- ? calculateScale({
- canvasSize: size,
- compositionHeight: dimensions.height,
- compositionWidth: dimensions.width,
- previewSize: prevSize.size,
- })
- : prevSize.size;
- const unsmoothened = smoothenZoom(oldSize);
- const added = unsmoothened + e.deltaY * ZOOM_PX_FACTOR;
- const smoothened = unsmoothenZoom(added);
+ const oldSize = prevSize.size === 'auto' ? scale : prevSize.size;
+ const smoothened = smoothenZoom(oldSize);
+ const added = smoothened + e.deltaY * ZOOM_PX_FACTOR;
+ const unsmoothened = unsmoothenZoom(added);
+
+ const {centerX, centerY} = getCenterPointWhileScrolling({
+ size,
+ clientX: e.clientX,
+ clientY: e.clientY,
+ compositionWidth: dimensions.width,
+ compositionHeight: dimensions.height,
+ scale,
+ translation: effectiveTranslation,
+ });
+
+ const zoomDifference = unsmoothened - oldSize;
+
+ const uvCoordinatesX = centerX / dimensions.width;
+ const uvCoordinatesY = centerY / dimensions.height;
+
+ const correctionLeft =
+ -uvCoordinatesX * (zoomDifference * dimensions.width) +
+ (1 - uvCoordinatesX) * zoomDifference * dimensions.width;
+ const correctionTop =
+ -uvCoordinatesY * (zoomDifference * dimensions.height) +
+ (1 - uvCoordinatesY) * zoomDifference * dimensions.height;
return {
translation: {
- x: effectiveTranslation.x,
- y: effectiveTranslation.y,
+ x: prevSize.translation.x - correctionLeft / 2,
+ y: prevSize.translation.y - correctionTop / 2,
},
- size: smoothened,
+ size: unsmoothened,
};
}
+ // Pan
return {
...prevSize,
translation: {
diff --git a/packages/cli/src/editor/helpers/get-effective-translation.ts b/packages/cli/src/editor/helpers/get-effective-translation.ts
index 8b5cb902c4..313cd76814 100644
--- a/packages/cli/src/editor/helpers/get-effective-translation.ts
+++ b/packages/cli/src/editor/helpers/get-effective-translation.ts
@@ -1,4 +1,5 @@
import type {Size} from '@remotion/player';
+import type {Translation} from '@remotion/player/src/utils/preview-size';
const getEffectiveXTranslation = ({
canvasSize,
@@ -81,3 +82,43 @@ export const getEffectiveTranslation = ({
}),
};
};
+
+export const getCenterPointWhileScrolling = ({
+ size,
+ clientX,
+ clientY,
+ compositionWidth,
+ compositionHeight,
+ scale,
+ translation,
+}: {
+ size: Size;
+ clientX: number;
+ clientY: number;
+ compositionWidth: number;
+ compositionHeight: number;
+ scale: number;
+ translation: Translation;
+}) => {
+ const mouseLeft = clientX - size.left;
+ const mouseTop = clientY - size.top;
+
+ const contentLeftPoint =
+ size.width / 2 - (compositionWidth * scale) / 2 - translation.x;
+ const contentTopPoint =
+ size.height / 2 - (compositionHeight * scale) / 2 - translation.y;
+
+ const offsetFromVideoLeft = Math.min(
+ compositionWidth,
+ Math.max(0, (mouseLeft - contentLeftPoint) / scale)
+ );
+ const offsetFromVideoTop = Math.min(
+ compositionHeight,
+ Math.max(0, (mouseTop - contentTopPoint) / scale)
+ );
+
+ return {
+ centerX: offsetFromVideoLeft,
+ centerY: offsetFromVideoTop,
+ };
+};