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
- The markup. The time, its range and the latitude of the dial.
- The control. The field, the drag, the sun's position and the shadow.
- 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
- An
<input type="time">drawn in a canvas kept its value, itsminandmaxand the form's validity: 04:10 made the form invalid. A drag sets the field's value, and the drag can't go past the range because the time is clamped to it. - The shadow and the engraved hour lines come from the same latitude, so the check could compare them: the shadow's edge was 0.000° from the 16:30 hour line. The sun's height also sets how dark the shadow is and how warm the stone looks.
- A
changeevent from an element in the shadow root doesn't reach the form outside, becausechangeisn't composed. The control sends its own from the host. The first check caught it. - The field shows the time in the browser's own format, so it read "03:20 PM" in the test browser where the original shows 15:20. The value the form sends is always
15:20. - The gnomon is drawn at the height its latitude gives it, which makes it taller and thinner than the one in the original.
- A drag on an emulated Pixel 7 set the time and a swipe beside the field scrolled the page. A swipe on the dial itself doesn't scroll the page, because the dial needs the drag. The time picker that Android opens from the field, and how a screen reader reads it inside the canvas, haven't been tried on a phone.