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 theShaderMount class for mounting shaders to DOM elements:
ShaderMount Constructor
TheShaderMount 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
UsegetShaderColorFromString to convert CSS colors to shader format:
Working with Images
For image-based shaders, pass loadedHTMLImageElement objects:
Updating Uniforms
Update shader parameters dynamically:Animation Control
Setting Speed
Setting Frame
Performance Control
Pixel Ratio
Control rendering resolution:Max Pixel Count
Limit total rendered pixels:Cleanup
Always dispose of shaders when removing them: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
UseShaderFitOptions for sizing:
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
