Skip to content

🎥 Make videos programmatically with React

implement buffer state for image

Jonny Burgerbe91ae29347e
diff --git a/packages/core/src/Img.tsx b/packages/core/src/Img.tsx
index cc6d84a4ac..f772c3e970 100644
--- a/packages/core/src/Img.tsx
+++ b/packages/core/src/Img.tsx
@@ -8,6 +8,7 @@ import React, {
import {cancelRender} from './cancel-render.js';
import {continueRender, delayRender} from './delay-render.js';
import {usePreload} from './prefetch.js';
+import {useBufferState} from './use-buffer-state.js';
function exponentialBackoff(errorCount: number): number {
return 1000 * 2 ** (errorCount - 1);
@@ -21,15 +22,17 @@ export type ImgProps = Omit<
'src'
> & {
maxRetries?: number;
+ pauseWhenLoading?: boolean;
src: string;
};
const ImgRefForwarding: React.ForwardRefRenderFunction<
HTMLImageElement,
ImgProps
-> = ({onError, maxRetries = 2, src, ...props}, ref) => {
+> = ({onError, maxRetries = 2, src, pauseWhenLoading, ...props}, ref) => {
const imageRef = useRef<HTMLImageElement>(null);
const errors = useRef<Record<string, number>>({});
+ const {delayPlayback} = useBufferState();
if (!src) {
throw new Error('No "src" prop was passed to <Img>.');
@@ -116,6 +119,9 @@ const ImgRefForwarding: React.ForwardRefRenderFunction<
}
const newHandle = delayRender('Loading <Img> with src=' + actualSrc);
+ const unblock = pauseWhenLoading
+ ? delayPlayback().unblock
+ : () => undefined;
const {current} = imageRef;
const onComplete = () => {
@@ -129,6 +135,7 @@ const ImgRefForwarding: React.ForwardRefRenderFunction<
);
}
+ unblock();
continueRender(newHandle);
};
@@ -145,9 +152,11 @@ const ImgRefForwarding: React.ForwardRefRenderFunction<
// If tag gets unmounted, clear pending handles because image is not going to load
return () => {
current?.removeEventListener('load', didLoad);
+ unblock();
+
continueRender(newHandle);
};
- }, [actualSrc]);
+ }, [actualSrc, delayPlayback, pauseWhenLoading]);
}
return (
diff --git a/packages/example/src/BufferState/Image.tsx b/packages/example/src/BufferState/Image.tsx
new file mode 100644
index 0000000000..ac57be4a44
--- /dev/null
+++ b/packages/example/src/BufferState/Image.tsx
@@ -0,0 +1,24 @@
+import React from 'react';
+import {AbsoluteFill, Img, Series} from 'remotion';
+
+const Inside: React.FC = () => {
+ return (
+ <Img
+ pauseWhenLoading
+ src="https://images.unsplash.com/photo-1707669291003-a7afa20b0a05?q=80&w=2487&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
+ />
+ );
+};
+
+export const NativeBufferStateForImage: React.FC = () => {
+ return (
+ <Series>
+ <Series.Sequence durationInFrames={100}>
+ <AbsoluteFill style={{backgroundColor: 'red'}} />
+ </Series.Sequence>
+ <Series.Sequence durationInFrames={100}>
+ <Inside />
+ </Series.Sequence>
+ </Series>
+ );
+};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 562f0ecb8f..aca9958510 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -11,6 +11,7 @@ import {
import {z} from 'zod';
import {TwentyTwoKHzAudio} from './22KhzAudio';
import BetaText, {betaTextSchema} from './BetaText';
+import {NativeBufferStateForImage} from './BufferState/Image';
import {NativeBufferState} from './BufferState/Simple';
import {NativeBufferStateForVideo} from './BufferState/Video';
import {CancelRender} from './CancelRender';
@@ -662,6 +663,14 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={200}
/>
+ <Composition
+ id="native-buffer-state-for-image"
+ component={NativeBufferStateForImage}
+ width={1280}
+ height={720}
+ fps={30}
+ durationInFrames={200}
+ />
<Composition
id="default-codec"
component={ColorInterpolation}