CSS shapes lit as solid objects
The pieces below are buttons and boxes whose outlines come from corner-shape, border-shape and
clip-path: shape(). A fragment shader treats each picture's alpha channel as the height of a solid and lights it: the edges
are bevelled, and each piece casts a shadow with its own outline. Move the pointer or drag a finger across the panel to move the light.
Press the pieces.
This paragraph wraps round the button on the left. The button is clipped with clip-path: shape() and its holder has
shape-outside with the same value, from one custom property. The shader gets the clipped picture, so the bevel and the
shadow follow the curve.
The light follows the pointer. With no input for a moment it moves in a circle.
Show the code alpha → height → light
- The markup. Each piece that is lit has
fx="solid". The keypad is one piece: its seven keys share a canvas. - The outlines, in CSS.
- The shader. Sixteen samples of alpha around each pixel give a height and a slope. The slope is the surface normal.
- The light, as two custom properties.
- Pressing, as a CSS transition.
- The page script. It connects the pointer and the tilt sensor to the two properties.
- The tilt sensor.
- Inside the fx library: the upload and the draw.
How it works
- The fx library moves each piece into a canvas and gives the shader the piece's picture as a texture with premultiplied alpha. Whatever CSS does to the outline is already in the alpha channel.
- For each pixel the shader reads alpha at 16 points in a disc of 7 px. The share of points inside the shape is the height: 1 well inside, about 0.5 on the edge. The average of the points' offsets, weighted by alpha, points away from the nearest edge. That gives a normal without a second pass.
- Labels are embossed with a second, smaller slope taken from the brightness of the picture.
- The shadow is the same alpha read 16 px towards the light, through 8 jittered samples.
- The seven keys of the keypad are one picture. A pressed key is scaled to 0.9 by
:active, which changes the alpha, and the lighting follows with no code for it.
Live checks
Each check builds its own fixture in a corner of the window. The panel above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 with software rendering. The rows above say what your browser does.
- Works: every outline is in the alpha channel.
corner-shape(squircle, bevel, scoop, notch, superellipse),border-shape: circle()andclip-path: shape()all change the picture the shader receives, so one shader lights all of them. - Works: normals from 16 samples. The bevel is smooth at 7 px. Below about 3 px the samples land less than a pixel apart and the bevel becomes a plain edge highlight. Above about 14 px the 16 samples show as faint steps.
- Hit testing follows all three outlines. A point in a corner that
corner-shape,border-shapeorclip-pathcut away is outside the element forelementFromPoint(), in a canvas and as plain DOM. A click in the gap between a piece's box and its outline doesn't press it. The shader moves no pixels, so no geometry update is needed. - Limit: the shader can't see behind a piece. A shadow is drawn in the canvas of the piece that casts it, inside 30 px of extra canvas round the piece. It doesn't darken a neighbour that is itself a canvas above it.
- Not verified here: the tilt sensor (the test browser has none), and the frame rate on a phone, where each piece costs 29 texture reads per pixel while the light moves.