Checkboxes drawn as physical switches
Six switches from ui-feed.dev, rebuilt with HTML-in-canvas. Each one is a custom element whose shadow
root holds a drawable canvas, and inside the canvas is one <input type="checkbox" role="switch"> (21 of them for the
answer sheet). The canvas draws a model from the input's checked: mercury that runs along a tilted vial, a ball that
swings in and knocks the thumb across, a rocker, a slide, soft clay, pencil. The input is never painted. Its hit region is put over
the part of the drawing a finger would press. I wanted to know if the checkbox could stay the thing that is tapped, focused with Tab,
toggled with Space and submitted, while nothing of it is on screen.
Credit: every control on this page is a rebuild of a demo on ui-feed.dev, and each section links to the original it came from. The originals are far higher fidelity than these, so go and look at them. These are examples of what HTML-in-canvas makes possible with a native form control underneath, not replacements. I worked from the published demos rather than their source: I looked at what each one does, worked out the mechanism and built it again. The designs and the ideas are theirs.
Mercury tilt switch
The checkbox tilts a glass tube on a brass post. The mercury inside it is a shallow-water simulation along the tube: 56 columns
of metal, each with a depth and a speed, with gravity along the glass taken from the tilt and the pressure taken from the difference
in depth between one column and the next. So it runs downhill, piles against the end wall, rebounds and sloshes before it settles.
Mercury doesn't wet glass, so a column shallower than a bead is pulled into its fuller neighbour, which is what holds it together as
one slug. The lamp is lit from where the metal is, not from checked, so it comes on a moment after the tap.
After mercury tilt switch (switch) on ui-feed.dev.
Show the code the markup, the shared switch class and the model
- The label is the element's own text. It is slotted into a
<label>inside the canvas, and the canvas draws that label. - What every switch shares. One checkbox, a slotted label, an
<output>for "On" and "Off", and the form value. - The model. A spring for the tilt, the shallow-water step for the metal, then the drawing and
hit(). - The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Newton's cradle
The middle ball is the thumb, in a slot. Switching on lifts the left ball and lets it go. When it lands, the thumb is knocked to the right and the right ball swings out. Switching off does the same from the other side. The swing dies away in about four seconds.
After newton's cradle (switch) on ui-feed.dev.
Show the code one damped swing and a thumb that waits for the strike
- The markup.
- The model. One angle for both end balls. The thumb waits for the first ball to land before it moves.
Industrial rockers
Three elements side by side, each with its own canvas and its own checkbox. The name plate is the slotted label, styled with CSS and drawn by the canvas.
After industrial rockers (switch) on ui-feed.dev.
Show the code a rocker with a lamp and a name plate
- Three elements.
checkedis an attribute, as on an input. - The model.
Frost alarm
A slide switch under a liquid crystal display. The digits are seven-segment shapes drawn by the canvas, the label is slotted text, and the bell blinks for two seconds after the alarm is set.
After frost alarm (switch) on ui-feed.dev.
Show the code a slide switch and a segment display
- The markup.
- The model.
Clay
A soft slab with a groove pressed into it. The thumb is on a loose spring, and it gets wider and flatter with its speed.
After clay (switch) on ui-feed.dev.
Show the code shadows inside clipped shapes
- The markup.
- The model. The raised and pressed edges are strokes with a shadow, drawn inside a clip.
Pencil answer sheet
A checkbox group. This one does draw its elements: the sheet is a grid of labels and appearance: none checkboxes,
drawn as one picture. The canvas adds the pencil. A mark is drawn stroke by stroke over a checked box, and a box that is unchecked
keeps a grey smudge.
After pencil answer sheet (checkbox group) on ui-feed.dev.
Show the code a drawn form with pencil over it
- Rows are
<option>children, columns are an attribute. - The element. One
put()for the whole sheet, then a mark at each input'soffsetLeftandoffsetTop.
How it works
The switches extend FeedControl in /lib/ui-feed/kit.js, the same base class as the
dot and tube displays. The shadow root holds a canvas with content="drawable". Its children
are the checkbox, a <label for> with a <slot> in it, and an <output>.
The checkbox has opacity: 0 and is never drawn. In each paint the canvas calls drawElementImage() for it with
globalAlpha = 0 and the rectangle of the vial, the slot or the rocker. That is enough to put its hit region there. A tap
in that rectangle is an ordinary click on a checkbox, so change fires, Space works when it has focus, and a tap on the
drawn label toggles it through for. The canvas draws a dashed ring when the input matches :focus-visible.
The models are stepped in the paint handler. A switch asks for another frame only while something is still moving, so a page of
switches at rest draws nothing. The value reaches the form through ElementInternals.setFormValue(): on (or
the value attribute) when checked and nothing when not, as a checkbox does. The line under each demo is the
FormData of the form around it.
Live checks
Findings
- A checkbox that is never painted is still the control. In all five switches the point at the middle of the drawn part
answered the
<input>, a click there changedchecked, and the form around it gotname=on. - The hit region can be any size. The mercury switch's checkbox is laid out at 240 by 200 and placed at 300 by 220. The rating on the other page places a 66 by 70 radio over each star. The draw rectangle is what counts.
- Slotted text in a shadow canvas works as a label. The word "Heating" is a bare text node of the host, drawn by the
canvas inside a
<label for>. A mouse click on the drawn word had the path slot, label, canvas, shadow root, host, and the label then clicked the checkbox.elementFromPoint()at the word answers the host. - The fixture stage of the live checks takes clicks while they run. It is a fixed, nearly transparent box at the bottom left. My first test of the label clicked a fixture in it instead of the demo, which is why the checks on this page wait for a tap.
changestops at the shadow root. The event isn't composed, so each element sends its ownchangefrom the host.inputis composed and needs nothing.- A model can lag the checkbox, and the form can't. The lamp follows the mercury and the thumb waits for the ball, but
checkedand the form value change at once. Someone who taps and submits within half a second submits the new value while the drawing still shows the old one. - Not verified: everything on a phone. Whether a touch on the drawn switch toggles it on Chrome on Android, how the swing
looks at the phone's frame rate, and what a screen reader says for a
role="switch"checkbox whose label is slotted.