Live DOM autostereogram
I wanted to know if an editable HTML form's contrast could drive a real-time GPU single-image random-dot autostereogram (the 1990s Magic Eye illusion) on one WebGL 2 canvas so typed letters and buttons pop out in hidden 3D while staying clickable. If free-fusing a stereogram doesn't work for your eyes, switch to cross-eye pair, red/cyan anaglyph, wiggle 3D or depth overlay right above the stage.
To view the autostereogram, relax your focus behind the screen until the two top guide dots split into three and the middle dot turns sharp. Tap or hold Peek live HTML to check where the controls sit, then click inside the stereogram to edit the headline, press Pop +, Invert depth or Pulse wave, or drag the relief slider.
Show the code Live depth form, single-pass backward strip chain, parallax heightfield march and ElementGL hit-box sync
- One depth-encoded live HTML form inside the canvas. Bright white letters and bevelled buttons sit on an obsidian ground inside
<canvas content="drawable" layoutsubtree>, so DOM luminance maps straight to 3D elevation. - Exact single-pass GPU autostereogram chain. Starting from each fragment's horizontal position, the shader steps left by
stripW - depth * maxShiftuntil it reaches the periodic seed strip, with zero FBOs and zero CPU readback. - Front-to-back heightfield raymarch for the other stereo modes. Cross-eye stereo pair, red/cyan anaglyph and wiggle 3D march along the horizontal epipolar ray so raised glyphs occlude the background behind them cleanly.
- One WebGL 2 context through
ElementGL. Thepainthandler uploads the form withtexElementSubImage2D()only when the DOM changes, and anIntersectionObserverpauses animated modes when the canvas scrolls off screen. - Hit-box alignment with
updateElementGeometry()andgeometryUnit(). After drawing, the form's transform is synced to the active viewport rectangle (tracking whichever eye half the pointer hovers in cross-eye mode) and toggles an attribute to clear Chromium's hit-test cache.
How it works
A classic single-image autostereogram links every horizontal pixel x to a twin pixel to its left at x - S(d), where S(d) = P - d * maxShift. Background points (d = 0) repeat at the full strip width P; raised foreground points (d = 1) repeat closer together so your eyes diverge behind the screen and lift the shape forward (or tick Cross-eye fuse to invert elevation when viewing cross-eyed). On the CPU people usually build that left-to-right across a pixel buffer, which would require reading the DOM pixels back every frame.
On the GPU you can invert the walk and trace backwards per fragment with no framebuffer ping-pong and no readback. At first I sampled depth with a two-step midpoint lookup, which looked flat because any stroke narrower than 0.5 * maxShift got jumped right over at high depth. Instead, each backward link now runs a 12-step front-to-back heightfield raymarch along the right-eye epipolar ray (px - 0.5 * (stripW - t * maxShift)) from t = 1 down to 0 before walking left into a crisp 2 px periodic SIRDS pattern. Because the live form uploads straight to WebGL 2 via ElementGL.upload() (texElementSubImage2D()), every keystroke, blinking caret, button press and slider drag shifts the 3D relief on the next paint event.
A 5-tap pillowed dilation turns narrow strokes into rounded ridges atop a raised card plinth, while keeping canvas.updateElementGeometry() (scaled by geometryUnit() for high-DPI screens) centred over the floating form. In Cross-eye pair mode the form appears in two side-by-side panels; because an element has one hit box at a time, moving or tapping over either panel swaps updateElementGeometry() onto that half and toggles data-hit-tick to clear Chromium's hit-test cache.
Live checks
These checks wait for a tap so they don't open a second WebGL context on load. Each check runs on its own isolated fixture on testStage() and retires its canvas when finished.
Findings
- Tracing the autostereogram chain backwards with only one or two probes erases thin glyphs when depth goes past 50%, because the foreground midpoint sits further from the strip edge than half the stroke width; a 12-step front-to-back raymarch per link keeps thin text intact up to maximum shift.
- Crisp 2 CSS px nearest-neighbour SIRDS dots lock much faster than smooth bilinear value noise, and raising the card plinth above the wall turns buttons and labels into a multi-level relief sculpture.
- In Cross-eye stereo pair mode the same live form is drawn twice (left and right eye halves), so Chromium's single
updateElementGeometry()matrix can only cover one panel at a time; tracking pointer position swaps the hit box between halves. - Sampling depth with a 5-tap mean/max blend widens thin 2D font strokes into smooth pillowed bevels without a separate blur FBO, and stops dark button labels from punching deep holes through light keycaps.