Skip to content

🎥 Make videos programmatically with React

pushing fixes

Jonny Burgerf090cf88ec91
diff --git a/packages/core/src/animated-image/AnimatedImage.tsx b/packages/core/src/animated-image/AnimatedImage.tsx
index 2f6bf1058a..507863196e 100644
--- a/packages/core/src/animated-image/AnimatedImage.tsx
+++ b/packages/core/src/animated-image/AnimatedImage.tsx
@@ -29,7 +29,6 @@ export const AnimatedImage = forwardRef<
onError,
loopBehavior = 'loop',
playbackRate = 1,
- onLoad,
fit = 'fill',
...props
},
@@ -102,7 +101,7 @@ export const AnimatedImage = forwardRef<
return () => {
controller.abort();
};
- }, [resolvedSrc, decodeHandle, onLoad, onError, initialLoopBehavior]);
+ }, [resolvedSrc, decodeHandle, onError, initialLoopBehavior]);
useLayoutEffect(() => {
if (!imageDecoder) {
diff --git a/packages/core/src/animated-image/decode-image.ts b/packages/core/src/animated-image/decode-image.ts
index 46def9b411..0f329a35e9 100644
--- a/packages/core/src/animated-image/decode-image.ts
+++ b/packages/core/src/animated-image/decode-image.ts
@@ -158,6 +158,14 @@ export const decodeImage = async ({
}
}
+ // If close to end, also cache first frame for smooth wrap around
+ if (
+ selectedTrack.frameCount - biggestIndex < 3 &&
+ loopBehavior === 'loop'
+ ) {
+ await getFrameByIndex(0);
+ }
+
clearCache(actualTimeInSec);
};
@@ -178,7 +186,7 @@ export const decodeImage = async ({
if (
durationFound !== null &&
timeInSec > durationFound &&
- loopBehavior === 'unmount-after-finish'
+ loopBehavior === 'clear-after-finish'
) {
return null;
}
diff --git a/packages/core/src/animated-image/props.ts b/packages/core/src/animated-image/props.ts
index 9df8a8cf0d..ecb0c83b1b 100644
--- a/packages/core/src/animated-image/props.ts
+++ b/packages/core/src/animated-image/props.ts
@@ -1,18 +1,12 @@
export type RemotionAnimatedImageLoopBehavior =
| 'loop'
| 'pause-after-finish'
- | 'unmount-after-finish';
+ | 'clear-after-finish';
export type RemotionAnimatedImageProps = {
src: string;
width?: number;
height?: number;
- onLoad?: (info: {
- width: number;
- height: number;
- delays: number[];
- frames: ImageData[];
- }) => void;
onError?: (error: Error) => void;
fit?: AnimatedImageFillMode;
playbackRate?: number;
diff --git a/packages/core/src/test/animated-image.test.tsx b/packages/core/src/test/animated-image.test.tsx
deleted file mode 100644
index a812cfee4a..0000000000
--- a/packages/core/src/test/animated-image.test.tsx
+++ /dev/null
@@ -1,16 +0,0 @@
-/**
- * @vitest-environment jsdom
- */
-import {render} from '@testing-library/react';
-import {describe, expect, test} from 'vitest';
-import {AnimatedImage} from '../animated-image';
-
-describe('Render correctly with props', () => {
- test('should render with default props', () => {
- expect(() =>
- render(
- <AnimatedImage src="https://gist.github.com/nolanlawson/73f15f3fe612b8770e79/raw/be4205effd089b38ccd197de544e6370785f6b5e/chopsticks.gif" />,
- ),
- ).not.toThrow();
- });
-});
diff --git a/packages/docs/components/TableOfContents/remotion.tsx b/packages/docs/components/TableOfContents/remotion.tsx
index 25d0493cbf..cedde3f01f 100644
--- a/packages/docs/components/TableOfContents/remotion.tsx
+++ b/packages/docs/components/TableOfContents/remotion.tsx
@@ -76,6 +76,10 @@ export const TableOfContents: React.FC = () => {
Alternative to <code>{'<Video>'}</code>
</div>
</TOCItem>
+ <TOCItem link="/docs/animatedimage">
+ <strong>{'<AnimatedImage>'}</strong>
+ <div>Disply a GIF, AVIF or animated WebP image</div>
+ </TOCItem>
<TOCItem link="/docs/iframe">
<strong>{'<IFrame>'}</strong>
<div>
diff --git a/packages/docs/docs/animatedimage.mdx b/packages/docs/docs/animatedimage.mdx
new file mode 100644
index 0000000000..0df706483e
--- /dev/null
+++ b/packages/docs/docs/animatedimage.mdx
@@ -0,0 +1,97 @@
+---
+image: /generated/articles-docs-animatedimage.png
+id: animatedimage
+title: '<AnimatedImage>'
+crumb: 'API'
+---
+
+# `<AnimatedImage>`<AvailableFrom v="4.0.246" />
+
+Renders an animated GIF, AVIF or WebP image and syncs it with Remotion's timeline.
+Relies on the [`ImageDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/ImageDecoder) Web API, meaning it only works in Google Chrome and Firefox as of writing.
+
+```tsx twoslash title="Loading a remote animated image"
+import {AnimatedImage} from 'remotion';
+
+export const WebpAnimatedImage = () => {
+ return (
+ <AnimatedImage src="https://mathiasbynens.be/demo/animated-webp-supported.webp" />
+ );
+};
+```
+
+```tsx twoslash title="Loading a local animated image"
+import {AnimatedImage, staticFile} from 'remotion';
+
+export const GifAnimatedImage = () => {
+ return <AnimatedImage src={staticFile('giphy.gif')} />;
+};
+```
+
+## Props
+
+### `src`
+
+The URL of the animated image. Can be a remote URL or a local file path.
+
+:::note
+Remote images need to support [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
+
+<details>
+ <summary>More info</summary>
+ <ul>
+ <li>
+ Remotion's origin is usually <code>http://localhost:3000</code>, but it
+ may be different if rendering on Lambda or the port is busy.
+ </li>
+ <li>
+ You can{' '}
+ <a href="/docs/chromium-flags#--disable-web-security">disable CORS</a>{' '}
+ during renders.
+ </li>
+ </ul>
+</details>
+:::
+
+### `width?`
+
+The display width.
+
+### `height?`
+
+The display height.
+
+### `fit?`
+
+Must be one of these values:
+
+- `'fill'`: The image will completely fill the container, and will be stretched if necessary. (_default_)
+- `'contain'`: The image is scaled to fit the box, while aspect ratio is maintained.
+- `'cover'`: The image completely fills the container and maintains it's aspect ratio. It will be cropped if necessary.
+
+### `style?`
+
+Allows to pass in custom CSS styles. You may not pass `width` and `height`, instead use the props `width` and `height` to set the size of the image.
+
+### `loopBehavior?`
+
+The looping behavior of the animated image. Can be one of these values:
+
+- `'loop'`: The animated image will loop infinitely. (_default_)
+- `'pause-after-finish'`: The animated image will play once and then show the last frame.
+- `'clear-after-finish'`: The animated image will play once and then clear the canvas.
+
+### `ref?`<AvailableFrom v="3.3.88" />
+
+You can add a [React ref](https://react.dev/learn/manipulating-the-dom-with-refs) to `<AnimatedImage />`. If you use TypeScript, you need to type it with `HTMLCanvasElement`.
+
+## Differences to `<Gif>`
+
+- `<AnimatedImage>` also supports AVIF and WebP images.
+- `<AnimatedImage>` uses the [`ImageDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/ImageDecoder) Web API, which has limited browser support.
+- `<AnimatedImage>` does not support the `onLoad` prop.
+
+## See also
+
+- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/animated-image/AnimatedImage.tsx)
+- [`<Gif>`](/docs/gif)
diff --git a/packages/docs/docs/gif/gif.mdx b/packages/docs/docs/gif/gif.mdx
index fa310bf68c..d800591497 100644
--- a/packages/docs/docs/gif/gif.mdx
+++ b/packages/docs/docs/gif/gif.mdx
@@ -1,15 +1,38 @@
---
image: /generated/articles-docs-gif-gif.png
slug: gif
-sidebar_label: "<Gif>"
-title: "<Gif>"
-crumb: "@remotion/gif"
+sidebar_label: '<Gif>'
+title: '<Gif>'
+crumb: '@remotion/gif'
---
_Part of the [`@remotion/gif`](/docs/gif) package_
Displays a GIF that synchronizes with Remotions [`useCurrentFrame()`](/docs/use-current-frame).
+```tsx twoslash
+import {useRef} from 'react';
+import {useVideoConfig} from 'remotion';
+// ---cut---
+import {Gif} from '@remotion/gif';
+
+export const MyComponent: React.FC = () => {
+ const {width, height} = useVideoConfig();
+ const ref = useRef<HTMLCanvasElement>(null);
+
+ return (
+ <Gif
+ ref={ref}
+ src="https://media.giphy.com/media/3o72F7YT6s0EMFI0Za/giphy.gif"
+ width={width}
+ height={height}
+ fit="fill"
+ playbackRate={2}
+ />
+ );
+};
+```
+
## Props
### `src`
@@ -22,15 +45,18 @@ The source of the GIF. Can be an URL or a local image - see [Importing assets](/
Remote GIFs need to support [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
<details>
-<summary>More info</summary>
-<ul>
-<li>
-Remotion's origin is usually <code>http://localhost:3000</code>, but it may be different if rendering on Lambda or the port is busy.
-</li>
-<li>
-You can <a href="/docs/chromium-flags#--disable-web-security">disable CORS</a> during renders.
-</li>
-</ul>
+ <summary>More info</summary>
+ <ul>
+ <li>
+ Remotion's origin is usually <code>http://localhost:3000</code>, but it
+ may be different if rendering on Lambda or the port is busy.
+ </li>
+ <li>
+ You can{' '}
+ <a href="/docs/chromium-flags#--disable-web-security">disable CORS</a>{' '}
+ during renders.
+ </li>
+ </ul>
</details>
:::
@@ -86,32 +112,14 @@ Values:
- `< 1`: Slows down the GIF speed (e.g., 0.5 plays it at half speed).
- `> 1:` Speeds up the GIF speed (e.g., 2.0 plays it at double speed).
-## Example
+## Differences to `<AnimatedImage>`
-```tsx twoslash
-import { useRef } from "react";
-import { useVideoConfig } from "remotion";
-// ---cut---
-import { Gif } from "@remotion/gif";
-
-export const MyComponent: React.FC = () => {
- const { width, height } = useVideoConfig();
- const ref = useRef<HTMLCanvasElement>(null);
-
- return (
- <Gif
- ref={ref}
- src="https://media.giphy.com/media/3o72F7YT6s0EMFI0Za/giphy.gif"
- width={width}
- height={height}
- fit="fill"
- playbackRate={2}
- />
- );
-};
-```
+- `<Gif>` does not support animated AVIF and WebP images.
+- `<Gif>` works in Safari as well since it uses a JavaScript-based GIF decoder.
+- `<Gif>` supports the [`onLoad`](#onload) prop.
## See also
+- [`<AnimatedImage>`](/docs/animatedimage)
- [`getGifDurationInSeconds()`](/docs/gif/get-gif-duration-in-seconds)
- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/gif/src/Gif.tsx)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 24bd37310b..8398434162 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -103,6 +103,7 @@ module.exports = {
'loop',
'measure-spring',
'offthreadvideo',
+ 'animatedimage',
'prefetch',
'random',
'register-root',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 35c8ea8bcf..4f631bcc54 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -107,6 +107,15 @@ export const articles = [
noAi: false,
slug: 'animated-emoji/index',
},
+ {
+ id: 'animatedimage',
+ title: '<AnimatedImage>',
+ relativePath: 'docs/animatedimage.mdx',
+ compId: 'articles-docs-animatedimage',
+ crumb: 'API',
+ noAi: false,
+ slug: 'animatedimage',
+ },
{
id: 'animating-properties',
title: 'Animating properties',
diff --git a/packages/docs/static/generated/articles-docs-animatedimage.png b/packages/docs/static/generated/articles-docs-animatedimage.png
new file mode 100644
index 0000000000..e1d6289728
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-animatedimage.png differ