Skip to content

🎥 Make videos programmatically with React

wip: document camera motion blur

Marcus Stenbeck35ebd6d4cbf6
diff --git a/packages/docs/components/CameraMotionBlurExample/CameraMotionBlurExample.tsx b/packages/docs/components/CameraMotionBlurExample/CameraMotionBlurExample.tsx
index 05a3e5df47..b432f69fd3 100644
--- a/packages/docs/components/CameraMotionBlurExample/CameraMotionBlurExample.tsx
+++ b/packages/docs/components/CameraMotionBlurExample/CameraMotionBlurExample.tsx
@@ -64,10 +64,10 @@ export const Square: React.FC = () => {
const MyComposition = ({
shutterAngle,
- iterations,
+ samples,
}: {
shutterAngle: number;
- iterations: number;
+ samples: number;
}) => {
return (
<AbsoluteFill
@@ -80,7 +80,7 @@ const MyComposition = ({
<h1>Still</h1>
</AbsoluteFill>
<Freeze frame={20}>
- <CameraMotionBlur shutterAngle={shutterAngle} iterations={iterations}>
+ <CameraMotionBlur shutterAngle={shutterAngle} samples={samples}>
<Square />
</CameraMotionBlur>
</Freeze>
@@ -95,7 +95,7 @@ const MyComposition = ({
<AbsoluteFill style={{ padding: 30 }}>
<h1>Animation</h1>
</AbsoluteFill>
- <CameraMotionBlur shutterAngle={shutterAngle} iterations={iterations}>
+ <CameraMotionBlur shutterAngle={shutterAngle} samples={samples}>
<Square />
</CameraMotionBlur>
</div>
@@ -105,7 +105,7 @@ const MyComposition = ({
export const CameraMotionBlurExample: React.FC = () => {
const [shutterAngle, setShutterAngle] = useState(180);
- const [iterations, setIterations] = useState(10);
+ const [samples, setSamples] = useState(10);
return (
<div>
@@ -122,7 +122,7 @@ export const CameraMotionBlurExample: React.FC = () => {
}}
inputProps={{
shutterAngle,
- iterations,
+ samples,
}}
autoPlay
loop
@@ -153,13 +153,13 @@ export const CameraMotionBlurExample: React.FC = () => {
min={1}
max={100}
step={1}
- value={iterations}
+ value={samples}
style={{ width: 90, marginRight: 8 }}
- onChange={(e) => setIterations(Number(e.target.value))}
+ onChange={(e) => setSamples(Number(e.target.value))}
/>
<code>
- iterations={"{"}
- {iterations}
+ samples={"{"}
+ {samples}
{"}"}
</code>
</label>
diff --git a/packages/docs/docs/miscellaneous/snippets/player-in-iframe.md b/packages/docs/docs/miscellaneous/snippets/player-in-iframe.md
index 1d234ebff2..41b74f5fd0 100644
--- a/packages/docs/docs/miscellaneous/snippets/player-in-iframe.md
+++ b/packages/docs/docs/miscellaneous/snippets/player-in-iframe.md
@@ -3,12 +3,11 @@ title: "Embedding a <Player> into an <iframe>"
---
:::info
-Credit to [Marcus Stenbeck](https://twitter.com/marcusstenbeck) for creating this snippet.
+Credit to [@marcusstenbeck](https://twitter.com/marcusstenbeck) for creating this snippet.
:::
This snippet is useful if you want to isolate the global styles of your homepage from the global styles of the [`<Player>`](/docs/player), for example if you are using TailwindCSS.
-
```diff title="Usage"
- import { Player } from '@remotion/player';
+ import { IframePlayer } from 'path/to/IframePlayer';
@@ -18,78 +17,78 @@ This snippet is useful if you want to isolate the global styles of your homepage
```
```tsx title="IframePlayer.tsx"
-import {Player, PlayerRef, PlayerProps} from '@remotion/player';
+import { Player, PlayerProps, PlayerRef } from "@remotion/player";
import React, {
- forwardRef,
- MutableRefObject,
- useEffect,
- useRef,
- useState,
-} from 'react';
-import ReactDOM from 'react-dom';
-
-const className = '__player';
+ forwardRef,
+ MutableRefObject,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
+import ReactDOM from "react-dom";
+
+const className = "__player";
const borderNone: React.CSSProperties = {
- border: 'none',
+ border: "none",
};
const IframePlayerWithoutRef = <T,>(
- props: PlayerProps<T>,
- ref: MutableRefObject<PlayerRef>
+ props: PlayerProps<T>,
+ ref: MutableRefObject<PlayerRef>
) => {
- const [contentRef, setContentRef] = useState<HTMLIFrameElement | null>(null);
- const resizeObserverRef = useRef<ResizeObserver | null>(null);
-
- const mountNode = contentRef?.contentDocument?.body;
-
- useEffect(() => {
- if (!contentRef || !contentRef.contentDocument) return;
-
- // Remove margin and padding so player fits snugly
- contentRef.contentDocument.body.style.margin = '0';
- contentRef.contentDocument.body.style.padding = '0';
-
- // When player div is resized also resize iframe
- resizeObserverRef.current = new ResizeObserver(([playerEntry]) => {
- const playerRect = playerEntry.contentRect;
- contentRef.width = String(playerRect.width);
- contentRef.height = String(playerRect.height);
- });
-
- // The remotion player element
- const playerElement = contentRef.contentDocument.querySelector(
- '.' + className
- );
- if (!playerElement) {
- throw new Error(
- 'Player element not found. Add a "' +
- className +
- '" class to the <Player>.'
- );
- }
- // Watch the player element for size changes
- resizeObserverRef.current.observe(playerElement as Element);
- return () => {
- // ContentRef changed: unobserve!
- (resizeObserverRef.current as ResizeObserver).unobserve(
- playerElement as Element
- );
- };
- }, [contentRef]);
-
- const combinedClassName = `${className} ${props.className ?? ''}`.trim();
-
- return (
- // eslint-disable-next-line @remotion/warn-native-media-tag
- <iframe ref={setContentRef} style={borderNone}>
- {mountNode &&
- ReactDOM.createPortal(
- // @ts-expect-error PlayerProps are incorrectly typed
- <Player<T> {...props} ref={ref} className={combinedClassName} />,
- mountNode
- )}
- </iframe>
- );
+ const [contentRef, setContentRef] = useState<HTMLIFrameElement | null>(null);
+ const resizeObserverRef = useRef<ResizeObserver | null>(null);
+
+ const mountNode = contentRef?.contentDocument?.body;
+
+ useEffect(() => {
+ if (!contentRef || !contentRef.contentDocument) return;
+
+ // Remove margin and padding so player fits snugly
+ contentRef.contentDocument.body.style.margin = "0";
+ contentRef.contentDocument.body.style.padding = "0";
+
+ // When player div is resized also resize iframe
+ resizeObserverRef.current = new ResizeObserver(([playerEntry]) => {
+ const playerRect = playerEntry.contentRect;
+ contentRef.width = String(playerRect.width);
+ contentRef.height = String(playerRect.height);
+ });
+
+ // The remotion player element
+ const playerElement = contentRef.contentDocument.querySelector(
+ "." + className
+ );
+ if (!playerElement) {
+ throw new Error(
+ 'Player element not found. Add a "' +
+ className +
+ '" class to the <Player>.'
+ );
+ }
+ // Watch the player element for size changes
+ resizeObserverRef.current.observe(playerElement as Element);
+ return () => {
+ // ContentRef changed: unobserve!
+ (resizeObserverRef.current as ResizeObserver).unobserve(
+ playerElement as Element
+ );
+ };
+ }, [contentRef]);
+
+ const combinedClassName = `${className} ${props.className ?? ""}`.trim();
+
+ return (
+ // eslint-disable-next-line @remotion/warn-native-media-tag
+ <iframe ref={setContentRef} style={borderNone}>
+ {mountNode &&
+ ReactDOM.createPortal(
+ // @ts-expect-error PlayerProps are incorrectly typed
+ <Player<T> {...props} ref={ref} className={combinedClassName} />,
+ mountNode
+ )}
+ </iframe>
+ );
};
export const IframePlayer = forwardRef(IframePlayerWithoutRef);
diff --git a/packages/docs/docs/motion-blur/camera-motion-blur.md b/packages/docs/docs/motion-blur/camera-motion-blur.md
index 4b2f1dfdb9..75df7f6cc6 100644
--- a/packages/docs/docs/motion-blur/camera-motion-blur.md
+++ b/packages/docs/docs/motion-blur/camera-motion-blur.md
@@ -12,22 +12,62 @@ const BlueSquare: React.FC = () => <div></div>
// - BlueSquare
```
-TODO
+The `<CameraMotionBlur>` produces natural looking motion blur similar to what would be produced by
+a film camera.
For this technique to work, the children must be absolutely positioned so many layers can be created without influencing the layout.
You can use the [`<AbsoluteFill>`](/docs/absolute-fill) component to absolutely position content.
+:::caution
+The technique is destructive to colors. It is recommended to keep the `samples` property as low as
+possible and carefully inspect that the output is of acceptable quality.
+:::
+
## API
Wrap your content in `<CameraMotionBlur>` and optionally add the following props in addition.
-### `iterations (optional)`
+### `shutterAngle (optional) (default = 180)`
+
+Controls the amount of blur.
+
+A lower value will result in less blur and a higher value will result in more.
+
+The blur also depends on the frames per second (FPS). Higher FPS will naturally have less blur and
+lower FPS will have more blur.
+
+In movies and TV common values are (FPS/shutter angle):
+
+- 24 fps / 180&deg; or 90&deg;
+- 25 fps / 180&deg; or 90&deg;
+- 30 fps / 180&deg; or 90&deg;
+- 50 fps / 180&deg; or 90&deg;
+- 60 fps / 180&deg; or 90&deg;
+
+<details>
+<summary>What is "shutter angle"?
+</summary>
+Many analog film cameras use rotating discs with partial cut-outs to block or let light through to
+expose the analog film. Zero degrees is equal to completely blocking the light, and 360 degrees is
+the same as not blocking any light at all.
+
+The most common values used in the film industry are 90 and 180 degrees. These values are the same
+as what you've experienced in most movies.
+
+Read more here: [Rotary disc shutter on Wikipedia](https://en.wikipedia.org/wiki/Rotary_disc_shutter)
+
+</details>
+
+### `samples (optional) (default = 10)`
-TODO
+The final image is an average of the samples. For a value of `10` the component will render ten
+frames with different time offsets and combine into a final image.
-### `shutterAngle (optional)`
+:::caution
+A high number will produce a higher quality blur at the cost of image quality. See example below.
-TODO
+Recommended values: 5-10.
+:::
## Example usage
@@ -39,7 +79,7 @@ import { AbsoluteFill } from "remotion";
export const MyComposition = () => {
return (
- <CameraMotionBlur>
+ <CameraMotionBlur shutterAngle={180} samples={10}>
<AbsoluteFill
style={{
backgroundColor: "white",
@@ -64,7 +104,8 @@ export const MyComposition = () => {
## Credits
-TODO
+The technique was developed by [@marcusstenbeck](https://twitter.com/marcusstenbeck) and inspired by
+the `<Trail>` component developed by [@UmungoBungo](https://github.com/UmungoBungo).
## See also
diff --git a/packages/motion-blur/src/CameraMotionBlur.tsx b/packages/motion-blur/src/CameraMotionBlur.tsx
index 11598aa804..b526c05e5b 100644
--- a/packages/motion-blur/src/CameraMotionBlur.tsx
+++ b/packages/motion-blur/src/CameraMotionBlur.tsx
@@ -4,35 +4,35 @@ import {AbsoluteFill, Freeze, useCurrentFrame} from 'remotion';
export type CameraMotionBlurProps = {
children: React.ReactNode;
shutterAngle?: number;
- iterations?: number;
+ samples?: number;
};
export const CameraMotionBlur: React.FC<CameraMotionBlurProps> = ({
children,
shutterAngle = 180,
- iterations = 10,
+ samples = 50,
}: CameraMotionBlurProps) => {
const currentFrame = useCurrentFrame();
if (
- typeof iterations !== 'number' ||
- Number.isNaN(iterations) ||
- !Number.isFinite(iterations)
+ typeof samples !== 'number' ||
+ Number.isNaN(samples) ||
+ !Number.isFinite(samples)
) {
throw new TypeError(
- `"iterations" must be a number, but got ${JSON.stringify(iterations)}`
+ `"samples" must be a number, but got ${JSON.stringify(samples)}`
);
}
- if (iterations % 1 !== 0) {
+ if (samples % 1 !== 0) {
throw new TypeError(
- `"iterations" must be an integer, but got ${JSON.stringify(iterations)}`
+ `"samples" must be an integer, but got ${JSON.stringify(samples)}`
);
}
- if (iterations < 0) {
+ if (samples < 0) {
throw new TypeError(
- `"iterations" must be non-negative, but got ${JSON.stringify(iterations)}`
+ `"samples" must be non-negative, but got ${JSON.stringify(samples)}`
);
}
@@ -58,21 +58,19 @@ export const CameraMotionBlur: React.FC<CameraMotionBlurProps> = ({
return (
<AbsoluteFill style={{isolation: 'isolate'}}>
- {new Array(iterations).fill(true).map((_, i) => {
- const iteration = i + 1;
- const iterationFrameOffset = shutterFraction * (iteration / iterations);
+ {new Array(samples).fill(true).map((_, i) => {
+ const sample = i + 1;
+ const sampleFrameOffset = shutterFraction * (sample / samples);
return (
<AbsoluteFill
key={`frame-${i.toString()}`}
style={{
- // @ts-ignore 'plus-lighter' is assignable, google "MDN plus-lighter"
+ // @ts-expect-error 'plus-lighter' is assignable, google "MDN plus-lighter"
mixBlendMode: 'plus-lighter',
- opacity: 1 / iterations,
+ filter: `opacity(${1 / samples})`,
}}
>
- <Freeze frame={currentFrame - iterationFrameOffset}>
- {children}
- </Freeze>
+ <Freeze frame={currentFrame - sampleFrameOffset}>{children}</Freeze>
</AbsoluteFill>
);
})}