Controls that wear with use
Three native controls that keep the marks of being used. Press Save and the paint chips where your finger landed. Press the dark key and it keeps your fingerprint and sits a little lower. Drag the slider and its track wears a dark groove where the thumb has been.
The wear is saved in this browser, so it's still there when you come back. Each control is a plain <button> or <input type="range"> drawn by a canvas.
A button whose paint chips
After Aging Button by Florian Kiem.
Show the code the button, the chips, the holes
- The markup. One
<button>. - A press. Where it landed and a seed go on a list, and the marks for it are drawn into two plain canvases.
- The paint. The button's picture, scuffs on it, holes cut through it, then the metal behind.
- The marks. A chip, a scuff and a fingerprint.
A key that sinks and keeps fingerprints
After Realistic Button by Adam Kowalski.
Show the code the key, the prints, the sinking
- The markup. A
<button aria-pressed>with a lamp. - A press. A print is stamped where the pointer went down.
- The paint. The base, the key drawn lower for every press, and the prints kept on its face.
- The marks.
A slider whose track wears a groove
My addition. The same idea on an <input type="range">.
Show the code the slider, the groove
- The markup.
- The count and the paint. Each value the thumb passes is counted, and the counts are drawn as a dark band on the slider's picture.
Show the code saving the wear, the styles, the whole library
- Saving. The wear is a list of presses in
localStorage, replayed on load. - Styles.
- The whole library file.
How it works
Each control has one or two plain canvases its own size that hold its wear. They aren't on the page and they're never read. A press draws a few marks into them, and the visible canvas draws them over the element's picture on every paint.
Chips have to remove paint, and under the paint there has to be metal. That takes three composite modes in one canvas:
drawElementImage()draws the button.source-atopdraws the scuffs, so they stay on the paint.destination-outdraws the chip canvas, which cuts a hole in the button's picture for each chip. The label is cut too.destination-overdraws behind what is left: first a dark, shifted copy of the canvas, which shows as the thickness of the paint at the edge of each hole, then a metal plate.
The button is still a button. Its hit box is its whole box, holes included, and the label a screen reader gets is the element's text, which no chip can remove.
Prints are strokes at low alpha, clipped to the key's face and drawn with source-atop. The key's resting place moves down 0.1 of a pixel for each press, to at most 5, and the base under it stays where it is, so the key gets shorter. The hit box moves with the picture.
The groove counts each value the thumb passes. The slider is dragged natively: the canvas only listens to input.
Saving. A mark is drawn from a seed, so the wear is saved as a short list of presses (where, and the seed) and drawn again on load. I didn't save pixels, which would have meant reading a canvas back.
Live checks
These run on their own canvases in the corner of the window. They don't touch the wear you've made.
Findings
- In Chromium 157,
destination-outwith a plain canvas cuts holes in a picture drawn bydrawElementImage(), anddestination-overthen draws only in the holes and round the element. - The hit box ignores the holes.
elementFromPoint()in the middle of a chip returns the button. source-atopkeeps adrawImage()of prints on the element's picture. Nothing lands outside it.- A mark drawn twice from the same seed is the same to the pixel, so wear can be saved as a list and never read from a canvas.
- A native range input dragged inside a canvas raises
inputas usual, and its picture can be drawn over while it's dragged. - The cut also removes the focus ring where a chip crosses it. After a few hundred presses the ring on Save has gaps. I left it: that's what the paint would do. A real product would draw the ring last.
- Not verified: how the page feels by touch on a phone, where a press has no exact point until the finger lifts. The page uses the point where the pointer went down.
- Not built: wear that follows the element when its size changes. The wear canvases are made once, at the size the control had on load.