
Liquid Glass
A slab of refractive glass that bends the image beneath it.
This browser cannot run WebGL2, so this is a still image. The code below works wherever WebGL2 is available.
Code
The usage always matches the preview, layers and performance settings included. No build step or package.
Copy embed gives one block to paste into any page or site builder, no setup. For your own code: add liquid-glass.js, ittybitty-shader.js, runtime.js, pointer.js, shape.js, texture.js next to this page. Highlighted values follow the controls.
<!-- Quickest: one element. Size it with CSS; it fills its box. -->
<script type="module" src="./ittybitty-shader.js"></script>
<ittybitty-shader effect="liquid-glass" style="width: 100%; aspect-ratio: 16 / 10;"></ittybitty-shader>
<!-- Or with JavaScript, for full control: -->
<canvas id="liquid-glass" aria-hidden="true" style="width: 100%; aspect-ratio: 16 / 10; display: block"></canvas>
<script type="module">
import { liquidGlass } from './liquid-glass.js';
const canvas = document.getElementById('liquid-glass');
const effect = liquidGlass(canvas, {
showImage: true,
showGlass: true,
image: '/examples/still-life.webp',
speed: 0.3,
shape: 'pill',
vector: 'itty',
width: 0.62,
height: 0.36,
radius: 1,
refraction: 0.7,
dispersion: 0.4,
frost: 0.25,
follow: true,
responsiveness: 0.5,
// Performance
dpr: 2,
adaptive: true,
});
// Later: effect.set(…), effect.pause(), effect.resume(), effect.destroy()
</script>
Details
A rounded pane of glass over an image: the bevel bends the picture, splits colour at the edges, frosts the interior and catches a rim of light. It follows your pointer, or drifts when there is none. Frost is free: it reads a blurrier mipmap level instead of running a blur.
- Payload
- 7.2 KB brotli for liquid-glass.js and ittybitty-shader.js and runtime.js and pointer.js and shape.js and texture.js, measured at build time. See the method.
- Resolution
- Device pixel ratio capped at 2, so the image stays sharp around the glass.
- Frame rate
- Display rate while drifting or following the pointer.
- When unseen
- Stops rendering when the canvas leaves the viewport or the tab is hidden, and resumes where it left off.
- Reduced motion
- The glass stays still unless you move the pointer.
- Layers
- Up to five effects fuse into one shader and one draw call. Filters such as dithering and halftone process the layers beneath them.
- Browsers
- Any browser with WebGL2: Chrome and Edge 56+, Firefox 51+, Safari 15+.
- Licence
- MIT. Copy it, change it, ship it.