Skip to content

🎥 Make videos programmatically with React

gif for rendering works

Jonny Burger056cdd3cea3f
diff --git a/packages/gif/src/GifForRendering.tsx b/packages/gif/src/GifForRendering.tsx
index 4523c25a43..9d04a694e3 100644
--- a/packages/gif/src/GifForRendering.tsx
+++ b/packages/gif/src/GifForRendering.tsx
@@ -1,9 +1,9 @@
-import {forwardRef, useState} from 'react';
+import {forwardRef, useEffect, useRef, useState} from 'react';
import {continueRender, delayRender} from 'remotion';
import {Canvas} from './canvas';
import {isCorsError} from './is-cors-error';
import type {GifState, RemotionGifProps} from './props';
-import {useParser} from './react-tools';
+import {parseGif} from './react-tools';
import {useCurrentGifIndex} from './useCurrentGifIndex';
export const GifForRendering = forwardRef<HTMLCanvasElement, RemotionGifProps>(
@@ -22,21 +22,35 @@ export const GifForRendering = forwardRef<HTMLCanvasElement, RemotionGifProps>(
);
const index = useCurrentGifIndex(state.delays);
+ const currentOnLoad = useRef(onLoad);
+ const currentOnError = useRef(onError);
+ currentOnLoad.current = onLoad;
+ currentOnError.current = onError;
- useParser(resolvedSrc, (info) => {
- if ('error' in info) {
- if (onError) {
- onError(info.error);
- } else {
- setError(info.error);
- }
- } else {
- onLoad?.(info);
- update(info);
- }
+ useEffect(() => {
+ const controller = new AbortController();
+ let done = false;
+ parseGif({controller, src})
+ .then((parsed) => {
+ currentOnLoad.current?.(parsed);
+ update(parsed);
+ done = true;
+ continueRender(id);
+ })
+ .catch((err) => {
+ if (currentOnError.current) {
+ currentOnError.current(err);
+ } else {
+ setError(err);
+ }
+ });
- continueRender(id);
- });
+ return () => {
+ if (!done) {
+ controller.abort();
+ }
+ };
+ }, [id, src]);
if (error) {
console.error(error.stack);
diff --git a/packages/gif/src/props.ts b/packages/gif/src/props.ts
index 7cbd1a5a16..635288e90b 100644
--- a/packages/gif/src/props.ts
+++ b/packages/gif/src/props.ts
@@ -3,7 +3,7 @@ export type RemotionGifProps = {
width?: number;
height?: number;
onLoad?: (info: {
- loaded: true;
+ loaded: boolean;
width: number;
height: number;
delays: number[];
diff --git a/packages/gif/src/react-tools.ts b/packages/gif/src/react-tools.ts
index 1afa1797d4..b2655feb5d 100644
--- a/packages/gif/src/react-tools.ts
+++ b/packages/gif/src/react-tools.ts
@@ -118,9 +118,9 @@ const useAsyncEffect = (
}, [...deps]);
};
-type ParserCallbackArgs =
+type OnParsedDone =
| {
- loaded: true;
+ loaded: boolean;
width: number;
height: number;
delays: number[];
@@ -128,10 +128,7 @@ type ParserCallbackArgs =
}
| {loaded: true; error: Error};
-const useParser = (
- src: string,
- callback: (data: ParserCallbackArgs) => void
-) => {
+const useParser = (src: string, callback: (data: OnParsedDone) => void) => {
const cb = useEventCallback(callback);
useAsyncEffect(
@@ -147,6 +144,17 @@ const useParser = (
);
};
+export const parseGif = async ({
+ src,
+ controller,
+}: {
+ src: string;
+ controller: AbortController;
+}) => {
+ const raw = await parse(src, {signal: controller.signal});
+ return generate(raw);
+};
+
const useWorkerSingleton = createSingleton(
() => makeWorker(),
(worker) => worker.terminate()
@@ -154,7 +162,7 @@ const useWorkerSingleton = createSingleton(
const useWorkerParser = (
src: string | boolean,
- callback: (data: ParserCallbackArgs) => void
+ callback: (data: OnParsedDone) => void
) => {
const cb = useEventCallback(callback);
const worker = useWorkerSingleton();