A sundial drawn over a time input

One more control from ui-feed.dev, rebuilt with HTML-in-canvas: a time input whose picker is a stone sundial. I wanted to know whether an <input type="time"> could stay the control, with its keys, its range and its value, while the canvas works out where the sun is and draws the shadow on the hour.

Credit: this is a rebuild of the sundial time input on ui-feed.dev. The original is far higher fidelity than this, so go and look at it. This is an example of what HTML-in-canvas makes possible with a native element underneath, not a replacement. I worked from the published demo rather than its source: I looked at what it does, worked out the mechanism and built it again. The design and the idea are theirs.

Sundial

The field at the top is an <input type="time"> with a min of 05:00 and a max of 19:00, and it is what the form sends. The canvas draws a sundial for 51°30′ N under it. Each hour line is at the angle a real horizontal dial has for that latitude, and the shadow is where the gnomon's shadow would fall: the sun's height and direction come from the time, so the shadow is long and the stone warm at dawn and dusk. Drag round the dial to set the time, or type one and the shadow turns to it.

After the sundial (time input) on ui-feed.dev.

Show the code the markup and the control
  1. The markup. The time, its range and the latitude of the dial.
  2. The control. The field, the drag, the sun's position and the shadow.
  3. The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.

How it works

The control is a custom element on the same base class as the small controls. Its shadow root holds a canvas with content="drawable", and the time field and an empty element over the dial are children of that canvas.

The stone, the engraved lines and the numbers are painted once into a picture. Each frame draws that picture, the shadow and the gnomon for the time on a spring, and the field over the top. A drag turns the point under the finger back into a time with the same formula the hour lines use, and sets the field's value.

Live checks

Findings