Form controls drawn as instruments
Five ideas from the element catalogue, built. In each one the canvas draws a form control as a different machine, and the real control still holds the value, takes the keyboard and does its own validation. Turn the knob, type in the counter, press Next with an empty field, and drag the clock hands. The line under each canvas is read from the form.
Rotary switch from a radio group
Five radio buttons. Their labels are drawn round a knob, and the knob points at the checked one. Tap a label, drag the knob, or focus a radio and use the arrow keys.
The radio group is the only state. Arrow keys, the one-checked rule and the form value are the browser's. The page adds the knob: it reads which radio is checked, and a drag checks the radio of the nearest position.
Show the code
- Mark each label
drawable. The form and the fieldset aren't drawn. - Draw the knob from the checked radio and the labels round it.
- A drag on the knob checks a radio.
- Styles.
Odometer for a number input
One <input type="number">. Tap the wheels and type, use the up and down arrow keys, or press the buttons. The wheels roll to the new value.
The input's text is transparent and the canvas draws the digits, because the picture of the old value is gone once the value changes. The input still draws the caret and the selection, and letter spacing puts each character over its wheel. Its focus ring is drawn by the canvas.
Show the code
- Make one character as wide as one wheel.
- Draw the wheels from the value, then the input on top.
- Styles.
Form on a prism, one fieldset per face
A three-step form. Next and Back turn the prism. Press Next with a field empty, or type a name on the first face and press Enter.
Validation messages, :invalid styles, the Enter key and the submitted data are the form's own. The page adds the turn, sets inert on the two faces that are turned away, and turns to a face when the browser reports an invalid field on it.
Show the code
- One form, three
drawablefieldsets. - Draw the faces in strips while the prism turns, and flat when it rests.
- Turn on Next, on Back and on
invalid. - Styles.
Dial gauge from a <meter>
The arc is the picture of a <meter> element, bent. Move the slider through the low and high marks, then change where the optimum is (the green mark): the browser picks the colour of the bar.
The element supplies the bar, its track and its colour rules. The page adds the bend, the needle and the tick labels, which it reads from the element's attributes.
Show the code
- Draw the meter in 100 slices round an arc, then the needle.
- Styles.
Clock hands from a time input
One <input type="time">, drawn under a clock face. Drag either hand, or type in the field. Taking the minute hand past 12 changes the hour.
The input holds the time and is the control a keyboard or a screen reader uses. The page draws the face from valueAsNumber and writes value while a hand is dragged.
Show the code
- Draw the face and hands from the value.
- A drag writes the value.
- Styles.
How it works
Each canvas holds the real control as a descendant marked drawable. A paint handler draws the machine with ordinary 2D calls and draws the control, or parts of it, with drawElementImage(). That call also moves the control's hit region to where it was drawn, so taps, the caret and the focus order need no code. Three techniques are used more than once:
- Strips. A 2D context has affine transforms only. The prism's perspective and the gauge's bend are made by drawing the same element many times, each time clipped to a narrow strip with its own transform. The last draw sets the hit region, so the prism ends each turn with one flat draw.
- A grip element. The knob and the clock face are canvas drawings, with no element. A round
drawableelement that is only a faint ring is drawn over each. It receives the pointer events and carriestouch-action: none, so the rest of the canvas still scrolls the page. - The control is the state. No demo keeps its own copy of the value. The paint handler reads
checked,valueAsNumberorvalue, and a drag writes them.
Show the code
- The paint loop and the draw helpers shared by the five demos.
- Each demo starts when its section comes near the screen.
- A lost context stops every demo.
- The controls are then shown as plain HTML.
Live checks
The checks wait for a tap. Each one builds its own small canvas in a corner of the window, and none of them touches the demos above.
Findings
Measured in headless Chromium 157 on 2026-11-21, with a mouse, a keyboard and emulated touch.
On a phone (Chrome 159 on Android, reported 2026-11-21): the first version of this page lost its canvases repeatedly: a white box with a broken-picture icon, then a white screen, then an empty canvas. The cause was found later on the phone by another experiment on this site: a drawable 2D canvas created with willReadFrequently: true, once drawn into and read, loses every 2D context on the page about every half second. The first version's live checks made such canvases on load. This version reads pixels from a drawImage() copy, keeps the checks behind a tap, and after one lost context stops drawing and shows every control as plain HTML. Whether that ends the losses on the phone isn't verified.
- A radio group works with only its labels drawn. The form and the fieldset aren't drawn. Each
drawablelabel is drawn rotated at its own place, a tap on it checks its radio, the arrow keys move the checked radio and the focus, andFormDataholds one value. - A
drawableelement drawn over canvas pixels works as a drag handle with its owntouch-action. A touch drag on the knob or the clock face didn't scroll the page, and a vertical swipe on the same canvas beside the knob scrolled it by the length of the swipe. - A number input with transparent text adds no pixels to the canvas. It still draws its caret and its selection, and a tap between two wheels puts the caret there. So a canvas can draw the digits itself and leave editing to the input.
- An outline isn't in an element's picture. The picture ends at the border box. The odometer's focus ring is drawn by the canvas from
:focus-visible, and the radio labels show focus with a border and an inset shadow. stepUp()raisespaintand noinputevent. The canvas redraws by itself. Code that mirrors the value somewhere else has to be told.- The browser's validation message appears at the drawn field. After Next on an empty field, and after Book with the box unchecked, the message pointed at the field where the canvas had drawn it.
- An
inertfieldset is still drawn, still submitted and still validated.elementFromPoint()returns the canvas at its place. Submitting with an invalid field on an inert face raisesinvalidon that field. The browser can't focus it, so the page turns the prism to it and focuses it. - Enter in a field on the first face submits the form through the Book button on the third face, which is inert. With later fields empty, that turns the prism to the first face that has one.
- Strips stand in for perspective and for a bend. 28 strips for a face of the prism and 100 for the gauge leave no visible steps at these sizes. During a turn the hit region of a face is that of its last strip, so the faces are inert until the flat draw at the end.
- A
<meter>keeps its colour rules when bent. The slices carry the green, yellow or red that the browser picks fromlow,highandoptimum. Settingvalueoroptimumraisespaint. - Not verified: everything on a phone. In particular the Android time picker that a tap on the time input may open, the on-screen keyboard over the odometer and the prism, where the validation message appears on Android, and how the strips perform on a real GPU.