Five more select pickers drawn by a canvas
Each control below is one <select> with appearance: base-select. Its options have been moved into a canvas
inside its picker, as on /select/, and the canvas does something a list of boxes cannot: it draws the same option
twice, moves options with springs, places them on a plan, reads its own pixels, and keeps the chosen option on screen after the
picker has gone. Open each one with a tap, a click or the keyboard. The values are read from the form in the usual way.
How it works
All five use the select picker library: the options are children of a <canvas content="drawable">
inside the select, and a layout function says where each one is drawn on every frame. The select still owns the value, focus,
arrow keys, typeahead and the form entry. The listings below are the layouts.
Show the code the five layouts
- Five ordinary selects. The page script fills in the options.
- The page script gives each select its layout.
- Lens.
place()draws every option small.over()draws the ones near the pointer again, enlarged, inside a clipped circle. The last draw of an option decides its hit box. - Springs. Each option has a position, a velocity and a swelling. The transform is a translation, a skew and a squash.
- Seat plan. Each option carries its row and its place in the row as data attributes.
- Colour field. A press that hits no option reads a pixel, makes an
<option>and chooses it. - Option into the button. The option's rectangle is copied out of the picker canvas before the picker closes.
- The drawing loop in the picker library that calls these layouts.
- The options' CSS.
Live checks
These open a small test select in a panel at the bottom of the window. They wait for a tap because a picker only opens after one.
Findings
- An option can be drawn more than once in a frame. The lens draws up to 30 options a second time. Both pictures appear. The hit box is where the option was drawn last, so a tap inside the lens reaches the enlarged option and the small copy underneath it takes no clicks.
- Hit testing keeps up with a simulation. The spring layout gives every option a new skewed, squashed transform on every frame, and
elementFromPoint()returns the option at its drawn position while it moves. - Only affine and perspective transforms have a hit box. An option that bends like cloth would need a mesh.
drawElementImage()andupdateElementGeometry()take one matrix for the whole element, so the spring layout skews and squashes each option as a whole. The mesh route on /physical/ maps the pointer by hand. - A select doesn't check
disabledwhen script chooses. A taken seat is a disabled option. The select itself ignores a click on it, but settingoption.selectedfrom script chooses it, so the picker library checksdisabledbefore a slide-and-lift choice. - The picker canvas can be read back.
drawImage()from a 2D canvas that has had options drawn into it withdrawElementImage()into another canvas works, andgetImageData()on that copy gives the pixel. The colour field copies the pressed pixel into a 1 by 1 canvas and reads it there. It 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. It keeps a second canvas with only the gradient in it and reads that one if the copy is refused. - An option added while the picker is open is drawn in the next frame. The new
<option>is appended to the canvas and given thedrawableattribute. It becomes the value and appears in<selectedcontent>like any other. - The chosen option can't be drawn after the picker closes. The options stop painting in the frame the select closes. The pixels already in the picker canvas remain, so the flight copies the option's rectangle from that canvas with
drawImage()into a small canvas shown as a popover. The picture is a still: a hover state or an animation in the option is frozen as it was on the last frame. - Not verified on real hardware. Measured in Chromium 157 without a window, driven by Playwright with a mouse and emulated touch at 412 px. Sliding a finger in these pickers on Android Chrome, and the frame rate of the spring layout on a phone, haven't been tried on a device.