Performance
How to keep shaders smooth: resolution, frame rate, passes and many canvases on one page.
A fragment shader costs roughly pixels × work per pixel × passes. Reduce whichever you can afford to.
In order of payoff#
- Render fewer pixels.
scale: 0.5quarter-costs the whole shader. CapmaxPixelRatioat 1.5 or 1 on large canvases. - Render fewer frames.
maxFps: 30for ambient motion. Pausing off-screen canvases is on by default. - Shrink intermediate passes. Blurs, glows and fields can run at
size: 'half'or'quarter'. - Pick the right format. Half float (
float: true) is faster and smaller than'full'. Use 8-bit when you don't need range. - Trim the shader. Fewer octaves, fewer ray steps, and
definesfor quality tiers.
Adaptive quality#
scale is read when the instance is created, so to adapt, rebuild at a lower scale when the frame rate stays low:
let scale = 1, sm = make(scale);
function make(s) {
const inst = createShader({ canvas, shader, scale: s });
let slow = 0;
inst.on('afterFrame', ({ fps }) => {
slow = fps < 40 ? slow + 1 : 0;
if (slow > 90 && scale > 0.5) { scale -= 0.25; inst.destroy(); sm = make(scale); }
});
return inst;
}Many canvases on one page#
Each instance has its own WebGL context, and browsers limit how many are alive at once (usually around 16). Pausing off-screen instances protects the frame rate but not the context count. For galleries and grids:
- create instances when they scroll near the viewport and call
sm.destroy()when they leave, as the examples gallery does, - or render one shader to a single large canvas and place it behind a grid with CSS.
Memory#
Float targets use 8 bytes per pixel at half float, 16 at full. A 4K full-float pass is about 130 MB, so size simulation buffers deliberately.
Reduced motion and battery#
Respect prefers-reduced-motion (see Playback & timing) and consider powerPreference: 'low-power' for decorative backgrounds.