# gradient

A living background of four soft color blobs or of aurora curtains, mixed in OKLab, without banding, with a light film grain.

Four soft blobs of color drift slowly over the element on looping paths that take minutes to repeat, their edges bent by noise. They lean toward the pointer and shift as the page scrolls. Colors mix in OKLab, so two colors meet through a clean middle instead of a muddy gray, and every pixel is dithered, so smooth ramps show no bands. A light film grain sits on top; set grain to 0 for a clean gradient or around 0.08 for a film look. The aurora style draws the first color as a night sky and the other three as curtains of light that wave along it, sharp at their lower edge and rising in uneven rays.

Module `backgrounds`, category Backgrounds. Import `@dioramajs/backgrounds/gradient`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/backgrounds/src/gradient.ts)

A launch card whose cover is four soft blobs of navy, violet, coral and gold drifting into one another under a light film grain.

```html title="index.html"
<article class="gd-launch">
  <div class="gd-wash" aria-hidden="true" data-dio="gradient"></div>
  <div class="gd-info">
    <p class="gd-kicker">Live stream, May 20, 18:00 CET</p>
    <h2>Launch week: Studio 5</h2>
    <p>Multiplayer editing, a new color engine and offline mode, shown live by the team that built them.</p>
    <a href="/launch">Save a seat</a>
  </div>
</article>
```

## Usage

Put it on: an empty decorative element behind the content, such as an aria-hidden div filling a hero; its CSS background is the look without WebGL.

`start()` registers `gradient`, so the attribute alone puts it on an element.

```html
<header class="hero"><div class="hero-bg" aria-hidden="true" data-dio="gradient" data-dio-color1="#0b1d3a" data-dio-color2="#3a2b8c" data-dio-color3="#e35d6a" data-dio-color4="oklch(85% 0.12 80)"></div><h1>Northern lights</h1></header>
```

In page transitions: Fades with the page transition.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'mesh' \| 'aurora'` | `'mesh'` | mesh drifts four soft blobs into a wash of color, for most heroes and sections; aurora draws the first color as a dark sky with the other three as waving curtains of light, for a night or tech hero over a dark page. |
| `color1` | `data-dio-color1` | `color` | `'#0b1d3a'` | Color of the first blob, any CSS color; with aurora, the sky. |
| `color2` | `data-dio-color2` | `color` | `'#3a2b8c'` | Color of the second blob; with aurora, the first curtain. |
| `color3` | `data-dio-color3` | `color` | `'#e35d6a'` | Color of the third blob; with aurora, the second curtain. |
| `color4` | `data-dio-color4` | `color` | `'#f6c177'` | Color of the fourth blob; with aurora, the third curtain. |
| `speed` | `data-dio-speed` | `number` 0 to 1 | `0.2` | How fast the blobs travel along their paths. |
| `scale` | `data-dio-scale` | `number` 0.3 to 3 | `1` | Size of the blobs: below 1 sharper patches, above 1 softer washes. |
| `warp` | `data-dio-warp` | `number` 0 to 1 | `0.4` | How much noise bends the blobs’ edges. |
| `attract` | `data-dio-attract` | `number` 0 to 1 | `0.15` | How far the blobs lean toward the pointer while it is over the element. |
| `scroll` | `data-dio-scroll` | `number` 0 to 1 | `0.25` | How far the blobs move along their paths while the element crosses the viewport. |
| `grain` | `data-dio-grain` | `number` 0 to 0.2 | `0.03` | Strength of the film grain, one CSS pixel per grain; 0 leaves only the dithering. |
| `fps` | `data-dio-fps` | `number` 0 to 60 | `30` | Most frames a second the drift draws; 0 holds it still. At most low 15, medium 24. |
| `octaves` (rebuilds) | `data-dio-octaves` | `number` 1 to 3 | `2` | Layers of noise in the warp, 1 to 3. At most low 1. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Paths of the blobs and shape of the noise. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `style`: `mesh`, `aurora`
  - `mesh` drifts four soft blobs into a wash of color, for most heroes and sections.
  - `aurora` draws the first color as a dark sky with the other three as waving curtains of light, for a night or tech hero over a dark page.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`hover`](/docs/api/inputs#hover), [`pointer`](/docs/api/inputs#pointer), [`progress`](/docs/api/inputs#progress), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size). Draws at a steady rate while it is on screen and stops when it leaves. Up to fps frames a second while on screen, none off screen or at fps 0; pointer and scroll draw at full rate. The grain changes with those frames and adds none. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. A still gradient: the blobs and the grain stop, and neither the pointer nor scroll moves them.

Runs on every device. The blobs lean toward a finger while it is on the screen; scrolling works as usual.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { gradient: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 2.3 KB the first time a page uses it. GPU: One pass over the element: one to three reads of a small noise tile, four exponentials and a hash per pixel. Memory: A 64 × 64 noise tile shared by every gradient on the page.

- low: At most 15 frames a second, one layer of noise in the warp.
- medium: At most 24 frames a second.

See [the quality plugin](/docs/effects/quality).

## Recipes

### Behind a SaaS hero

Put the gradient on an empty `aria-hidden` element that fills the hero, and start the app with `canvas: 'behind'` so the heading and buttons sit over the canvas. The element’s CSS background is what visitors see until the engine draws, and all the time without WebGL, so give it the same colors.

```html
<header class="hero">
  <div
    class="hero-bg"
    aria-hidden="true"
    data-dio="gradient"
    data-dio-color1="#0a1a3f"
    data-dio-color2="#3d5afe"
    data-dio-color3="#00c2a8"
    data-dio-color4="#e8f0ff"
  ></div>
  <h1>Payroll that runs on the 25th, every month</h1>
  <a class="cta" href="/signup">Start a free trial</a>
