Form controls drawn as machines
Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas. Each is drawn as a machine with
a part that is pushed, turned or held: the tappets round a plug-in timer, the speed slide of a record player, the knob of a darkroom
timer, a lamp on a cord, a telegraph key. Under each drawing are native elements: 96 checkboxes, three radios, an
<input type="number"> with a range beside it, a row of buttons, a text field. I wanted to know if a hit region
could be put on a ring with a rotated draw, and if a button that is held down and a range that is turned could drive the drawing.
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, and three of them are built on photographs, 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.
Timer tappets
A plug-in timer has 96 tappets round its dial, one for each quarter of an hour. Push one out and the socket is on for that
quarter. Here each tappet is an <input type="checkbox">. The ring turns with the clock, so the time now is
always under the black pointer at the upper left, and the checkboxes turn with it: each one's hit region is placed by a draw with
no alpha inside ctx.rotate(). The value is the list of spans that are on.
After the tappets of a 24-hour timer (toggle group) on ui-feed.dev.
Show the code the markup and the control
- The markup. The value is spans of the day, to the quarter hour.
- The control. 96 checkboxes made in script, the value, the arrow keys, and the draw that turns each one onto the ring.
hit(). A draw with no alpha moves an element's hit region.- The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Record player speed
Three radios: 33⅓, 45 and 78. The record has an angle and a speed, and the motor pulls the speed toward the chosen one with a time constant of 0.7 seconds, so going from 33⅓ to 78 takes a couple of seconds to run up. The sheen on the grooves stays where the light is while the label and the bands between tracks turn under it. The text on the label is the element's own text, slotted and drawn rotated.
After record player speed (segmented control) on ui-feed.dev.
Show the code the markup, the motor and the control
- The markup.
- The motor. Three lines.
- The whole control. Radios and labels, the record, the tonearm and its shadow, and the knob on a spring.
Darkroom timer
Seconds of exposure, from 1 to 60. The numbers are on a tape round a drum behind a red window. Each number is placed by the
sine of its angle on the drum and narrowed by the cosine, which is how a cylinder looks from the front. Press minus or plus, turn
the knob (a tenth of a turn is one second, and the tape follows your hand between the numbers), or tap the window and type: an
<input type="number"> lies over it. The knob is an <input type="range">, so the arrow keys
turn it.
After darkroom enlarger timer (number input) on ui-feed.dev.
Show the code the markup and the control
- The markup.
- The control. Four native elements, the turn of the knob from the pointer's direction, and the drum.
Pendant lamp
A row of buttons, one for each person, and a card for the one in use. Point at a face, tap it or Tab to it. The lamp hangs from
a trolley that runs along the ceiling to that button, and the lamp is a pendulum on the trolley: it is left behind when the trolley
sets off, swings past when it stops, and settles. Each face is lit by how near the lamp is, and the card waits for the lamp to
arrive. The people are <option> children of the element, and each data- attribute is a row of the
card.
After pendant lamp (hover card) on ui-feed.dev. The original has photographs. These faces are drawn.
Show the code the markup, the pendulum and the control
- The people are options.
- The pendulum. A trolley on a spring, and a bob whose pivot accelerates.
- The whole control. The buttons, the light on each face, and the card drawn with the alpha of its fade.
Morse key
A text field with a list of suggestions under it. Type and the list narrows, the arrow keys move through it and Enter picks.
Beside each name is the Morse code for its next letter. The key at the bottom is a <button>: a short press is
a dot, a press longer than 220 ms is a dash, and a pause of 650 ms ends the letter, which is then typed into the field. So
dash dot dot dot, a pause, then dot dash dot, gets you to Bread. The lever is on a stiff spring and stops hard on the contact.
With the sound attribute, as here, it sounds a tone while it is down.
After morse key (autocomplete) on ui-feed.dev. The original key is a photograph. This one is drawn.
Show the code the markup, the key and the control
- The suggestions are options.
- The key. Timing a press, ending a letter, and the tone.
- The whole control. The field, the list with a code beside each row, and the drawing of the key.
How it works
Each control is a custom element on the same base class as the physical switches and the
simulations. Its shadow root holds a canvas with content="drawable", and the native
elements are children of that canvas. The canvas draws the machine from their state and calls drawElementImage() for
each element: with full alpha where the element should be seen (the speed labels, the card, the text field and its list), and with
globalAlpha = 0 where only its hit region is wanted.
A hit region follows the transform of the draw that placed it. The tappets use that: the context is rotated to each tappet's
angle before its checkbox is drawn, so 96 small boxes lie along 96 radii. The knob of the darkroom timer and the Morse key keep
their own pointer state. The knob captures the pointer and reads its direction from the knob's centre. The key sets a flag on
pointerdown and clears it on pointerup, because :active did not hold under a touch on the
pump's release cap.
Four of the five stop asking for frames when nothing moves. The record never stops turning, so that control draws for as long as it is on screen. The timer also draws once every 30 seconds, to turn the ring with the clock.
Live checks
Findings
- A rotated draw places a rotated hit region. Each tappet's checkbox is 30 by 9 pixels and drawn with no alpha under
ctx.rotate(). The point at the middle of a tappet answered that tappet's checkbox at every angle the check tried. - An element drawn rotated is still clickable where it is drawn. The side text on the record label is slotted text drawn on a disc that turns, and the tappets' checkboxes are placed under the same kind of rotation.
- A number field can be typed in without being seen. The field over the red window has
opacity: 0. A tap focuses it, digits change the value, and the tape is the only display. There is no caret to see, so the canvas draws the hairline white while the field has focus. - A press can be timed. The key measures the time between
pointerdownandpointerupon a button. With Space or Enter it ignores the repeatedkeydownevents a held key sends. - The card is an element drawn with alpha.
globalAlphaapplies todrawElementImage(), so the card fades without a CSS transition. Its content is replaced when the lamp arrives, which is later than the moment the button is chosen. - Not verified: everything on a phone. Whether a tappet 9 pixels wide can be hit by a finger, turning the knob by touch, how long a press a thumb makes on the key, whether the tone plays, and what a screen reader says for 96 checkboxes.