Controls behind glass, fog and refracted light

Four more controls from ui-feed.dev, rebuilt with HTML-in-canvas. A password field under fogged glass that you wipe with a finger, a <select> under a lens that bends the room behind it, and two colour inputs driven by ranges: one that splits a beam through a prism, one that draws interference in a foil. The glass and the foil are the WebGL shader in /lib/glass-gl.js, the one the glass skins on /canvas-skin/ use. An element can only be drawn by a canvas it is inside, so each control draws its own elements, copies that rectangle out to a plain canvas with drawImage(), and that copy is what the shared shader samples. I wanted to know whether a control can be put behind a real piece of computed glass and still be typed into.

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.

Drawing route: measuring.

Fogged glass

An <input type="password"> behind condensation. Drag across the pane to wipe it, and the fog closes back over it in about four seconds, from the rim of the wiped part inwards. The wiped part shows a second, read-only input with the same text and no dots, so the characters are readable only where the glass is clear. The eye button switches the password input's own type, which clears the dots everywhere.

After fogged glass (password input) on ui-feed.dev.

Show the code the fog, the wipe and the clear copy
  1. The markup.
  2. The element. The field is drawn once, that rectangle is blurred back over itself, each wipe cuts a disc out of the fog with destination-out, and the clear copy is drawn inside a clip made of the fresh discs.
  3. The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.

Spatial glass

A native <select> under a lens. The room behind it is a <div> of CSS gradients drawn by the same canvas, so what the glass refracts is an element's picture. Tapping opens the browser's own picker.

After spatial glass (select) on ui-feed.dev.

Show the code a shared shader over one element's picture
  1. The options are the data. The element copies them into an internal <select>.
  2. The element.
  3. Getting the background to the shader. drawImage() from the drawable canvas into a plain one, which the shader uploads as a texture.
  4. The glass shader. A signed distance field, a surface normal, then refract() three times with different indices.

Prism

A colour picker that is an optical bench. White light goes into a glass prism, and a screen with a slit in it lets one wavelength through: that wavelength is the colour. Turn the prism by hand, with a finger or the mouse anywhere on the dial, and the spectrum sweeps across the slit. The dial is an <input type="range"> for the angle, and there are two more for the wavelength and the hue. They are one quantity seen three ways, so setting the wavelength or the hue solves for the angle and the prism turns to it. The picture is a trace, not a gradient. The index of the glass is Cauchy's formula, so each of 49 wavelengths is refracted by its own amount with Snell's law as vectors, and at every face Fresnel's equations split the light into the part that goes through and the part that is thrown back, which is where the faint beams off the other faces come from. The prism is equilateral, so the light only reaches the slit for about 14 of every 120 degrees, and the control says when it doesn't.

After prism (color picker) on ui-feed.dev.

Show the code a range with no track, and wavelengths as colours
  1. The markup.
  2. The optics. Snell's law as vectors, the nearest face the ray meets, Fresnel's equations for the share reflected at each face, and total internal reflection past the critical angle.
  3. The element. The ranges are never drawn. hit() puts the angle over the dial and the other two over their tracks, and the solver turns a wavelength into an angle.
  4. Wavelength to sRGB.

Hologram

Three ranges for hue, saturation and brightness, and an <input type="color"> that holds the result. The square is a shader: the thin film's thickness changes across it and with a slow tilt, and each wavelength reinforces at its own thickness, which is where the bands come from. The three tracks are drawn by the canvas; the ranges themselves are not painted.

After hologram (color picker) on ui-feed.dev.

Show the code a shader with no background, and three ranges
  1. The markup.
  2. The element and its shader. shade() with no source: the panel is computed from the uniforms alone.
  3. HSB and hex.

How it works

The controls extend FeedControl in /lib/ui-feed/kit.js, the same base class as the dot and tube displays and the switches. A drawable canvas sits in the shadow root with the native elements inside it, and the paint handler draws them where it wants them.

Two of them want a fragment shader over what is behind the control. A drawable canvas cannot be a WebGL canvas and still draw elements, and an element can only be drawn by a canvas it is inside, so the shader cannot see the elements directly. The way round it is to copy: the control draws the elements into its own 2D canvas, copies that rectangle into a plain canvas with drawImage() (which is allowed from a drawable canvas and is not a readback), and the shared WebGL context uploads the plain canvas as a texture. The result comes back as a drawImage(). One WebGL context serves the whole page, which matters on Android, where a tab gets about eight.

The hologram needs no background at all: shade() is called with no source, and the foil is computed from the hue, saturation, brightness and tilt uniforms. When there is no WebGL 2, or its context is lost, both controls draw a 2D version instead and the line above says which route is in use.

The fog needs no shader either. It is the field's own picture, blurred with ctx.filter onto a plain canvas of its own, with a white gradient on top and a disc cut out of it with destination-out for each point the finger passed through. That canvas is then laid over the pane. Each disc closes from its rim inwards over about four seconds. The clear text is a second draw of a read-only copy of the input, under the fog, clipped to the discs that are still open. My first version cut the discs out of the control's own canvas, which also cut out the field under the fog: on a dark page every wipe was a black hole.

Live checks

Findings