Skip to content

🎥 Make videos programmatically with React

hmm

JonnyBurger47fcc952b0d9
diff --git a/packages/example/src/Transitions/WebGlTransition.tsx b/packages/example/src/Transitions/WebGlTransition.tsx
index 2980573044..1a18823905 100644
--- a/packages/example/src/Transitions/WebGlTransition.tsx
+++ b/packages/example/src/Transitions/WebGlTransition.tsx
@@ -1,22 +1,14 @@
-import {
- linearTiming,
- TransitionSeries,
- useTransitionProgress,
-} from '@remotion/transitions';
+import {tint} from '@remotion/canvas-effects';
+import {linearTiming, TransitionSeries} from '@remotion/transitions';
import {zoomBlur} from '@remotion/transitions/zoom-blur';
import React from 'react';
-import {AbsoluteFill, Sequence} from 'remotion';
+import {Easing} from 'remotion';
+import {AbsoluteFill, Img, Sequence, staticFile} from 'remotion';
export const Letter: React.FC<{
readonly children: React.ReactNode;
readonly color: string;
}> = ({children, color}) => {
- const {
- entering,
- exiting,
- isInTransitionSeries: isInTransition,
- } = useTransitionProgress();
-
return (
<AbsoluteFill
style={{
@@ -27,8 +19,7 @@ export const Letter: React.FC<{
color: 'white',
}}
>
- {children} {entering.toFixed(2)} {exiting.toFixed(2)}{' '}
- {String(isInTransition)}
+ {children}
</AbsoluteFill>
);
};
@@ -38,15 +29,31 @@ export const WebGlTransition: React.FC = () => {
<TransitionSeries>
<TransitionSeries.Sequence durationInFrames={90}>
<Sequence>
- <Letter color="orange">A</Letter>
+ <Img className="absolute" src={staticFile('1.jpg')}></Img>
+ <Letter color="transparent">A</Letter>
</Sequence>
</TransitionSeries.Sequence>
<TransitionSeries.Transition
- presentation={zoomBlur({})}
- timing={linearTiming({durationInFrames: 20})}
+ presentation={zoomBlur({
+ _experimentalEffects: [
+ tint({
+ color: 'red',
+ }),
+ ],
+ })}
+ timing={linearTiming({
+ durationInFrames: 40,
+ easing: Easing.bezier(0.36, 0.53, 0, 1),
+ })}
/>
<TransitionSeries.Sequence durationInFrames={90}>
- <Letter color="pink">B</Letter>
+ <Sequence>
+ <Img
+ className="absolute object-cover"
+ src={staticFile('2.jpg')}
+ ></Img>
+ <Letter color="transparent">B</Letter>
+ </Sequence>
</TransitionSeries.Sequence>
</TransitionSeries>
);
diff --git a/packages/transitions/src/TransitionSeries.tsx b/packages/transitions/src/TransitionSeries.tsx
index 174b6af6c5..fb3171a230 100644
--- a/packages/transitions/src/TransitionSeries.tsx
+++ b/packages/transitions/src/TransitionSeries.tsx
@@ -439,7 +439,7 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
const UppercaseNextPresentation = nextPresentation.component;
const UppercasePrevPresentation = prevPresentation.component;
- const RequiresOverlay = prevPresentation.requiresOverlay;
+ const RequiresOverlay = prevPresentation.overlay;
return (
<Sequence
// eslint-disable-next-line react/no-array-index-key
@@ -480,7 +480,7 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
</UppercasePrevPresentation>
</WrapInExitingProgressContext>
</UppercaseNextPresentation>
- {RequiresOverlay && (
+ {RequiresOverlay && nextProgress > 0 && nextProgress < 1 && (
<RequiresOverlay
refToMethods={refToMethods}
presentationProgress={nextProgress}
@@ -496,7 +496,7 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
const UppercasePrevPresentation = prevPresentation.component;
- const RequiresOverlay = prevPresentation.requiresOverlay;
+ const RequiresOverlay = prevPresentation.overlay;
return (
<Sequence
// eslint-disable-next-line react/no-array-index-key
@@ -523,7 +523,7 @@ const TransitionSeriesChildren: FC<{readonly children: React.ReactNode}> = ({
{child}
</WrapInEnteringProgressContext>
</UppercasePrevPresentation>
- {RequiresOverlay && (
+ {RequiresOverlay && prevProgress > 0 && prevProgress < 1 && (
<RequiresOverlay
refToMethods={refToMethods}
presentationProgress={prevProgress}
diff --git a/packages/transitions/src/presentations/zoom-blur.tsx b/packages/transitions/src/presentations/zoom-blur.tsx
index 989a1ad8c7..a36e93bd36 100644
--- a/packages/transitions/src/presentations/zoom-blur.tsx
+++ b/packages/transitions/src/presentations/zoom-blur.tsx
@@ -1,6 +1,18 @@
-import {useMemo, useRef, useLayoutEffect, useImperativeHandle} from 'react';
+import {
+ useMemo,
+ useRef,
+ useLayoutEffect,
+ useImperativeHandle,
+ useState,
+} from 'react';
import type {RefObject} from 'react';
-import {AbsoluteFill, useVideoConfig} from 'remotion';
+import type {EffectsProp} from 'remotion';
+import {
+ AbsoluteFill,
+ Internals,
+ useCurrentFrame,
+ useVideoConfig,
+} from 'remotion';
import type {
OverlayComponentProps,
TransitionPresentation,
@@ -8,8 +20,8 @@ import type {
} from '../types';
export type ZoomBlurProps = {
- scale?: number;
rotation?: number;
+ _experimentalEffects?: EffectsProp;
};
export const ZoomBlurPresentation: React.FC<
@@ -128,6 +140,8 @@ export type GLState = {
uTime: WebGLUniformLocation | null;
uPrev: WebGLUniformLocation | null;
uNext: WebGLUniformLocation | null;
+ uAspect: WebGLUniformLocation | null;
+ uMaxAngle: WebGLUniformLocation | null;
};
const VERTEX_SHADER = `#version 300 es
@@ -144,6 +158,8 @@ precision highp float;
uniform sampler2D u_prev;
uniform sampler2D u_next;
uniform float u_time;
+uniform float u_aspect;
+uniform float u_max_angle;
in vec2 v_uv;
out vec4 outColor;
@@ -151,6 +167,24 @@ out vec4 outColor;
const int SAMPLES = 16;
const float STRENGTH = 0.35;
+vec2 transformUV(vec2 uv, float angle, float scale) {
+ vec2 p = uv - 0.5;
+ p.x *= u_aspect;
+ p /= scale;
+ float c = cos(-angle);
+ float s = sin(-angle);
+ p = vec2(p.x * c - p.y * s, p.x * s + p.y * c);
+ p.x /= u_aspect;
+ return p + 0.5;
+}
+
+float coverScale(float angle) {
+ float c = abs(cos(angle));
+ float s = abs(sin(angle));
+ float ar = max(u_aspect, 1.0 / u_aspect);
+ return c + ar * s;
+}
+
vec4 zoomBlur(sampler2D tex, vec2 uv, float strength) {
vec2 dir = uv - 0.5;
vec4 acc = vec4(0.0);
@@ -164,8 +198,15 @@ vec4 zoomBlur(sampler2D tex, vec2 uv, float strength) {
void main() {
float mixT = clamp(u_time, 0.0, 1.0);
- vec4 prev = zoomBlur(u_prev, v_uv, STRENGTH * (1.0 - mixT));
- vec4 next = zoomBlur(u_next, v_uv, STRENGTH * mixT);
+
+ float nextAngle = u_max_angle * mixT;
+ float prevAngle = -u_max_angle * (1.0 - mixT);
+
+ vec2 prevUV = transformUV(v_uv, prevAngle, coverScale(prevAngle));
+ vec2 nextUV = transformUV(v_uv, nextAngle, coverScale(nextAngle));
+
+ vec4 prev = zoomBlur(u_prev, prevUV, STRENGTH * (1.0 - mixT));
+ vec4 next = zoomBlur(u_next, nextUV, STRENGTH * mixT);
outColor = mix(prev, next, (1.0 - mixT));
}`;
@@ -236,7 +277,7 @@ const createTexture = (gl: WebGL2RenderingContext): WebGLTexture => {
};
export const init = (
- canvas: HTMLCanvasElement,
+ canvas: OffscreenCanvas,
stateRef: RefObject<GLState | null>,
) => {
const gl = canvas.getContext('webgl2', {premultipliedAlpha: true});
@@ -269,6 +310,8 @@ export const init = (
uTime: gl.getUniformLocation(program, 'u_time'),
uPrev: gl.getUniformLocation(program, 'u_prev'),
uNext: gl.getUniformLocation(program, 'u_next'),
+ uAspect: gl.getUniformLocation(program, 'u_aspect'),
+ uMaxAngle: gl.getUniformLocation(program, 'u_max_angle'),
};
return () => {
@@ -292,6 +335,7 @@ export const draw = ({
width,
height,
time,
+ rotation,
}: {
prevImage: ElementImage | null;
nextImage: ElementImage | null;
@@ -299,8 +343,19 @@ export const draw = ({
width: number;
height: number;
time: number;
+ rotation: number;
}) => {
- const {gl, program, prevTex, nextTex, uTime, uPrev, uNext} = state;
+ const {
+ gl,
+ program,
+ prevTex,
+ nextTex,
+ uTime,
+ uPrev,
+ uNext,
+ uAspect,
+ uMaxAngle,
+ } = state;
if (
!prevImage ||
!nextImage ||
@@ -348,26 +403,33 @@ export const draw = ({
gl.uniform1i(uNext, 1);
gl.uniform1f(uTime, time);
+ gl.uniform1f(uAspect, width / height);
+ gl.uniform1f(uMaxAngle, rotation);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
export const ShaderOverlay: React.FC<OverlayComponentProps<ZoomBlurProps>> = ({
refToMethods,
- passedProps: {scale = 3, rotation = Math.PI / 2},
- presentationProgress,
+ passedProps: {rotation = Math.PI / 6, _experimentalEffects = []},
}) => {
const {width, height} = useVideoConfig();
+ const [offscreenCanvas] = useState(() => new OffscreenCanvas(width, height));
const canvasRef = useRef<HTMLCanvasElement>(null);
const stateRef = useRef<GLState | null>(null);
+ const frame = useCurrentFrame();
+ const frameRef = useRef(frame);
+ frameRef.current = frame;
useLayoutEffect(() => {
- const cleanup = init(canvasRef.current!, stateRef);
+ const cleanup = init(offscreenCanvas, stateRef);
return () => {
cleanup();
};
- }, []);
+ }, [offscreenCanvas]);
+
+ const chainState = Internals.useEffectChainState();
useImperativeHandle(refToMethods, () => {
return {
@@ -387,6 +449,18 @@ export const ShaderOverlay: React.FC<OverlayComponentProps<ZoomBlurProps>> = ({
width,
height,
time: progress,
+ rotation,
+ });
+ offscreenCanvas.width = width;
+ offscreenCanvas.height = height;
+ Internals.runEffectChain({
+ state: chainState.get(width, height)!,
+ source: offscreenCanvas,
+ effects: _experimentalEffects,
+ frame: frameRef.current,
+ width,
+ height,
+ output: canvasRef.current,
});
},
clear: () => {
@@ -399,13 +473,26 @@ export const ShaderOverlay: React.FC<OverlayComponentProps<ZoomBlurProps>> = ({
};
});
+ const outerStyle: React.CSSProperties = useMemo(() => {
+ return {
+ pointerEvents: 'none',
+ };
+ }, []);
+
+ const innerStyle: React.CSSProperties = useMemo(() => {
+ return {
+ width: '100%',
+ height: '100%',
+ };
+ }, []);
+
return (
- <AbsoluteFill style={{pointerEvents: 'none'}}>
+ <AbsoluteFill style={outerStyle}>
<canvas
ref={canvasRef}
width={width}
height={height}
- style={{width: '100%', height: '100%'}}
+ style={innerStyle}
/>
</AbsoluteFill>
);
@@ -417,6 +504,6 @@ export const zoomBlur = (
return {
component: ZoomBlurPresentation,
props: props ?? {},
- requiresOverlay: ShaderOverlay,
+ overlay: ShaderOverlay,
};
};
diff --git a/packages/transitions/src/types.ts b/packages/transitions/src/types.ts
index 3aa0ee4aa0..83a7a92871 100644
--- a/packages/transitions/src/types.ts
+++ b/packages/transitions/src/types.ts
@@ -45,9 +45,7 @@ export type TransitionPresentation<
TransitionPresentationComponentProps<PresentationProps>
>;
props: PresentationProps;
- requiresOverlay?: LooseComponentType<
- OverlayComponentProps<PresentationProps>
- >;
+ overlay?: LooseComponentType<OverlayComponentProps<PresentationProps>>;
};
export type TransitionPresentationComponentProps<