Skip to content

🎥 Make videos programmatically with React

three canvas is scaling

Jonny Burger1c7e23b4d2c5
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index 321a8ddeaa..e519ae8cc9 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -57,8 +57,8 @@ export const Composition = <T,>({
);
}
- validateDimension(width, 'width');
- validateDimension(height, 'height');
+ validateDimension(width, 'width', 'Composition');
+ validateDimension(height, 'height', 'Composition');
validateDurationInFrames(durationInFrames);
validateFps(fps);
registerComposition<T>({
diff --git a/packages/core/src/validation/validate-dimensions.ts b/packages/core/src/validation/validate-dimensions.ts
index 7e95665afa..636aee2f78 100644
--- a/packages/core/src/validation/validate-dimensions.ts
+++ b/packages/core/src/validation/validate-dimensions.ts
@@ -1,31 +1,35 @@
-export const validateDimension = (amount: number, nameOfProp: string) => {
+export const validateDimension = (
+ amount: number,
+ nameOfProp: string,
+ componentName: string
+) => {
if (typeof amount !== 'number') {
throw new Error(
- `The "${nameOfProp}" of a composition must be a number, but you passed a value of type ${typeof amount}`
+ `The "${nameOfProp}" prop of the <${componentName}/> component must be a number, but you passed a value of type ${typeof amount}`
);
}
if (isNaN(amount)) {
throw new TypeError(
- `The "${nameOfProp}" of a composition must not be NaN, but is NaN.`
+ `The "${nameOfProp}" prop of the <${componentName}/> component must not be NaN, but is NaN.`
);
}
if (!Number.isFinite(amount)) {
throw new TypeError(
- `The "${nameOfProp}" of a composition must be finite, but is ${amount}.`
+ `The "${nameOfProp}" prop of the <${componentName}/> component must be finite, but is ${amount}.`
);
}
if (amount % 1 !== 0) {
throw new TypeError(
- `The "${nameOfProp}" of a composition must be an integer, but is ${amount}.`
+ `The "${nameOfProp}" prop of the <${componentName}/> component must be an integer, but is ${amount}.`
);
}
if (amount <= 0) {
throw new TypeError(
- `The "${nameOfProp}" of a composition must be positive, but got ${amount}.`
+ `The "${nameOfProp}" prop of the <${componentName}/> component must be positive, but got ${amount}.`
);
}
};
diff --git a/packages/docs/docs/three-canvas.md b/packages/docs/docs/three-canvas.md
index ec0879f483..6e82074f17 100644
--- a/packages/docs/docs/three-canvas.md
+++ b/packages/docs/docs/three-canvas.md
@@ -13,6 +13,8 @@ Since React Three Fiber is a custom renderer, normally the React contexts that s
Instead of using React Three Fibers `useFrame` API, you can (and must) write your animations fully declaratively using Remotions `useCurrentFrame` API. This will ensure that you can scrub back and forth in the timeline and pause the animation.
+A browser bug [would normally cause the layout to be broken](https://github.com/pmndrs/react-three-fiber/issues/1394) because we apply a `scale` transform to the canvas in preview mode. To work around this problem, the `<ThreeCanvas />` requires the `width` and `height` props to be set.
+
## Example
A spinning, color changing, scaling cube. This example can also be found in the `examples` folder of the Remotion repo.
@@ -24,36 +26,33 @@ import {ThreeCanvas} from '@remotion/three';
const ThreeBasic: React.FC = () => {
const frame = useCurrentFrame();
+ const {width, height} = useVideoConfig();
return (
- <div style={{display: 'flex', width: '100%', backgroundColor: 'white'}}>
- <ThreeCanvas
- orthographic={false}
- camera={{fov: 75, position: [0, 0, 470]}}
+ <ThreeCanvas
+ orthographic={false}
+ width={width}
+ height={height}
+ style={{backgroundColor: 'white'}}
+ camera={{fov: 75, position: [0, 0, 470]}}
+ >
+ <ambientLight intensity={0.15} />
+ <pointLight args={[undefined, 0.4]} position={[200, 200, 0]} />
+ <mesh
+ position={[0, 0, 0]}
+ rotation={[frame * 0.06 * 0.5, frame * 0.07 * 0.5, frame * 0.08 * 0.5]}
+ scale={interpolate(Math.sin(frame / 10), [-1, 1], [0.8, 1.2])}
>
- <ambientLight intensity={0.15} />
- <pointLight args={[undefined, 0.4]} position={[200, 200, 0]} />
-
- <mesh
- position={[0, 0, 0]}
- rotation={[
- frame * 0.06 * 0.5,
- frame * 0.07 * 0.5,
- frame * 0.08 * 0.5,
+ <boxGeometry args={[100, 100, 100]} />
+ <meshStandardMaterial
+ color={[
+ Math.sin(frame * 0.12) * 0.5 + 0.5,
+ Math.cos(frame * 0.11) * 0.5 + 0.5,
+ Math.sin(frame * 0.08) * 0.5 + 0.5,
]}
- scale={interpolate(Math.sin(frame / 10), [-1, 1], [0.8, 1.2])}
- >
- <boxGeometry args={[100, 100, 100]} />
- <meshStandardMaterial
- color={[
- Math.sin(frame * 0.12) * 0.5 + 0.5,
- Math.cos(frame * 0.11) * 0.5 + 0.5,
- Math.sin(frame * 0.08) * 0.5 + 0.5,
- ]}
- />
- </mesh>
- </ThreeCanvas>
- </div>
+ />
+ </mesh>
+ </ThreeCanvas>
);
};
diff --git a/packages/example/src/ThreeBasic/Mesh.tsx b/packages/example/src/ThreeBasic/Mesh.tsx
index 4107d7e4f8..e1d982e251 100644
--- a/packages/example/src/ThreeBasic/Mesh.tsx
+++ b/packages/example/src/ThreeBasic/Mesh.tsx
@@ -1,24 +1,21 @@
-import {Stage} from '@react-three/drei';
import React from 'react';
import {interpolate, useCurrentFrame} from 'remotion';
export const Mesh: React.FC = () => {
const frame = useCurrentFrame();
return (
- <Stage>
- <mesh
- position={[0, 0, 0]}
- rotation={[frame * 0.06 * 0.5, frame * 0.07 * 0.5, frame * 0.08 * 0.5]}
- scale={interpolate(Math.sin(frame / 10), [-1, 1], [0.8, 1.2])}
- >
- <boxGeometry args={[100, 100, 100]} />
- <meshStandardMaterial
- color={[
- Math.sin(frame * 0.12) * 0.5 + 0.5,
- Math.cos(frame * 0.11) * 0.5 + 0.5,
- Math.sin(frame * 0.08) * 0.5 + 0.5,
- ]}
- />
- </mesh>
- </Stage>
+ <mesh
+ position={[0, 0, 0]}
+ rotation={[frame * 0.06 * 0.5, frame * 0.07 * 0.5, frame * 0.08 * 0.5]}
+ scale={interpolate(Math.sin(frame / 10), [-1, 1], [0.8, 1.2])}
+ >
+ <boxGeometry args={[100, 100, 100]} />
+ <meshStandardMaterial
+ color={[
+ Math.sin(frame * 0.12) * 0.5 + 0.5,
+ Math.cos(frame * 0.11) * 0.5 + 0.5,
+ Math.sin(frame * 0.08) * 0.5 + 0.5,
+ ]}
+ />
+ </mesh>
);
};
diff --git a/packages/example/src/ThreeBasic/index.tsx b/packages/example/src/ThreeBasic/index.tsx
index 6a4f5ffb16..48fbe6f96f 100644
--- a/packages/example/src/ThreeBasic/index.tsx
+++ b/packages/example/src/ThreeBasic/index.tsx
@@ -1,17 +1,22 @@
import {ThreeCanvas} from '@remotion/three';
import React from 'react';
+import {useVideoConfig} from 'remotion';
import {Mesh} from './Mesh';
const ThreeBasic: React.FC = () => {
+ const {width, height} = useVideoConfig();
return (
- <div style={{display: 'flex', width: '100%', backgroundColor: 'white'}}>
- <ThreeCanvas
- orthographic={false}
- camera={{fov: 75, position: [0, 0, 470]}}
- >
- <Mesh />
- </ThreeCanvas>
- </div>
+ <ThreeCanvas
+ width={width}
+ height={height}
+ style={{backgroundColor: 'white'}}
+ orthographic={false}
+ camera={{fov: 75, position: [0, 0, 470]}}
+ >
+ <ambientLight intensity={0.15} />
+ <pointLight args={[undefined, 0.4]} position={[200, 200, 0]} />
+ <Mesh />
+ </ThreeCanvas>
);
};
diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsx
index e8636c4392..57010b112e 100644
--- a/packages/player/src/Player.tsx
+++ b/packages/player/src/Player.tsx
@@ -98,8 +98,8 @@ export const PlayerFn = <T,>(
);
}
- Internals.validateDimension(compositionHeight, 'compositionHeight');
- Internals.validateDimension(compositionWidth, 'compositionWidth');
+ Internals.validateDimension(compositionHeight, 'compositionHeight', 'Player');
+ Internals.validateDimension(compositionWidth, 'compositionWidth', 'Player');
Internals.validateDurationInFrames(durationInFrames);
Internals.validateFps(fps);
diff --git a/packages/three/src/ThreeCanvas.tsx b/packages/three/src/ThreeCanvas.tsx
index 5a6c662790..e300ed54e0 100644
--- a/packages/three/src/ThreeCanvas.tsx
+++ b/packages/three/src/ThreeCanvas.tsx
@@ -1,14 +1,36 @@
-import { Canvas } from '@react-three/fiber';
-import React from 'react';
+import { Canvas, useThree } from '@react-three/fiber';
+import React, { useLayoutEffect, useState } from 'react';
import { Internals } from 'remotion';
-export type ThreeCanvasProps = React.ComponentProps<typeof Canvas>;
+export type ThreeCanvasProps = React.ComponentProps<typeof Canvas> & {
+ width: number;
+ height: number;
+};
+
+const Scale = ({ width, height }: { width: number; height: number }) => {
+ const { set, setSize: threeSetSize } = useThree();
+ const [setSize] = useState(() => threeSetSize);
+ useLayoutEffect(() => {
+ setSize(width, height);
+ set({ setSize: () => null });
+ return () => set({ setSize });
+ }, [setSize, width, height, set]);
+ return null;
+};
export const ThreeCanvas = (props: ThreeCanvasProps) => {
- const { children, ...rest } = props;
+ const { children, width, height, style, ...rest } = props;
+ Internals.validateDimension(width, 'width', 'ThreeCanvas');
+ Internals.validateDimension(height, 'height', 'ThreeCanvas');
const contexts = Internals.useRemotionContexts();
+ const actualStyle = {
+ width: props.width,
+ height: props.height,
+ ...(style ?? {}),
+ };
return (
- <Canvas {...rest}>
+ <Canvas style={actualStyle} {...rest}>
+ <Scale width={width} height={height} />
<Internals.RemotionContextProvider contexts={contexts}>
{children}
</Internals.RemotionContextProvider>