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
  1. The markup.
  2. 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.
  3. The control. One <details> for each option, and the draw.
  4. put() and hit(). A draw places an element's hit region, with or without alpha.
  5. 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
  1. The markup. One option for each leg.
  2. The map. The height function, the contour lines, the path and the scale.
  3. 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
  1. The markup.
  2. The pictures. From a row to a column, each one a little after the one before.
  3. 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
  1. The markup. Each departure is four figures, hours then minutes.
  2. 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
  1. The markup.
  2. Filling a box with an element's ink. Where the glyphs are inside the output, and the draw that stretches them.
  3. 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