Split-flap output element
A departure board made of six <output> elements, drawn by a canvas. The form under the canvas sets their values. When a value changes, the canvas flips each character that differs
to its new one, card by card. Type a message, move the delay slider or press Next departure. The Flip menu switches between stepping through the alphabet and turning straight from the old picture to the new one.
Show the code output values read on paint, flaps drawn from pieces of elements
- The board. Six
<output>elements in a drawable section. They belong to the form through theformattribute. - The form, outside the canvas.
- The form sets values. Nothing here talks to the canvas.
- The styles. Fixed-pitch text, and the drum.
- On
paint, find what changed. Compare each output's value with the characters the flaps show. - The faces of a flap. A character from the drum, from the live board or from a snapshot of the board.
- One flap part way through a flip.
- Drawing a piece of an element with the nine-argument
drawElementImage().
How it works
Setting output.value changes the element's text, and the browser fires paint on the canvas. The paint handler reads the value of each output and compares it, character by character,
with what its flaps show. That comparison is the only link between the form and the board. The canvas code has no input listener and no observer.
Each character has a box, measured with a Range. The canvas draws a card behind every box, then the section itself, so a settled character is the browser's own text and can be selected.
A flap in motion is drawn from pieces of elements with the nine-argument form of drawElementImage(), which copies a source rectangle of an element's picture to a destination rectangle.
In the first mode a flap steps through the characters of a drum: a line of text in the same font that holds every character a flap can show. The drum is a second drawable child of the canvas. It's never drawn whole:
its characters are drawn one at a time. There's one drum for each style of output, so the yellow row flips in yellow. In the second mode the old face of a flap is a piece of a snapshot taken
with captureElementImage() before the change, and the new face is a piece of the live section. That mode needs no drum and works for any character.
Live checks
These run on their own fixtures in the corner of the window, after a tap on the button.
Findings
- Setting
output.valuefirespaintwith no call from the page. A paint handler can treat the event as "something changed" and read the values itself. drawElementImage(source, sx, sy, sw, sh, dx, dy, dw, dh)works for an element and for anElementImagesnapshot. The source rectangle is in CSS pixels of the element.- A snapshot from
captureElementImage()keeps the old value after the output changes, so one element can be the old face and the new face of a flap.captureElementImage()also works outside a paint handler. - The drum is a direct child of the canvas for a reason. As a
drawableelement nested in the section, its picture held only the characters that were inside the window, and which ones changed with the place the drum was last drawn. Flaps then showed blank cards for the rest. A direct child of the canvas is recorded whole. Keycaps drawn from kbd has a live check for this. - An
<output form="…">inside the canvas is inform.elementsof a form outside it. - With
letter-spacing, the box of a character includes the space after it, and the glyph is at the left of the box. The cards are shifted left by half the spacing to centre the glyphs. - Not verified: Chrome 159 for Android, and whether a screen reader announces the new value once (an
<output>is a live region). Turn on TalkBack, press Next departure and listen for one announcement per row.