Documentation
Everything it does, on one page.
GradFlow is one React component. It renders a canvas, fills its parent, and animates a gradient on the GPU.
Installation
The package has no dependencies of its own. It needs React 18 or newer.
Usage
The canvas takes the size of its parent, so give the parent a size and a position. Placing the gradient in an absolutely positioned layer with a negative z-index keeps it behind your content; isolate on the section stops that layer from slipping behind the page background.
GradFlow is a client component. You can import it from a server component without adding 'use client' yourself.
Props
| Name | Type | Default | Description |
|---|---|---|---|
| preset | PresetName | none | Start from one of the built-in looks. |
| config | GradientConfigInput | aurora defaults | Type, colors, speed, scale and noise. Overrides the preset field by field. |
| transition | number | 700 | Milliseconds a config or preset change takes to blend in. 0 switches instantly. |
| interactive | boolean | false | Let the pattern swell under the pointer. |
| paused | boolean | false | Freeze the animation on a single frame. |
| preserveDrawingBuffer | boolean | false | Keep the canvas readable for toBlob and toDataURL. |
| className | string | none | Classes for the canvas element. |
| ref | Ref<HTMLCanvasElement> | none | Forwarded to the canvas. |
Config
Every field is optional. Anything you leave out comes from the preset if you passed one, and from the defaults otherwise.
| Name | Type | Default | Description |
|---|---|---|---|
| type | GradientType | 'aurora' | One of the 20 shaders below. |
| color1 | string | RGB | #d21570 | Hex string or { r, g, b } with 0 to 255 channels. |
| color2 | string | RGB | #8c3ff8 | Same formats as color1. |
| color3 | string | RGB | #56ffc3 | Same formats as color1. |
| speed | number | 0.5 | Animation rate. 0 stops time; around 0.3 to 1 reads as ambient. |
| scale | number | 1.2 | Zoom of the pattern. Higher values show more, finer detail. |
| noise | number | 0.32 | Film grain from 0 to 0.5. Hides banding on wide, soft ramps. |
Gradient types
20 shaders. Click one to open it in the playground.
- auroraCurtains of light over a dark sky.
- eclipseNewA ring of light around a dark disc.
- prismNewOne band field, split three ways.
- horizonNewA soft glow rising from the bottom edge.
- causticsNewLight through moving water.
- glassNewA soft field behind fluted glass.
- nebulaNewWarped dust clouds and a star field.
- dunesNewRidgelines drifting at their own pace.
- silkFolded, iterated sine cloth.
- foldNewSheets of light, creased and overlapping.
- raysNewShafts of light from above.
- topoNewA contour map that keeps redrawing.
- meshThree blobs of color, always blending.
- halftoneNewA rotated dot screen, print style.
- smokeTurbulent, slow-rolling plumes.
- animatedA rotating two-layer blend.
- waveStacked sine waves across the frame.
- stripeWarped diagonal bands.
- conicA sweep around the center.
- linearA vertical ramp with a gentle sway.
Presets
22 ready-made looks. A preset sets every config field; pass config alongside it to change some of them.
Transitions
When preset or config changes, the canvas blends to the new look instead of jumping. Colors, speed, scale and noise interpolate; a change of type dissolves the old pattern into the new one along a noise front. transition sets the duration in milliseconds, and 0 turns blending off.
Changing speed never makes the pattern skip. Time is accumulated frame by frame, so a faster speed only means the pattern moves faster from where it already is.
Pointer interaction
With interactive set, the pattern is magnified under the pointer and the effect trails behind fast movement. The pointer is tracked on the window, so it keeps working when the canvas sits behind other content.
Exporting a still
The ref points at the canvas element. Browsers clear a WebGL canvas after each frame is shown, so set preserveDrawingBuffer on any gradient you intend to read back.
Performance
A gradient is one triangle and one fragment shader, rendered at up to twice the device pixel ratio. The render loop stops while the canvas is off screen and while the tab is hidden, and starts again on its own.
Each GradFlow owns a WebGL context. Browsers allow roughly sixteen per page and drop the oldest past that, so keep the number of gradients mounted at once in single digits. If a context is lost, GradFlow rebuilds its program when the browser restores it.
Accessibility
Visitors who ask their system for reduced motion get one still frame of the gradient, with the pointer effect off. Use paused to give everyone a control over it, and keep enough contrast between your text and the brightest part of the pattern; a scrim over the edge your text sits on is usually enough.