Skip to content

🎥 Make videos programmatically with React

cool

JonnyBurger64e37fc9f5b7
diff --git a/packages/docs/docs/remotion/html-in-canvas.mdx b/packages/docs/docs/remotion/html-in-canvas.mdx
index 19b7a397ca..9ff78c3957 100644
--- a/packages/docs/docs/remotion/html-in-canvas.mdx
+++ b/packages/docs/docs/remotion/html-in-canvas.mdx
@@ -12,7 +12,7 @@ HTML-in-canvas is only available in Chrome 149 and later and if the `chrome://fl
## Example
-TODO
+`<HtmlInCanvas>` with default 2D painting (omit [`onPaint`](#onpaint)):
```tsx twoslash title="MyComp.tsx"
import {HtmlInCanvas} from 'remotion';
@@ -138,48 +138,334 @@ const onPaint: HtmlInCanvasOnPaint = ({canvas, element, elementImage}) => {
### WebGL
-Upload each frame with `texElementImage2D` (here the GL context and texture come from [`onInit`](#oninit) stored on a ref):
+Create the `webgl2` context, program, VAO, and texture in [`onInit`](#oninit). Each paint uploads the capture with [`texElementImage2D`](https://github.com/WICG/html-in-canvas) and draws a fullscreen triangle pair.
+
+<details>
+<summary>This example is long. Expand to see it:</summary>
+
+```tsx title="WebGL2: minimal full component"
+import React, {useCallback, useRef} from 'react';
+import {
+ AbsoluteFill,
+ HtmlInCanvas,
+ HtmlInCanvasOnInit,
+ HtmlInCanvasOnPaint,
+ useVideoConfig,
+} from 'remotion';
+
+type GlState = {
+ gl: WebGL2RenderingContext;
+ program: WebGLProgram;
+ uTex: WebGLUniformLocation | null;
+ texture: WebGLTexture;
+ vao: WebGLVertexArrayObject;
+};
-```tsx title="WebGL2: texElementImage2D (excerpt)"
-const onPaint: HtmlInCanvasOnPaint = ({elementImage}) => {
- const gl = gpuRef.current?.gl;
- const texture = gpuRef.current?.texture;
- if (!gl || !texture) {
- return;
- }
+const VS = `#version 300 es
+in vec2 a_pos;
+in vec2 a_uv;
+out vec2 v_uv;
+void main() {
+ gl_Position = vec4(a_pos, 0.0, 1.0);
+ v_uv = a_uv;
+}`;
+
+const FS = `#version 300 es
+precision highp float;
+uniform sampler2D u_tex;
+in vec2 v_uv;
+out vec4 o;
+void main() {
+ o = texture(u_tex, v_uv);
+}`;
+
+function linkProgram(gl: WebGL2RenderingContext, vsSrc: string, fsSrc: string) {
+ const vert = gl.createShader(gl.VERTEX_SHADER)!;
+ gl.shaderSource(vert, vsSrc);
+ gl.compileShader(vert);
+ const frag = gl.createShader(gl.FRAGMENT_SHADER)!;
+ gl.shaderSource(frag, fsSrc);
+ gl.compileShader(frag);
+ const program = gl.createProgram()!;
+ gl.attachShader(program, vert);
+ gl.attachShader(program, frag);
+ gl.linkProgram(program);
+ gl.deleteShader(vert);
+ gl.deleteShader(frag);
+ return program;
+}
+
+const QUAD = new Float32Array([
+ -1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1,
+]);
+
+export const WebGLExample: React.FC = () => {
+ const {width, height} = useVideoConfig();
+ const gpuRef = useRef<GlState | null>(null);
+
+ const onInit: HtmlInCanvasOnInit = useCallback(({canvas}) => {
+ const gl = canvas.getContext('webgl2', {
+ alpha: true,
+ premultipliedAlpha: true,
+ antialias: false,
+ });
+ if (!gl) {
+ throw new Error('WebGL2 unavailable');
+ }
+
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
+
+ const program = linkProgram(gl, VS, FS);
+ const uTex = gl.getUniformLocation(program, 'u_tex');
+
+ const texture = gl.createTexture()!;
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
+
+ const buffer = gl.createBuffer()!;
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
+ gl.bufferData(gl.ARRAY_BUFFER, QUAD, gl.STATIC_DRAW);
+
+ const vao = gl.createVertexArray()!;
+ gl.bindVertexArray(vao);
+ const locPos = gl.getAttribLocation(program, 'a_pos');
+ const locUv = gl.getAttribLocation(program, 'a_uv');
+ gl.enableVertexAttribArray(locPos);
+ gl.vertexAttribPointer(locPos, 2, gl.FLOAT, false, 16, 0);
+ gl.enableVertexAttribArray(locUv);
+ gl.vertexAttribPointer(locUv, 2, gl.FLOAT, false, 16, 8);
+
+ gpuRef.current = {gl, program, uTex, texture, vao};
+
+ return () => {
+ gl.deleteProgram(program);
+ gl.deleteTexture(texture);
+ gl.deleteVertexArray(vao);
+ gl.deleteBuffer(buffer);
+ gpuRef.current = null;
+ };
+ }, []);
+
+ const onPaint: HtmlInCanvasOnPaint = useCallback(({elementImage}) => {
+ const gpu = gpuRef.current;
+ if (!gpu) {
+ return;
+ }
+
+ const {gl} = gpu;
+ gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
+ gl.useProgram(gpu.program);
+
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, gpu.texture);
+ gl.texElementImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ elementImage,
+ );
+
+ if (gpu.uTex) {
+ gl.uniform1i(gpu.uTex, 0);
+ }
+
+ gl.bindVertexArray(gpu.vao);
+ gl.drawArrays(gl.TRIANGLES, 0, 6);
+ }, []);
- gl.bindTexture(gl.TEXTURE_2D, texture);
- gl.texElementImage2D(
- gl.TEXTURE_2D,
- 0,
- gl.RGBA,
- gl.RGBA,
- gl.UNSIGNED_BYTE,
- elementImage,
+ return (
+ <HtmlInCanvas width={width} height={height} onInit={onInit} onPaint={onPaint}>
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ color: 'white',
+ fontSize: 120,
+ }}
+ >
+ <h1>Hello</h1>
+ </AbsoluteFill>
+ </HtmlInCanvas>
);
- // ...bind program, set uniforms, draw your quad
};
```
+</details>
+
### WebGPU
-```tsx title="WebGPU: copyElementImageToTexture (excerpt)"
-const onPaint: HtmlInCanvasOnPaint = ({elementImage}) => {
- const gpu = gpuRef.current;
- if (!gpu) {
- return;
- }
+Request a device and configure the [`webgpu`](https://developer.mozilla.org/en-US/docs/Web/API/GPUCanvasContext) context in [`onInit`](#oninit). Each paint copies the element into a [`GPUTexture`](https://developer.mozilla.org/en-US/docs/Web/API/GPUTexture) with `copyElementImageToTexture`, then draws a fullscreen triangle to the swapchain.
+
+<details>
+<summary>This example is long. Expand to see it:</summary>
+
+```tsx title="WebGPU: minimal full component"
+import React, {useCallback, useRef} from 'react';
+import {
+ AbsoluteFill,
+ HtmlInCanvas,
+ HtmlInCanvasOnInit,
+ HtmlInCanvasOnPaint,
+ useVideoConfig,
+} from 'remotion';
+
+const WGSL = `
+struct VsOut {
+ @builtin(position) pos: vec4f,
+ @location(0) uv: vec2f,
+}
+@vertex
+fn vs(@builtin(vertex_index) i: u32) -> VsOut {
+ var p = array(vec2f(-1.0, -3.0), vec2f(-1.0, 1.0), vec2f(3.0, 1.0));
+ var uv = array(vec2f(0.0, 2.0), vec2f(0.0, 0.0), vec2f(2.0, 0.0));
+ var o: VsOut;
+ o.pos = vec4f(p[i], 0.0, 1.0);
+ o.uv = uv[i];
+ return o;
+}
+@group(0) @binding(0) var samp: sampler;
+@group(0) @binding(1) var tex: texture_2d<f32>;
+@fragment
+fn fs(in: VsOut) -> @location(0) vec4f {
+ return textureSample(tex, samp, in.uv);
+}
+`;
+
+type GpuState = {
+ device: GPUDevice;
+ context: GPUCanvasContext;
+ pipeline: GPURenderPipeline;
+ sampler: GPUSampler;
+ texture: GPUTexture;
+ bindGroup: GPUBindGroup;
+ width: number;
+ height: number;
+};
- gpu.device.queue.copyElementImageToTexture(
- elementImage,
- gpu.width,
- gpu.height,
- {texture: gpu.texture},
+export const WebGPUExample: React.FC = () => {
+ const {width, height} = useVideoConfig();
+ const gpuRef = useRef<GpuState | null>(null);
+
+ const onInit: HtmlInCanvasOnInit = useCallback(async ({canvas}) => {
+ const gpuApi = navigator.gpu;
+ if (!gpuApi) {
+ throw new Error('WebGPU not available');
+ }
+
+ const adapter = await gpuApi.requestAdapter();
+ if (!adapter) {
+ throw new Error('No WebGPU adapter');
+ }
+
+ const device = await adapter.requestDevice();
+ const context = canvas.getContext('webgpu');
+ if (!context) {
+ throw new Error('WebGPU context unavailable on OffscreenCanvas');
+ }
+
+ const format = gpuApi.getPreferredCanvasFormat();
+ context.configure({device, format, alphaMode: 'premultiplied'});
+
+ const module = device.createShaderModule({code: WGSL});
+ const pipeline = device.createRenderPipeline({
+ layout: 'auto',
+ vertex: {module, entryPoint: 'vs'},
+ fragment: {
+ module,
+ entryPoint: 'fs',
+ targets: [{format}],
+ },
+ primitive: {topology: 'triangle-list'},
+ });
+
+ const texture = device.createTexture({
+ size: {width: canvas.width, height: canvas.height},
+ format: 'rgba8unorm',
+ usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
+ });
+
+ const sampler = device.createSampler({
+ magFilter: 'linear',
+ minFilter: 'linear',
+ });
+
+ const bindGroup = device.createBindGroup({
+ layout: pipeline.getBindGroupLayout(0),
+ entries: [
+ {binding: 0, resource: sampler},
+ {binding: 1, resource: texture.createView()},
+ ],
+ });
+
+ gpuRef.current = {
+ device,
+ context,
+ pipeline,
+ sampler,
+ texture,
+ bindGroup,
+ width: canvas.width,
+ height: canvas.height,
+ };
+
+ return () => {
+ texture.destroy();
+ gpuRef.current = null;
+ };
+ }, []);
+
+ const onPaint: HtmlInCanvasOnPaint = useCallback(({elementImage}) => {
+ const gpu = gpuRef.current;
+ if (!gpu) {
+ return;
+ }
+
+ const {device, context, pipeline, texture, bindGroup} = gpu;
+
+ device.queue.copyElementImageToTexture(elementImage, gpu.width, gpu.height, {
+ texture,
+ });
+
+ const encoder = device.createCommandEncoder();
+ const view = context.getCurrentTexture().createView();
+ const pass = encoder.beginRenderPass({
+ colorAttachments: [
+ {
+ view,
+ clearValue: {r: 0, g: 0, b: 0, a: 0},
+ loadOp: 'clear',
+ storeOp: 'store',
+ },
+ ],
+ });
+ pass.setPipeline(pipeline);
+ pass.setBindGroup(0, bindGroup);
+ pass.draw(3);
+ pass.end();
+ device.queue.submit([encoder.finish()]);
+ }, []);
+
+ return (
+ <HtmlInCanvas width={width} height={height} onInit={onInit} onPaint={onPaint}>
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ fontSize: 120,
+ }}
+ >
+ <h1>Hello</h1>
+ </AbsoluteFill>
+ </HtmlInCanvas>
);
- // ...encode render pass, submit command buffer
};
```
+</details>
+
### Async callbacks
You can use `await` inside `onPaint` and Remotion will keep the frame open via [`delayRender()`](/docs/delay-render) until the promise settles.
diff --git a/packages/example/src/HtmlInCanvas/index.tsx b/packages/example/src/HtmlInCanvas/index.tsx
index 1eb5f0bd13..2c0f823ea1 100644
--- a/packages/example/src/HtmlInCanvas/index.tsx
+++ b/packages/example/src/HtmlInCanvas/index.tsx
@@ -1,5 +1,7 @@
export {HtmlInCanvasComplexText} from './complex-text';
export {HtmlInCanvasComposeAsyncBitmap} from './compose-async-bitmap';
+export {HtmlInCanvasDocsMinimalWebGL} from './minimal-docs-webgl';
+export {HtmlInCanvasDocsMinimalWebGPU} from './minimal-docs-webgpu';
export {HtmlInCanvasComposeWebGL} from './compose-webgl';
export {HtmlInCanvasComposeWebGLCrt} from './compose-webgl-crt';
export {HtmlInCanvasComposeWebGPU} from './compose-webgpu';
diff --git a/packages/example/src/HtmlInCanvas/minimal-docs-webgl.tsx b/packages/example/src/HtmlInCanvas/minimal-docs-webgl.tsx
new file mode 100644
index 0000000000..7ae03b33b4
--- /dev/null
+++ b/packages/example/src/HtmlInCanvas/minimal-docs-webgl.tsx
@@ -0,0 +1,159 @@
+/**
+ * Minimal WebGL2 + HtmlInCanvas sample (same code as /docs/remotion/html-in-canvas).
+ */
+import React, {useCallback, useRef} from 'react';
+import {
+ AbsoluteFill,
+ HtmlInCanvas,
+ HtmlInCanvasOnInit,
+ HtmlInCanvasOnPaint,
+ useVideoConfig,
+} from 'remotion';
+
+type GlState = {
+ gl: WebGL2RenderingContext;
+ program: WebGLProgram;
+ uTex: WebGLUniformLocation | null;
+ texture: WebGLTexture;
+ vao: WebGLVertexArrayObject;
+};
+
+const VS = `#version 300 es
+in vec2 a_pos;
+in vec2 a_uv;
+out vec2 v_uv;
+void main() {
+ gl_Position = vec4(a_pos, 0.0, 1.0);
+ v_uv = a_uv;
+}`;
+
+const FS = `#version 300 es
+precision highp float;
+uniform sampler2D u_tex;
+in vec2 v_uv;
+out vec4 o;
+void main() {
+ o = texture(u_tex, v_uv);
+}`;
+
+function linkProgram(
+ gl: WebGL2RenderingContext,
+ vsSrc: string,
+ fsSrc: string,
+): WebGLProgram {
+ const vert = gl.createShader(gl.VERTEX_SHADER)!;
+ gl.shaderSource(vert, vsSrc);
+ gl.compileShader(vert);
+ const frag = gl.createShader(gl.FRAGMENT_SHADER)!;
+ gl.shaderSource(frag, fsSrc);
+ gl.compileShader(frag);
+ const program = gl.createProgram()!;
+ gl.attachShader(program, vert);
+ gl.attachShader(program, frag);
+ gl.linkProgram(program);
+ gl.deleteShader(vert);
+ gl.deleteShader(frag);
+ return program;
+}
+
+const QUAD = new Float32Array([
+ -1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1,
+]);
+
+export const HtmlInCanvasDocsMinimalWebGL: React.FC = () => {
+ const {width, height} = useVideoConfig();
+ const gpuRef = useRef<GlState | null>(null);
+
+ const onInit: HtmlInCanvasOnInit = useCallback(({canvas}) => {
+ const gl = canvas.getContext('webgl2', {
+ alpha: true,
+ premultipliedAlpha: true,
+ antialias: false,
+ });
+ if (!gl) {
+ throw new Error('WebGL2 unavailable');
+ }
+
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
+
+ const program = linkProgram(gl, VS, FS);
+ const uTex = gl.getUniformLocation(program, 'u_tex');
+
+ const texture = gl.createTexture()!;
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
+
+ const buffer = gl.createBuffer()!;
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
+ gl.bufferData(gl.ARRAY_BUFFER, QUAD, gl.STATIC_DRAW);
+
+ const vao = gl.createVertexArray()!;
+ gl.bindVertexArray(vao);
+ const locPos = gl.getAttribLocation(program, 'a_pos');
+ const locUv = gl.getAttribLocation(program, 'a_uv');
+ gl.enableVertexAttribArray(locPos);
+ gl.vertexAttribPointer(locPos, 2, gl.FLOAT, false, 16, 0);
+ gl.enableVertexAttribArray(locUv);
+ gl.vertexAttribPointer(locUv, 2, gl.FLOAT, false, 16, 8);
+
+ gpuRef.current = {gl, program, uTex, texture, vao};
+
+ return () => {
+ gl.deleteProgram(program);
+ gl.deleteTexture(texture);
+ gl.deleteVertexArray(vao);
+ gl.deleteBuffer(buffer);
+ gpuRef.current = null;
+ };
+ }, []);
+
+ const onPaint: HtmlInCanvasOnPaint = useCallback(({elementImage}) => {
+ const gpu = gpuRef.current;
+ if (!gpu) {
+ return;
+ }
+
+ const {gl} = gpu;
+ gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
+ gl.useProgram(gpu.program);
+
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, gpu.texture);
+ gl.texElementImage2D(
+ gl.TEXTURE_2D,
+ 0,
+ gl.RGBA,
+ gl.RGBA,
+ gl.UNSIGNED_BYTE,
+ elementImage,
+ );
+
+ if (gpu.uTex) {
+ gl.uniform1i(gpu.uTex, 0);
+ }
+
+ gl.bindVertexArray(gpu.vao);
+ gl.drawArrays(gl.TRIANGLES, 0, 6);
+ }, []);
+
+ return (
+ <HtmlInCanvas
+ width={width}
+ height={height}
+ onInit={onInit}
+ onPaint={onPaint}
+ >
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ color: 'white',
+ fontSize: 120,
+ }}
+ >
+ <h1>Hello</h1>
+ </AbsoluteFill>
+ </HtmlInCanvas>
+ );
+};
diff --git a/packages/example/src/HtmlInCanvas/minimal-docs-webgpu.tsx b/packages/example/src/HtmlInCanvas/minimal-docs-webgpu.tsx
new file mode 100644
index 0000000000..f886e3d2ad
--- /dev/null
+++ b/packages/example/src/HtmlInCanvas/minimal-docs-webgpu.tsx
@@ -0,0 +1,205 @@
+/**
+ * Minimal WebGPU + HtmlInCanvas sample (same code as /docs/remotion/html-in-canvas).
+ */
+import React, {useCallback, useRef} from 'react';
+import {
+ AbsoluteFill,
+ HtmlInCanvas,
+ HtmlInCanvasOnInit,
+ HtmlInCanvasOnPaint,
+ useVideoConfig,
+} from 'remotion';
+
+// Narrow types so `OffscreenCanvas#getContext('webgpu')` and
+// `GPUQueue#copyElementImageToTexture` typecheck (see compose-webgpu.tsx).
+type GpuCanvasContext = {
+ configure(d: {
+ device: GPUDevice;
+ format: string;
+ alphaMode: 'premultiplied' | 'opaque';
+ }): void;
+ getCurrentTexture(): GPUTexture;
+};
+
+type GpuNavigator = {
+ requestAdapter(): Promise<GPUAdapter | null>;
+ getPreferredCanvasFormat(): string;
+};
+
+const WGSL = `
+struct VsOut {
+ @builtin(position) pos: vec4f,
+ @location(0) uv: vec2f,
+}
+@vertex
+fn vs(@builtin(vertex_index) i: u32) -> VsOut {
+ var p = array(vec2f(-1.0, -3.0), vec2f(-1.0, 1.0), vec2f(3.0, 1.0));
+ var uv = array(vec2f(0.0, 2.0), vec2f(0.0, 0.0), vec2f(2.0, 0.0));
+ var o: VsOut;
+ o.pos = vec4f(p[i], 0.0, 1.0);
+ o.uv = uv[i];
+ return o;
+}
+@group(0) @binding(0) var samp: sampler;
+@group(0) @binding(1) var tex: texture_2d<f32>;
+@fragment
+fn fs(in: VsOut) -> @location(0) vec4f {
+ return textureSample(tex, samp, in.uv);
+}
+`;
+
+type GpuState = {
+ device: GPUDevice;
+ context: GpuCanvasContext;
+ pipeline: GPURenderPipeline;
+ sampler: GPUSampler;
+ texture: GPUTexture;
+ bindGroup: GPUBindGroup;
+ width: number;
+ height: number;
+};
+
+export const HtmlInCanvasDocsMinimalWebGPU: React.FC = () => {
+ const {width, height} = useVideoConfig();
+ const gpuRef = useRef<GpuState | null>(null);
+
+ const onInit: HtmlInCanvasOnInit = useCallback(async ({canvas}) => {
+ if (typeof navigator === 'undefined' || !('gpu' in navigator)) {
+ throw new Error('WebGPU not available');
+ }
+
+ const gpuApi = (navigator as unknown as {gpu: GpuNavigator}).gpu;
+ const adapter = await gpuApi.requestAdapter();
+ if (!adapter) {
+ throw new Error('No WebGPU adapter');
+ }
+
+ const device = await adapter.requestDevice();
+ const context = (
+ canvas as unknown as {
+ getContext(id: 'webgpu'): GpuCanvasContext | null;
+ }
+ ).getContext('webgpu');
+ if (!context) {
+ throw new Error('WebGPU context unavailable on OffscreenCanvas');
+ }
+
+ const format = gpuApi.getPreferredCanvasFormat() as GPUTextureFormat;
+ context.configure({device, format, alphaMode: 'premultiplied'});
+
+ const module = device.createShaderModule({code: WGSL});
+ const pipeline = device.createRenderPipeline({
+ layout: 'auto',
+ vertex: {module, entryPoint: 'vs'},
+ fragment: {
+ module,
+ entryPoint: 'fs',
+ targets: [{format}] as GPUColorTargetState[],
+ },
+ primitive: {topology: 'triangle-list'},
+ });
+
+ const TextureUsage = (
+ globalThis as unknown as {
+ GPUTextureUsage: {COPY_DST: number; TEXTURE_BINDING: number};
+ }
+ ).GPUTextureUsage;
+
+ const texture = device.createTexture({
+ size: {width: canvas.width, height: canvas.height},
+ format: 'rgba8unorm',
+ usage: TextureUsage.COPY_DST | TextureUsage.TEXTURE_BINDING,
+ });
+
+ const sampler = device.createSampler({
+ magFilter: 'linear',
+ minFilter: 'linear',
+ });
+
+ const bindGroup = device.createBindGroup({
+ layout: (
+ pipeline as unknown as {
+ getBindGroupLayout(i: number): GPUBindGroupLayout;
+ }
+ ).getBindGroupLayout(0),
+ entries: [
+ {binding: 0, resource: sampler},
+ {binding: 1, resource: texture.createView()},
+ ],
+ });
+
+ gpuRef.current = {
+ device,
+ context,
+ pipeline,
+ sampler,
+ texture,
+ bindGroup,
+ width: canvas.width,
+ height: canvas.height,
+ };
+
+ return () => {
+ texture.destroy();
+ gpuRef.current = null;
+ };
+ }, []);
+
+ const onPaint: HtmlInCanvasOnPaint = useCallback(({elementImage}) => {
+ const gpu = gpuRef.current;
+ if (!gpu) {
+ return;
+ }
+
+ const {device, context, pipeline, texture, bindGroup} = gpu;
+
+ const queue = device.queue as unknown as {
+ copyElementImageToTexture(
+ source: unknown,
+ width: number,
+ height: number,
+ destination: {texture: GPUTexture},
+ ): void;
+ };
+ queue.copyElementImageToTexture(elementImage, gpu.width, gpu.height, {
+ texture,
+ });
+
+ const encoder = device.createCommandEncoder();
+ const view = context.getCurrentTexture().createView();
+ const pass = encoder.beginRenderPass({
+ colorAttachments: [
+ {
+ view,
+ clearValue: {r: 0, g: 0, b: 0, a: 0},
+ loadOp: 'clear',
+ storeOp: 'store',
+ },
+ ],
+ });
+ pass.setPipeline(pipeline);
+ pass.setBindGroup(0, bindGroup);
+ pass.draw(3);
+ pass.end();
+ device.queue.submit([encoder.finish()]);
+ }, []);
+
+ return (
+ <HtmlInCanvas
+ width={width}
+ height={height}
+ onInit={onInit}
+ onPaint={onPaint}
+ >
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ fontSize: 120,
+ }}
+ >
+ <h1>Hello</h1>
+ </AbsoluteFill>
+ </HtmlInCanvas>
+ );
+};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 871c249617..299af25216 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -49,6 +49,8 @@ import {
HtmlInCanvasComposeWebGL,
HtmlInCanvasComposeWebGLCrt,
HtmlInCanvasComposeWebGPU,
+ HtmlInCanvasDocsMinimalWebGL,
+ HtmlInCanvasDocsMinimalWebGPU,
HtmlInCanvasDemo,
HtmlInCanvasPrivacy,
HtmlInCanvasReactSvg,
@@ -928,6 +930,22 @@ export const Index: React.FC = () => {
width={1920}
durationInFrames={120}
/>
+ <Composition
+ id="html-in-canvas-docs-minimal-webgl"
+ component={HtmlInCanvasDocsMinimalWebGL}
+ fps={30}
+ height={1080}
+ width={1920}
+ durationInFrames={120}
+ />
+ <Composition
+ id="html-in-canvas-docs-minimal-webgpu"
+ component={HtmlInCanvasDocsMinimalWebGPU}
+ fps={30}
+ height={1080}
+ width={1920}
+ durationInFrames={120}
+ />
<Composition
id="html-in-canvas-complex-text"
component={HtmlInCanvasComplexText}