🎥 Make videos programmatically with React
coool
Jonny Burgerd1f98847f665
diff --git a/packages/example/src/3DContext/Div3D.tsx b/packages/example/src/3DContext/Div3D.tsxnew file mode 100644index 0000000000..53d9850935--- /dev/null+++ b/packages/example/src/3DContext/Div3D.tsx@@ -0,0 +1,27 @@+import {useRef} from 'react';+import {DivExtrusion} from './DivExtrusion';+import {FrontFace} from './FrontFace';+import {RectProvider} from './path-context';++export const Div3D: React.FC<{+ children: React.ReactNode;+ width: number;+ height: number;+ depth: number;+ cornerRadius: number;+}> = ({children, width, height, depth, cornerRadius}) => {+ const ref = useRef<HTMLDivElement>(null);++ return (+ <RectProvider height={height} width={width} cornerRadius={cornerRadius}>+ <div+ ref={ref}+ className="relative"+ style={{width, height, position: 'relative'}}+ >+ <DivExtrusion depth={depth} />+ <FrontFace depth={depth}>{children}</FrontFace>+ </div>+ </RectProvider>+ );+};diff --git a/packages/example/src/3DContext/DivExtrusion.tsx b/packages/example/src/3DContext/DivExtrusion.tsxindex 71e83ffa36..612b70a514 100644--- a/packages/example/src/3DContext/DivExtrusion.tsx+++ b/packages/example/src/3DContext/DivExtrusion.tsx@@ -1,20 +1,11 @@-import {PathInternals, reduceInstructions} from '@remotion/paths';-import {makeRect} from '@remotion/shapes';-import {MatrixTransform4D} from '@remotion/svg-3d-engine';import {SvgExtrusion} from './Extrusion';+import {useRect} from './path-context';export const DivExtrusion: React.FC<{- height: number;- width: number;- cornerRadius: number;depth: number;- transformatations: MatrixTransform4D[];-}> = ({height, width, cornerRadius, depth, transformatations}) => {- const {instructions} = makeRect({height, width, cornerRadius});+}> = ({depth}) => {+ const {viewBox, height, width} = useRect();- const {viewBox} = PathInternals.getBoundingBoxFromInstructions(- reduceInstructions(instructions),- );return (<svgviewBox={viewBox}@@ -27,13 +18,7 @@ export const DivExtrusion: React.FC<{}}pointerEvents="none">- <SvgExtrusion- transformations={transformatations}- cornerRadius={cornerRadius}- depth={depth}- height={height}- width={width}- />+ <SvgExtrusion depth={depth} /></svg>);};diff --git a/packages/example/src/3DContext/Extrusion.tsx b/packages/example/src/3DContext/Extrusion.tsxindex c53557208a..f9a8e11509 100644--- a/packages/example/src/3DContext/Extrusion.tsx+++ b/packages/example/src/3DContext/Extrusion.tsx@@ -1,28 +1,25 @@import {parsePath} from '@remotion/paths';-import {makeRect} from '@remotion/shapes';import {extrudeAndTransformElement,- MatrixTransform4D,reduceMatrices,translateX,translateY,} from '@remotion/svg-3d-engine';import {Faces} from '../3DEngine/Faces';+import {useRect} from './path-context';+import {useTransformations} from './transformation-context';export const SvgExtrusion: React.FC<{- transformations: MatrixTransform4D[];depth: number;- height: number;- width: number;- cornerRadius: number;-}> = ({transformations, cornerRadius, depth, height, width}) => {- const {path} = makeRect({height, width, cornerRadius});- const reduced = reduceMatrices(transformations);+}> = ({depth}) => {+ const {path, width, height} = useRect();++ const transformations = useTransformations();const centerOriented = reduceMatrices([translateX(-width / 2),translateY(-height / 2),- reduced,+ transformations,translateX(width / 2),translateY(height / 2),]);@@ -39,5 +36,6 @@ export const SvgExtrusion: React.FC<{strokeWidth: 3,transformations: centerOriented,});+return <Faces elements={[extruded]} />;};diff --git a/packages/example/src/3DContext/FrontFace.tsx b/packages/example/src/3DContext/FrontFace.tsxindex a185b7519a..d85cce9336 100644--- a/packages/example/src/3DContext/FrontFace.tsx+++ b/packages/example/src/3DContext/FrontFace.tsx@@ -1,20 +1,17 @@import {makeMatrix3dTransform,- MatrixTransform4D,reduceMatrices,translateZ,} from '@remotion/svg-3d-engine';+import {useRect} from './path-context';+import {useTransformations} from './transformation-context';export const FrontFace: React.FC<{- width: number;- height: number;children: React.ReactNode;depth: number;- transformations: MatrixTransform4D[];-}> = ({width, height, children, depth, transformations: transformatations}) => {- const reduced = reduceMatrices(transformatations);-- const frontFace = reduceMatrices([translateZ(-depth), reduced]);+}> = ({children, depth}) => {+ const frontFace = reduceMatrices([translateZ(-depth), useTransformations()]);+ const {height, width} = useRect();return (<divdiff --git a/packages/example/src/3DContext/Outer.tsx b/packages/example/src/3DContext/Outer.tsxdeleted file mode 100644index 4a0c699347..0000000000--- a/packages/example/src/3DContext/Outer.tsx+++ /dev/null@@ -1,39 +0,0 @@-import {MatrixTransform4D} from '@remotion/svg-3d-engine';-import {useRef} from 'react';-import {DivExtrusion} from './DivExtrusion';-import {FrontFace} from './FrontFace';--export const Div3D: React.FC<{- children: React.ReactNode;- width: number;- height: number;- depth: number;- transformatations: MatrixTransform4D[];- cornerRadius: 10;-}> = ({children, width, height, depth, transformatations, cornerRadius}) => {- const ref = useRef<HTMLDivElement>(null);-- return (- <div- ref={ref}- className="relative"- style={{width, height, position: 'relative'}}- >- <DivExtrusion- transformatations={transformatations}- cornerRadius={cornerRadius}- depth={depth}- height={height}- width={width}- />- <FrontFace- height={height}- width={width}- depth={depth}- transformations={transformatations}- >- {children}- </FrontFace>- </div>- );-};diff --git a/packages/example/src/3DContext/index.tsx b/packages/example/src/3DContext/index.tsxindex 332701f72d..fa8051e98c 100644--- a/packages/example/src/3DContext/index.tsx+++ b/packages/example/src/3DContext/index.tsx@@ -1,6 +1,6 @@-import {rotateX, rotateY, scaled} from '@remotion/svg-3d-engine';import {AbsoluteFill} from 'remotion';-import {Div3D} from './Outer';+import {Div3D} from './Div3D';+import {RotateX, RotateY, Scale} from './transformation-context';const Comp = () => {const width = 200;@@ -10,35 +10,29 @@ const Comp = () => {const angle = Math.PI / 4;- const transformations = [- scaled(1.1),- rotateX(-Math.PI / 16),- rotateX(Math.sin(angle) / 4),- // rotateY(-Math.PI / 8),- rotateY(-Math.cos(angle) / 4),- ];-return (<AbsoluteFill className="flex justify-center items-center bg-[#F8FAFC]">- <Div3D- width={width}- height={height}- depth={40}- transformatations={transformations}- cornerRadius={10}- >- <div- style={{- borderRadius: cornerRadius,- fontFamily: 'GT Planar',- backgroundColor: 'white',- border: '2px solid black',- }}- className="text-black flex justify-center items-center font-sans text-2xl border-solid border-black font-bold cursor-pointer flex-1"- >- Get started- </div>- </Div3D>+ <Scale factor={1.3}>+ <RotateX radians={-Math.PI / 16}>+ <RotateX radians={Math.sin(angle) / 4}>+ <RotateY radians={-Math.cos(angle) / 4}>+ <Div3D width={width} height={height} depth={40} cornerRadius={10}>+ <div+ style={{+ borderRadius: cornerRadius,+ fontFamily: 'GT Planar',+ backgroundColor: 'white',+ border: '2px solid black',+ }}+ className="text-black flex justify-center items-center font-sans text-2xl border-solid border-black font-bold cursor-pointer flex-1"+ >+ Get started+ </div>+ </Div3D>+ </RotateY>+ </RotateX>+ </RotateX>+ </Scale></AbsoluteFill>);};diff --git a/packages/example/src/3DContext/path-context.tsx b/packages/example/src/3DContext/path-context.tsxnew file mode 100644index 0000000000..9233541daf--- /dev/null+++ b/packages/example/src/3DContext/path-context.tsx@@ -0,0 +1,38 @@+import {makeRect} from '@remotion/shapes';+import React, {useMemo} from 'react';++type PathContext = {+ height: number;+ width: number;+ path: string;+ viewBox: string;+};++export const pathContext = React.createContext<PathContext | null>(null);++export const RectProvider: React.FC<{+ readonly width: number;+ readonly height: number;+ readonly cornerRadius: number;+ readonly children: React.ReactNode;+}> = ({cornerRadius, height, width, children}) => {+ const {path} = makeRect({height, width, cornerRadius});++ const viewBox = `0 0 ${width} ${height}`;++ const value = useMemo(() => {+ return {height, width, path, viewBox};+ }, [height, path, viewBox, width]);++ return <pathContext.Provider value={value}>{children}</pathContext.Provider>;+};++export const useRect = () => {+ const value = React.useContext(pathContext);++ if (!value) {+ throw new Error('useRect must be used within a RectProvider');+ }++ return value;+};diff --git a/packages/example/src/3DContext/transformation-context.tsx b/packages/example/src/3DContext/transformation-context.tsxnew file mode 100644index 0000000000..6bf3305510--- /dev/null+++ b/packages/example/src/3DContext/transformation-context.tsx@@ -0,0 +1,66 @@+import {+ MatrixTransform4D,+ reduceMatrices,+ rotateX,+ rotateY,+ scaled,+} from '@remotion/svg-3d-engine';+import React, {useContext, useMemo} from 'react';++type Context = MatrixTransform4D;++export const TransformContext = React.createContext<Context>(scaled(1));++export const NewTransform: React.FC<{+ readonly children: React.ReactNode;+ readonly transform: MatrixTransform4D;+}> = ({children, transform}) => {+ const oldValue = React.useContext(TransformContext);++ const newValue = React.useMemo(() => {+ return reduceMatrices([oldValue, transform]);+ }, [oldValue, transform]);++ return (+ <TransformContext.Provider value={newValue}>+ {children}+ </TransformContext.Provider>+ );+};++export const RotateX: React.FC<{+ readonly children: React.ReactNode;+ readonly radians: number;+}> = ({children, radians}) => {+ return (+ <NewTransform transform={useMemo(() => rotateX(radians), [radians])}>+ {children}+ </NewTransform>+ );+};++export const RotateY: React.FC<{+ readonly children: React.ReactNode;+ readonly radians: number;+}> = ({children, radians}) => {+ return (+ <NewTransform transform={useMemo(() => rotateY(radians), [radians])}>+ {children}+ </NewTransform>+ );+};++export const Scale: React.FC<{+ readonly children: React.ReactNode;+ readonly factor: number;+}> = ({children, factor}) => {+ return (+ <NewTransform transform={useMemo(() => scaled(factor), [factor])}>+ {children}+ </NewTransform>+ );+};++export const useTransformations = () => {+ return useContext(TransformContext);+};