# water

A headline under water: the surface bends the letters, focuses dancing caustics on them and ripples under the pointer.

The text lies a few centimeters under a real water surface. On the page the water is a puddle around the words with a wandering, rounded shore, shallow and nearly clear at its edge and deeper toward the letters, where its surface curls down to the page in a thin bright line; on an element with its own background it fills that box like a pool with walls. A height field simulated on the GPU, plus a slow swell, gives every point of the surface a normal: the letters are seen bent through it, the light it focuses draws a net of caustics over the letters and the floor, and the surface reflects a sky with a window and the glints of the sun by Fresnel. The water tints what lies deeper, red first. Moving the pointer drags a wake, a click drops a stone that rings out, rain lands in rings on its own, and ice is clear with frost and cracks, thinner over the letters so their color reads, until the pointer melts a clear patch that freezes over again.

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

A lido's summer headline in deep blue letters lying in a puddle of clear water with a wandering shore, caustics dancing over the letters.

```html title="index.html"
<section class="wa-pool">
  <p class="wa-kicker">Lakeside Lido · open from 1 June</p>
  <h2 class="wa-headline" data-dio="water">Summer swim</h2>
  <p class="wa-line">A heated 50 meter pool, lanes at sunrise and a shallow end for the little ones. Season passes go on sale on Monday.</p>
</section>
```

## Usage

Put it on: a heading or short text in a large font, on a solid page color or with its own solid background, with padding of about a third of an em for the puddle. Not img, video, canvas, svg or form fields.

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

```html
<h1 class="hero" data-dio="water">Summer swim</h1>
```

In page transitions: The water fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'pool' \| 'rain' \| 'ice'` | `'pool'` | pool is clear water with a moving swell; rain rings out under falling drops; ice is clear, frosted and cracked. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'loop' \| 'hover'` | `'loop'` | loop keeps the water moving; hover keeps it still until the pointer comes over it. |
| `depth` | `data-dio-depth` | `number` 0 to 3 | `1` | How strongly the surface bends the letters seen through it; 0 keeps them straight. |
| `caustics` | `data-dio-caustics` | `number` 0 to 2 | `1` | Brightness of the light the waves focus on the letters and the floor; 0 turns it off. |
| `ripples` | `data-dio-ripples` | `number` 0 to 3 | `1` | Strength of the wake and the drops the pointer makes; 0 leaves the water alone. |
| `rain` | `data-dio-rain` | `number` 0 to 30 | `6` | Raindrops a second with the rain style; 0 stops the rain. |
| `damping` | `data-dio-damping` | `number` 0 to 1 | `0.5` | How fast ripples die out; 0 lets them run and bounce for a long time. |
| `tint` | `data-dio-tint` | `number` 0 to 1.5 | `0.5` | Depth of the water color over the letters and the page; 0 is perfectly clear. |

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

### Variants

- `style`: `pool`, `rain`, `ice`
  - `pool` is clear water with a moving swell.
  - `rain` rings out under falling drops.
  - `ice` is clear, frosted and cracked.
