Controls drawn over page buttons, a fieldset of checkboxes, a menubar and a file input

Four more controls from ui-feed.dev, rebuilt with HTML-in-canvas: a pagination marker, a road sign with things to avoid, a menubar over a workshop's ledger and a T-shirt you drop artwork onto. Three of them have a marker that slides over native buttons and shows the text under it in another colour. I wanted to know if the canvas could do that with the buttons' own pictures, with no second copy of the text.

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. The place names, the ledger, the sample artwork and the T-shirt here are my own and painted by the canvas.

Pagination

The pages are <button>s in a <nav>, and the one you're on has aria-current="page". The red marker has two edges on springs. The edge on the side you're going is stiff and the other is soft, so the marker stretches across the pages in between and then catches up. Under the marker the canvas draws the same <nav> again with a filter that turns its black ink into the paper colour.

After the pagination whose marker travels (pagination) on ui-feed.dev.

Show the code the markup and the control
  1. The markup. How many pages, and the one to start on.
  2. The control. The buttons, the two springs and the second draw under the marker.
  3. The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.

Route avoidances

A road sign. The keys under "Avoid" are a <fieldset> of four <input type="checkbox">s, each stretched over its <label>. When you check one, the canvas wipes the key yellow and strikes through its pictogram, and draws the quickest route that avoids everything checked, from the bottom of the map to the arrow. The time is an <output> drawn as amber dots, and the old time scrolls up out of the board as the new one comes in.

After the route avoidances sign (toggle group) on ui-feed.dev.

Show the code the markup and the control
  1. The markup. Where to and the time now. avoid="tolls" starts with keys checked.
  2. The control. The fieldset, the route chosen from what is checked, and the board of dots.

Workshop menubar

A role="menubar" over a bike workshop's ledger, which is a <table>. The menus hold buttons with role="menuitem", an <input type="checkbox"> with role="menuitemcheckbox" and radios with role="menuitemradio", so the settings are real form state. The keys are a menubar's: Left and Right between menus, Up and Down in a menu, Right into a submenu, Escape out, and a letter jumps to an item. The blue bar is drawn under the focused item and slides to the next one, and the item's text is drawn white where the bar is. "Post job card" adds a row and Undo (or Ctrl+Z) takes it away. "Show VAT column" narrows the column a frame at a time.

After the workshop ledger menubar (menubar) on ui-feed.dev.

Show the code the markup and the control
  1. The markup. One option for each row of the ledger. data-unpaid marks the ones "Unpaid only" keeps.
  2. The control. The menus, their keys, the commands and the bars.

T-shirt print

An <input type="file" accept="image/png"> covers the whole picture and is drawn with no alpha, so a click or a dropped file anywhere on the shirt lands on it, and "Browse files" is a <label> for it. A file dragged over it changes the headline. A PNG at least 1200 pixels on its short side is printed onto the shirt in one blue ink at its size in inches at 300 dpi, with rulers and the number of colours it has. A smaller one or a file that isn't a PNG gets the reason in the headline, which comes in with its two inks out of register. Use the sample if you have no PNG to hand.

After the printed T-shirt artwork drop (file input) on ui-feed.dev.

Show the code the markup and the control
  1. The markup. The smallest side in pixels it accepts, and the dots per inch it prints at.
  2. The control. The file input, the checks on the file and the print.

How it works

Each control is a custom element on the same base class as the scene controls. Its shadow root holds a canvas with content="drawable", and the native elements are children of that canvas.

The markers on the pagination, the menubar and the menus are drawn the same way. The canvas draws the element (the <nav>, the menubar or the open menu) once as it is. Then it clips to the marker's rectangle, fills the marker's colour and draws the same element again with ctx.filter set to brightness(0) invert(1), which makes every pixel of its ink white. The text under the marker changes colour as the marker moves, part of a letter at a time, and the element's hit region stays where it was.

The yellow wipe on the road sign's keys is the same idea with the pictograms, which the canvas draws itself: once in white and once in black inside the wipe.

Live checks

Findings