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
  1. The markup. One option for each channel, with its level in decibels.
  2. Holding a handle. The pointer is captured on the range, and the control works out where the handle goes.
  3. The control. The fader law, a range, a label and an output for each channel, and three form values from one element.
  4. hit(). A draw with no alpha moves an element's hit region.
  5. 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
  1. The markup. The stations are options.
  2. The flywheel and the pull of a station.
  3. 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.

Back squat

After weight on the bar (number input) on ui-feed.dev.

Show the code the markup, the number field and the control
  1. The markup.
  2. The number field. Typing, the two buttons, and a value kept to the step.
  3. 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>.

Heavy linen, ink

After curtain drop (number input) on ui-feed.dev.

Show the code the markup and the control
  1. The markup.
  2. 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>.

Buttercup root
cross-section

After the microscope objective (segmented control) on ui-feed.dev.

Show the code the markup, the turret and the control
  1. The markup.
  2. The turret. When the light is cut, when the picture changes, and the focus coming in.
  3. 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