Pool floor

Light through waves

Every curved wave on the surface is a small lens. Where they focus sunlight, the floor lights up in a moving net.

How it works

A light net from noise ridges

Real caustics come from refraction: each wave bends sunlight a little, and where many rays land on the same spot the floor gets bright. Simulating that needs a shader. An SVG filter can fake the look instead: the bright lines are the places where a noise function crosses zero.

1. Ridges: feTurbulence type="turbulence"

type="fractalNoise" sums smooth noise around grey. type="turbulence" sums the absolute noise instead, so every place where the noise changes sign becomes a thin valley near 0, surrounded by higher values. With one octave those valleys form closed, curved cells, which is already the shape of a caustic net. More octaves add smaller valleys and make it choppier.

2. Keep only the ridges: feComponentTransfer

A feColorMatrix writes the noise's red channel into alpha and paints the colors white. Then <feFuncA type="table"> remaps alpha: input 0 (the valley) becomes 1, and everything above Line width becomes 0. In between it falls off as (1 − x / width)², so the lines have a bright core and soft sides. The table is linear between its entries; 32 are plenty. With 12 entries, the current one would be:

1 0 0 0 0 0 0 0 0 0 0 0

The result is an alpha mask of thin, bright lines. A 0.6 px blur takes the hard pixel steps off, since the turbulence is computed at 8 bits and the steep table magnifies its banding.

3. Make it dance: feDisplacementMap

Sliding the net would look like a moving picture of a net. Real caustics change shape as the waves pass, so a second, smoother noise (the surface) bends the ridge pattern through feDisplacementMap, by up to half of Warp px. The surface is two crossing layers of fractal noise that travel in different directions, so its bends keep changing, and the net with them. The cells themselves only drift at 30% of the speed.

Both noises are computed on small tiles and slid with feOffset, the same trick the water ripple page explains in detail. The two tiles have different sizes (384 and 528 px here), so their combination only repeats after a few thousand px and the tiling never lines up into a visible grid. With a smaller ridge tile the repeat does show on a wide screen, which is why the default is 384.

4. Light: feFlood and feSpecularLighting

feFlood in the light color, cut to the mask with feComposite operator="in", is the flat light. For Sheen, feSpecularLighting treats the mask as a height map (the lines are ridges 3 px high) lit by a distant light from the upper left: one side of each line gets a sharper glint, which is what makes them look wet rather than painted on. The two are added with feComposite operator="arithmetic", weighted by Intensity.

5. The floor

The content is displaced by the same surface noise (Wobble), so the floor moves in step with the light, then washed with the water color: an feFlood composited atop, which colors only where there is content. Last, the light is clipped to the floor and screened over it with feBlend mode="screen", which can only brighten, like real light.

<filter id="caustics" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB"
        x="-5" y="-5" width="max(W, 1056) + 10" height="…">
  <!-- moving tiled noise, as on the water ripple page: -->
  <!-- "cells": type="turbulence" baseFrequency="0.013020833333333334 0.013020833333333334"
       numOctaves="1" on a 384 px tile, drifting -->
  <!-- "surface": fractalNoise baseFrequency="0.00946969696969697 0.00946969696969697"
       on a 528 px tile, two crossing layers -->

  <feDisplacementMap in="cells" in2="surface" scale="50"
                     xChannelSelector="R" yChannelSelector="G" result="bent" />
  <feColorMatrix in="bent" values="0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  1 0 0 0 0" />
  <feComponentTransfer>
    <feFuncA type="table" tableValues="1 0.291 0.006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" />
  </feComponentTransfer>
  <feGaussianBlur stdDeviation="0.6" result="lines" />

  <feFlood flood-color="#e8fdff" />
  <feComposite in2="lines" operator="in" result="glow" />
  <feSpecularLighting in="lines" surfaceScale="3" specularExponent="20"
                      lighting-color="#e8fdff">
    <feDistantLight azimuth="225" elevation="55" />
  </feSpecularLighting>
  <feComposite in2="lines" operator="in" result="glint" />
  <feComposite in="glow" in2="glint" operator="arithmetic"
               k2="0.7" k3="0.42" result="light" />

  <feDisplacementMap in="SourceGraphic" in2="surface" scale="5" … result="floor" />
  <feFlood flood-color="#0a6f86" flood-opacity="0.35" />
  <feComposite in2="floor" operator="atop" result="floor" />
  <feComposite in="light" in2="floor" operator="in" result="light" />
  <feBlend in="light" in2="floor" mode="screen" />
</filter>

On the page it is one CSS declaration, filter: url(#caustics), plus the small loop from the ripple page that moves the feOffsets.

Browser support and performance

Every primitive here is part of SVG filters on HTML, Baseline widely available since 2015. Checked in headless Chromium; Firefox and Safari are untested for this page.

  • This is the heaviest filter in the collection. Each frame computes two noise tiles, two displacements, a lighting pass and several composites over the whole element. The frame rate is capped (30 fps by default), and the loop stops offscreen, in background tabs and inside a paused region (the button on the stage). Keep the element no bigger than it needs to be.
  • Reduced motion. Under prefers-reduced-motion: reduce the net stands still; it still looks like a pool floor, it just doesn't move.
  • Readability. Bright moving lines over text make it hard to read, and the tint lowers its contrast. Put the effect on a background layer, as here, with the text in a solid card on top. The flickering lines move slowly and smoothly, so they don't flash.
  • Static alternative. If the motion isn't needed, a single rendered frame saved as an image costs nothing to show.

Back to the demo ↑