- `trigger`: `loop`, `hover`
  - `loop` keeps the water moving.
  - `hover` keeps it still until the pointer comes over it.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`over`](/docs/api/inputs#input-over), [`pointer`](/docs/api/inputs#pointer), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width). Draws at a steady rate while it is on screen and stops when it leaves. With trigger loop, 30 frames a second while on screen (ice: only while a melted patch refreezes or ripples run). With hover, 30 a second while the pointer is over and the ripples settle, none at rest: the still water stays drawn. 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. The water stays still: the swell holds its shape, there are no ripples, rain or melting, and the still water stays drawn.

Runs on every device. There is no wake without a mouse. A tap drops a ripple and, with hover, wakes the swell or the rain for a moment; on ice it melts a patch.

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

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

## Cost and quality

Loads 13.2 KB the first time a page uses it. GPU: Two small half-float render targets of a few hundred cells a side, stepped about 60 times a second with nine reads per cell while waves run. The surface reads the simulation nine times and the letters once per pixel, plus the ice map on ice. Memory: A canvas of the text at twice its CSS size with mip levels, two small simulation targets, a small half-float map of the shore of the puddle built once on the CPU and, for ice, a map of frost and cracks at about the size of the box.

- low: The text is rastered at 1 device pixel per CSS pixel instead of 2 and the simulation grid is coarser.
- medium: The simulation grid is a little coarser.

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

## Recipes

### Rain on a dark page

`style="rain"` drops `rain` drops a second at random across the heading. Each lands as a small crater that rings out in a round wave, bending the letters as it passes and focusing a ring of light. On a dark page the caustics glow in the water itself, so the rings read even where the page is nearly black.

A live session title in pale letters on a night-blue page, with raindrops ringing out across the words.

```html title="index.html"
<div class="wa-rain">
  <p>Live session · Friday 9 p.m.</p>
  <h2 class="wa-rain-title" data-dio="water" data-dio-style="rain">After the storm</h2>
  <p>A new album played from start to finish, recorded in an empty harbor warehouse.</p>
</div>
```

### An ice label that thaws under the pointer

`style="ice"` freezes the surface into clear ice: frost gathers at the edges and in patches, thinner over the letters so their color reads, cracks run through it and the letters under it soften a little. The pointer melts a clear patch that stays open for a moment and then freezes over again, with a ring of slush at its edge. Give the heading its own background color and padding, and the ice fills that box like a frozen pond with walls.

```html
<h2 class="label" data-dio="water" data-dio-style="ice">Winter sale</h2>
```

## Notes

The surface is simulated on the GPU: a small grid of heights and speeds over the heading is stepped about 60 times a second by the wave equation with damping, and the pointer’s wake, clicks and raindrops are added to it as drops. A slow swell of five waves from different directions is added on top. The normal of every point of the surface bends the line of sight down to the letters, so they waver under the waves, and the same bend focuses the sun into caustics: the light a patch of surface gathers lands where its curvature points it, bright along thin lines and dim between them. The surface reflects a sky with a window and the sun’s glints, weighted by Fresnel, so the reflection grows on the slopes of the ripples. `tint` sets how much light the water takes on the way down, red first, and how much of its own color it scatters back.

Without its own background, the water lies in a puddle around the words: its shore wanders between about a seventh and two fifths of an em past the letters and rounds off before the edge of the box, so no rectangle shows. Give the heading padding of about a third of an em for it. The water is shallow and nearly clear at the shore and deeper toward the letters, and at the shore the surface curls down to the page in a thin bright line. The page color behind is the floor. With a solid background color on the heading, that color is the floor of a pool whose walls shade its edges and reflect the waves. Over a background image only the letters, the tint and the light are drawn.

With `trigger="always"` the swell keeps moving and the caustics dance at 30 frames a second while the heading is on screen. With `trigger="hover"` the water holds still until the pointer comes over it and draws nothing once the ripples settle; ice draws frames only while a patch refreezes. Under reduced motion the water stays still. The heading stays real text in the DOM: screen readers read it, search engines index it and find in page works. The canvas cannot draw some text the way the browser does: `background-clip: text`, `text-shadow`, `-webkit-text-stroke` and vertical writing modes. An element with any of them, or with a background image of its own, keeps its text as it is, and a warning in development names the reason. `style` and `trigger` rebuild the effect when they change.

## Related

- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [counter](/docs/effects/counter.md): Stats count up to their value on real 3D mechanics: odometer drums, slot machine reels or flip cards.
- [dust](/docs/effects/dust.md): A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.
- [emboss](/docs/effects/emboss.md): Letters pressed into or raised out of paper, leather, brushed metal, stone or plaster, lit by a light that follows the pointer.
- [extrude](/docs/effects/extrude.md): A headline becomes a solid 3D block: beveled letters with lit side walls, seen in perspective, that turn toward the pointer.
- [fire](/docs/effects/fire.md): The letters burn: flames rise from their tops, sparks drift up in depth and the firelight flickers on the page.
- [hologram](/docs/effects/hologram.md): A heading becomes a 3D hologram: projected from a glowing emitter, built of slices in depth, crossed by scan lines.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
