ShaderMount is the core class of @paper-design/shaders. It takes a parent <div>, a GLSL fragment shader, and a set of uniform values, then creates and manages a <canvas> inside the div that renders the shader.
The canvas is automatically sized to match the parent element and re-renders on resize. When speed is non-zero, the shader animates via requestAnimationFrame. When speed is 0, no rAF loop runs — static shaders have zero recurring performance cost.
Constructor
The
<div> to mount the shader into. The shader canvas matches its size and updates automatically via ResizeObserver. Must be a real DOM element (node type 1).The GLSL fragment shader source code (WebGL 2 / GLSL ES 3.00). The library provides a vertex shader internally — you only supply the fragment shader.
An object of uniform values to pass to the shader. Supports
boolean, number, number[], number[][], and HTMLImageElement. See ShaderMountUniforms.Optional attributes passed to
canvas.getContext('webgl2', ...). Use this to control things like premultipliedAlpha or preserveDrawingBuffer.Animation speed multiplier.
0 means static (no rAF loop). Positive values animate forward; negative values animate in reverse. The value is a multiplier on delta time in milliseconds.Starting frame offset in milliseconds. Use this to produce deterministic results or to seek to a specific point in the animation.
The minimum device pixel ratio to render at. Defaults to
2 to ensure 2× rendering even on 1× screens for better anti-aliasing. Reduce to improve performance; increase together with maxPixelCount for sharper output on high-DPI screens.The maximum number of physical pixels to render (
1920 × 1080 × 4 by default, i.e. a 4K screen at 2× DPI). The canvas may be larger in the DOM but rendering quality is capped at this limit. Reduce to improve performance on large screens.Names of texture uniforms that should have mipmaps generated. When listed,
generateMipmap() is called and LINEAR_MIPMAP_LINEAR filtering is applied, which improves quality when the texture is displayed at a smaller size than its native resolution.Public methods
Updates one or more uniform values. You can pass a partial object containing only the uniforms that changed — values that haven’t changed are skipped via an internal cache to avoid redundant GPU calls. Triggers an immediate re-render.
Changes the animation speed. Setting to
0 cancels the rAF loop; setting to a non-zero value restarts it. Automatically paused when the browser tab is hidden and resumed when it becomes visible again.Seeks the animation to a specific frame (in milliseconds from the start). Useful for scrubbing or producing deterministic snapshots. Triggers an immediate re-render.
Updates the maximum physical pixel cap and triggers a resize recalculation. Defaults to
1920 × 1080 × 4 if called without an argument.Updates the minimum pixel ratio and triggers a resize recalculation. Defaults to
2 if called without an argument.Returns the current animation time in milliseconds from frame zero. Useful for reading the playhead position before disposing or transferring state.
Cleans up all WebGL resources (textures, program, buffers), cancels the rAF loop, disconnects the
ResizeObserver, removes event listeners, and detaches the canvas from the DOM. Always call dispose() when you no longer need the shader to prevent memory leaks.Properties
| Property | Type | Description |
|---|---|---|
parentElement | PaperShaderElement | The div the shader was mounted into. Has a paperShaderMount property pointing back to the ShaderMount instance. |
canvasElement | HTMLCanvasElement | The <canvas> element created and managed by ShaderMount. It is prepended as the first child of parentElement. |
Usage example
PaperShaderElement
After mounting,ShaderMount attaches itself to parentElement.paperShaderMount and marks the element with the data-paper-shader attribute. The element is typed as PaperShaderElement:
ShaderMount instance from a reference to the DOM element.
isPaperShaderElement
true if the element has a paperShaderMount property (i.e. ShaderMount has been mounted on it). Safe to use across document boundaries such as iframes or Picture-in-Picture windows.
ShaderMount injects a small stylesheet (@layer paper-shaders) into the document <head> on first use. This positions the canvas absolutely within the parent and sets isolation: isolate so the shader doesn’t bleed into surrounding stacking contexts.