Skip to content

🎥 Make videos programmatically with React

fix docs failure with video texture

Jonny Burger083321d7053e
diff --git a/packages/three/src/use-video-texture.ts b/packages/three/src/use-video-texture.ts
index 794edd9e98..62b6c5e42a 100644
--- a/packages/three/src/use-video-texture.ts
+++ b/packages/three/src/use-video-texture.ts
@@ -25,27 +25,40 @@ const warnAboutRequestVideoFrameCallback = () => {
export const useVideoTexture = (
videoRef: React.RefObject<HTMLVideoElement>
): VideoTexture | null => {
- const [loaded] = useState(() =>
- delayRender(`Waiting for texture in useVideoTexture() to be loaded`)
- );
+ const [loaded] = useState(() => {
+ if (typeof document === 'undefined') {
+ return 0;
+ }
+
+ return delayRender(`Waiting for texture in useVideoTexture() to be loaded`);
+ });
const [videoTexture, setVideoTexture] = useState<VideoTexture | null>(null);
const [vidText] = useState(
() => import('three/src/textures/VideoTexture.js')
);
+ const [error, setError] = useState<Error | null>(null);
const frame = useCurrentFrame();
+ if (error) {
+ throw error;
+ }
+
const onReady = useCallback(() => {
- vidText.then(({VideoTexture}) => {
- if (!videoRef.current) {
- throw new Error('Video not ready');
- }
-
- const vt = new VideoTexture(videoRef.current);
- videoRef.current.width = videoRef.current.videoWidth;
- videoRef.current.height = videoRef.current.videoHeight;
- setVideoTexture(vt);
- continueRender(loaded);
- });
+ vidText
+ .then(({VideoTexture}) => {
+ if (!videoRef.current) {
+ throw new Error('Video not ready');
+ }
+
+ const vt = new VideoTexture(videoRef.current);
+ videoRef.current.width = videoRef.current.videoWidth;
+ videoRef.current.height = videoRef.current.videoHeight;
+ setVideoTexture(vt);
+ continueRender(loaded);
+ })
+ .catch((err) => {
+ setError(err);
+ });
}, [loaded, vidText, videoRef]);
React.useEffect(() => {