17 ideas from the element catalogue, built
The element catalogue measures what each HTML element does inside a drawable canvas and lists things to build with it. I built the first 17 of them on this page. Each one keeps the real element: the form control still holds the value, the link is still a link, and the canvas only decides where and how the element is drawn. Under each demo is what the element still does without help and what the page had to add.
Findings
- Any descendant with
drawablecan be drawn on its own, with its own hit region. It doesn't have to be a child of the canvas, and its parent doesn't have to be drawn. This works for<option>in a list box,<li>in<ul>and<menu>, and<td>in a table. The parent keeps its job: the select still owns the selection, the table cell is still a cell. The wheel, the cover flow, the radial menu and the table chart are built on this. - Give
drawElementImage()a size when the context is scaled by the device pixel ratio.drawElementImage(el, x, y)draws the element at its size in device pixels. Underctx.scale(devicePixelRatio)that is scaled a second time, so on a phone every element was three times too large until the calls becamedrawElementImage(el, x, y, el.offsetWidth, el.offsetHeight). - A draw with
globalAlpha = 0, or one that is clipped away completely, still moves the hit region. The CRT terminal and the floor plan draw the element in distorted pieces and then make one invisible flat draw to say where input goes. - 2D canvas transforms are affine, and so are the hit regions. Rotation, shear and non-uniform scale all hit test correctly, including a range input turned a quarter turn and
<area>shapes on a rotated, squashed image. A bend (the CRT bulge) has no matching hit region, so input uses the flat rectangle. ctx.filterandglobalCompositeOperationapply to element pictures like any other draw: blur for glow and for a dialog backdrop,destination-inwith a dot pattern for the LED panel.- A range input drawn turned a quarter turn can't be dragged by touch. Mouse drags and taps are mapped through the transform. Touch drags are tested for direction in screen space first, so a vertical drag on a horizontal slider is ignored. It also needs
touch-action: none, or the drag scrolls the page. See Mixing desk. - Don't move the hit region of a focusable element far away to hide it. Focusing it then scrolls the page towards that place.
updateElementGeometry()with a transform that shrinks it to a point inside the canvas avoids the scroll, andinerttakes it out of the Tab order. showPopover()from script doesn't make the invoking button the anchor unless it's passed assource. A tap on apopovertargetbutton does.- Not tested in the cloud browser: the browser's own find-in-page scrolling the view in Find and camera, and whether Chrome on Android gives
<select multiple>a list box to draw in Picker wheel.
Picker wheel from <select multiple>
Twelve <option> elements drawn round a drum. Drag or use the mouse wheel to turn it. Tap an option to select it; with a mouse, Ctrl or Shift selects several, as in any list box.
The select is never drawn, only its options. Selection, the form value, arrow keys and type-ahead are the list box's own. The page adds the turning and moves an option's hit region away when it is on the back of the drum.
Show the code
- Mark each option
drawable. - Draw each option at its angle. The squash is a vertical scale, so the hit region is squashed with it.
- Styles.
Mixing desk from rotated <input type="range">
Five range inputs, laid out horizontally and drawn turned a quarter turn. Drag a fader up and down. The button starts five tones whose volumes are the input values.
With a mouse the browser maps the drag through the rotation: moving up raises the value. With touch, a tap on the track is mapped too, but a drag isn't followed. Chrome tests the direction of a touch drag on screen to tell a slider drag from a scroll, and for a horizontal slider a vertical drag fails that test. A slider drawn at 17 degrees still drags by touch; at 90 degrees the page has to set the value from the pointer.
Show the code
- Five range inputs in the canvas.
- Rotate, then draw.
touch-action: noneon the inputs.- Set the value for touch drags.
Step sequencer from checkboxes
A form with four fieldsets of eight checkboxes. Tap pads to set the pattern, then Play. Clear is a type="reset" button, so it restores the pattern in the markup.
The whole form is one picture and every checkbox in it is hit where it is drawn. The canvas adds the moving column and the rings. The pattern is ordinary form data.
Show the code
- The form. The script adds one fieldset of checkboxes per sound.
- Draw the column behind the form and the rings in front.
- Checkboxes styled as pads.
<textarea> as a CRT terminal
Tap the screen and type. The textarea is drawn in 24 bands to bulge the picture, then again blurred for the glow, with scan lines on top.
The caret, selection and on-screen keyboard are the textarea's. Input uses the flat rectangle, so near the left and right edges a tap lands up to 6 pixels from the bent picture.
Show the code
- A textarea in the canvas.
- Bands, glow, scan lines, then an invisible flat draw for input.
- Styles.
<dialog> in the canvas with a drawn backdrop
Tick some files and press Delete. The dialog is opened with show(), so it stays a child of the canvas. The canvas blurs the page behind it and draws the backdrop.
showModal() puts a dialog in the top layer, which the page paints and the canvas can't draw. With show() the canvas draws it, and the page supplies what modal gives for free: inert on the content behind, Escape, and returning focus.
Show the code
- The page content and the dialog are both children of the canvas.
- Filter the page, draw the backdrop, draw the dialog.
- Opening, closing and focus.
Popover call-outs anchored to drawn buttons
Three buttons drawn on a camera that the canvas moves. Each opens a popover that CSS positions above its button. Tap a number to open or close its call-out.
The popovers are outside the canvas, in the top layer. Anchor positioning uses the place where the button was last drawn, so they follow with no script. position-try-fallbacks flips one below when it would leave the viewport.
Show the code
- Buttons in the canvas, popovers outside it.
- The positioning is two CSS declarations.
- Draw each button at its point on the drawing.
inert on children that aren't drawn
Three panels are children of the canvas and one is drawn. Press Next a few times, then untick the box and try again, with Tab as well if you have a keyboard.
An element that is no longer drawn keeps the hit region of its last draw and stays in the Tab order. Without inert, taps on the empty outlines reach hidden fields and Tab moves focus to controls nobody can see.
Show the code
- Three panels.
- Draw one, set
inerton the rest.
Find-in-page moving a camera
The box shows one of twelve places laid out in a grid three wide. Search for lighthouse, ferry or heron with the form, or with the browser's own find-in-page. Some text is in hidden="until-found" parts: try tide table.
The view is a scroll container. When something scrolls it to a match, the page takes the scroll back and replays it as a flight with a zoom. The form on this page selects the match and scrolls to it, which is tested. The browser's find-in-page should cause the same scroll, and open the until-found parts with a beforematch event, but a script can't open it, so that route is untested here.
Show the code
- Catch the scroll, undo it, fly there.
- A small view over a large grid.
MathML terms moving between lines
Five lines of a solved equation, each a <math> element. Press Next. A term that is in both lines travels from its old place to its new one.
getBoundingClientRect() works on MathML tokens inside a drawn element, which is all the animation needs: each term is the line's picture clipped to that token's box and moved. Between animations the current line is drawn whole, so its text can be selected.
Show the code
- Give the same
data-kto a term in each line it appears in. - Move a clipped piece of the old line and a clipped piece of the new line.
<video controls> on a drawn screen
Press play on the video's own controls. The canvas draws the video three times: blurred and enlarged as light on the wall, upside down as a reflection, and last as the screen.
The native controls and the captions are part of the element's picture, so they appear in the glow and the reflection as well. The controls take taps on the last copy drawn. A playing video fires paint for every frame, so there's no animation loop.
Show the code
- A video with controls and a caption track.
- Three draws under one shear.
Image map on a tilted floor plan
An <img usemap> drawn turned 45 degrees and squashed. Tap a room. Each room is an <area>, and Tab moves between them.
The browser hit tests the <area> rectangles through the rotation and the squash. The click event's target is the area. The raised room is the same image drawn again through a clip.
Show the code
- An image, a map and five areas.
- Draw the floor, raise the chosen room, place the hit region on the floor.
Cover flow from <ul>
Seven list items. Swipe sideways, tap a cover at the side, or use Tab and the arrow keys. The button on the front cover is a real button.
The <ul> isn't drawn. Each <li> has its own picture and its own hit region, so a tap on a side cover arrives at that item with no arithmetic in the page. The list is still a list of seven items to a screen reader.
Show the code
- A list with
drawableon each item. - Draw each item with its own shear, far ones first.
- Styles.
Radial menu from <menu>
Press Tools. The eight items of a <menu> fly out round the button. Arrow keys move between them and Escape closes.
Each button is hit where it lands, part-way through the animation too. While the menu is closed nothing is drawn for it, so the menu is inert.
Show the code
- A button and a menu.
- Place each item on the circle.
- Styles.
Table cells raised into a bar chart
A table of numbers. Each cell is drawn on top of a bar as tall as its number. Tap a number and type a new one. The slider lays the table flat again.
Table cells with drawable stay table cells: the layout is still the table's, and each one can be drawn somewhere else. The hit region of a cell is on top of its bar, so the caret goes where the number is shown.
Show the code
- A table with
drawableon every cell. - Stack the cell's outline, then draw the cell on top.
- Styles.
<ins> and <del> animated as a change
One paragraph with deletions and insertions marked up. Press play, or drag the slider. The deleted words are struck through and fade, the text reflows, and the inserted words are wiped in.
getClientRects() gives one box per line of each <del> and <ins>. The article is drawn once with those boxes clipped out, then once per box with its own alpha or wipe. The markup still says what was deleted and inserted.
Show the code
- The paragraph.
- Draw the unchanged text, then each changed box.
- CSS picks which words are laid out.
An editable <style> element
The text box is a <style> element with display: block and contenteditable. Change a value, for example the colour or rotate. The badge above it restyles as you type.
The browser reparses the sheet on every edit, and both elements fire paint, so there's no editor code. The rules apply to the whole document, which is why this one is scoped to #stylePreview.
Show the code
- A preview and a style element, both drawable.
- Draw both.
- Make the style element visible.
LED ticker from <marquee>
A <marquee> drawn enlarged and cut into dots. The browser scrolls the text. The link in it can be tapped as it goes past.
A marquee fires paint each time it moves, so the canvas has no timer. The dots are a pattern applied with destination-in. The hit region of the marquee and its link is the enlarged picture.
Show the code
- A marquee.
- Draw, mask with dots, add the panel behind and the glow in front.
The shared paint loop
Every demo on this page uses the same 40 lines to size its canvas, listen for paint and stop when it's off screen.
Show the code
mount(),put(),hitOnly()andpark().