makeHtmlInCanvasPresentation()v4.0.456
Allows you to pass a shader to create a HTML-in-canvas-based @remotion/transitions presentation.
For a step-by-step walkthrough, see Custom HTML-in-canvas presentations.
API UsageconstmyPresentation =makeHtmlInCanvasPresentation (myShader );
Show myShader implementation
Minimal crossfade shaderimport {makeHtmlInCanvasPresentation , typeHtmlInCanvasShader } from '@remotion/transitions'; constVERTEX = `#version 300 es in vec2 a_pos; out vec2 v_uv; void main() { v_uv = vec2(a_pos.x * 0.5 + 0.5, 0.5 - a_pos.y * 0.5); gl_Position = vec4(a_pos, 0.0, 1.0); }`; constFRAGMENT = `#version 300 es precision highp float; uniform sampler2D u_prev; uniform sampler2D u_next; uniform float u_time; in vec2 v_uv; out vec4 outColor; void main() { outColor = mix(texture(u_next, v_uv), texture(u_prev, v_uv), u_time); }`; constmyShader :HtmlInCanvasShader <{}> = (canvas ) => { constgl =canvas .getContext ('webgl2', {premultipliedAlpha : true})!; constcompile = (src : string,type : number) => { constsh =gl .createShader (type )!;gl .shaderSource (sh ,src );gl .compileShader (sh ); returnsh ; }; constprogram =gl .createProgram ()!;gl .attachShader (program ,compile (VERTEX ,gl .VERTEX_SHADER ));gl .attachShader (program ,compile (FRAGMENT ,gl .FRAGMENT_SHADER ));gl .linkProgram (program ); constbuffer =gl .createBuffer ();gl .bindBuffer (gl .ARRAY_BUFFER ,buffer );gl .bufferData (gl .ARRAY_BUFFER , newFloat32Array ([-1, -1, 1, -1, -1, 1, 1, 1]),gl .STATIC_DRAW , ); constaPos =gl .getAttribLocation (program , 'a_pos');gl .enableVertexAttribArray (aPos );gl .vertexAttribPointer (aPos , 2,gl .FLOAT , false, 0, 0); constprevTex =gl .createTexture (); constnextTex =gl .createTexture (); return {clear : () => {gl .clearColor (0, 0, 0, 0);gl .clear (gl .COLOR_BUFFER_BIT ); },cleanup : () => {gl .deleteProgram (program );gl .deleteTexture (prevTex );gl .deleteTexture (nextTex );gl .deleteBuffer (buffer ); },draw : ({prevImage ,nextImage ,width ,height ,time }) => {gl .viewport (0, 0,width ,height );gl .useProgram (program );gl .activeTexture (gl .TEXTURE0 );gl .bindTexture (gl .TEXTURE_2D ,prevTex ); if (prevImage ) {gl .texImage2D (gl .TEXTURE_2D , 0,gl .RGBA ,gl .RGBA ,gl .UNSIGNED_BYTE ,prevImage , ); }gl .uniform1i (gl .getUniformLocation (program , 'u_prev'), 0);gl .activeTexture (gl .TEXTURE1 );gl .bindTexture (gl .TEXTURE_2D ,nextTex ); if (nextImage ) {gl .texImage2D (gl .TEXTURE_2D , 0,gl .RGBA ,gl .RGBA ,gl .UNSIGNED_BYTE ,nextImage , ); }gl .uniform1i (gl .getUniformLocation (program , 'u_next'), 1); constt = !prevImage ? 0 : !nextImage ? 1 :time ;gl .uniform1f (gl .getUniformLocation (program , 'u_time'),t );gl .drawArrays (gl .TRIANGLE_STRIP , 0, 4); }, }; }; constmyPresentation =makeHtmlInCanvasPresentation (myShader );
Arguments
shader
A function of type HtmlInCanvasShader<Props>.
Must return an object with three callbacks: clear, cleanup and draw.
clear()
Called when there is nothing to draw (both scenes are unmounted).
Typically clears the WebGL color buffer.
cleanup()
Called once when the transition unmounts.
Release any GL resources you allocated.
draw()
Called every frame the transition is active.
Has the type HtmlInCanvasShaderDraw<Props>; the argument has the type HtmlInCanvasShaderDrawParams<Props>.
prevImage
OffscreenCanvas | null — the rasterized entering scene. null while the scene is unmounted or has not yet rendered.
nextImage
OffscreenCanvas | null — the rasterized exiting scene. null while the scene is unmounted or has not yet rendered.
Upload these sources to WebGL textures using texImage2D().
width / height
The pixel size of the canvas to draw into.
time
A number between 0 and 1.
time = 0 is the transition start, and time = 1 is the transition end.
The transition moves from the exiting scene (nextImage) to the entering scene (prevImage).
When one image is missing, draw the available scene using the corresponding endpoint of the presentation.
With transition motion blur enabled, draw() is called for each exposure sample.
Each call must produce a complete image independently of earlier draws.
pixelRatiov4.0.545
The number of capture pixels per CSS pixel. Multiply CSS-pixel dimensions by this value when drawing into the canvas.
Remotion supplies this field on every draw.
presentationDurationInFramesv4.0.545
The transition duration, independent of the current time. Use it for duration-dependent effects such as a flash lasting a fixed number of frames.
Remotion supplies this field on every draw.
passedProps
Whatever you typed your Props as.
Return value
A presentation that you can pass to a <TransitionSeries.Transition>.
The returned function accepts your shader props and the following options:
effects?
Effects to apply after the transition shader. Defaults to an empty array.
effectsOutputSize?v4.0.536
An object with positive integer width and height values that sets the effect resolution. By default, effects use the transition canvas dimensions. This does not change the shader or output dimensions.
Types
HtmlInCanvasShader
import type {
HtmlInCanvasShader ,} from '@remotion/transitions';HtmlInCanvasShaderDraw
import type {
HtmlInCanvasShaderDraw ,} from '@remotion/transitions';HtmlInCanvasShaderDrawParams
import type {
HtmlInCanvasShaderDrawParams ,} from '@remotion/transitions';Compatibility
| Browsers | Servers | Environments | |||||||
|---|---|---|---|---|---|---|---|---|---|
Chrome | Firefox | Safari | Node.js | Bun | Serverless Functions | ||||
Previewing HTML-in-canvas presentations requires Chrome 157 or later.
See also
- Custom HTML-in-canvas presentations — full walkthrough and example
- HTML-in-canvas
- Custom presentations — the CSS-based path