Flaps, blinds and a deck of cards
Four more controls from ui-feed.dev, rebuilt with HTML-in-canvas. In all four the part that moves is a
picture of the control's own element, drawn in pieces: half of a date field's digit squashed towards a hinge, one tall list of
destinations drawn through a slot, a page of text sliding up a lift shaft, and a card rotated in off the edge. None of them is a picture
of a picture. Each piece is a fresh drawElementImage() of a live element, which is also what puts its hit region under your
finger. I wanted to know how far an element's picture can be cut up and transformed before the control underneath stops working.
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 form control 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.
Split-flap date
One <input type="date">, with its month, day and year shown on three drums. Change the date and each drum whose
digits changed turns: the top half of the old number falls to the hinge, then the new number rises from it. Both halves are the same
<span> drawn twice, clipped and squashed, with its text swapped at the halfway point.
After tumble calendar (date input) on ui-feed.dev.
Show the code a flap is half a picture, squashed
- The markup. One date field. The drums are internal.
- The element. The still halves are two clipped draws of the span; the turning one is a third, inside a scale on the y axis with the hinge as the origin.
- The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Destination blind
The roller blind in the front window of a bus, and the original is a scroll area, so this one is a native scroller: a 150 px box
with a line for each stop and scroll-snap-type: y mandatory. The canvas draws that element's own picture, which already
holds its scroll position, so dragging the blind, flinging it, the snap at the end and the arrow keys are all the browser's. The
chosen stop is the snapped line, and the element reports it to the form.
After destination blind (scroll area) on ui-feed.dev.
Show the code one tall element drawn through a window
- The options are the data.
data-numberis the stop number printed on the blind. - The element. One
drawElementImage()of the scroller. Each line ismix-blend-mode: multiplyover the lit yellow, so the ink takes the colour of the window. - Drawing and hit regions.
hit()places an element without painting it.
Lift floor indicator
Pagination drawn as the panel above a lift door. The pages are the element's own children, one <section> each,
reached through a <slot> inside the canvas. The floor number lights as the lift arrives, the arrow points the way
the spring is travelling, and the page slides into the card from that direction.
After lift floor indicator (pagination) on ui-feed.dev.
Show the code slotted pages, and a spring that lights the numbers
- The pages are the light DOM.
data-floornames the floor. - The element. The lit amount of each number is one minus its distance from the spring, so two numbers glow while the lift is between floors.
- Measuring the slice.
slice()walksoffsetTopup to the element that holds the slot.
Riffled deck
The same idea again, drawn as a deck of cards. The card on top is the chosen <section>, drawn inside a rotate and
a translate that run down to nothing over half a second. The cards behind it are plain shapes, not pictures. What is drawn is one slice
of the whole stack of pages, clipped to the card, because a slotted page has no picture of its own. The third card has a checkbox on
it, and it ticks where the transform put it.
After riffle (pagination) on ui-feed.dev.
Show the code a slice of the stack, drawn inside a transform
- The pages are the light DOM.
- The element. The numbered buttons are one internal
<nav>, drawn whole, so one draw places every button's hit region.
How it works
All four extend FeedControl in /lib/ui-feed/kit.js, the base class the
dot and tube displays, the switches and the
glass controls use. A drawable canvas sits in the shadow root with the native elements inside it, and the
paint handler draws them in the places the control wants them.
What is new here is cutting one element's picture up. A drawElementImage() inside a clip gives half a drum; the same call
inside ctx.scale(1, k) around the hinge gives that half falling. The browser paints the element once and both draws are of
that paint, so the digits are the page's own text in the page's font, at whatever size the canvas is. Two of the controls get their pages
from a <slot> inside the canvas, which is where author data belongs; the drums, the numbered buttons and the step
buttons are internal, because an author has nothing to say about them.
The pages took a detour. Only an element with the drawable attribute can be passed to
drawElementImage(), and a page that arrives through a slot has not got it. Setting it on each <section>
gave the page a hit region where the canvas drew it, but the picture itself stayed empty, whenever I set the attribute. What works is to
draw the <div> the slot sits in, which is a child of the canvas and does have the attribute: that draws the whole
stack of pages, so it is offset by the height of the pages above the one that is wanted and clipped to the card. The elements on the
page are hit at the place that slice put them, which is how the checkbox on the third card works.
The one trap worth repeating: the slice is measured with offsetTop, not with
getBoundingClientRect(). The rectangle of an element the canvas has drawn reports the draw, in device pixels, so feeding it
back as the next draw's size grows the picture by the device scale every frame. Nothing is read back either: no
getImageData(), no toDataURL().
Live checks
Findings
- Half an element's picture, squashed, is enough for a flap. Two clipped draws for the still halves and a third inside a
scale(1, k)for the moving one. Swapping the element'stextContentat the halfway point is what makes the old number fall and the new one rise, and the swap costs one extra paint. - A native scroller inside a drawable canvas is scrolled through the canvas. A wheel over the place the canvas drew the
blind scrolled the element inside it, the snap settled it on a line (
scrollTop216 for the fourth of five 72 px lines), and each scroll raised apaintevent, so the drawing kept up without a frame loop. The control readsscrollTopfor the stop and reports it withElementInternals, because a scroll area has no value of its own. - Only a
drawableelement can be passed todrawElementImage(). Chromium 157 throwsDescendants passed to DrawElementImage must have the 'drawable' attribute
for anything else, which is why the lift's two call buttons are drawn as the one<nav>that holds them rather than one at a time. The base class puts the attribute on the canvas's own children, so this only bites for elements deeper in, and for author content arriving through a slot. - Putting
drawableon a slotted element gave it a hit region but still no picture. Points over the drawn card began answering the<section>instead of the canvas, but the draw itself stayed empty on every frame I tried, with the attribute set at startup, a frame later, and toggled off and on. So both pagers draw the page as a slice of the drawable<div>the slot sits in: the whole stack of pages, offset so that the one that is wanted lands in the card, and clipped to it. - The elements inside a drawn picture are hit where that picture put them, which is what makes the slice work. The
<nav>is drawn whole and each of its buttons is clickable; the third card of the deck is rotated, translated and scaled on the way in, and the checkbox on it ticks at the place the transform put it. getBoundingClientRect()on an element the canvas has drawn reports where the draw put it, in device pixels. I measured the pages that way at first, and every frame fed that number back as the next draw's size: the card grew by a factor of about 2.6 a frame until it saturated at nine times its real size.offsetTop,offsetLeftandoffsetWidthreport the layout and are not affected, so the slice is measured with those.- The content of a drawn scroller is hit at the place the picture put it. The middle of the drawn window answers the
<span>of the line that is showing there, not the scroller and not the canvas. - Rebuilding the numbered buttons on every page change loses focus, because the focused button is removed. The riffle control
remembers which button had focus and puts it back after it writes the new
<nav>, falling back to the current page's button when the old one is gone. - Not verified: everything on a phone. Whether tapping the blind opens the native select picker on Chrome on Android, whether the flap turn holds 60 frames a second there, and whether a finger drags the blind itself rather than the page. A mouse wheel scrolls it in the test browser, but a touch drag on a scroller inside a drawable canvas has not been tried on a phone.