π₯ Make videos programmatically with React
Docs: Shaders
JonnyBurger463140c8f9be
diff --git a/.claude/skills/docs-demo/SKILL.md b/.claude/skills/docs-demo/SKILL.mdnew file mode 100644index 0000000000..eaa75236c9--- /dev/null+++ b/.claude/skills/docs-demo/SKILL.md@@ -0,0 +1,57 @@+---+name: docs-demo+description: Add an interactive demo to the Remotion documentation. Use when creating a new <Demo> component for docs pages.+---++# Adding an Interactive Demo to Docs++Interactive demos render a Remotion composition inline in documentation pages using `@remotion/player`. They live in `packages/docs/components/demos/`.++## Steps++1. **Create a component** in `packages/docs/components/demos/` (e.g. `MyDemo.tsx`). It should be a standard React component using Remotion hooks like `useCurrentFrame()` and `useVideoConfig()`.++2. **Register the demo** in `packages/docs/components/demos/types.ts`:+ - Import the component+ - Export a `DemoType` object with these fields:+ - `id`: unique string used in `<Demo type="..." />`+ - `comp`: the React component+ - `compWidth` / `compHeight`: canvas dimensions (e.g. 1280x720)+ - `fps`: frame rate (typically 30)+ - `durationInFrames`: animation length+ - `autoPlay`: whether it plays automatically+ - `options`: array of interactive controls (can be empty `[]`)++3. **Add to the demos array** in `packages/docs/components/demos/index.tsx`:+ - Import the demo constant from `./types`+ - Add it to the `demos` array++4. **Use in MDX** with `<Demo type="your-id" />`++## Options++Options add interactive controls below the player. Each option needs `name` and `optional` (`'no'`, `'default-enabled'`, or `'default-disabled'`).++Supported types:++- `type: 'numeric'` β slider with `min`, `max`, `step`, `default`+- `type: 'boolean'` β checkbox with `default`+- `type: 'enum'` β dropdown with `values` array and `default`+- `type: 'string'` β text input with `default`++Option values are passed to the component as `inputProps`. Access them as regular React props.++## Example registration++```ts+export const myDemo: DemoType = {+ comp: MyDemoComp,+ compHeight: 720,+ compWidth: 1280,+ durationInFrames: 150,+ fps: 30,+ id: 'my-demo',+ autoPlay: true,+ options: [],+};+```diff --git a/.claude/skills/writing-docs/SKILL.md b/.claude/skills/writing-docs/SKILL.mdindex e0bafeddd5..059f1cc72a 100644--- a/.claude/skills/writing-docs/SKILL.md+++ b/.claude/skills/writing-docs/SKILL.md@@ -94,7 +94,7 @@ console.log('Hello');<Demo type="rect"/>```-Demos must be implemented in `packages/docs/components/demos/index.tsx`.+Demos must be implemented in `packages/docs/components/demos/index.tsx`. See the `docs-demo` skill for details on adding new demos.### AvailableFromdiff --git a/packages/docs/components/demos/ShaderDemo.tsx b/packages/docs/components/demos/ShaderDemo.tsxnew file mode 100644index 0000000000..6de2156a0b--- /dev/null+++ b/packages/docs/components/demos/ShaderDemo.tsx@@ -0,0 +1,104 @@+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);+}+`;++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]);++ return (+ <AbsoluteFill>+ <canvas ref={canvasRef} width={width} height={height} />+ </AbsoluteFill>+ );+};diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsxindex c4487bf77b..e4f4855355 100644--- a/packages/docs/components/demos/index.tsx+++ b/packages/docs/components/demos/index.tsx@@ -26,6 +26,7 @@ import {rotateDemo,roundedTextBoxDemo,scaleDemo,+ shaderDemo,skewDemo,slidePresentationDemo,slidePresentationDemoLongThreshold,@@ -75,6 +76,7 @@ const demos: DemoType[] = [roundedTextBoxDemo,springDemo,springDampingDemo,+ shaderDemo,];export const Demo: React.FC<{diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.tsindex baaed82d2a..b826b3cc9e 100644--- a/packages/docs/components/demos/types.ts+++ b/packages/docs/components/demos/types.ts@@ -29,6 +29,7 @@ import {SkewDemo,TranslateDemo,} from './Translate';+import {ShaderDemoComp} from './ShaderDemo';import {TriangleDemo} from './Triangle';export type Option = {@@ -885,6 +886,17 @@ export const springDemo: DemoType = {],};+export const shaderDemo: DemoType = {+ comp: ShaderDemoComp,+ compHeight: 720,+ compWidth: 1280,+ durationInFrames: 150,+ fps: 30,+ id: 'shader',+ autoPlay: true,+ options: [],+};+export const springDampingDemo: DemoType = {comp: SpringDemo,compHeight: 400,diff --git a/packages/docs/docs/ai/dynamic-compilation.mdx b/packages/docs/docs/ai/dynamic-compilation.mdxindex 975219eea3..a2773040ea 100644--- a/packages/docs/docs/ai/dynamic-compilation.mdx+++ b/packages/docs/docs/ai/dynamic-compilation.mdx@@ -1,4 +1,5 @@---+image: /generated/articles-docs-ai-dynamic-compilation.pngcrumb: 'AI'title: Just-in-time compilation of Remotion codesidebar_label: Just-in-time compilationdiff --git a/packages/docs/docs/shaders.mdx b/packages/docs/docs/shaders.mdxnew file mode 100644index 0000000000..4f5ab07a42--- /dev/null+++ b/packages/docs/docs/shaders.mdx@@ -0,0 +1,148 @@+---+image: /generated/articles-docs-shaders.png+id: shaders+title: Shaders+crumb: '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.++## Retrieving a WebGL context++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.++<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]);++ return (+ <AbsoluteFill>+ <canvas ref={canvasRef} width={width} height={height} />+ </AbsoluteFill>+ );+};+```++:::note+Shader source: [GLSL Sandbox](https://glslsandbox.com/e#24303.0).+:::++## Drawing a shader to a DOM element++WebGL shaders can only render into a `<canvas>` element.+You cannot apply a shader as a post-processing effect to arbitrary DOM elements.++## 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:++```bash+npx 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)diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex 21523bbae0..bfa217509c 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -849,6 +849,7 @@ const sidebars: SidebarsConfig = {'noise-visualization','maps','animation-math',+ 'shaders',],},{diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex 1d583c230f..d6b69730d7 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -107,6 +107,24 @@ export const articles = [noAi: false,slug: 'ai/claude-code',},+ {+ id: 'ai/dynamic-compilation',+ title: 'Just-in-time compilation of Remotion code',+ relativePath: 'docs/ai/dynamic-compilation.mdx',+ compId: 'articles-docs-ai-dynamic-compilation',+ crumb: 'AI',+ noAi: false,+ slug: 'ai/dynamic-compilation',+ },+ {+ id: 'ai/generate',+ title: 'Generate Remotion Code using LLMs',+ relativePath: 'docs/ai/generate.mdx',+ compId: 'articles-docs-ai-generate',+ crumb: 'AI',+ noAi: false,+ slug: 'ai/generate',+ },{id: 'ai/index',title: 'Building with Remotion and AI',@@ -5142,6 +5160,15 @@ export const articles = [noAi: false,slug: 'series',},+ {+ id: 'shaders',+ title: 'Shaders',+ relativePath: 'docs/shaders.mdx',+ compId: 'articles-docs-shaders',+ crumb: 'Designing visuals',+ noAi: false,+ slug: 'shaders',+ },{id: 'shapes/circle',title: '<Circle />',diff --git a/packages/docs/static/generated/articles-docs-ai-dynamic-compilation.png b/packages/docs/static/generated/articles-docs-ai-dynamic-compilation.pngnew file mode 100644index 0000000000..cfad1cc08cBinary files /dev/null and b/packages/docs/static/generated/articles-docs-ai-dynamic-compilation.png differdiff --git a/packages/docs/static/generated/articles-docs-ai-generate.png b/packages/docs/static/generated/articles-docs-ai-generate.pngnew file mode 100644index 0000000000..d28f57266fBinary files /dev/null and b/packages/docs/static/generated/articles-docs-ai-generate.png differdiff --git a/packages/docs/static/generated/articles-docs-shaders.png b/packages/docs/static/generated/articles-docs-shaders.pngnew file mode 100644index 0000000000..d92a409352Binary files /dev/null and b/packages/docs/static/generated/articles-docs-shaders.png differ