Skip to content

🎥 Make videos programmatically with React

refine props and documentation

Jonny Burgerbb068db25319
diff --git a/packages/docs/components/MotionBlurExample/MotionBlurExample.tsx b/packages/docs/components/MotionBlurExample/MotionBlurExample.tsx
index 34c5ae145a..cb0ce7b10b 100644
--- a/packages/docs/components/MotionBlurExample/MotionBlurExample.tsx
+++ b/packages/docs/components/MotionBlurExample/MotionBlurExample.tsx
@@ -2,21 +2,32 @@ import { Player } from "@remotion/player";
import React, { useState } from "react";
import {
AbsoluteFill,
+ Freeze,
interpolate,
spring,
useCurrentFrame,
useVideoConfig,
} from "remotion";
-import { MotionBlur } from '@remotion/motion-blur';
+import { MotionBlur } from "@remotion/motion-blur";
const square: React.CSSProperties = {
height: 150,
width: 150,
- backgroundColor: '#0b84f3',
+ backgroundColor: "#0b84f3",
borderRadius: 14,
};
+const row: React.CSSProperties = {
+ display: "flex",
+ flexDirection: "row",
+ alignItems: "center",
+};
+
+const spacer: React.CSSProperties = {
+ width: 40,
+};
+
export const Square: React.FC = () => {
const frame = useCurrentFrame();
const { fps, height } = useVideoConfig();
@@ -35,8 +46,8 @@ export const Square: React.FC = () => {
return (
<AbsoluteFill
style={{
- justifyContent: 'center',
- alignItems: 'center',
+ justifyContent: "center",
+ alignItems: "center",
transform: `translateY(${y}px) rotate(${rotate}rad)`,
}}
>
@@ -46,34 +57,60 @@ export const Square: React.FC = () => {
};
const MyComposition = ({
- opacity,
- iterations,
- frameDelay,
+ blurOpacity,
+ layers,
+ lagInFrames,
}: {
- opacity: number,
- iterations: number,
- frameDelay: number,
+ blurOpacity: number;
+ layers: number;
+ lagInFrames: number;
}) => {
return (
<AbsoluteFill
style={{
- backgroundColor: 'white',
- justifyContent: 'center',
- alignItems: 'center',
+ flexDirection: "row",
}}
>
- <MotionBlur opacity={opacity} frameDelay={frameDelay} iterations={iterations}>
- <Square />
- </MotionBlur>
+ <div style={{ flex: 1, position: "relative" }}>
+ <AbsoluteFill style={{ padding: 30 }}>
+ <h1>Still</h1>
+ </AbsoluteFill>
+ <Freeze frame={38}>
+ <MotionBlur
+ blurOpacity={blurOpacity}
+ lagInFrames={lagInFrames}
+ layers={layers}
+ >
+ <Square />
+ </MotionBlur>
+ </Freeze>
+ </div>
+ <div
+ style={{
+ width: 1,
+ background: "var(--ifm-color-emphasis-300)",
+ }}
+ />
+ <div style={{ flex: 1, position: "relative" }}>
+ <AbsoluteFill style={{ padding: 30 }}>
+ <h1>Animation</h1>
+ </AbsoluteFill>
+ <MotionBlur
+ blurOpacity={blurOpacity}
+ lagInFrames={lagInFrames}
+ layers={layers}
+ >
+ <Square />
+ </MotionBlur>
+ </div>
</AbsoluteFill>
);
};
-
export const MotionBlurExample: React.FC = () => {
- const [opacity, setOpacity] = useState(1);
- const [frameDelay, setFrameDelay] = useState(0.3);
- const [iterations, setIterations] = useState(50);
+ const [blurOpacity, setBlurOpacity] = useState(1);
+ const [lagInFrames, setFrameDelay] = useState(0.3);
+ const [layers, setLayers] = useState(50);
return (
<div>
@@ -81,54 +118,73 @@ export const MotionBlurExample: React.FC = () => {
component={MyComposition}
compositionWidth={1280}
compositionHeight={720}
- controls
- durationInFrames={150}
+ durationInFrames={70}
fps={30}
style={{
width: "100%",
+ border: "1px solid var(--ifm-color-emphasis-300)",
+ borderRadius: "var(--ifm-pre-border-radius)",
}}
inputProps={{
- opacity,
- frameDelay,
- iterations,
+ blurOpacity,
+ lagInFrames,
+ layers,
}}
+ autoPlay
loop
/>
- <div style={{ marginTop: '50px' }}>
- <label>
+ <br />
+ <div style={{ ...row, fontSize: 20 }}>
+ <label style={row}>
<input
- type="number"
+ type="range"
min={0}
- step={0.05}
- value={opacity}
+ max={100}
+ value={layers}
style={{ width: 90, marginRight: 8, padding: 8 }}
- onChange={(e) => setOpacity(Number(e.target.value))}
+ onChange={(e) => setLayers(Number(e.target.value))}
/>
- Opacity
+ <code>
+ layers={"{"}
+ {layers}
+ {"}"}
+ </code>
</label>
- </div>
- <div>
- <label>
+ <div style={spacer} />
+
+ <label style={row}>
<input
- type="number"
+ type="range"
min={0}
- value={iterations}
- style={{ width: 90, marginRight: 8, padding: 8 }}
- onChange={(e) => setIterations(Number(e.target.value))}
+ max={1}
+ step={0.05}
+ value={blurOpacity}
+ style={{ width: 90, marginRight: 8 }}
+ onChange={(e) => setBlurOpacity(Number(e.target.value))}
/>
- Iterations
+ <code>
+ blurOpacity={"{"}
+ {blurOpacity}
+ {"}"}
+ </code>
</label>
- </div>
- <div>
- <label>
+ <div style={spacer} />
+
+ <label style={row}>
<input
- type="number"
+ type="range"
min={0}
- value={frameDelay}
+ max={4}
+ step={0.1}
+ value={lagInFrames}
style={{ width: 90, marginRight: 8, padding: 8 }}
onChange={(e) => setFrameDelay(Number(e.target.value))}
/>
- Frame Delay
+ <code>
+ lagInFrames={"{"}
+ {lagInFrames}
+ {"}"}
+ </code>
</label>
</div>
</div>
diff --git a/packages/docs/docs/motion-blur/component.md b/packages/docs/docs/motion-blur/component.md
index 782bd32520..f695251728 100644
--- a/packages/docs/docs/motion-blur/component.md
+++ b/packages/docs/docs/motion-blur/component.md
@@ -9,55 +9,60 @@ const BlueSquare: React.FC = () => <div></div>
// - BlueSquare
```
-Credits to [@UmungoBungo](https://github.com/UmungoBungo) for inventing this technique.
+The `<MotionBlur>` component duplicates it's children and adds a time offset to each layer in order to create a motion blur effect.
+
+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.
-The motion blur is achieved by rendering the previous frame at a lower opacity multiple times to create a blur effect.
+## API
-For this technique to work, the element must be wrapped in an [`<AbsoluteFill>`](https://remotion.dev/docs/absolute-fill) so the previous frames can be layered under the original.
+Wrap your content in `<MotionBlur>` and add the following props in addition.
-Motion blur can be added to any element by wrapping it in `<MotionBlur>`:
+### `layers`
-## API
+How many layers are added below the content. Must be an integer
-### `frameDelay`
-The `frameDelay` parameter determines how many frames each layer is lagging behind.
+### `lagInFrames`
-### `opacity`
-The `opacity` defines the highest opacity of a layer. The lowest opacity is 0.
+How many frames each layer is lagging behind the last one. Can also a floating point number.
-### `iterations`
-The `iterations` parameter defines how many layers are added together.
+### `blurOpacity`
+The `blurOpacity` defines the highest opacity of a layer. The lowest opacity is 0.
## Example usage
```tsx twoslash
// @include: example-BlueSquare
// ---cut---
-import React from 'react';
-import {AbsoluteFill} from 'remotion';
-import {MotionBlur} from '@remotion/motion-blur';
+import { MotionBlur } from "@remotion/motion-blur";
+import { AbsoluteFill } from "remotion";
export const MyComposition = () => {
return (
- <AbsoluteFill
- style={{
- backgroundColor: 'white',
- justifyContent: 'center',
- alignItems: 'center',
- }}
- >
- <MotionBlur opacity={1} frameDelay={0.1} iterations={50}>
+ <MotionBlur layers={50} lagInFrames={0.1} blurOpacity={1}>
+ <AbsoluteFill
+ style={{
+ backgroundColor: "white",
+ justifyContent: "center",
+ alignItems: "center",
+ }}
+ >
<BlueSquare />
- </MotionBlur>
- </AbsoluteFill>
+ </AbsoluteFill>
+ </MotionBlur>
);
};
```
+
## Demo
<MotionBlurExample />
+## Credits
+
+Credits to [@UmungoBungo](https://github.com/UmungoBungo) for inventing this technique.
+
## See also
-- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/motion-blur/src/MotionBlur.tsx)
\ No newline at end of file
+- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/motion-blur/src/MotionBlur.tsx)
diff --git a/packages/motion-blur/src/MotionBlur.tsx b/packages/motion-blur/src/MotionBlur.tsx
index fec2dc0678..eecaaff7be 100644
--- a/packages/motion-blur/src/MotionBlur.tsx
+++ b/packages/motion-blur/src/MotionBlur.tsx
@@ -1,33 +1,78 @@
import React from 'react';
-import { AbsoluteFill, Freeze, useCurrentFrame } from 'remotion';
+import {AbsoluteFill, Freeze, useCurrentFrame} from 'remotion';
type MotionBlurProps = {
children: React.ReactNode;
- iterations: number;
- frameDelay: number;
- opacity: number;
+ layers: number;
+ lagInFrames: number;
+ blurOpacity: number;
};
export const MotionBlur: React.FC<MotionBlurProps> = ({
children,
- iterations,
- frameDelay,
- opacity
+ layers,
+ lagInFrames,
+ blurOpacity,
}: MotionBlurProps) => {
const frame = useCurrentFrame();
+
+ if (
+ typeof layers !== 'number' ||
+ Number.isNaN(layers) ||
+ !Number.isFinite(layers)
+ ) {
+ throw new TypeError(
+ `"layers" must be a number, but got ${JSON.stringify(layers)}`
+ );
+ }
+
+ if (layers % 1 !== 0) {
+ throw new TypeError(
+ `"layers" must be an integer, but got ${JSON.stringify(layers)}`
+ );
+ }
+
+ if (layers < 0) {
+ throw new TypeError(
+ `"layers" must be non-negative, but got ${JSON.stringify(layers)}`
+ );
+ }
+
+ if (
+ typeof blurOpacity !== 'number' ||
+ Number.isNaN(blurOpacity) ||
+ !Number.isFinite(blurOpacity)
+ ) {
+ throw new TypeError(
+ `"blurOpacity" must be a number, but got ${JSON.stringify(blurOpacity)}`
+ );
+ }
+
+ if (
+ typeof lagInFrames !== 'number' ||
+ Number.isNaN(lagInFrames) ||
+ !Number.isFinite(lagInFrames)
+ ) {
+ throw new TypeError(
+ `"lagInFrames" must be a number, but got ${JSON.stringify(lagInFrames)}`
+ );
+ }
+
return (
- <>
- {new Array(iterations).fill(true).map((_, i) => {
+ <AbsoluteFill>
+ {new Array(layers).fill(true).map((_, i) => {
return (
<AbsoluteFill
key={`frame-${i.toString()}`}
- style={{ opacity: opacity - ((i + 1) / iterations) * opacity }}
+ style={{
+ opacity: blurOpacity - ((i + 1) / layers) * blurOpacity,
+ }}
>
- <Freeze frame={frame - frameDelay * i}>{children}</Freeze>
+ <Freeze frame={frame - lagInFrames * i}>{children}</Freeze>
</AbsoluteFill>
);
})}
{children}
- </>
+ </AbsoluteFill>
);
};