Six more select and selection experiments
Six questions left on the lists at the end of /select/, /datalist/,
/inputs/ and /selection/. Each one keeps the browser's own element, a
<select>, an <input> or selectable text, and has a 2D canvas draw it. Each section says what was
asked of the API and what happened.
A select multiple drawn by a canvas
The fourteen chips are the <option> elements of one <select multiple>. Tap a chip to choose it.
With a mouse, Ctrl and Shift click work as in a list box, and so do the arrow keys with Shift. With the lasso on, drag a loop round
some chips and lift: the chips inside become the selection (by touch, or with Ctrl or Shift held, they're added to it). The canvas
draws a chosen chip larger, turned, with a shadow.
Show the code list box options drawn as chips, and a lasso
- The select is a child of the canvas. The select and every option have the
drawableattribute. - One call.
- Inside the library.
#paint()gives the select a hit box over the whole canvas, then draws each option.choose()applies a lasso.
Rich suggestions from a select with no button
Type in the field, for example le or york. The list is the picker of a customisable
<select> that has no button and no height. Each row is an <option> holding a swatch, a name and
a second line, which a <datalist> option can't hold. The rows that match slide up to their new rank and the rest
fade. The bar behind each row is drawn by the canvas from the option's data-n, here the number of platforms. Arrow Down and Arrow Up move the band,
Enter takes the row, Escape closes the list. A tap on a row takes it.
Show the code an input and a select with no button
- An input and a select. The options hold markup.
- One call.
- Inside the library. The
suggestlayout ranks the rows.open()callsshowPicker()and puts the focus back in the input.
Selection in 3D
The text is on a plane that leans away from you. Select some of it with the mouse, by touch, or with Shift and the arrow keys after a click in it. The selection is the browser's own: the canvas tells it where the plane is with one matrix that has a perspective row. The selected lines are drawn a second time on a slab that rises off the plane, with a shadow under it. The slider turns the plane.
Show the code strips for the picture, one matrix for the hit box
- An article in a canvas.
- One call, and the slider.
- Inside the library.
#strips()draws the picture,matrix()is the plane, and the end of#paint()gives it toupdateElementGeometry().
Translate the selection in place
Select part of the text and press Translate. Every sentence the selection touches is covered by the canvas and its translation is written into the same lines. The article's own text doesn't change: select across a translated sentence and copy, and the clipboard holds the English. If the browser has the Translator API with its model in place, the page uses it. If the model has to be downloaded, your tap starts the download. Until then, and in a browser without the API, the page uses a table of these five sentences that was written in advance, and says so.
Show the code a translation written into the rectangles of a range
- Each sentence is a span.
- The page picks a provider and calls
translate(). - Inside the library.
#paint()asks a Range for the sentence's line rectangles and fills them.
Haptics on selection change
Select text here and extend the selection. Each word that enters or leaves it gives a short pulse, and each sentence a longer one:
navigator.vibrate(8) and navigator.vibrate(24). The canvas draws a ring at the moving end of the selection
for each pulse and keeps a strip of the last 48 along the bottom, so the timing can be seen on a device with no motor.
Show the code a pulse per word
- An article in a canvas.
- One call.
- Inside the library.
#change()counts words and sentences onselectionchange.
Selection effects for form controls
Select some of the text in the field or in the text area: double tap a word, drag, or press Ctrl and A. The selected letters ride a band that moves in a wave. Both controls are real and editable. The browser gives no rectangles for a selection inside a control, so the canvas measures the letters itself, then draws each one again from the control's own picture.
Show the code measuring a selection inside an input and a textarea
- Two controls in a canvas.
- One call.
- Inside the library.
letters()measures,#paint()draws the slices.
How it works
Every canvas on this page has a 2D context. None reads pixels back. An element that is drawn in pieces is drawn with the
nine-argument form of drawElementImage(), which takes a source rectangle. Each draw moves the element's one hit box, so
each experiment ends its frame by putting the hit box where it wants it: with a last draw at no opacity, or with
updateElementGeometry() and a matrix. A demo starts when its section comes within 300 px of the screen.
Live checks
These run on test elements in a panel at the bottom of the window, not on the demos above. They wait for a tap because they open a test picker on top of the page.
Findings
- A canvas inside a
<select multiple>isn't laid out. The parser keeps it, but its box is 0 by 0 anddrawElementImage()of an option throws "No cached paint record", with and withoutappearance: base-select. Putting the select inside the canvas works: an option with thedrawableattribute can be drawn on its own, anywhere. - A click on an option drawn outside the select's own box chooses nothing. Measured with real clicks and emulated taps from a test script, not by a live check:
elementFromPoint()gave the option in some runs and the canvas in others, so it isn't evidence either way. With the select's box covering the canvas, click, Ctrl click, Shift click, dragging, the arrow keys and a touch tap all choose the option that is drawn under the pointer. - A list box option draws its text and its
::before, and no child elements. The swatch on each chip is a::before. The colour of a chosen option is the browser's. showPicker()on a select needs a user activation, and a key press is one. It moves the focus to an option, and the next key goes to the select. Callingfocus()on the input straight after leaves the picker open, and typing carries on.- A source rectangle stays on the hit box. After a nine-argument draw, only the source rectangle of that draw is hit tested, and
updateElementGeometry()moves that cut box without making it whole. A five-argument draw at no opacity makes it whole again. - A 2D canvas can set a hit box in perspective.
updateElementGeometry()takes a 4 by 4 matrix with a perspective row on a canvas with a 2D context. Selecting with the pointer then lands on the letter that is drawn there, although the picture was made from 2 px strips. - Range rectangles under that matrix are bounding boxes. The page measures the selected lines on a second copy of the article that is laid out in the canvas and never drawn. The copy needs the
drawableattribute too:updateElementGeometry(), which moves its hit box out of the way, throws for a child without it. - Text written by the canvas over a sentence doesn't change the selection. The Range, the highlight geometry and the copied text stay those of the original words.
Translator.availability()didn't settle in headless Chromium 157. The promise neither resolved nor rejected, so the page gives it 1.5 s and then uses the table. The API route isn't verified.- A selection inside an
<input>or<textarea>has no Range.measureText()with the input's font gives the letters of an input. A hidden block with the same font and width gives those of a textarea. - Not verified on real hardware. These results are from Chromium 157 driven by Playwright. On Android a
<select multiple>is a button that opens a dialog, so its options may have no picture: the page then draws the labels as text and says so. Vibration, touch selection handles on the leaning text, and the on-screen keyboard with the suggestion list have not been tried.