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
  1. The select is a child of the canvas. The select and every option have the drawable attribute.
  2. One call.
  3. 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
  1. An input and a select. The options hold markup.
  2. One call.
  3. Inside the library. The suggest layout ranks the rows. open() calls showPicker() 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.

The lock fills in about four minutes. Keep the boat away from the gates while the water rises.

When the levels are equal, the upper gates open with a light push. Close both paddles before you leave.

The next lock is two kilometres upstream.

Show the code strips for the picture, one matrix for the hit box
  1. An article in a canvas.
  2. One call, and the slider.
  3. Inside the library. #strips() draws the picture, matrix() is the plane, and the end of #paint() gives it to updateElementGeometry().

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.

The lock fills in about four minutes. Keep the boat away from the gates while the water rises.

When the levels are equal, the upper gates open with a light push. Close both paddles before you leave.

The next lock is two kilometres upstream.

Show the code a translation written into the rectangles of a range
  1. Each sentence is a span.
  2. The page picks a provider and calls translate().
  3. 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.

The lock fills in about four minutes. Keep the boat away from the gates while the water rises.

When the levels are equal, the upper gates open with a light push. Close both paddles before you leave.

Show the code a pulse per word
  1. An article in a canvas.
  2. One call.
  3. Inside the library. #change() counts words and sentences on selectionchange.

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
  1. Two controls in a canvas.
  2. One call.
  3. 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