Accordions and tabs drawn while they open
Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas. Each one shows and hides
content: two accordions, an order summary that expands, and two sets of tabs. Under the drawings are
<details> elements and <input type="radio">s. I wanted to know if a canvas could draw a
<details> closing, when a closed one has no content left to draw, and how far the picture of an element can
be stretched before it looks wrong.
Credit: every control on this page is a rebuild of a demo on ui-feed.dev, and each section links to the original it came from. The originals are far higher fidelity than these, so go and look at them. These are examples of what HTML-in-canvas makes possible with a native element underneath, not replacements. I worked from the published demos rather than their source: I looked at what each one does, worked out the mechanism and built it again. The designs and the ideas are theirs.
Fragrance notes
Three <details>, one open at a time. The canvas draws each one as a picture and clips it to as much of
its notes as are open so far, so a section slides shut while the next one opens. The plus sign and the rules are drawn by the
canvas. Tap the open section to close it. The sections are <option> children, with the notes in
data-notes. The bottle is drawn once, on a plain canvas, and copied in each frame.
After fragrance notes (accordion) on ui-feed.dev.
Show the code the markup, the fold and the control
- The markup.
- A
<details>that closes over time. The click on the summary is cancelled, and the element closes when the canvas has finished drawing it shut. - The control. One
<details>for each option, and the draw. put()andhit(). A draw places an element's hit region, with or without alpha.- The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Coast path map
Five legs of a walk, each a <details>, and one is always open. Tap a leg and its map unfolds on four
panels of paper while the map above or below it folds away. A red band crosses the heading of the open leg, and the heading
is drawn a second time in white where the band has reached. The maps are made up: a height for every point, contour lines
found with marching squares, woods, a shore and a path with a tick at each kilometre. Each <option> is a leg.
After the coast path map (accordion) on ui-feed.dev.
Show the code the markup, the map and the control
- The markup. One option for each leg.
- The map. The height function, the contour lines, the path and the scale.
- The control. The headings, the red band and the four panels.
Order summary
One <details>. Closed, it shows the total and a row of pictures. Open, each picture goes to the start of
its own line in a list, one after another, and the names and prices come in after them. The list, the prices and the totals
are the element's own content, drawn as its picture. The three pictures are drawn by the canvas. The sums come from the
<option> children and the shipping and vat attributes.
After the order summary (collapsible) on ui-feed.dev.
Show the code the markup, the pictures and the control
- The markup.
- The pictures. From a row to a column, each one a little after the one before.
- The whole control.
Bus stop timetable
Three radios for the days and a <dl> of departures for each. The paper tab slides to the checked day, and
the day names are drawn twice: white on the black strip, and inverted with a filter where the paper tab is. The timetables
are laid out side by side by the canvas and slide across. All three stay laid out, and the two that aren't checked are
inert. Each <option> is a day, with its departures in data-times.
After the bus stop timetable (tabs) on ui-feed.dev.
Show the code the markup and the control
- The markup. Each departure is four figures, hours then minutes.
- The control. The radios, a list for each day, and the draw.
Kinetic poster
Four radios and an <output>. The figure is the output's own text, set 240 pixels high, and its picture is
stretched so that the ink of the figures fills the poster from edge to edge whatever the number is. When the period changes,
the old figure is pressed flat against the top while the new one grows from the bottom. The names of the tabs are labels,
drawn at 0.62 of their width to make a condensed face out of an ordinary one.
After the kinetic poster (tabs) on ui-feed.dev.
Show the code the markup, the stretch and the control
- The markup.
- Filling a box with an element's ink. Where the glyphs are inside the output, and the draw that stretches them.
- The control. The radios, the two outputs and the tab widths.
How it works
Each control is a custom element on the same base class as the meters and ranges. Its
shadow root holds a canvas with content="drawable", and the native elements are children of that canvas. The canvas
calls drawElementImage() for each one, which draws its picture and puts its hit region in the same place.
A <details> is hard to animate from a picture, because a closed one has no content in its picture. Opening
is easy: the element opens at once and the canvas clips its picture to a growing height. For closing, the control cancels the
click on the <summary>, draws less of the picture each frame, and removes the open attribute
when nothing of the content is left. The form value changes at the click. For about a third of a second the element is open
while the control says it's closed. If something else opens or closes the element, its toggle event tells the
control and the drawing jumps to match.
Each <details> is drawn whole inside a clip, so its hit region is larger than what you see of it. The
sections are drawn from the top down, and where two hit regions overlap the element drawn last answers, so the heading below
takes the taps over the hidden part.
The tabs are radios with no alpha under drawn labels. The bus timetables are three <dl>s drawn side by
side at an offset that follows a spring. The poster's figure is one drawElementImage() with a width and height
worked out from where the ink of the text is, measured with measureText() on a second context with the output's
computed font. All five stop asking for frames when nothing moves.
Live checks
Findings
- A canvas can draw a
<details>closing if the element stays open until the drawing is done. 140 ms after a click the first section was drawn about half open and still had itsopenattribute. It lost it when the drawing reached zero. - A paint can come before the
toggleevent. When a script setopenon a closed section, the canvas painted first, and my first version saw an open element it was drawing shut and closed it again. The control now remembers the state it last gave the element. - The hidden part of a clipped element still has a hit region, and the element drawn after it wins. The point 30 units under the drawn edge of a half-closed section answered the heading below it.
- An element wider than its canvas is drawn whole. The output with "58,204" is 902 by 240 CSS pixels inside a control 360 wide. Drawn at 0.36 of its width and 1.10 of its height, the figures were sharp in the test browser.
- A filter turns one picture into two colours. The headings of the walk and the names of the days are each drawn
twice from the same element, the second time inside a clip with
brightness(0) invert(1)orinvert(1). inertelements are still drawn. The two timetables that aren't checked are inert, they slide past during a change, and the point in the panel answered only the checked day's list.- The bottle and the pictures of the order are far from the originals. Theirs look like photographs. Mine are gradients and paths drawn on a plain canvas.
- Not verified: everything on a phone, what a screen reader says while a section is open in the element and closed in the drawing, find in page opening a section, and the fonts. The originals use condensed and Didone faces, and the test browser had neither.