Five more input pickers
Five controls, each on a real <input> element that keeps its value, its form entry, its input and
change events and its keyboard handling, with a canvas doing the drawing. They're the ideas listed at the end of
/inputs/. Each one asks a separate question of the API: drawing one element in slices, drawing under a set of
buttons, reading pixels back from a canvas that draws elements, magnifying buttons, and drawing a datalist option inside an input.
A range input bent round a dial
The track, the thumb and the tick marks are the browser's own picture of <input type="range">. The canvas draws that
picture in slices 3 px wide, each under its own rotation. Drag round the arc, or focus the dial with Tab and use the arrow keys, Home
and End. The number in the middle is an <output> element drawn by the same canvas.
Show the code a range input drawn in slices round an arc
- The input is a child of the canvas. Its datalist gives it tick marks.
- One call.
- Inside the library.
#paint()draws the slices with the nine-argument form ofdrawElementImage().valueAt()turns the pointer's angle into a value.
A date range on one calendar
Two <input type="date"> elements share one calendar. Tap either field, tap the first day, then the last day. With a
mouse the band follows the pointer between the two taps. By touch, press on the first day, slide to the last and lift. The band and
the two discs are drawn by the canvas under the day buttons. On a keyboard, press Alt and Arrow Down in a field, move with the arrow
keys and press Enter on each day.
Show the code two date inputs and one calendar
- Two ordinary date inputs.
- One picker for both. The popover opens next to the element that holds them.
- Inside the library: open the popover in place of the browser's picker.
- The band. Two day numbers move towards the chosen days a little each frame, and the canvas fills the cells between them.
- The whole picker.
begin(),preview()andfinish()are the three steps of a choice. - The shared part that moves and draws the buttons.
A colour wheel that reads its own pixels
Tap the swatch. The wheel is two canvas gradients. Press on it and the pixel under the pointer is copied into a 1 by 1 canvas with
drawImage(), read there with getImageData() and written to the input. During a drag the colour is worked out from the position, and the pixel is read again on release, so the canvas isn't read on every frame. The colours of the input's <datalist> are real buttons, each
drawn on the wheel where its own colour is. The brightness slider is a real range input drawn by the same canvas.
Show the code a wheel, swatch buttons and a pixel read
- A colour input with a datalist.
- One call.
- Draw the wheel, then read one pixel of it. The read is in the same canvas that draws the buttons.
- The whole picker.
spot()gives the place of a colour, which is where its swatch button is drawn.
A week picker on a year view
Tap the field. Every week of the year is a button, drawn at about half size in rows of thirteen. The weeks near the pointer or finger are pushed apart and drawn larger. Slide over the weeks and lift to choose, or tap one. The arrows step the year. A year has 52 or 53 weeks: 2026 has 53.
Show the code 53 week buttons under a fisheye
- An ordinary week input.
- One call.
- Inside the library.
base()is the small grid andplace()applies the fisheye to it.
Ghost text from a datalist
Type the start of a station name, for example Le or new. The rest of the first matching
<option> is drawn after the caret at reduced opacity. Tab, Arrow Right or End accepts it, and so does a tap on the
dimmed text, because the option is hit tested where it is drawn. Escape hides it. The input and the options are children of one
canvas, which draws the input and then part of one option.
Show the code an option drawn at the caret
- The input and its datalist are children of the canvas.
- One call.
- Inside the library.
#paint()measures the typed text withmeasureText()and draws the option from that point with the nine-argument form ofdrawElementImage().
Live checks
These run on test inputs in a panel at the bottom of the window, not on the fields above. They wait for a tap because they open test pickers on top of the page.
Findings
- One element can be drawn many times in one frame, in pieces.
drawElementImage()has a nine-argument form with a source rectangle, in CSS pixels of the element. The dial calls it about 134 times a frame. A clip with the five-argument form gives the same picture. - The tick marks of a range input's datalist bend with it. They are part of the input's picture.
- The hit box doesn't bend. Each draw moves the element's one hit box, so after the slices it sits where the last slice was drawn. The dial gives the input
pointer-events: none, maps the pointer's angle to a value itself, and finishes with one draw at no opacity that leaves the box over the whole dial. - The focus ring isn't in the picture. An outline outside the element's border box isn't drawn by
drawElementImage(). The dial draws its own ring, and the text input uses an outline with a negative offset. - A canvas that draws elements can be read back.
drawImage()from it into a second canvas works, andgetImageData()on that copy gives the pixel. The canvas isn't tainted afterdrawElementImage()of same-origin content. The page reads through a 1 by 1 copy and never callsgetImageData()on the drawable canvas, because on an Android phone a drawable 2D canvas made withwillReadFrequently: trueand then read lost every 2D context on the page. The colour wheel reads the pixel after drawing the wheel and before drawing the buttons, so a swatch or the marker is never read by mistake. - A datalist option can be drawn inside the picture of an input. The source rectangle form draws the option from the end of the typed letters onwards. With the source rectangle, only the drawn part of the option is hit tested, so a tap on the ghost text reaches the option and a tap on the typed text reaches the input.
- Not verified on real hardware. These results are from Chromium 157 driven by Playwright with a mouse and emulated touch. Touch on a phone, the on-screen keyboard with the ghost text, and whether the read-only step stops the Android date and week dialogs haven't been tried.