Finger shadows on a raised control panel

I wanted to know if an ElementGL fragment shader can cone-trace the soft shadow of a hovering finger across a panel of native controls, treating each control's layout box as a raised block, so the pads cast their own shadows and the finger's shadow steps where it crosses them. Move over the panel, hold Alt or Shift and turn the wheel or drag Hover height, and press a pad. A plain wheel still scrolls the page.

hover 40% · window light
Shadow pad · capsule light 48 px up

The finger hovers above the pads. Press one to latch it down.

Starting the ElementGL finger shadow shader...

Show the code height map from layout boxes, capsule cone trace, hit sync
  1. A real control panel inside the drawable canvas. Six <button aria-pressed> pads, a <select>, three checkboxes, two range inputs and two buttons on a transparent form sized with left, top, width and height.
  2. Elevations from layout. Walks offsetLeft and offsetTop up to the form for each control, which ignore transforms, and hands the boxes, heights and corner radii to the shader as uElevRects[], uElevH[] and uElevR[].
  3. Cone-traced capsule and block shadows. Each pixel sits on the height map, then a cone towards the light is tested against the finger capsule (tip to base) and against every raised block nearby.
  4. Hover height from the pointer. Pointer position sets the tip, the slider and Alt or Shift with the wheel set the hover height (a plain wheel scrolls the page), and pen or touch pressure brings the tip down to the surface below it.
  5. Size and hit sync. Copies the canvas box into --card-w and --card-h, then calls updateElementGeometry() only when the unit or buffer size changes.

How it works

The idea comes from Romain Guy's Finger Shadows in Compose (source), where an AGSL RuntimeShader models the finger as a capsule and cone-traces the soft shadow it casts on the screen. I moved it into one ElementGL WebGL 2 canvas and gave the surface heights: every control on the panel is a raised block whose box comes from its layout and whose height comes from its role, so pads stand 16 px proud, the select and sliders less, and a latched pad sinks to 4 px.

For each pixel the shader looks up the height of whatever is under it, then sends a cone from that point towards the light. The finger is a capsule from the tip under the pointer to a base up and away towards the hand. Where the cone passes close to the capsule the pixel darkens, and the cone's width at that distance sets the penumbra, so the shadow is sharp and dark at contact and wide and faint high up. The same cone is tested against the raised blocks, so the pads cast short shadows of their own. Because the top of a pad is closer to the finger than the surface around it, the finger's shadow lands in a different place on the pad than beside it and breaks into steps at every edge.

The form is a direct child of <canvas layoutsubtree>. I size it with left: 0; top: 0 and a width and height copied from the canvas box, because Blink gives an inset: 0 child its content height there, and the stretched texture would move taps away from the controls.

Live checks

These checks wait for a tap so the page keeps one WebGL context until asked. The shader checks run on their own small fixtures and give their canvases back afterwards. The state and hit checks run against the panel on this page.

Findings