Meters, progress and ranges drawn as readings

Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas. Each one shows a reading: the phase of the moon on each day of a month, ounces poured into a cup, gigabytes used on a phone, the temperature at a time of day, and the stops within a walk. Under each drawing are native elements: a <table> of buttons, a <progress>, six <meter>s and two <input type="range">. I wanted to know if one draw of a table would place all of its buttons, and if a range could have its handle on a circle.

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.

Lunar month

A month as a <table> with a <button> for each day. The table is drawn once as an element, which gives the day numbers, the names of the quarters and every button's hit region. The canvas draws a moon in each cell from the age of the moon on that day, counted from the new moon of 6 January 2000. Change the month and the moons carry on from where they were and run through their phases to the new dates.

After the lunar month (calendar) on ui-feed.dev.

Show the code the markup, the phase and the control
  1. The markup.
  2. The phase of the moon. The age as a fraction of the cycle, which day a quarter falls on, and the shape of the lit part.
  3. The control. The table of buttons, the arrow keys, 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.

Measured fill

A <progress> and one <button>. Press the knob and a valve opens: the flow builds up, runs at 1.7 oz a second, and is throttled over the last half ounce, so the count slows into the last tenth. Press again to stop part way, and press when it's full to empty it. The display is drawn as liquid crystal, with the unlit segments faintly visible. The text under "Fill" is the element's own.

8 oz cup

After measured fill (progress) on ui-feed.dev.

Show the code the markup, the valve and the control
  1. The markup.
  2. The valve. The flow, the throttle near full, and the end.
  3. The whole control. Seven-segment figures, the bar of 32 cells and the knob.

Storage freed

A <meter> for each kind of file and one for the total. The rows are real elements drawn as pictures, with the meter restyled. The button clears one kind. Each amount is on a spring and the total is their sum, so the figure counts down and the strip of marks goes out a mark at a time. The kinds are <option> children, and the one with data-clear gets the button.

After storage freed (meter) on ui-feed.dev.

Show the code the markup and the control
  1. The markup. One option for each kind of file, in gigabytes.
  2. The control. A row with a meter for each option, the springs, and the strip of marks.

Forecast

One range over the hours the forecast covers. Each <option> is a time with a temperature, and the big figure is the temperature between them at the time under the thumb, on a smooth curve through the forecasts. The figure is an <output> and it's on a spring, so it trails a fast drag. The sky goes dark outside the hours between sunrise and sunset.

Lisbon

After the forecast slider on ui-feed.dev.

Show the code the markup, the curve and the control
  1. The markup. The forecasts are options.
  2. The temperature between two forecasts.
  3. The whole control.

Walk to a stop

One range, in minutes of walking. Its handle rides on a circle on a street map, and the radius is how far you get in that time at 80 metres a minute. Drag the handle away from the centre or toward it. Stops inside the circle turn solid and are counted along the bottom of the circle. The map is made up: a grid of streets from a fixed seed, drawn twice on two plain canvases, one for outside the circle and one for inside.

Piazza Solferino

After walk to stop (slider) on ui-feed.dev.

Show the code the markup and the control
  1. The markup.
  2. The control. The made-up city, the range on the circle, the two maps and the lettering round the edge.

How it works

Each control is a custom element on the same base class as the sliders and number fields. Its shadow root holds a canvas with content="drawable", and the native elements are children of that canvas. The canvas draws the reading from their state and calls drawElementImage() for each element: with full alpha where the element should be seen (the table, the rows of meters, the button, the temperature), and with globalAlpha = 0 where only its hit region is wanted.

The calendar draws its <table> once. That one draw places 35 buttons, because the hit region of a drawn element covers its descendants where they were laid out. The moons are drawn by the canvas over the picture of the table, at positions worked out from the size of a cell.

A <progress> or a <meter> takes no input, so nothing needs to reach it. It's there to hold the value for assistive technology and for the plain fallback. The small meters in the storage rows are drawn as they are, restyled with ::-webkit-meter-bar and ::-webkit-meter-optimum-value. The progress bar and the total are placed with no alpha under a strip the canvas draws.

The range on the map is moved by the pointer's distance from the centre of the circle, with the pointer captured on the range. All five stop asking for frames when nothing moves.

Live checks

Findings