Recipes
Short, complete answers to common jobs: backgrounds, hover buttons, scroll, mouse trails, video filters.
Each recipe is self-contained. The examples show larger pages.
A full-page animated background#
createShader({
shader: `void main() {
vec2 p = gl_FragCoord.xy / u_resolution * 3.0;
float v = sin(p.x + u_time) + sin(p.y * 1.3 - u_time * 0.7) + sin((p.x + p.y) * 0.8 + u_time * 0.4);
fragColor = vec4(vec3(0.08, 0.1, 0.18) + 0.06 * v, 1.0);
}`,
maxFps: 30, scale: 0.5, // cheap: soft gradients look identical at half res
});#glcanvas { z-index: -1; pointer-events: none; }A button that reacts on hover#
const sm = createShader({ canvas: '#btn-canvas', shader, uniforms: { u_hover: 0 }, pointer: { target: btn, smooth: 0.2 } });
btn.addEventListener('pointerenter', () => sm.tween('u_hover', 1, { duration: 200 }));
btn.addEventListener('pointerleave', () => sm.tween('u_hover', 0, { duration: 450, ease: 'outCubic' }));The shader buttons example does this with several styles at once.
Scroll-linked progress#
const sm = createShader({ canvas, shader, uniforms: { u_progress: 0 } });
addEventListener('scroll', () => {
const max = document.documentElement.scrollHeight - innerHeight;
sm.set('u_progress', max ? scrollY / max : 0);
}, { passive: true });A mouse trail with feedback#
createShader({
passes: [
{ name: 'trail', pingpong: true, float: true, channels: ['trail'], shader: `
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float d = smoothstep(0.04, 0.0, distance(uv * vec2(u_aspect, 1.0), u_mouseUV * vec2(u_aspect, 1.0)));
fragColor = vec4(max(texture(iChannel0, uv).rgb * 0.97, vec3(d)), 1.0);
}` },
{ name: 'out', channels: ['trail'], shader: `
void main() { fragColor = vec4(texture(iChannel0, gl_FragCoord.xy / u_resolution).rgb * vec3(0.4, 0.7, 1.0), 1.0); }` },
],
});Filter a video#
createShader({
canvas, size: [1280, 720], fit: 'cover',
passes: [{ channels: [{ video: 'clip.mp4' }], shader: `
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float g = dot(texture(iChannel0, uv).rgb, vec3(0.299, 0.587, 0.114));
fragColor = vec4(vec3(step(0.5, g)), 1.0);
}` }],
});Distort an image on hover#
createShader({ canvas, passes: [{ channels: ['photo.jpg'], shader: `
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float d = distance(uv, u_mouseUV);
uv += (uv - u_mouseUV) * 0.15 * exp(-d * 6.0);
fragColor = texture(iChannel0, uv);
}` }] });Shader text with a 2D canvas#
Draw text with the 2D API, feed it as a channel, and use its alpha as a mask or height map. This is how the title on the home page works.
const c = Object.assign(document.createElement('canvas'), { width: 1200, height: 300 });
const g = c.getContext('2d'); g.font = '800 200px sans-serif'; g.fillStyle = '#fff'; g.fillText('Hello', 40, 220);
createShader({ canvas, shader: `void main() { fragColor = vec4(vec3(texture(iChannel0, gl_FragCoord.xy / u_resolution).a), 1.0); }`,
channels: [{ element: c, dynamic: false }] });Pause when the user prefers reduced motion#
const reduced = matchMedia('(prefers-reduced-motion: reduce)').matches;
const sm = createShader({ canvas, shader, autoplay: !reduced });
if (reduced) sm.render();Export a looping clip#
const sm = createShader({ canvas, shader, loop: 4 });
await sm.record({ fps: 60, duration: 4000, download: 'loop.webm' });Many scenes on one canvas#
Browsers allow about 16 live WebGL contexts per page. To show many shaders, give them one canvas and play one at a time. A paused instance never draws over the one that is playing, and its buffers still follow resizes. The home page works this way.
const stage = document.querySelector('#stage');
const scenes = {
intro: createShader({ canvas: stage, shader: introFrag, autoplay: false }),
sim: createShader({ canvas: stage, passes: simPasses, autoplay: false }),
};
let active = scenes.intro.play();
function show(name) {
active.pause();
active = scenes[name].play();
}Many thumbnails, one context#
For a grid of previews, render each shader on one hidden canvas with step(), then copy the frame into a plain 2D canvas per card. The gallery page uses this.
const shared = document.createElement('canvas');
const thumbs = [...document.querySelectorAll('canvas[data-shader]')].map(card => ({
ctx: card.getContext('2d'),
sm: createShader({ canvas: shared, shader: card.dataset.shader, size: [480, 300], autoplay: false, preserveDrawingBuffer: true }),
}));
(function loop() {
requestAnimationFrame(loop);
for (const t of thumbs) {
t.sm.step(); // render one frame into the shared canvas
t.ctx.drawImage(shared, 0, 0, t.ctx.canvas.width, t.ctx.canvas.height);
}
})();