Scroll areas drawn over scroll containers, a range and details
Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas. These ones are about what happens while something scrolls, resizes or opens: a paint chart whose chips stack up as they leave, a list whose rows turn over beside the thumb you hold, a bank statement with a heading that shrinks, a seismograph split by a bar you drag and the running order of a record. I wanted to know whether a real scroll container, with its wheel, touch and keys, could be drawn in pieces and bent out of shape by the canvas while it carries on scrolling natively.
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.
Paint chart
One <div> that scrolls, with a row for each colour. The canvas draws it twice: as it is from a little
way down, and squashed into the top strip in ten bands, each flatter than the one under it, so the colours that have
scrolled away pile up like chips seen edge on. The wheel, a swipe and the arrow keys all scroll the real container.
The pill on the right is a thumb the canvas draws over an empty <div> that takes the drag.
After the paint chart (scroll area) on ui-feed.dev.
Show the code the markup and the control
- The markup. One option for each colour;
data-darkgives it light text. - The control. The container drawn as it is and in squashed bands, and the thumb.
- The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Dragging the thumb
A scroll container of tracks and a thumb the canvas draws. While you hold the thumb, the rows level with it are drawn
from a second copy of the list with every label on black, so the names you are passing stand out. The copy is
inert and hidden from assistive technology, and it is drawn at the container's scroll position inside a
band that opens on a spring when you press and closes when you let go.
After dragging the thumb (scroll area) on ui-feed.dev.
Show the code the markup and the control
- The markup. One option for each track.
- The control. The list, the copy on black and the band beside the thumb.
Account statement
Each day is a <section> with a position: sticky heading, inside a scroll container.
The big heading of the month is drawn smaller and fainter over the first 120 units of scroll, a one-line heading fades in
in its place, and the container grows into the room it leaves. The balance after each payment is worked out from the
closing balance on the element and written into an <output>.
After the account statement (scroll area) on ui-feed.dev.
Show the code the markup and the control
- The markup. One option for each payment, newest first, with its day.
- The control. The balances, the sticky days and the heading that shrinks.
Seismograph
The bar between the trace and the table of events is an <input type="range">, so it has a value,
a label and Page Up and Down. A range is dragged along its length, so the control turns a drag across it into a value
and swaps Up and Down so the bar moves the way the key points. The table is a scroll container that gets the height
the trace leaves. The canvas draws the trace, a row a minute with the newest at the bottom, the glow and the scan lines.
After the seismograph event list (resizable) on ui-feed.dev.
Show the code the markup and the control
- The markup. One option for each event: the time, the magnitude, the depth and the place.
- The control. The range as a bar, the keys, the trace and the table.
LP running order
A <details> for each side of the record, with a row for each track. Closed, a side is one hatched
bar on the time axis. As it opens, the bar of each track drops out of it to its own row, one after another, and the
side below moves down with the drawn height rather than jumping with the layout.
After the LP running order (waterfall) on ui-feed.dev.
Show the code the markup and the control
- The markup. An optgroup for each side and an option for each track.
- The control. The sides, the axis and the bars that drop.
- A
<details>that closes over time.
How it works
Each control is a custom element on the same base class as the list controls. Its shadow
root holds a canvas with content="drawable", and the native elements are children of that canvas.
Three of the controls are built round a scroll container. The canvas draws its picture where and how it likes, and
the container still scrolls itself: the wheel, a swipe and the keys move scrollTop, the element raises a paint
event, and the canvas draws it again. Where the drawing bends the container (the squashed chips), a last draw with no
alpha at its real size puts the hit region back, so a swipe anywhere on it still scrolls.
A scroll bar inside a canvas isn't drawn, so each one here is drawn by the canvas over an empty <div>
that takes the drag and sets scrollTop. The split in the seismograph is a range input rather than a
<div>, so it has a value and keys of its own.
Live checks
Findings
- A scroll container drawn by the canvas keeps scrolling itself. The wheel, a swipe and the keys move
scrollTop, the container raises a paint event and the canvas draws it again, with no scroll listener needed for the drawing. - The same container can be drawn more than once and bent: the paint chart draws it as it is and again in ten squashed bands. A last draw with no alpha at its real place puts the hit region back, so a swipe over the squashed stack still scrolled it.
- A scroll bar isn't drawn in a canvas, so each thumb here is drawn by the canvas over an empty
<div>that takes the drag. That div needs a height of its own: with none, its hit region never appeared and the first drag did nothing. position: stickyworked inside a scroll container that is a child of the canvas: scrolled 220 units, the day heading at the top edge of the statement was the one for the rows under it.setPointerCapture()throws for a pointer made by a script, and optional chaining doesn't stop it, because the method exists. The thumbs catch it.- The seismograph clock repainted the canvas every frame at first, because the draw wrote the time into the header each time and that change raised a paint event. Writing it only when it changes brought it down to a paint or two a second.
- Swipes, thumb drags and taps worked on an emulated Pixel 7. Nothing here has been run on a real phone, and the fonts of the originals weren't available.