Controller, keyboard and haptic navigation of drawn UI

A panel of buttons drawn through a shader. Move focus with the arrow keys, a game controller, or the direction pad under the panel. The shader magnifies and lights the focused button, takes the colour out of the rest, and leans the panel toward it. Each move asks for a short vibration.

Harbour desk

Nothing is open.

    Show the code focus moved by keys, a controller and a pad, drawn by a shader
    1. The panel. Buttons in two groups. focusgroup gives each group arrow keys and one tab stop. fx="focus" draws the panel through the shader.
    2. Tell the shader where focus is. A focusin listener writes four CSS custom properties.
    3. The CSS. Transitions on those properties move the lit area. The focus ring is an ordinary outline.
    4. The nearest button in a direction, for the controller, the pad and the script route for arrow keys.
    5. Move and press. focus({ focusVisible: true }) and click().
    6. The controller. One function reads a controller state. Events call it when the browser has them, a per-frame poll otherwise.
    7. The vibration.
    8. The shader. Magnify, light, grey out, lean. GLSL and WGSL.
    9. The page script.

    How it works

    The panel is one drawable element, uploaded to a texture on every paint and drawn by a fragment shader (fx: WebGPU when it works, else WebGL). Focus is DOM focus on <button> elements. A focusin listener writes the centre and size of the focused button into four CSS custom properties, CSS transitions move them, and the shader reads them as uniforms. The effect also returns a matrix for the lean, which the library passes to updateElementGeometry(), so a tap lands on the button that is drawn under the finger.

    Three inputs move focus. Arrow keys go through the focusgroup attribute when the browser has it, which moves along DOM order, or through the page's own function, which picks the nearest button in the direction pressed. A game controller and the direction pad under the panel always use the page's function, because focusgroup answers only to keys. The controller is read by one function, called from rawgamepadinputchange events when the browser has them and from a poll of navigator.getGamepads() once per frame when it does not. The line under the demo says which of these this browser has, and the log records what moved focus each time.

    Live checks

    ResultExperimentDetail

    Findings