Form controls drawn as simulations

Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas. In each of them the drawing is a small simulation: air squeezed in a pump, a wave on a string, flames that light the candles beside them, a probe pulled into orbit, a ball that bounces across a calendar. Under each drawing is the native element for the job: an <input type="number">, radios, a <select size>, a table of buttons. I wanted to know if the model could run in the canvas's paint handler while the element stays the thing that is tapped, typed in, moved with the arrow keys and submitted.

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.

Hand pump

The value is the pressure in a tyre, and the field in the dial is an <input type="number"> you can type in. Drag the handle down and up to pump. The cylinder is modelled as a volume of air: pushing the piston down raises its pressure, and only when that passes the pressure in the tyre does the check valve lift and let air through. So the first part of each stroke moves nothing, and a stroke adds less the fuller the tyre is. Hold the black cap on the pipe to let air out. The handle is an <input type="range">, so with the keyboard the arrow keys, Home and End pump it.

After hand pump (number input) on ui-feed.dev.

Show the code the markup, the gas step and the drawing
  1. The markup. The attributes are the ones a number input has.
  2. The gas. Boyle's law for the cylinder, one shared pressure while the valve is open, and the release.
  3. The whole control. The five native elements, the drag on the handle, the gauge and the cut-away pump.
  4. The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.

Plucked strings

A radio group. Each option is an <option> child of the element, and its data-weight sets both the weight of its label and how heavy its string is. Choosing one plucks its string where you tapped it. The string is 40 points joined by tension with both ends fixed, so the kink you made runs to the ends, reflects and comes back. The tension is the same for all six and the heavy ones have more mass, so a wave crosses Black about two and a half times slower than Thin. Drawing a pointer across the strings strums them without choosing any.

After plucked strings (radio group) on ui-feed.dev.

Show the code the markup, the wave step and the control
  1. The options are light DOM. The element makes one radio and one label for each.
  2. The string. A pluck is a triangle added to the string. The step is the wave equation with a little loss.
  3. The whole control. Radios and labels are direct children of the canvas. Each radio's hit region covers its ring and its string.

Candles

A rating out of five, which is five radios. The canvas lights that many candles. Each candle is shaded from its own flame and from the flames either side, with the light falling off with the square of the distance, so an unlit candle next to a lit one is bright on the side facing it. Raise the rating and a match is carried along the row. Lower it and the others go out with a red wick and a second and a half of smoke.

How was the evening?

After candles (rating) on ui-feed.dev.

Show the code the markup, a flame and the control
  1. The question is the element's text. It is slotted into a span inside the canvas and drawn from there.
  2. A flame. Two teardrops and a halo, leaning on two sine waves.
  3. The whole control. The match, the light on each candle, the smoke, and a tall hit region for each radio.

Orbital capture

The list on the right is a <select size="6">, drawn as the browser painted it, so the arrow keys and typing a first letter work. The field on the left has a gravity well level with each row and a probe in orbit round the chosen one. Choose another and the probe is steered toward the speed a circular orbit would have at its distance from the new well, with a pull inward that fades as it arrives. It leaves on a tangent, curves across the field and is wound in, does one lap and parks, and the control stops drawing. The contour lines are the potential of the wells, traced once with marching squares.

After orbital capture (listbox) on ui-feed.dev.

Show the code the markup, the steering and the control
  1. The rows are options. data-note is the second line, which the canvas writes over the picture of the select.
  2. The steering. One step of the probe.
  3. The whole control. The select and its styles, the wells, the contours and the trail.

Bouncing ball

One month as a <table> with a <button> for each day. The canvas draws the table as it is, a dimple under each number and a squash ball in the chosen one. Choose another day and the ball is thrown there: one hop for about every 70 pixels, each 0.62 of the height of the one before, then two small bounces in the dimple. The shadow moves away from the ball and softens as it rises. The big number waits for the ball to land. The form value doesn't.

After bouncing ball (calendar) on ui-feed.dev.

Show the code the markup, the throw and the control
  1. The markup. The month comes from the value.
  2. The throw. Hops along a straight line, then a settle.
  3. The whole control. The table, the arrow keys, the dimples and the ball.

How it works

Each control is a custom element built on the same base class as the physical switches. Its shadow root holds a canvas with content="drawable", and the native elements are children of that canvas. In each paint the canvas steps its model by the time since the last paint, draws the scene, and calls drawElementImage() for the elements: with full alpha for the ones that should be seen as they are (the number field, the labels, the select, the table), and with globalAlpha = 0 for the ones that only need a hit region (the pump handle, the radios).

The draw function returns whether anything is still moving. If it is, the base class asks for one more paint. A pump at rest, a string that has stopped and a ball in its dimple ask for nothing. The candles are the exception: a flame never holds still, so that control is limited to 30 frames a second and stops only when every candle is out, or when it scrolls off screen.

The value reaches the form through ElementInternals.setFormValue(). The line under each demo is the FormData of the form around it.

Live checks

Findings