</header>
```

```css
.hero {
  position: relative;
  isolation: isolate;
  min-height: 80vh;
}
.hero-bg {
  position: absolute;
  inset: 0;
  z-index: -1;
  background: linear-gradient(135deg, #0a1a3f, #3d5afe 55%, #00c2a8);
}
```

```ts title="src/main.ts"
import { start } from '@dioramajs/diorama'

export const app = start({ canvas: 'behind' })
```

Keep the hero itself without a background color, or it covers the canvas behind it.

### Aurora over a dark hero

The `aurora` style turns the first color into a night sky and the other three into curtains of light. Pick a near-black first color and three bright ones: it suits a night, travel or developer brand on a dark page.

Green, blue and violet curtains of light waving over a dark navy sky.

```html title="index.html"
<div class="gd-sky" aria-hidden="true" data-dio="gradient" data-dio-style="aurora" data-dio-color1="#050b1a" data-dio-color2="#2bd59a" data-dio-color3="#4f7cff" data-dio-color4="#b45cff"></div>
```

### Film grain for a studio intro

Raise `grain` to around 0.08 for a printed, filmic surface, and `speed` a little so the blobs visibly travel on the first screen of a portfolio.

```html
<section class="intro">
  <div
    class="intro-bg"
    aria-hidden="true"
    data-dio="gradient"
    data-dio-color1="#1b1410"
    data-dio-color2="#7a3b2e"
    data-dio-color3="#d9a066"
    data-dio-color4="#f2e3c9"
    data-dio-grain="0.08"
    data-dio-speed="0.2"
  ></div>
  <h1>Halden Studio, interiors and objects</h1>
</section>
```

### A calm backdrop for a long page

With `fps` at 0 the blobs stay where they are and move only as the page scrolls, so a long article or a pricing page gets a background that never draws at rest. Turn the grain off as well when text sits right over it.

```html
<div
  class="backdrop"
  aria-hidden="true"
  data-dio="gradient"
  data-dio-color1="#f4efe6"
  data-dio-color2="#e7d8f5"
  data-dio-color3="#cfe7e1"
  data-dio-color4="#fbe3c8"
  data-dio-fps="0"
  data-dio-scroll="0.6"
  data-dio-grain="0"
></div>
```

## Notes

The docs canvas sits above the page, so the examples here draw nothing over the gradient: with the canvas above, the gradient covers whatever is inside its box. Put text over a gradient with `canvas: 'behind'`, as in the first recipe.

Colors are any CSS colors, `oklch()` included. The effect mixes them in OKLab, so two hues meet through a clean middle instead of a gray band, and it dithers every pixel, so dark ramps show no steps.

It draws up to `fps` frames a second while it is on screen and none off screen. The pointer and scroll draw at full rate while they move it. On the low tier it draws at most 15 frames a second with one layer of noise, which keeps it cheap on phones.

`style`, `octaves` and `seed` rebuild the effect when they change. The colors, speeds and grain change live.

## Related

- [blobs](/docs/effects/blobs.md): Soft blobs of brand color drifting behind a hero: merging like a lava lamp, as blurred orbs or as glossy glass drops.
- [clouds](/docs/effects/clouds.md): A sky of soft layered clouds for the time of day, daylight, a sunset or a moonlit night, drifting in the wind and parting on scroll.
- [dots](/docs/effects/dots.md): A dot grid behind a hero or in a card that answers the pointer: dots near it grow, move aside or glow, then settle back.
- [grid](/docs/effects/grid.md): A grid floor in perspective running toward the viewer under a glowing horizon: neon synthwave, a blueprint or plain thin lines.
- [stars](/docs/effects/stars.md): A night sky of twinkling stars in depth layers that lean with the pointer and the scroll, with shooting stars and a warp flight.
- [waves](/docs/effects/waves.md): Topographic contour lines or flowing wave lines across a hero or footer, slowly changing and shifted by scroll.
- [drops](/docs/effects/drops.md): Water drops on glass over a hero or the whole page: rain that beads and runs, or meltwater left by a thaw.
- [snow](/docs/effects/snow.md): Snow, cherry petals or autumn leaves drifting down over a section or the whole page, for a seasonal campaign.
- [Performance and quality](/docs/guides/performance.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
