Skip to content

🎥 Make videos programmatically with React

compose

Jonny Burger8eacef0b5b6d
diff --git a/packages/example/src/Compose/Compose.tsx b/packages/example/src/Compose/Compose.tsx
index 429a4087cf..c1d20513fe 100644
--- a/packages/example/src/Compose/Compose.tsx
+++ b/packages/example/src/Compose/Compose.tsx
@@ -23,9 +23,8 @@ const width = (height / 16) * 9;
const cornerRadius = 10;
const depth = 60;
-const Label: React.FC<{
- children: React.ReactNode;
-}> = ({children}) => {
+const Label: React.FC<React.HTMLAttributes<HTMLDivElement>> = (rest) => {
+ const opacity = useLabelOpacity();
return (
<div
className="text-white "
@@ -37,100 +36,112 @@ const Label: React.FC<{
justifyContent: 'center',
paddingLeft: 20,
fontSize: 24,
+ opacity,
}}
- >
- {children}
- </div>
+ {...rest}
+ />
);
};
const VideoLayers: React.FC<{
label: string;
-}> = ({label}) => {
+ delay: number;
+}> = ({label, delay}) => {
return (
- <Rotations delay={0}>
- <AbsoluteFill id="video" className="flex justify-center items-center">
- <ExtrudeDiv
- width={width}
- height={height}
- depth={depth}
- cornerRadius={10}
- backFace={
- <AbsoluteFill
- className="bg-gray-700"
- style={{
- borderRadius: cornerRadius,
- border: '3px solid black',
- }}
- ></AbsoluteFill>
- }
- bottomFace={<Label>{label}</Label>}
+ <Rotations zIndexHack={false} delay={delay}>
+ <ExtrudeDiv
+ width={width}
+ height={height}
+ depth={depth}
+ cornerRadius={10}
+ backFace={
+ <AbsoluteFill
+ className="bg-gray-700"
+ style={{
+ borderRadius: cornerRadius,
+ border: '3px solid black',
+ }}
+ ></AbsoluteFill>
+ }
+ bottomFace={<Label>{label}</Label>}
+ >
+ <div
+ style={{
+ borderRadius: cornerRadius,
+ overflow: 'hidden',
+ fontFamily: 'GT Planar',
+ backgroundColor: 'white',
+ border: '3px solid black',
+ }}
+ className="text-black flex justify-center items-center font-sans text-2xl font-bold flex-1"
>
<div
style={{
- borderRadius: cornerRadius,
- overflow: 'hidden',
- fontFamily: 'GT Planar',
backgroundColor: 'white',
- border: '3px solid black',
+ width: '100%',
+ height: '100%',
}}
- className="text-black flex justify-center items-center font-sans text-2xl font-bold flex-1"
- >
- <div
- style={{
- backgroundColor: 'white',
- width: '100%',
- height: '100%',
- }}
- ></div>
- </div>
- </ExtrudeDiv>
- </AbsoluteFill>
+ ></div>
+ </div>
+ </ExtrudeDiv>
</Rotations>
);
};
-const CaptionLayers = () => {
+const CaptionLayers: React.FC<{
+ delay: number;
+}> = ({delay: delay}) => {
return (
- <Rotations delay={1}>
- <AbsoluteFill id="captions" className="flex justify-center items-center">
- <ExtrudeDiv
- width={300}
- height={60}
- depth={depth}
- cornerRadius={10}
- backFace={<AbsoluteFill className="bg-black"></AbsoluteFill>}
- bottomFace={<Label>&lt;Captions /&gt;</Label>}
+ <Rotations zIndexHack delay={1 + delay}>
+ <ExtrudeDiv
+ width={300}
+ height={60}
+ depth={depth}
+ cornerRadius={10}
+ bottomFace={<Label>&lt;Captions /&gt;</Label>}
+ backFace={
+ <AbsoluteFill
+ className="bg-black"
+ style={{borderRadius: cornerRadius, overflow: 'hidden'}}
+ ></AbsoluteFill>
+ }
+ >
+ <div
+ style={{
+ borderRadius: cornerRadius,
+ overflow: 'hidden',
+ fontFamily: 'GT Planar',
+ backgroundColor: 'white',
+ border: '3px solid black',
+ }}
+ className="text-black flex justify-center items-center font-sans text-2xl font-bold flex-1"
>
- <div
- style={{
- borderRadius: cornerRadius,
- overflow: 'hidden',
- fontFamily: 'GT Planar',
- backgroundColor: 'white',
- border: '3px solid black',
- }}
- className="text-black flex justify-center items-center font-sans text-2xl font-bold flex-1"
- >
- <div>
- Hallo{' '}
- <span className="bg-blue-600 px-2 py-1 rounded text-white">
- IPT!
- </span>
- </div>
+ <div>
+ Hallo{' '}
+ <span className="bg-blue-600 px-2 py-1 rounded text-white">
+ IPT!
+ </span>
</div>
- </ExtrudeDiv>
- </AbsoluteFill>
+ </div>
+ </ExtrudeDiv>
</Rotations>
);
};
+const LabelOpacityContext = React.createContext<number>(1);
+
+const useLabelOpacity = () => {
+ return React.useContext(LabelOpacityContext);
+};
+
const Rotations: React.FC<{
children: React.ReactNode;
delay: number;
-}> = ({children, delay}) => {
+ zIndexHack: boolean;
+}> = ({children, delay, zIndexHack}) => {
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
+ const opacity = useLabelOpacity();
const spr = spring({
fps,
@@ -143,13 +154,27 @@ const Rotations: React.FC<{
return (
<RotateY radians={rotate - 0.4}>
<RotateX radians={-0.6}>
- <RotateZ radians={-0.5}>{children}</RotateZ>
+ <RotateZ radians={-0.5}>
+ <LabelOpacityContext.Provider value={spr * opacity}>
+ <AbsoluteFill
+ id="video"
+ style={{
+ zIndex: zIndexHack ? (spr > 0.3 ? 1 : -1) : undefined,
+ }}
+ className="flex justify-center items-center"
+ >
+ {children}
+ </AbsoluteFill>
+ </LabelOpacityContext.Provider>
+ </RotateZ>
</RotateX>
</RotateY>
);
};
export const Compose = () => {
+ const animationStart = 50;
+
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
const bRollEnter = spring({
@@ -158,7 +183,7 @@ export const Compose = () => {
config: {
damping: 200,
},
- delay: 50,
+ delay: 50 + animationStart,
durationInFrames: 25,
durationRestThreshold: 0.0001,
});
@@ -168,16 +193,21 @@ export const Compose = () => {
config: {
damping: 200,
},
- delay: 90,
+ delay: 90 + animationStart,
durationInFrames: 25,
durationRestThreshold: 0.0001,
});
- const endCard = interpolate(frame, [120, 145], [0, 1], {
- extrapolateLeft: 'clamp',
- extrapolateRight: 'clamp',
- easing: Easing.bezier(0.42, 0, 0.58, 1),
- });
+ const endCard = interpolate(
+ frame,
+ [120 + animationStart, 145 + animationStart],
+ [0, 1],
+ {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ easing: Easing.bezier(0.42, 0, 0.58, 1),
+ },
+ );
const layerDownProgress =
interpolate(bRollEnter, [0, 0.5], [0, 1], {
@@ -196,27 +226,41 @@ export const Compose = () => {
const liftCaptions = interpolate(endCard, [0, 1], [0, -2000]);
+ const outAnimation = spring({
+ fps,
+ frame,
+ config: {
+ damping: 200,
+ },
+ delay: 220,
+ durationInFrames: 20,
+ });
+
return (
<AbsoluteFill className="flex justify-center items-center">
<Scale factor={1.3}>
<TranslateX px={firstX}>
<TranslateZ px={1.5 * depth * layerDownProgress}>
- <VideoLayers label="<Video />" />
+ <VideoLayers delay={animationStart} label="<Video />" />
</TranslateZ>
</TranslateX>
<TranslateY
px={interpolate(bRollEnter + bRollExit, [0, 1, 2], [1500, 0, -1500])}
>
- <VideoLayers label="<BRoll />" />
+ <VideoLayers delay={animationStart} label="<BRoll />" />
</TranslateY>
{endCard > 0 && (
<TranslateX px={secondX}>
- <VideoLayers label="<EndCard />" />
+ <RotateY radians={Math.PI * outAnimation}>
+ <LabelOpacityContext.Provider value={1 - outAnimation}>
+ <VideoLayers delay={animationStart} label="<EndCard />" />
+ </LabelOpacityContext.Provider>
+ </RotateY>
</TranslateX>
)}
<TranslateY px={190}>
- <TranslateZ px={-depth + liftCaptions}>
- <CaptionLayers />
+ <TranslateZ px={-depth * 2 + liftCaptions}>
+ <CaptionLayers delay={animationStart} />
</TranslateZ>
</TranslateY>
</Scale>
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 6fa41e9d16..6522469e93 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -1504,7 +1504,7 @@ export const Index: React.FC = () => {
width={1080}
height={1080}
fps={30}
- durationInFrames={1000}
+ durationInFrames={240}
/>
<Composition
id="3DContext"