Sliders and number fields with a scale to read
Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas. Each one shows a quantity
the way an instrument would: decibels beside a fader, megahertz behind a needle, pounds as plates on a bar, inches as the length of
a curtain, magnification as the view through a microscope. Under each drawing are native elements: three
<input type="range">, one more range, two <input type="number"> and four radios. I wanted to
know if a range could sit under a handle that the canvas moves, and if one element could submit several values.
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.
Console faders
Three channels of a mixing desk. Each fader is an <input type="range"> placed over its cap. A fader's travel
isn't linear in decibels: the scale is spread out round 0 dB and squeezed toward silence, so the range holds the position of the
cap and the control converts it through the marks printed beside the slot. There's a detent at 0 dB, and a double click puts a
cap there. The names on the tape are <option> children, and the one element submits a value for each
channel.
After the console fader on ui-feed.dev.
Show the code the markup, the handle and the control
- The markup. One option for each channel, with its level in decibels.
- Holding a handle. The pointer is captured on the range, and the control works out where the handle goes.
- The control. The fader law, a range, a label and an output for each channel, and three form values from one element.
hit(). A draw with no alpha moves an element's hit region.- The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Hi-fi tuner
One range from 88 to 108 MHz. The slide has a flywheel behind it: flick it and let go, and it coasts until friction stops it.
Within 0.3 MHz of a station the tuner pulls itself the rest of the way in, the stereo lamp comes on and the station's name
appears. The stations are <option> children. The needle is on its own spring, so it trails the slide a
little.
After the hi-fi tuner (slider) on ui-feed.dev.
Show the code the markup, the flywheel and the control
- The markup. The stations are options.
- The flywheel and the pull of a station.
- The whole control.
Weight on the bar
An <input type="number"> in steps of 5 lb. The control works out the plates for one end of the bar,
heaviest first, and slides each one on. A plate that's no longer needed slides off. The bar dips when a plate lands and
springs back. Tap the big number to type a weight. The list at the right is an <output>, and the title is
the element's own text.
After weight on the bar (number input) on ui-feed.dev.
Show the code the markup, the number field and the control
- The markup.
- The number field. Typing, the two buttons, and a value kept to the step.
- The control. Choosing plates, sliding them on and off, and the dip of the bar.
Curtain drop
An <input type="number"> for the drop of a pair of curtains, in inches from the pole to the hem. The floor
is 97 inches under the pole, so the note says how far the hem is from it, and past 97 the cloth lies on the floor. The hem is on
a spring: the curtains overshoot a new length and the folds sway before they hang still. The price is worked out from the drop
and shown in an <output>.
After curtain drop (number input) on ui-feed.dev.
Show the code the markup and the control
- The markup.
- The control. The note, the yardage and the price, and the folds with their sway.
Microscope objective
Four radios: 4, 10, 40 and 100 times. Choosing one turns the turret a quarter turn for each step. The view goes dark while no
objective is over the slide, then comes back at the new magnification, out of focus at first. The specimen is a drawing of a
few hundred cells with positions in millimetres, so each objective shows more of the same root: at 40 times the starch grains
inside the cells appear. The field of view is an <output>.
After the microscope objective (segmented control) on ui-feed.dev.
Show the code the markup, the turret and the control
- The markup.
- The turret. When the light is cut, when the picture changes, and the focus coming in.
- The whole control. The cells, the radios, the engraved ring and the scale bar.
How it works
Each control is a custom element on the same base class as the machines and the
simulations. Its shadow root holds a canvas with content="drawable", and the native
elements are children of that canvas. The canvas draws the instrument from their state and calls
drawElementImage() for each element: with full alpha where the element should be seen (the tape labels, the red
readouts, the frequency, the list of plates, the price), and with globalAlpha = 0 where only its hit region is
wanted.
A range under a handle isn't asked where the handle is. The range is horizontal and 60 units wide, and the fader cap travels
240 units vertically, so on pointerdown the control captures the pointer on the range and works out the position
from the pointer. The range's own input events are ignored while it's held, and used when they come from the
keyboard. The range has touch-action: none, because that property is read from the drawn element the touch
lands on.
The fader bank is one form control with three values. ElementInternals.setFormValue() takes a
FormData, so the element submits mix-kick, mix-vocal and mix-keys.
The view through the microscope is drawn on a second, plain canvas when the objective changes, and copied onto the control
with drawImage() under ctx.filter = blur() while the focus comes in. All five stop asking for frames
when nothing moves.
Live checks
Findings
- A range can sit under a handle that moves on the other axis. Each fader's range is horizontal, 60 by 44 units, and placed over its cap with a draw with no alpha. The point at the middle of each cap answered that channel's range, and a drag made of pointer events took the first channel to the -20 mark.
- A range under a held handle keeps sending
input. The control ignores those while the pointer is down and uses them when they come from the keyboard. - One element can submit several values.
setFormValue()with aFormDatagave the formmix-kick,mix-vocalandmix-keys. - A focused field that isn't shown still blinks. The number fields have
opacity: 0, and while one has focus its caret raises a paint event at each blink. The barbell counted 1 paint event in 300 ms at rest until the check blurred the field. ctx.filterblurs a copied canvas. The specimen is drawn on a plain canvas and copied withdrawImage()underblur()while the focus comes in. The cells are drawn sharp once, and the blur is applied to the copy.- A fixture below the bottom of the screen has no hit test.
elementFromPoint()answered nothing for the row of radios 348 units down the fixture, and answered the radio at 327. - Not verified: everything on a phone. Dragging a fader and flicking the tuning slide by touch, typing in the hidden number fields with a phone keyboard, how fast the blur draws, and what a screen reader says for each control.