🎥 Make videos programmatically with React
`docs`: Rewrite shaders docs with createEffect (#9046)
Jonny Burger51cb023ad943
diff --git a/packages/docs/components/demos/ShaderDemo.tsx b/packages/docs/components/demos/ShaderDemo.tsxindex 6de2156a0b..4eaef14011 100644--- a/packages/docs/components/demos/ShaderDemo.tsx+++ b/packages/docs/components/demos/ShaderDemo.tsx@@ -1,104 +1,25 @@-import {useCallback, useEffect, useRef} from 'react';-import {AbsoluteFill, useCurrentFrame, useVideoConfig} from 'remotion';--const VERTEX_SHADER = `-attribute vec2 position;-void main() {- gl_Position = vec4(position, 0.0, 1.0);-}-`;--const FRAGMENT_SHADER = `-#ifdef GL_ES-precision mediump float;-#endif--uniform float time;-uniform vec2 resolution;--const float Pi = 3.14159;--void main()-{- vec2 p = 0.001 * gl_FragCoord.xy;- for(int i = 1; i < 7; i++)- {- vec2 newp = p;- newp.x += 0.6 / float(i) * cos(float(i) * p.y + (time * 20.0) / 10.0 + 0.3 * float(i)) + 400.0 / 20.0;- newp.y += 0.6 / float(i) * cos(float(i) * p.x + (time * 20.0) / 10.0 + 0.3 * float(i + 10)) - 400.0 / 20.0 + 15.0;- p = newp;- }- vec3 col = vec3(0.5 * sin(3.0 * p.x) + 0.5, 0.5 * sin(3.0 * p.y) + 0.5, sin(p.x + p.y));- gl_FragColor = vec4(col, 1.0);-}-`;+import {halftoneLinearGradient} from '@remotion/effects/halftone-linear-gradient';+import React from 'react';+import {Solid, useVideoConfig} from 'remotion';export const ShaderDemoComp: React.FC = () => {- const frame = useCurrentFrame();- const {fps, width, height} = useVideoConfig();- const canvasRef = useRef<HTMLCanvasElement>(null);- const glRef = useRef<{- gl: WebGLRenderingContext;- timeLoc: WebGLUniformLocation;- resLoc: WebGLUniformLocation;- } | null>(null);-- const initGl = useCallback((canvas: HTMLCanvasElement) => {- const gl = canvas.getContext('webgl');- if (!gl) return null;-- const compile = (type: number, src: string) => {- const s = gl.createShader(type)!;- gl.shaderSource(s, src);- gl.compileShader(s);- return s;- };-- const program = gl.createProgram()!;- gl.attachShader(program, compile(gl.VERTEX_SHADER, VERTEX_SHADER));- gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT_SHADER));- gl.linkProgram(program);- gl.useProgram(program);-- const buf = gl.createBuffer();- gl.bindBuffer(gl.ARRAY_BUFFER, buf);- gl.bufferData(- gl.ARRAY_BUFFER,- new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]),- gl.STATIC_DRAW,- );- const pos = gl.getAttribLocation(program, 'position');- gl.enableVertexAttribArray(pos);- gl.vertexAttribPointer(pos, 2, gl.FLOAT, false, 0, 0);-- return {- gl,- timeLoc: gl.getUniformLocation(program, 'time')!,- resLoc: gl.getUniformLocation(program, 'resolution')!,- };- }, []);-- useEffect(() => {- const canvas = canvasRef.current;- if (!canvas || glRef.current) return;- glRef.current = initGl(canvas);- }, [initGl]);-- useEffect(() => {- const ctx = glRef.current;- if (!ctx) return;- const {gl, timeLoc, resLoc} = ctx;-- const time = frame / fps;- gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);- gl.uniform1f(timeLoc, time);- gl.uniform2f(resLoc, gl.canvas.width, gl.canvas.height);- gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);- }, [frame, fps]);+ const {width, height} = useVideoConfig();return (- <AbsoluteFill>- <canvas ref={canvasRef} width={width} height={height} />- </AbsoluteFill>+ <Solid+ width={width}+ height={height}+ color="black"+ effects={[+ halftoneLinearGradient({+ firstStopDotSize: 0,+ secondStopDotSize: 42,+ firstStopPosition: [0, 0.5],+ secondStopPosition: [1, 0.5],+ gridSize: 24,+ dotColor: '#0b84f3',+ }),+ ]}+ />);};diff --git a/packages/docs/docs/shaders.mdx b/packages/docs/docs/shaders.mdxindex 4f5ab07a42..c8c4aa8dd8 100644--- a/packages/docs/docs/shaders.mdx+++ b/packages/docs/docs/shaders.mdx@@ -5,144 +5,95 @@ title: Shaderscrumb: 'Designing visuals'----You can use WebGL shaders in Remotion by rendering to a `<canvas>` element.-[All animations must be driven](/docs/animating-properties#always-animate-using-usecurrentframe) by [`useCurrentFrame()`](/docs/use-current-frame) to ensure deterministic rendering during both preview and final output.+You can use shader effects in Remotion to generate backgrounds or transform content.-## Retrieving a WebGL context+## Shaders as backgrounds-Get a reference to a `<canvas>` element using [`useRef()`](https://react.dev/reference/react/useRef), then call `canvas.getContext("webgl")` to obtain a [`WebGLRenderingContext`](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext). Initialize the GL state once (e.g. compile shaders, set up buffers), then update uniforms each frame based on the current time derived from [`useCurrentFrame()`](/docs/use-current-frame) and [`useVideoConfig()`](/docs/use-video-config).--## Example--The following component renders an animated shader.-The `time` uniform is computed from `frame / fps`, making the animation frame-accurate and deterministic.+[`@remotion/effects`](/docs/effects/api) includes many effect presets that can be applied to canvas-based components.+To create a shader background, apply an effect such as [`halftoneLinearGradient()`](/docs/effects/halftone-linear-gradient) to [`<Solid>`](/docs/solid).<Demo type="shader" />-```tsx twoslash title="BurlFigure.tsx"-import {useCurrentFrame, useVideoConfig, AbsoluteFill} from 'remotion';-import {useCallback, useRef, useEffect} from 'react';--const VERTEX_SHADER = `-attribute vec2 position;-void main() {- gl_Position = vec4(position, 0.0, 1.0);-}-`;--const FRAGMENT_SHADER = `-#ifdef GL_ES-precision mediump float;-#endif--uniform float time;-uniform vec2 resolution;--const float Pi = 3.14159;--void main()-{- vec2 p = 0.001 * gl_FragCoord.xy;- for(int i = 1; i < 7; i++)- {- vec2 newp = p;- newp.x += 0.6 / float(i) * cos(float(i) * p.y + (time * 20.0) / 10.0 + 0.3 * float(i)) + 400.0 / 20.0;- newp.y += 0.6 / float(i) * cos(float(i) * p.x + (time * 20.0) / 10.0 + 0.3 * float(i + 10)) - 400.0 / 20.0 + 15.0;- p = newp;- }- vec3 col = vec3(0.5 * sin(3.0 * p.x) + 0.5, 0.5 * sin(3.0 * p.y) + 0.5, sin(p.x + p.y));- gl_FragColor = vec4(col, 1.0);-}-`;--export const BurlFigure: React.FC = () => {- const frame = useCurrentFrame();- const {fps, width, height} = useVideoConfig();- const canvasRef = useRef<HTMLCanvasElement>(null);- const glRef = useRef<{- gl: WebGLRenderingContext;- timeLoc: WebGLUniformLocation;- resLoc: WebGLUniformLocation;- } | null>(null);-- const initGl = useCallback((canvas: HTMLCanvasElement) => {- const gl = canvas.getContext('webgl');- if (!gl) return null;-- const compile = (type: number, src: string) => {- const s = gl.createShader(type)!;- gl.shaderSource(s, src);- gl.compileShader(s);- return s;- };-- const program = gl.createProgram()!;- gl.attachShader(program, compile(gl.VERTEX_SHADER, VERTEX_SHADER));- gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT_SHADER));- gl.linkProgram(program);- gl.useProgram(program);-- const buf = gl.createBuffer();- gl.bindBuffer(gl.ARRAY_BUFFER, buf);- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);- const pos = gl.getAttribLocation(program, 'position');- gl.enableVertexAttribArray(pos);- gl.vertexAttribPointer(pos, 2, gl.FLOAT, false, 0, 0);-- return {- gl,- timeLoc: gl.getUniformLocation(program, 'time')!,- resLoc: gl.getUniformLocation(program, 'resolution')!,- };- }, []);-- useEffect(() => {- const canvas = canvasRef.current;- if (!canvas || glRef.current) return;- glRef.current = initGl(canvas);- }, [initGl]);-- useEffect(() => {- const ctx = glRef.current;- if (!ctx) return;- const {gl, timeLoc, resLoc} = ctx;-- const time = frame / fps;- gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);- gl.uniform1f(timeLoc, time);- gl.uniform2f(resLoc, gl.canvas.width, gl.canvas.height);- gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);- }, [frame, fps]);+```tsx twoslash title="HalftoneBackground.tsx"+import {halftoneLinearGradient} from '@remotion/effects/halftone-linear-gradient';+import {Solid, useVideoConfig} from 'remotion';++export const HalftoneBackground: React.FC = () => {+ const {width, height} = useVideoConfig();return (- <AbsoluteFill>- <canvas ref={canvasRef} width={width} height={height} />- </AbsoluteFill>+ <Solid+ width={width}+ height={height}+ color="black"+ effects={[+ halftoneLinearGradient({+ firstStopDotSize: 0,+ secondStopDotSize: 42,+ firstStopPosition: [0, 0.5],+ secondStopPosition: [1, 0.5],+ gridSize: 24,+ dotColor: '#0b84f3',+ }),+ ]}+ />);};```-:::note-Shader source: [GLSL Sandbox](https://glslsandbox.com/e#24303.0).-:::+You can also vibe code a custom effect using [`createEffect()`](/docs/create-effect).++## Apply shaders to content++Use [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) to capture HTML content and apply shader effects to its pixels.+This example applies [`fisheye()`](/docs/effects/fisheye) to a DOM element:-## Drawing a shader to a DOM element+```tsx twoslash title="FisheyeContent.tsx"+import {fisheye} from '@remotion/effects/fisheye';+import {AbsoluteFill, HtmlInCanvas, useVideoConfig} from 'remotion';-WebGL shaders can only render into a `<canvas>` element.-You cannot apply a shader as a post-processing effect to arbitrary DOM elements.+export const FisheyeContent: React.FC = () => {+ const {width, height} = useVideoConfig();++ return (+ <HtmlInCanvas+ width={width}+ height={height}+ effects={[+ fisheye({+ fieldOfView: 2.5,+ }),+ ]}+ >+ <AbsoluteFill+ style={{+ alignItems: 'center',+ backgroundColor: '#0b84f3',+ color: 'white',+ fontFamily: 'sans-serif',+ fontSize: 120,+ justifyContent: 'center',+ }}+ >+ Hello+ </AbsoluteFill>+ </HtmlInCanvas>+ );+};+```## Rendering-When rendering a video that uses WebGL, you need to pass [`--gl=angle`](/docs/gl-options) to ensure the headless browser uses the GPU:+When rendering a video that uses WebGL, pass [`--gl=angle`](/docs/gl-options) to ensure the headless browser can create a WebGL context:```bash-npx remotion render MyComp --gl=angle+bunx remotion render MyComp --gl=angle```Without this flag, the headless browser may not have a WebGL context available.## See also-- [`useCurrentFrame()`](/docs/use-current-frame)-- [`useVideoConfig()`](/docs/use-video-config)-- [Animation math](/docs/animation-math)+- [`@remotion/effects`](/docs/effects/api)+- [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas)+- [`<Solid>`](/docs/solid)