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
  1. The markup. One date field. The drums are internal.
  2. 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.
  3. 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
  1. The options are the data. data-number is the stop number printed on the blind.
  2. The element. One drawElementImage() of the scroller. Each line is mix-blend-mode: multiply over the lit yellow, so the ink takes the colour of the window.
  3. 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.

Ground

Enquiries, lost property and the staff entrance on the lane side.

First

Fabric, haberdashery and the alterations counter.

Second

Lighting, clocks and the repair desk. Bring the receipt.

Third

Tea room, and the stairs up to the roof garden in summer.

After lift floor indicator (pagination) on ui-feed.dev.

Show the code slotted pages, and a spring that lights the numbers
  1. The pages are the light DOM. data-floor names the floor.
  2. 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.
  3. Measuring the slice. slice() walks offsetTop up 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.

Fore-edge

The edge opposite the spine. Paint it while the leaves are fanned and the picture hides when the book is shut.

Signature

A sheet folded into leaves and sewn through the fold. The numbers in the margin keep the binder honest.

Deckle

The ragged edge a sheet of handmade paper keeps where the pulp met the frame.

Colophon

The note at the end that says who printed it, where, and in what type.

Tipped in

A single leaf pasted to its neighbour along one narrow strip, usually a plate.

After riffle (pagination) on ui-feed.dev.

Show the code a slice of the stack, drawn inside a transform
  1. The pages are the light DOM.
  2. 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