Skip to content

🎥 Make videos programmatically with React

docs and new API

Jonny Burgerad7f3af67283
diff --git a/packages/core/src/loop/index.tsx b/packages/core/src/loop/index.tsx
index a38dd7b8a5..688eef2f62 100644
--- a/packages/core/src/loop/index.tsx
+++ b/packages/core/src/loop/index.tsx
@@ -20,10 +20,7 @@ type LoopContextType = {
durationInFrames: number;
};
-const LoopContext = createContext<LoopContextType | null>({
- iteration: 0,
- durationInFrames: 0,
-});
+const LoopContext = createContext<LoopContextType | null>(null);
const useLoop = () => {
return React.useContext(LoopContext);
diff --git a/packages/docs/components/LoopExamples/LoopExamples.tsx b/packages/docs/components/LoopExamples/LoopExamples.tsx
index 76766f81f7..6e61c93678 100644
--- a/packages/docs/components/LoopExamples/LoopExamples.tsx
+++ b/packages/docs/components/LoopExamples/LoopExamples.tsx
@@ -28,7 +28,7 @@ const BlueSquare: React.FC = () => {
transform: `translateY(${interpolate(
animation,
[0, 1],
- [600, 0]
+ [600, 0],
)}px)`,
justifyContent: "center",
alignItems: "center",
@@ -63,9 +63,9 @@ const NestedExample: React.FC = () => (
</Loop>
);
-export const LoopExamples: React.FC<{ type: "base" | "times" | "nested" }> = ({
- type,
-}) => {
+export const LoopExamples: React.FC<{
+ readonly type: "base" | "times" | "nested";
+}> = ({ type }) => {
const component = (() => {
if (type === "base") {
return BaseExample;
@@ -91,7 +91,8 @@ export const LoopExamples: React.FC<{ type: "base" | "times" | "nested" }> = ({
durationInFrames={150}
fps={30}
style={{
- width: "100%",
+ backgroundColor: "#eee",
+ height: 200,
}}
loop
/>
diff --git a/packages/docs/docs/audio.mdx b/packages/docs/docs/audio.mdx
index d60f95d441..71b0af59ec 100644
--- a/packages/docs/docs/audio.mdx
+++ b/packages/docs/docs/audio.mdx
@@ -47,6 +47,12 @@ export const MyVideo = () => {
};
```
+### `loopVolumeCurveBehavior`<AvailableFrom v="4.0.142" />
+
+Controls the `frame` which is returned when using the [`volume`](#volume) callback function and adding the [`loop`](#loop) attribute.
+
+Can be either `"repeat"` (default, start from 0 on each iteration) or `"extend"` (keep increasing frames).
+
### `startFrom` / `endAt`
`<Audio>` has two more helper props you can use:
diff --git a/packages/docs/docs/loop.md b/packages/docs/docs/loop.md
deleted file mode 100644
index e9a7793acd..0000000000
--- a/packages/docs/docs/loop.md
+++ /dev/null
@@ -1,109 +0,0 @@
----
-image: /generated/articles-docs-loop.png
-id: loop
-title: <Loop>
-crumb: "API"
----
-
-import { LoopExamples, BlueSquare } from "../components/LoopExamples/LoopExamples";
-
-```twoslash include example
-const BlueSquare: React.FC = () => <div></div>
-// - BlueSquare
-```
-
-_available from v2.5.0_
-
-The `<Loop />` component allows you to quickly lay out an animation so it repeats itself.
-
-## API
-
-The Loop component is a high order component and accepts, besides it's children, the following props:
-
-- `durationInFrames` _(required)_: How many frames of the content the loop should include.
-
-- `times` _(optional)_: How many times to loop the content. By default it will be `Infinity`.
-
-- `layout`: _(optional)_: Either `"absolute-fill"` _(default)_ or `"none"` By default, your content will be absolutely positioned. If you would like to disable layout side effects, pass `layout="none"`.
-
-- `style`: _(optional, available from v3.3.4)_: CSS styles to be applied to the container. If `layout` is set to `none`, there is no container and setting this style is not allowed.
-
-:::info
-Good to know: You can nest loops within each other and they will cascade. For example, a loop that has a duration of 30 frames which is inside a loop that has a duration of 75 will play 2 times. However, nested loops are not currently displayed in the timeline.
-:::
-
-## Examples
-
-All the examples below are based on the following animation of a blue square:
-
-<LoopExamples />
-<br />
-
-```tsx twoslash
-// @include: example-BlueSquare
-// ---cut---
-const MyVideo = () => {
- return <BlueSquare />;
-};
-```
-
-### Continuous loop
-
-<LoopExamples type="base" />
-<br />
-
-```tsx twoslash
-// @include: example-BlueSquare
-import { Loop } from "remotion";
-// ---cut---
-const MyVideo = () => {
- return (
- <Loop durationInFrames={50}>
- <BlueSquare />
- </Loop>
- );
-};
-```
-
-### Fixed count loop
-
-<LoopExamples type="times" />
-<br />
-
-```tsx twoslash
-// @include: example-BlueSquare
-import { Loop } from "remotion";
-// ---cut---
-const MyVideo = () => {
- return (
- <Loop durationInFrames={50} times={2}>
- <BlueSquare />
- </Loop>
- );
-};
-```
-
-### Nested loop
-
-<LoopExamples type="nested" />
-<br />
-
-```tsx twoslash
-// @include: example-BlueSquare
-import { Loop } from "remotion";
-// ---cut---
-const MyVideo = () => {
- return (
- <Loop durationInFrames={75}>
- <Loop durationInFrames={30}>
- <BlueSquare />
- </Loop>
- </Loop>
- );
-};
-```
-
-## See also
-
-- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/loop/index.tsx)
-- [`<Sequence>`](/docs/sequence)
diff --git a/packages/docs/docs/loop.mdx b/packages/docs/docs/loop.mdx
new file mode 100644
index 0000000000..9f0e144a5f
--- /dev/null
+++ b/packages/docs/docs/loop.mdx
@@ -0,0 +1,178 @@
+---
+image: /generated/articles-docs-loop.png
+id: loop
+title: <Loop>
+crumb: "API"
+---
+
+import {
+ LoopExamples,
+ BlueSquare,
+} from "../components/LoopExamples/LoopExamples";
+
+```twoslash include example
+const BlueSquare: React.FC = () => <div></div>
+// - BlueSquare
+```
+
+_available from v2.5.0_
+
+The `<Loop />` component allows you to quickly lay out an animation so it repeats itself.
+
+```tsx twoslash title="MyComp.tsx"
+// @include: example-BlueSquare
+import { Loop } from "remotion";
+// ---cut---
+const MyComp = () => {
+ return (
+ <Loop durationInFrames={50} times={2}>
+ <BlueSquare />
+ </Loop>
+ );
+};
+```
+
+:::note
+Good to know: You can nest loops within each other and they will cascade.
+:::
+
+## API
+
+The Loop component is a high order component and accepts, besides it's children, the following props:
+
+### `durationInFrames`
+
+How many frames one iteration of the loop should be long.
+
+### `times`
+
+_optional_
+
+How many times to loop the content. By default it will be `Infinity`.
+
+### `layout`
+
+_optional_
+
+Either `"absolute-fill"` _(default)_ or `"none"`.
+By default, your content will be absolutely positioned.
+If you would like to disable layout side effects, pass `layout="none"`.
+
+### `style`<AvailableFrom v="3.3.4"/>
+
+_optional_
+
+CSS styles to be applied to the container. If `layout` is set to `none`, there is no container and setting this style is not allowed.
+
+## Examples
+
+All the examples below are based on the following animation of a blue square:
+
+<LoopExamples />
+<br />
+
+```tsx twoslash
+// @include: example-BlueSquare
+// ---cut---
+const MyComp = () => {
+ return <BlueSquare />;
+};
+```
+
+### Continuous loop
+
+<LoopExamples type="base" />
+<br />
+
+```tsx twoslash
+// @include: example-BlueSquare
+import { Loop } from "remotion";
+// ---cut---
+const MyComp = () => {
+ return (
+ <Loop durationInFrames={50}>
+ <BlueSquare />
+ </Loop>
+ );
+};
+```
+
+### Fixed count loop
+
+<LoopExamples type="times" />
+<br />
+
+```tsx twoslash
+// @include: example-BlueSquare
+import { Loop } from "remotion";
+// ---cut---
+const MyComp = () => {
+ return (
+ <Loop durationInFrames={50} times={2}>
+ <BlueSquare />
+ </Loop>
+ );
+};
+```
+
+### Nested loop
+
+<LoopExamples type="nested" />
+<br />
+
+```tsx twoslash
+// @include: example-BlueSquare
+import { Loop } from "remotion";
+// ---cut---
+const MyComp = () => {
+ return (
+ <Loop durationInFrames={75}>
+ <Loop durationInFrames={30}>
+ <BlueSquare />
+ </Loop>
+ </Loop>
+ );
+};
+```
+
+## `useLoop()`<AvailableFrom v="4.0.142" />
+
+A child component can use the `Loop.useLoop()` hook to get information about the current loop.
+You should check for `null`, which is the case if the component is not inside a loop.
+
+If inside a loop, an object with two properties is returned:
+
+- `durationInFrames`: The duration of the loop in frames as passed to the `<Loop />` component.
+- `iteration`: The current iteration of the loop, starting at 0.
+
+```tsx twoslash
+import React from "react";
+import { Loop, useCurrentFrame } from "remotion";
+
+const LoopedVideo: React.FC = () => {
+ return (
+ <Loop durationInFrames={50} times={3} name="MyLoop">
+ <Child />
+ </Loop>
+ );
+};
+
+const Child = () => {
+ const frame = useCurrentFrame(); // 75
+ const loop = Loop.useLoop();
+
+ if (loop === null) {
+ // Not inside a loop
+ } else {
+ console.log(loop.durationInFrames); // 50
+ console.log(loop.iteration); // 1
+ }
+
+ return <div>{frame}</div>;
+};
+```
+
+## See also
+
+- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/loop/index.tsx)
+- [`<Sequence>`](/docs/sequence)
diff --git a/packages/docs/docs/offthreadvideo.mdx b/packages/docs/docs/offthreadvideo.mdx
index 43c0b0d65d..1118cac441 100644
--- a/packages/docs/docs/offthreadvideo.mdx
+++ b/packages/docs/docs/offthreadvideo.mdx
@@ -97,6 +97,12 @@ export const MyComposition = () => {
};
```
+### `loopVolumeCurveBehavior`<AvailableFrom v="4.0.142" />
+
+Controls the `frame` which is returned when using the [`volume`](#volume) callback function and wrapping `OffthreadVideo` in a [`<Loop>`](/docs/loop).
+
+Can be either `"repeat"` (default, start from 0 on each iteration) or `"extend"` (keep increasing frames).
+
### `style`
You can pass any style you can pass to a native HTML element. Keep in mind that during rendering, `<OffthreadVideo>` renders an [`<Img>`](/docs/img) tag, but a `<video>` tag is used during preview.
diff --git a/packages/docs/docs/video.mdx b/packages/docs/docs/video.mdx
index 8a86b0fa08..2137f810e1 100644
--- a/packages/docs/docs/video.mdx
+++ b/packages/docs/docs/video.mdx
@@ -126,6 +126,12 @@ export const MyComposition = () => {
};
```
+### `loopVolumeCurveBehavior`<AvailableFrom v="4.0.142" />
+
+Controls the `frame` which is returned when using the [`volume`](#volume) callback function and adding the [`loop`](#loop) attribute.
+
+Can be either `"repeat"` (default, start from 0 on each iteration) or `"extend"` (keep increasing frames).
+
### `name`<AvailableFrom v="4.0.71"/>
_optional_