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
Show the code focus moved by keys, a controller and a pad, drawn by a shader
- The panel. Buttons in two groups.
focusgroupgives each group arrow keys and one tab stop.fx="focus"draws the panel through the shader. - Tell the shader where focus is. A
focusinlistener writes four CSS custom properties. - The CSS. Transitions on those properties move the lit area. The focus ring is an ordinary outline.
- The nearest button in a direction, for the controller, the pad and the script route for arrow keys.
- Move and press.
focus({ focusVisible: true })andclick(). - The controller. One function reads a controller state. Events call it when the browser has them, a per-frame poll otherwise.
- The vibration.
- The shader. Magnify, light, grey out, lean. GLSL and WGSL.
- 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
| Result | Experiment | Detail |
|---|
Findings
- Measured, headless Chromium 157, 21 November 2026. The key presses below were real key events sent by a test script, not the live checks, which can't send trusted keys.
focusgrouparrow keys work on buttons drawn through a shader. Withfocusgroup="toolbar wrap", Right went Tides, Knots, Lights, Charts: along DOM order, past the end of the drawn row. Up and Down didn't move focus. Home and End went to the first and last button. Each group was one tab stop, and Shift+Tab into a group returned to the button it had last.- A grid needs the page's own function.
focusgroupmoves along one axis, so it can't go to the tile below. The script route measures the buttons from layout and picks the nearest one in the direction pressed. It gave the expected button in six cases, including "nothing that way" at an edge. reading-flowchanges wherefocusgroupand Tab go. With the rows reversed byorderandreading-flow: grid-rowson the grid, Right from Tides went to Journal and then Berths, which is the drawn order. Withreading-flow: normalthe same keys went Knots, Lights, which is DOM order and runs against the picture. Tab withoutfocusgroupbehaved the same way.focus({ focusVisible: true })makes a controller move show a ring, and the ring is in the texture. The tile matched:focus-visibleand 793 pixels of the outline colour were read back from the canvas. WithfocusVisible: falseit didn't match and there were none.- The shader needs four numbers. A
focusinlistener writes the centre and radius of the focused button into CSS custom properties, and transitions move them. The colour spread in the far corner of the picture fell from 53.6 to 8.7 when focus arrived. - Taps reach the buttons while the panel leans.
elementFromPoint()returned the right tile at the drawn centre of three tiles. A synthetic touch on a tile of the leaning panel in the demo pressed that tile. - The direction pad keeps focus in the panel. Its buttons have
tabindex="-1"and cancelmousedown, so a tap on them doesn't move focus. In the test each synthetic tap on the pad moved focus by one button, andnavigator.vibrate()returned true. - Removing the
focusgroupattribute logs a console error ("focusgroup requires a recognized behavior token"). Setting it tononeturns it off without the error. rawgamepadinputchangeandnavigator.hapticsare missing in Chromium 157. The page pollsgetGamepads()once per frame while a controller is connected. The controller path was run with plain objects shaped like aGamepad: right, a held repeat, stick down and button 0 gave B, B, C, F and one press.- Not verified. A real controller (its mapping, its
vibrationActuator, and the event path where the origin trial is on), whether the phone vibrates and how a 12 ms pulse feels, a screen reader's announcement of each move, the direction pad under a real thumb, and WebGPU on screen. The line under the demo reports the controller and counts the vibrations the browser accepted.