Customisable <select> with a canvas-drawn picker
A select with appearance: base-select has a picker made of ordinary HTML. I tested the two ways to combine that
with HTML-in-canvas: a canvas inside the select that draws the options, and a select inside a canvas. Each select below is one
<select> element with real <option> children. Open them with a tap, a click or the keyboard.
Show the code a picker drawn by a canvas
- Write an ordinary select and add
picker="…". The options are filled in by the page script here; they can equally be written in the HTML. The form reads the values as usual. - Load the library. The rest of the page script fills in options and prints the form's values.
- Inside the library: move the options into a drawable canvas inside the select. A select collects options from all its descendants, so they remain its options.
- A layout is a function from an option's index to a matrix. This is the wheel. The fisheye, radial and carousel layouts have the same shape.
- The 2D route: one
drawElementImage()per option. The call draws the option and moves its hit box to the same place, so the browser's own click, hover and focus handling choose options. - The first
paintevent is the "picker opened" signal. A select fires no event when its picker opens. - The carousel layout returns a matrix with perspective.
- The WebGL route: each option is a texture, uploaded with
texElementSubImage2D(). - After a GPU frame,
updateElementGeometry()gives each option its hit box, using the same matrix as the shader. - The WebGPU route uploads with
drawElementImageToTexture(). - Choosing and closing from script. There is no
hidePicker(), so an empty popover is shown to close the picker. - The options are styled with CSS.
- The whole library.
<script type="module" src="/lib/select-picker.js"></script>
<select name="time" picker="wheel">
<option>09:00</option>
<option>09:30</option>
</select>
Three more layouts, and a canvas in the button
The same library with three more layout functions. In the first and last select here the button also contains a canvas: it draws
the <selectedcontent> element, and when the value changes the old value rolls out and the new one rolls in.
Show the code more layouts, and the button
- The markup. A
<canvas>around<selectedcontent>in the button is optional. - The hand of cards rotates each card about a point below the canvas.
- The grid scales and displaces each option by its distance from the pointer.
- The helix is a 3D layout, drawn with WebGPU or WebGL.
rollSelected(select): a canvas in the button. The browser still fills in<selectedcontent>. The canvas keeps the picture of the old value and draws both during the change.- The page script fills in the options and calls
rollSelected().
A canvas inside each option
The card is a form drawn into a canvas with a 2D filter. The select next to it chooses the filter. Each option holds a
small canvas that shows the card as it is now with that option's filter applied, so the previews keep the clock's current time and
whatever is typed in the field. The button shows the chosen option through <selectedcontent>, which clones the
option's canvas element without its pixels, so the page draws the clone as well.
Show the code previews inside options
- The card is a drawable child of one canvas. Each option holds its own small canvas.
- The card's canvas keeps an unfiltered copy of its picture. An element can only be drawn into its own canvas, so the option canvases are given pixels with
drawImage(), each with its ownfilter. <selectedcontent>replaces its clone on every change. A cloned canvas is blank, so the page draws into the new clone when it appears.- The picker is styled with
::picker(select).
A select drawn in a canvas
Here the select is a drawable child of a canvas, and the canvas draws it rotated. The button is hit tested where it is drawn. The picker isn't part of the canvas: the browser paints it in the top layer, upright, anchored to the bounding box of the drawn button.
Show the code a select as a drawable child
- The select sits in a drawable element inside the canvas.
- One rotated
drawElementImage(). Nothing is done for the picker.
Findings
- Options can live in a canvas inside the select. With
appearance: base-select,<select><canvas content="drawable"><option drawable>parses,select.optionsfinds the options, and the canvas draws each one wherever it likes. Click, hover,:checked, arrow keys, typeahead, Enter and Escape are still the select's own. The four pickers above add no keyboard code. - Hit testing follows the drawing. In a 2D canvas,
drawElementImage()under a transform moves the option's hit box with it. With WebGL or WebGPU,updateElementGeometry()accepts the carousel's perspective matrix. Where two options overlap, the one drawn last gets the click.z-indexon the options doesn't change that. - An option that isn't drawn keeps its last hit box. The library moves options that are off the wheel or carousel far outside the canvas. They stay reachable with the arrow keys, and the layout turns to the focused option.
- The select fires no event when its picker opens. No
toggleorbeforetogglereached the select in Chromium 157. The canvas's firstpaintevent is the earliest signal. Before that the options have no paint record and drawing them throws. - There's no
hidePicker().blur(), a synthetic Escape key,option.click()and togglingdisabledorinertall leave the picker open. Showing an emptypopover="auto"element closes it, because the picker is in the same stack. The fisheye and radial pickers use that after a slide-and-lift gesture, because a touch that moved sends no click. Settingappearance: autofor one style recalculation also closes the picker, but if the mouse is over the picker at that moment the select's computedappearancestaysautoafterwards and it becomes the browser's own select. - A press chooses on mouseup, not on click. A mouse drag that ends over an option chooses it. The library captures the pointer on the canvas once a drag passes 7 px, so the release goes to the canvas.
touch-action: nonedidn't stop the page panning. With touch emulation, a vertical drag on the wheel scrolled the page and cancelled the pointer, withtouch-action: noneon the canvas and on the options. The library cancelstouchmoveinside the picker instead.- One tap opens one picker.
showPicker()needs user activation and uses it up, and synthetic clicks and keys don't open a picker. The live checks below therefore share a single test select. - A canvas in the button can draw
<selectedcontent>. With<button><canvas><selectedcontent drawable>the browser still fills in the chosen option on every change, and the canvas draws it. The hand of cards and the helix above keep the pixels of the old value and roll the new one in. - A canvas inside an option works, with two limits. It can draw its own drawable children once the picker is open (while closed there is no paint record).
<selectedcontent>clones the canvas element on every change, and the clone has no pixels and nopainthandler. - A select in a canvas keeps its picker out of the canvas. The picker is top-layer content. It isn't in the picture
drawElementImage(select)draws, and the outer canvas can't draw an option of the open picker either ("No cached paint record"). It opens upright next to the bounding box of the drawn button, whatever transform the button was drawn with. To transform the picker too, put a canvas inside the select as above. - The picker can't animate out. When the select closes, the options stop painting in the same frame, so the canvas has nothing to draw a closing animation with. The opening animation runs because the picker is already open.
- Not verified on real hardware. These results are from Chromium 157 with software rendering, driven by Playwright with mouse and emulated touch. The WebGPU route was checked by rendering to a texture and reading it back, not by showing it. Touch dragging inside the picker on Android Chrome is something I haven't tried on a device.
More select modes to try
- A lens over a long list. Draw the whole list small and put a magnifying shader under the finger, using the lens code from /lens/. The fisheye list above does this with per-option transforms. A shader could bend the text continuously.
- Options as physical objects. Options hang on springs or fall into a pile, using the mesh hit testing from /physical/. Shake the phone to shuffle. It answers whether a choice can be made by throwing an option at a target.
- A map or seat plan as the picker. Each option is drawn at a position on an image: seats in a theatre, pins on a map, keys on a piano. The select keeps the value and the keyboard order.
- A colour select that samples its own pixels. Options are drawn over a gradient, and the picker reads the canvas back to offer the colour under the pointer as a new option.
- A multi-select that shows its history. Keep an
ElementImageof the picker at each change and let the person scrub back, as on /time-scrubber/. - A picker shared with another screen. Stream the picker canvas to a phone as on /remote/, so a select on a television is operated from the phone with the phone's own touch gestures.
- A transition from the option to the button. The chosen card flies from the picker into the button. This needs the option to keep painting after the picker closes, which it doesn't today. A copy of its pixels taken on the last frame would work.
<select multiple>. A list box is in the page, not the top layer, so a canvas could draw it without any picker.- Other controls. /datalist/ draws the suggestions of
<input list>with a canvas, and /inputs/ does the same for date, time and colour pickers.
Live checks
These open small test selects in a panel at the bottom of the window. They need a tap to start because a picker only opens after one.