Anatomy of the hero: a halftone field in one draw call
How the animated dot field on the home page works: a single fragment shader, simplex noise, domain warping, and a cursor ripple, in about 150 lines of WebGL with no libraries.
- Published
- Reading time
- 3 min
- Tags
- webgl · performance
The background on the home page looks like a lot is going on: thousands of dots breathing, contour lines drifting across them, and a ripple that follows your cursor. It is one triangle, one fragment shader, and one draw call per frame. No three.js, no canvas library, nothing to download beyond the component itself.
This post walks through how it works.
Why not a library
three.js is excellent, but it adds well over 100 KB of gzipped JavaScript before you write any code. This effect needs none of what it provides: no scene graph, no camera, no meshes, no lighting. Everything happens per pixel, so a hand-written fragment shader is the right size for the job. The whole component, shader source included, is smaller than most hero images.
One triangle that covers the screen
To run a fragment shader over every pixel, you only need geometry that covers the viewport. The usual trick is a single oversized triangle instead of two triangles making a quad:
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);The triangle extends past the screen edges, and the GPU clips it. One triangle avoids the redundant shading work along the diagonal seam a quad would have.
A grid of dots
Each pixel works out which grid cell it belongs to, and how far it is from that cell's center:
vec2 center = (floor(frag / u_cell) + 0.5) * u_cell;
float dotMask = 1.0 - smoothstep(size - 0.8, size + 0.8, length(frag - center));If the pixel is within size of the center, it is part of a dot. The smoothstep across about 1.6 pixels gives the edge anti-aliasing for free, so the canvas can run with antialias: false.
All the interesting behavior comes from how size changes per cell.
Noise, then noise on the noise
The dot sizes come from simplex noise sampled at each cell's center. One layer of noise looks like blobs, so the shader sums four octaves of it (fractal Brownian motion), each at double the frequency and half the amplitude:
float fbm(vec2 p) {
float value = 0.0;
float amplitude = 0.5;
for (int i = 0; i < 4; i++) {
value += amplitude * snoise(p);
p = p * 2.03 + 17.0;
amplitude *= 0.5;
}
return value;
}That is still a bit static. The step that makes it look like flowing terrain is domain warping: using noise to offset where you sample noise.
vec2 warp = vec2(fbm(p + vec2(0.0, t)), fbm(p + vec2(5.2, -t)));
float n = fbm(p + 1.5 * warp + vec2(t * 0.6, 0.0));The two warp fields move in opposite directions over time, so the final pattern folds and swirls instead of just sliding.
Contour lines
Topographic maps draw a line wherever the terrain crosses a fixed height. The shader does the same thing: it multiplies the field by four, takes the fractional part, and lights up the cells that land near the middle of each band.
float contour = 1.0 - smoothstep(0.0, 0.07, abs(fract(n * 4.0) - 0.5));Those cells get slightly bigger and pick up a little of the accent color. It is a cheap effect that reads as structure.
The cursor
The pointer position is passed in as a uniform and eased toward its target in JavaScript every frame, so the effect glides instead of snapping. In the shader it becomes a soft Gaussian lens, with a sine wave travelling outward from it:
float lens = exp(-(d * d) / (radius * radius)) * u_mouseStrength;
float ripple = 0.55 + 0.45 * sin(d * 0.04 - u_time * 2.6);Inside the lens, dots grow with the ripple and shift fully into the accent color.
Staying cheap
A shader that runs every frame over every pixel has to be careful with work. The component:
- Caps the device pixel ratio at 1.75. A 4K display at 2× or 3× density would otherwise multiply the per-frame cost for detail nobody can see in a dot pattern.
- Stops rendering when it is off screen, using an
IntersectionObserver, and when the tab is hidden. - Reads its colors from CSS variables (
--shader-dot,--shader-accent), and re-reads them when the theme changes. The palette lives in one stylesheet. - Fades in after the first frame. If WebGL is unavailable, the canvas simply never draws, and the CSS gradient behind it remains.
The name in front of it is plain HTML animated with CSS, so it paints immediately and does not wait for any of this JavaScript to load.
Credits
The simplex noise implementation is Ian McEwan and Stefan Gustavson's well-known GLSL version (Ashima Arts, MIT license). The rest is about 150 lines in hero-shader.tsx.