water
A headline under water: the surface bends the letters, focuses dancing caustics on them and ripples under the pointer.
Lakeside Lido · open from 1 June
Summer swim
A heated 50 meter pool, lanes at sunrise and a shallow end for the little ones. Season passes go on sale on Monday.
Live preview needs WebGPU or WebGL 2. 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.
This example failed to draw.
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. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add water<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>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.wa-headline', 'water', { style: 'pool', trigger: 'loop', depth: 1, caustics: 1, ripples: 1, rain: 6, damping: 0.5, tint: 0.5,})import { start } from '@dioramajs/diorama'import water from '@dioramajs/text/water'
const app = start()app.attach('.wa-headline', water({ style: 'pool', trigger: 'loop', depth: 1, caustics: 1, ripples: 1, rain: 6, damping: 0.5, tint: 0.5,}))Page transitions
The water fades with the page. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
stylerebuildsdata-dio-style | 'pool' | 'rain' | 'ice' | 'pool' |
|
triggerrebuildsdata-dio-trigger | 'loop' | 'hover' | 'loop' |
|
depthdata-dio-depth | number0 to 3 | 1 | How strongly the surface bends the letters seen through it; 0 keeps them straight. |
causticsdata-dio-caustics | number0 to 2 | 1 | Brightness of the light the waves focus on the letters and the floor; 0 turns it off. |
ripplesdata-dio-ripples | number0 to 3 | 1 | Strength of the wake and the drops the pointer makes; 0 leaves the water alone. |
raindata-dio-rain | number0 to 30 | 6 | Raindrops a second with the rain style; 0 stops the rain. |
dampingdata-dio-damping | number0 to 1 | 0.5 | How fast ripples die out; 0 lets them run and bounce for a long time. |
tintdata-dio-tint | number0 to 1.5 | 0.5 | Depth of the water color over the letters and the page; 0 is perfectly clear. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta, height, over, pointer, reducedMotion, size and 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. How a frame is decided.
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.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { water: { reducedMotion: 'off' } } })Cost and quality
- Download
- 13.2 KB min+gzip 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.
- Frames
- Draws while on screen
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
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. |
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.
Live session · Friday 9 p.m.
After the storm
A new album played from start to finish, recorded in an empty harbor warehouse.
Live preview needs WebGPU or WebGL 2. A live session title in pale letters on a night-blue page, with raindrops ringing out across the words.
This example failed to draw.
<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.
<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.