Skip to main content

Installation

Install the vanilla JavaScript package from npm:
Pin your dependency version — breaking changes may ship under 0.0.x versioning.

Basic Usage

The vanilla package provides the ShaderMount class for mounting shaders to DOM elements:
The container element will automatically get a data-paper-shader attribute and the shader canvas will be positioned absolutely to fill it.

ShaderMount Constructor

The ShaderMount class signature:

Parameters

  • parentElement: The DOM element to mount the shader canvas into
  • fragmentShader: The GLSL fragment shader code (import from package)
  • uniforms: Object containing uniform values for the shader
  • webGlContextAttributes: Optional WebGL context configuration
  • speed: Animation speed multiplier (0 = static, negative = reverse)
  • frame: Starting animation frame in milliseconds
  • minPixelRatio: Minimum pixel ratio for rendering (default: 2)
  • maxPixelCount: Maximum pixels to render (default: 1920 × 1080 × 4)
  • mipmaps: Array of uniform names that should use mipmaps (e.g., ['u_image'])

Available Shaders

Import fragment shaders from the package:

Uniform Types

Shaders accept various uniform types:

Working with Colors

Use getShaderColorFromString to convert CSS colors to shader format:

Working with Images

For image-based shaders, pass loaded HTMLImageElement objects:
Images must be fully loaded before passing to ShaderMount. The image must have complete === true and naturalWidth > 0.

Updating Uniforms

Update shader parameters dynamically:
Only pass the uniforms that changed - the ShaderMount caches uniform values and skips unnecessary updates.

Animation Control

Setting Speed

Setting Frame

Performance Control

Pixel Ratio

Control rendering resolution:

Max Pixel Count

Limit total rendered pixels:
See the Performance Guide for detailed optimization strategies.

Cleanup

Always dispose of shaders when removing them:
Failing to dispose shaders can cause memory leaks. Always call dispose() when removing shaders from the page.

Accessing from DOM

You can access the ShaderMount instance from the parent element:

Automatic Behaviors

Automatic Pause

Shaders automatically pause when the browser tab is hidden and resume when visible:

Automatic Resizing

Shaders automatically resize when the container element changes size:

Pinch Zoom Support

Shaders maintain quality during pinch zoom on mobile devices:

Complete Example

Here’s a complete example with all features:

Using Shader Metadata

Some shaders export metadata for dynamic usage:

Fit Options

Use ShaderFitOptions for sizing:
See Sizing & Fit Guide for details.

TypeScript Support

Full TypeScript definitions are included:

Troubleshooting

Shader not rendering

1

Check WebGL support

Paper Shaders requires WebGL 2:
2

Verify container has size

The container must have width and height (via CSS or inline styles).
3

Check console for errors

Look for WebGL shader compilation errors in the browser console.

Memory leaks

  • Always call shaderMount.dispose() when removing shaders
  • Remove event listeners if you added custom ones
  • Clear references to the ShaderMount instance

Images not loading

  • Ensure images are fully loaded before passing to ShaderMount
  • Set crossOrigin = 'anonymous' for external images
  • Check network tab for failed image requests
  • Verify CORS headers for cross-origin images

Next Steps

React Usage

Use Paper Shaders in React applications

Customization

Deep dive into customizing shader parameters

Sizing & Fit

Control shader sizing and responsive behavior

Performance

Optimize shaders for production