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.

Heating

After mercury tilt switch (switch) on ui-feed.dev.

Show the code the markup, the shared switch class and the model
  1. The label is the element's own text. It is slotted into a <label> inside the canvas, and the canvas draws that label.
  2. What every switch shares. One checkbox, a slotted label, an <output> for "On" and "Off", and the form value.
  3. The model. A spring for the tilt, the shallow-water step for the metal, then the drawing and hit().
  4. 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.

Do not disturb

After newton's cradle (switch) on ui-feed.dev.

Show the code one damped swing and a thumb that waits for the strike
  1. The markup.
  2. 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.

Porch Garage Garden

After industrial rockers (switch) on ui-feed.dev.

Show the code a rocker with a lamp and a name plate
  1. Three elements. checked is an attribute, as on an input.
  2. 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.

Frost alarm

After frost alarm (switch) on ui-feed.dev.

Show the code a slide switch and a segment display
  1. The markup.
  2. 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.

Do not disturb

After clay (switch) on ui-feed.dev.

Show the code shadows inside clipped shapes
  1. The markup.
  2. 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
  1. Rows are <option> children, columns are an attribute.
  2. The element. One put() for the whole sheet, then a mark at each input's offsetLeft and offsetTop.

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