Blog

How I Built a Mesh Gradient Generator with WebGL and OKLab

Why a good mesh gradient blends in OKLab, uses Gaussian weights instead of inverse distance, and pushes every pixel with noise. With live demos and the free tool.

Published on


Views

0 views

Reading time:4 min

Mesh gradients are everywhere: landing page backgrounds, covers, wallpapers. I wanted my own, one where I could drag the colors around, see it full screen and take it away as a PNG, CSS or a link. That became the mesh gradient generator in the tools section.

Along the way, three decisions turned out to separate a good gradient from a muddy one. This post walks through them, each with a demo you can touch.

The mesh gradient generator full screen: a blue, white and orange gradient with draggable dots and the settings panels top right
The tool: you drag the color dots, and the panels float top right.

Blend in OKLab, not sRGB

At heart, a gradient is mixing colors. The naive way is to average the RGB values, and that is where the trouble starts: sRGB is not spaced the way eyes see it. Halfway between two saturated colors, the mix loses chroma and drifts toward a dull grey.

OKLab is a color space built so that numeric distances match perceived ones. Mix there and the midpoint keeps the brightness and color of both ends. CSS already supports it with linear-gradient(in oklab, …), so you can compare without a line of JavaScript:

sRGB
OKLab
The same two colors, mixed in sRGB and in OKLab. Watch the middle: sRGB goes grey, OKLab keeps the color.

In the tool, each color is converted to OKLab once, on the CPU, and the shader blends in that space before going back to sRGB to paint.

Gaussian weights, not inverse distance

With several colors spread over the canvas, each pixel has to decide how much of each it gets. My first version used inverse distance: the closer to a dot, the more weight, with 1 / dⁿ. It works, but it has a flaw you see right away: the weight shoots up as you near a dot, so every color sits in a round spot with a hard center.

The fix was switching to a Gaussian bell, exp(-d² / r²). It has no peak at the dot: the color rises softly and falls softly, and neighbours melt together. The softness setting only changes the bell's radius.

1 / dⁿ
gauss
Softness0.50
Drag the softness. Inverse distance always keeps a hard spot at each color; the Gaussian one melts them together.
shader.ts
float reach = mix(0.12, 0.44, uSoftness);
float spread = 1.0 / (reach * reach);
float weight = exp(-dot(delta, delta) * spread) + 0.000001;
lab += uLab[i] * weight;
total += weight;

The + 0.000001 avoids a division by zero far from every dot, where all the bells are close to nothing.

The swirl: move where each pixel looks

With OKLab and Gaussian weights the gradient is already clean, but also a little predictable: round, well-blended blobs. What gives it that organic feel, like smoke or silk, is the swirl.

The trick is called domain warping. The colors do not move. Before computing the blend, each pixel shifts its position by a smooth noise field (fbm, four octaves of value noise) and looks the colors up from there. Neighbouring pixels shift almost the same amount, so the image bends without breaking.

Swirl0.45
Swirl size0.35
Raise the swirl. The colors do not move; each pixel looks them up from a slightly shifted place.

The swirl amount is how far pixels shift; the swirl size is the noise frequency. With animation on, the noise field drifts slowly over time and the gradient breathes.

Grain and export

The last step is grain: a little per-pixel noise that takes away the plastic look and hides banding in very soft gradients. Exporting surfaced a detail here. If the grain is one pixel, a 4K PNG makes it so fine that it disappears when viewed scaled down.

The fix is grain in blocks that grow with the output, measured against a preview of about 900 pixels. That way a 3840 × 2160 PNG has the same grain you see on screen.

Export redraws the gradient at its final size, on a separate canvas, with the same shader.

The CSS is an approximation: one radial-gradient layer per dot over the average color. CSS cannot blend in OKLab per pixel or swirl, so it reads softer than the canvas, but it works as a background with no JavaScript. The link keeps the whole state in the URL, so sharing is copying the address bar.

Try it

The mesh gradient generator is free, runs in the browser and needs no sign-up. It comes with eight presets, a randomize button and PNG export up to 4K. For more utilities in the same spirit, the same section has the layered shadow generator and the OKLCH color scale.