Skip to content

🎥 Make videos programmatically with React

adapt to coding style

Jonny Burger4b6814bed602
diff --git a/packages/three/src/RemotionThreeCanvas.tsx b/packages/three/src/RemotionThreeCanvas.tsx
index 85d47bee0c..8531456554 100644
--- a/packages/three/src/RemotionThreeCanvas.tsx
+++ b/packages/three/src/RemotionThreeCanvas.tsx
@@ -1,130 +1,140 @@
-/// <reference path="./effects/postprocessing.d.ts" />
-
-/* eslint-disable @typescript-eslint/no-non-null-assertion */
+import { Canvas, render, unmountComponentAtNode } from '@react-three/fiber';
import React from 'react';
import ReactDOM from 'react-dom';
-import { Canvas, render, unmountComponentAtNode } from '@react-three/fiber';
-
-import { RemotionThreeContext, RemotionThreeContextType } from './RemotionThreeContext';
import { Internals } from 'remotion';
-
-export default function RemotionThreeCanvas({
- children,
- tabIndex,
- resize,
- id,
- style,
- className,
- events,
- ...rest
-}: React.ComponentProps<typeof Canvas>) {
- const wrapperRef = React.useRef<HTMLDivElement>(null);
- const canvasRef = React.useRef<HTMLCanvasElement>(null);
-
- const remotionContexts = Internals.useRemotionContexts();
-
- const [threeDomNodesWrapper, remotionThreeContext] = useCreateRemotionThreeContext();
-
- const width = wrapperRef.current?.clientWidth;
- const height = wrapperRef.current?.clientHeight;
- const size = React.useMemo(() => !width || !height ? undefined : ({ width, height }), [width, height]);
-
- React.useLayoutEffect(() => {
- if (!canvasRef.current) {
- return;
- }
- const threeFiberContent = (
- <RemotionThreeContext.Provider value={remotionThreeContext}>
- <Internals.RemotionContextProvider contexts={remotionContexts}>
- {children}
- </Internals.RemotionContextProvider>
- </RemotionThreeContext.Provider>
- );
- render(threeFiberContent, canvasRef.current, {
- frameloop: 'demand',
- orthographic: true,
- ...rest,
- size,
- });
- });
-
- React.useEffect(() => {
- const canvas = canvasRef.current!;
- return () => {
- unmountComponentAtNode(canvas);
- };
- }, []);
-
- return (
- <div
- ref={wrapperRef}
- id={id}
- className={className}
- tabIndex={tabIndex}
- style={{
- position: 'relative',
- width: '100%',
- height: '100%',
- overflow: 'hidden',
- ...style
- }}
- >
- <canvas ref={canvasRef} style={{ display: 'block' }} />
- {threeDomNodesWrapper}
- </div>
- );
-}
+import {
+ RemotionThreeContext,
+ RemotionThreeContextType,
+} from './RemotionThreeContext';
+
+export const RemotionThreeCanvas = ({
+ children,
+ tabIndex,
+ resize,
+ id,
+ style,
+ className,
+ events,
+ ...rest
+}: React.ComponentProps<typeof Canvas>) => {
+ const wrapperRef = React.useRef<HTMLDivElement>(null);
+ const canvasRef = React.useRef<HTMLCanvasElement>(null);
+
+ const remotionContexts = Internals.useRemotionContexts();
+
+ const [
+ threeDomNodesWrapper,
+ remotionThreeContext,
+ ] = useCreateRemotionThreeContext();
+
+ const width = wrapperRef.current?.clientWidth;
+ const height = wrapperRef.current?.clientHeight;
+ const size = React.useMemo(
+ () => (!width || !height ? undefined : { width, height }),
+ [width, height]
+ );
+
+ React.useLayoutEffect(() => {
+ if (!canvasRef.current) {
+ return;
+ }
+ const threeFiberContent = (
+ <RemotionThreeContext.Provider value={remotionThreeContext}>
+ <Internals.RemotionContextProvider contexts={remotionContexts}>
+ {children}
+ </Internals.RemotionContextProvider>
+ </RemotionThreeContext.Provider>
+ );
+ render(threeFiberContent, canvasRef.current, {
+ frameloop: 'demand',
+ orthographic: true,
+ ...rest,
+ size,
+ });
+ });
+
+ React.useEffect(() => {
+ const canvas = canvasRef.current as HTMLCanvasElement;
+ return () => {
+ unmountComponentAtNode(canvas);
+ };
+ }, []);
+
+ return (
+ <div
+ ref={wrapperRef}
+ id={id}
+ className={className}
+ tabIndex={tabIndex}
+ style={{
+ position: 'relative',
+ width: '100%',
+ height: '100%',
+ overflow: 'hidden',
+ ...style,
+ }}
+ >
+ <canvas ref={canvasRef} style={{ display: 'block' }} />
+ {threeDomNodesWrapper}
+ </div>
+ );
+};
function useCreateRemotionThreeContext() {
- const isMountedRef = React.useRef(true);
- React.useEffect(() => () => {
- isMountedRef.current = false;
- }, []);
-
- const portalRef = React.useRef<HTMLDivElement>(null);
-
- const remotionThreeContext = React.useState<RemotionThreeContextType>(() => ({
- useDomNode: (node) => {
- const _remotionContexts = Internals.useRemotionContexts();
-
- const containerNode = React.useState(() => document.createElement('div'))[0];
- React.useEffect(() => {
- portalRef.current!.appendChild(containerNode);
- return () => {
- ReactDOM.unmountComponentAtNode(containerNode);
- containerNode.remove();
- };
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
-
- React.useEffect(() => {
- // Updating threeDomNodes has to happen outside the react-three render loop, so use a timeout
- setTimeout(() => {
- if (isMountedRef.current) {
- const finalNode = (
- <Internals.RemotionContextProvider contexts={_remotionContexts}>
- {node}
- </Internals.RemotionContextProvider>
- );
- ReactDOM.render(finalNode, containerNode);
- }
- });
- });
- }
- }))[0];
-
- const threeDomNodesWrapper = (
- <div
- ref={portalRef}
- style={{
- position: 'absolute',
- top: 0,
- left: 0,
- overflow: 'hidden',
- visibility: 'hidden'
- }}
- />
- );
- return [threeDomNodesWrapper, remotionThreeContext] as const;
+ const isMountedRef = React.useRef(true);
+ React.useEffect(
+ () => () => {
+ isMountedRef.current = false;
+ },
+ []
+ );
+
+ const portalRef = React.useRef<HTMLDivElement>(null);
+
+ const remotionThreeContext = React.useState<RemotionThreeContextType>(() => ({
+ useDomNode: (node) => {
+ const _remotionContexts = Internals.useRemotionContexts();
+
+ const containerNode = React.useState(() =>
+ document.createElement('div')
+ )[0];
+ React.useEffect(() => {
+ (portalRef.current as HTMLDivElement).appendChild(containerNode);
+ return () => {
+ ReactDOM.unmountComponentAtNode(containerNode);
+ containerNode.remove();
+ };
+ }, []); // eslint-disable-line react-hooks/exhaustive-deps
+
+ React.useEffect(() => {
+ // Updating threeDomNodes has to happen outside the react-three render loop, so use a timeout
+ setTimeout(() => {
+ if (isMountedRef.current) {
+ const finalNode = (
+ <Internals.RemotionContextProvider contexts={_remotionContexts}>
+ {node}
+ </Internals.RemotionContextProvider>
+ );
+ ReactDOM.render(finalNode, containerNode);
+ }
+ });
+ });
+ },
+ }))[0];
+
+ const threeDomNodesWrapper = (
+ <div
+ ref={portalRef}
+ style={{
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ overflow: 'hidden',
+ visibility: 'hidden',
+ }}
+ />
+ );
+ return [threeDomNodesWrapper, remotionThreeContext] as const;
}
// Alternative implementation which does not directly call ReactDOM.render.
diff --git a/packages/three/src/index.ts b/packages/three/src/index.ts
index 8ce173eeae..db8bae54c1 100644
--- a/packages/three/src/index.ts
+++ b/packages/three/src/index.ts
@@ -1,17 +1,11 @@
-
+export * from './effects/SwirlEffect';
+export { default as SwirlEffect } from './effects/SwirlEffect';
export * from './RemotionThreeCanvas';
-export { default as RemotionThreeCanvas } from './RemotionThreeCanvas';
-
+export { RemotionThreeCanvas } from './RemotionThreeCanvas';
export { useRemotionThreeDomNode } from './RemotionThreeContext';
-
export * from './ThreeVideo';
export { default as ThreeVideo } from './ThreeVideo';
-
export * from './useVideoTexture';
export { default as useVideoTexture } from './useVideoTexture';
-
-export * from './effects/SwirlEffect';
-export { default as SwirlEffect } from './effects/SwirlEffect';
-
export * from './utils/TexturedShaderMaterial';
export { default as TexturedShaderMaterial } from './utils/TexturedShaderMaterial';