Most UI libraries are flat rectangles with a shadow. I wanted components that feel like soft clay: they swell when you point at them, squash when you press them, and melt into each other. So I built Claymorph UI, 25 web components where a single WebGL2 shader draws all the clay on the page.
The inspiration came from liquid-glass by Bilal Abdullah Gürşen: open-source glass controls for React where every button, switch and slider is a refracting WebGL lens with spring physics. I cloned it, studied how it works, and wondered what the same idea would feel like as soft clay instead of glass.
This post is about how that works: one GL context for a whole page, signed distance fields, a one-line melt, and springs that don't care about frame rate.
One context, many canvases
The obvious design is a WebGL canvas per component. It breaks fast: browsers cap how many WebGL contexts a page can keep alive, and a settings page alone has more than that many switches and buttons.
So there's one hidden WebGL2 canvas for the whole page. Each element gets a plain 2D canvas behind it, its view. On every frame the engine renders a view into the shared GL canvas and copies the result into the view's canvas with drawImage. One program, one context, as many elements as you like.
Two details keep it cheap:
- A view is only redrawn when its shapes change. The engine builds a key from every shape's position, size, colour and material, and skips the draw when the key matches the last one.
- The loop stops. When no spring is moving, the engine stops asking for animation frames until something wakes it, like a hover or a resize.
Every element is a few rounded rectangles
A button is one rounded rectangle. A switch is a track and a knob. An attached select is the trigger plus the list. Each view hands the shader up to 16 shapes, and the shader measures how far every pixel is from each one with a signed distance field:
float shape(vec2 p, int i) { vec4 r = uRect[i]; vec2 h = r.zw * 0.5; float rr = min(uRadius[i], min(h.x, h.y)); vec2 q = abs(p - r.xy - h) - h + rr; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - rr; } Negative inside, positive outside, zero on the edge. Everything else is built on that number.
Melting is one line
To melt two shapes into one, you don't take the closer of the two distances (min), you take a smooth minimum. Where the shapes are far apart it's the same as min; where they're close, it bridges the gap with a curve whose size is k:
float smin(float a, float b, float k) { if (k <= 0.0) return min(a, b); float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0); return mix(b, a, h) - k * h * (1.0 - h); } Every shape has a melt radius. Shapes with a melt join the group before them, and the shader folds the group with smin. Animating k is what makes the logo's bead sink into the blob, and what lets a select's list stay fused to its trigger while it pours out.
From a distance to clay
A distance field gives you a silhouette, not a surface. For the clay look, the shader treats the outer band of each shape, its bevel, as a rounded edge: the closer a pixel is to the edge, the steeper the surface. The direction comes from the gradient of the distance field, so the normal follows the melted outline too, necks and all.
With a normal, it's ordinary lighting, tuned to look like clay rather than plastic:
- a wrapped diffuse term, so the shade rolls softly round the edge;
- a little subsurface colour where the surface turns away from the light;
- a broad, dim sheen instead of a sharp highlight;
- a faint rim, and some noise in the normal so it reads as a material, not a gradient;
- a shadow from the same distance field, shifted away from the light and blurred.
Wells, like an input or a slider's track, are the same shapes with a negative height.
Ghosts: how a popup grows out of its button
A popup lives in the browser's top layer, so it's a different element, with a different canvas, from the button that opened it. To make it look like one piece of clay, the popup's view includes the trigger as a ghost: a shape that other shapes melt into, but that isn't drawn, and that erases whatever is under it. The neck between the list and the button is drawn by the popup; the button is drawn by the button. Where they meet, the two copies line up to the pixel, because every view ticks its springs before any view draws, so both canvases show the same frame.
The dialog takes this furthest. Its clay is pulled out of the button as a bead with a neck, flies to the middle of the page with a tail that stretches with its speed, and swells into the dialog.
Springs that don't care about frame rate
All the motion is springs: hover, press, the list's size, the dialog's flight. Instead of stepping a spring with a small fixed timestep, each one is solved in closed form, as a damped harmonic oscillator, for whatever time passed since the last frame:
const x0 = this.x - this.target const a = zeta * omega const wd = omega * Math.sqrt(1 - zeta * zeta) const e = Math.exp(-a * dt) const b = (this.v + a * x0) / wd this.x = this.target + e * (x0 * Math.cos(wd * dt) + b * Math.sin(wd * dt)) So a 120 Hz screen and a throttled background tab land on the same curve. The overshoot of an underdamped spring is the wobble you see; the logo even squashes along its velocity. With prefers-reduced-motion, springs jump straight to their target.
Behaviour comes from ui-sdk
I didn't want to write another select. Every clay element wraps a part from ui-sdk, which owns the keyboard, focus, ARIA, popups and forms. The clay element only measures that part and draws the material and the motion. Without WebGL2, the same clay is drawn in CSS, and in forced colors the canvases step aside for system colours.
<clay-select attached color="teal"> <ui-select default-value="week"> <ui-select-label>Period</ui-select-label> <ui-select-trigger><button><ui-select-value></ui-select-value></button></ui-select-trigger> <ui-select-content> <ui-select-item value="day">Today</ui-select-item> <ui-select-item value="week">This week</ui-select-item> </ui-select-content> </ui-select> </clay-select> Try it
From npm, each element has its own entry, so a page with one button ships about 22 KB gzipped, ui-sdk included. Or, on a page with no build step, two tags:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/claymorph-ui@0/dist/cdn/claymorph-ui.css" /> <script type="module" src="https://cdn.jsdelivr.net/npm/claymorph-ui@0/dist/cdn/claymorph-ui.js"></script> - Docs and live demos: claymorph-ui.lauroguedes.dev
- Source (MIT): github.com/lauroguedes/claymorph-ui
- Good first issues are open if you'd like to add a component
I'd love to hear what you'd build with it, or what the clay should do next.


