Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply best practices for creating programmatic videos with Remotion and React.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/gifs.md
1---2name: gif3description: Displaying GIFs, APNG, AVIF and WebP in Remotion4metadata:5tags: gif, animation, images, animated, apng, avif, webp6---78# Using Animated images in Remotion910## Basic usage1112Use `<AnimatedImage>` to display a GIF, APNG, AVIF or WebP image synchronized with Remotion's timeline:1314```tsx15import { AnimatedImage, staticFile } from "remotion";1617export const MyComposition = () => {18return (19<AnimatedImage src={staticFile("animation.gif")} width={500} height={500} />20);21};22```2324Remote URLs are also supported (must have CORS enabled):2526```tsx27<AnimatedImage28src="https://example.com/animation.gif"29width={500}30height={500}31/>32```3334## Sizing and fit3536Control how the image fills its container with the `fit` prop:3738```tsx39// Stretch to fill (default)40<AnimatedImage src={staticFile("animation.gif")} width={500} height={300} fit="fill" />4142// Maintain aspect ratio, fit inside container43<AnimatedImage src={staticFile("animation.gif")} width={500} height={300} fit="contain" />4445// Fill container, crop if needed46<AnimatedImage src={staticFile("animation.gif")} width={500} height={300} fit="cover" />47```4849## Playback speed5051Use `playbackRate` to control the animation speed:5253```tsx54<AnimatedImage src={staticFile("animation.gif")} width={500} height={500} playbackRate={2} /> {/* 2x speed */}55<AnimatedImage src={staticFile("animation.gif")} width={500} height={500} playbackRate={0.5} /> {/* Half speed */}56```5758## Looping behavior5960Control what happens when the animation finishes:6162```tsx63// Loop indefinitely (default)64<AnimatedImage src={staticFile("animation.gif")} width={500} height={500} loopBehavior="loop" />6566// Play once, show final frame67<AnimatedImage src={staticFile("animation.gif")} width={500} height={500} loopBehavior="pause-after-finish" />6869// Play once, then clear canvas70<AnimatedImage src={staticFile("animation.gif")} width={500} height={500} loopBehavior="clear-after-finish" />71```7273## Styling7475Use the `style` prop for additional CSS (use `width` and `height` props for sizing):7677```tsx78<AnimatedImage79src={staticFile("animation.gif")}80width={500}81height={500}82style={{83borderRadius: 20,84position: "absolute",85top: 100,86left: 50,87}}88/>89```9091## Getting GIF duration9293Use `getGifDurationInSeconds()` from `@remotion/gif` to get the duration of a GIF.9495```bash96npx remotion add @remotion/gif97```9899```tsx100import { getGifDurationInSeconds } from "@remotion/gif";101import { staticFile } from "remotion";102103const duration = await getGifDurationInSeconds(staticFile("animation.gif"));104console.log(duration); // e.g. 2.5105```106107This is useful for setting the composition duration to match the GIF:108109```tsx110import { getGifDurationInSeconds } from "@remotion/gif";111import { staticFile, CalculateMetadataFunction } from "remotion";112113const calculateMetadata: CalculateMetadataFunction = async () => {114const duration = await getGifDurationInSeconds(staticFile("animation.gif"));115return {116durationInFrames: Math.ceil(duration * 30),117};118};119```120121## Alternative122123If `<AnimatedImage>` does not work (only supported in Chrome and Firefox), you can use `<Gif>` from `@remotion/gif` instead.124125```bash126npx remotion add @remotion/gif # If project uses npm127bunx remotion add @remotion/gif # If project uses bun128yarn remotion add @remotion/gif # If project uses yarn129pnpm exec remotion add @remotion/gif # If project uses pnpm130```131132```tsx133import { Gif } from "@remotion/gif";134import { staticFile } from "remotion";135136export const MyComposition = () => {137return <Gif src={staticFile("animation.gif")} width={500} height={500} />;138};139```140141The `<Gif>` component has the same props as `<AnimatedImage>` but only supports GIF files.142