Cinema optical rack-focus across three depth planes
I wanted to know if one ElementGL WebGL 2 canvas could composite three overlapping drawable HTML planes at separate depths with variable-aperture golden-angle disc bokeh and cinema focus breathing. Tap any plane directly on the canvas, press Tab to move focus into a control, or use the focus buttons and aperture slider below.
Starting canvas…
Show the code three drawable depth planes, spring optics, 24-tap disc bokeh shader, WebGL pipeline and hit-box sync
- Three staggered
drawableplanes in one canvas. Far settings sheet (#rf-bg), midground editable workspace card (#rf-mid) and foreground inspector toast (#rf-fg) sit inside one<canvas content="drawable" layoutsubtree>. - Aperture Circle of Confusion, deterministic
tMsspring and plane layout. Computes normalized defocus acrossf/1.4tof/11, evaluates the damped spring at exact timestamps and keeps primary buttons uncovered on 412 px phones. - 24-tap golden-angle Poisson disc bokeh shader. Uses a single sharp tap when
uBlurPx < 0.25and switches to linear-light 24-tap Vogel disc accumulation with specular bloom and premultiplied alpha feathering when out of focus. - Single-context WebGL 2 compositing. Calls
stage.begin()before uploading dirty planes insidepaintwithUNPACK_PREMULTIPLY_ALPHA_WEBGL = true, draws back-to-front directly to the default framebuffer and unbinds the VAO. - Focus-breathing hit-box calibration. Syncs
updateElementGeometry()withgeometryUnit(), the1 + 0.015 * coCbreathing scale anddataset.hitTickso every control on all three planes stays clickable. - Five-argument 2D and plain-HTML fallbacks. Draws the three depth planes with five-argument
drawElementImage()without WebGL 2, or moves them into#plain-hoston context loss.
How it works
I placed three separate drawable sections inside one <canvas id="rf-canvas" content="drawable" layoutsubtree> backed by one ElementGL WebGL 2 context. When you tap a plane, press Tab into one of its controls or pick a focal preset, a deterministic tMs damped-spring solver moves focusDepth smoothly between 0.0 (Far), 0.5 (Mid) and 1.0 (Near).
Inside the paint event listener, state.hasPainted flips to true, stage.begin() sets the viewport and backing store before any upload, and only dirty planes upload via gl.texElementSubImage2D() with gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL set to true and trilinear mipmaps generated via gl.generateMipmap(). Each frame draws directly to the default framebuffer (null) with zero offscreen FBO passes:
- Studio optical backdrop: draws a dark radial vignette with a subtle focus ring tied to
uFocusDepth. - Crisp active plane: when a plane's Circle of Confusion blur radius drops below
0.25CSS px,FOCUS_FRAGsamples LOD 0 with one unpadded texture tap so typography and inputs stay razor sharp. - 24-tap golden-angle disc bokeh: defocused planes expand their quad by
26CSS px and sample 24 golden-angle Vogel disc offsets at a mipmap LOD scaled to the Circle of Confusion radius in linear light, so bright pills, indicator LEDs and card corners feather smoothly into circular bokeh discs weighted byf/1.4tof/11without repeated text ghosts.
Out-of-focus planes also breathe outward by 1 + 0.015 * coC around their centres. syncPlaneGeometry() multiplies geometryUnit() by that same breathing matrix for each plane, writes canvas.updateElementGeometry() and toggles dataset.hitTick so clicks and text selection hit the right control across all three overlapping planes.
Live checks
These checks build isolated fixtures on testStage() when you press the button, leaving the stage above untouched.
| Result | Experiment | Detail |
|---|
Findings
Measured in headless Google Chrome 160.0.8188.0 Canary (2026-11-27) at 412x915 (DPR 2.625) and 1040x900 (DPR 1). Real Chrome 159 on Android and npm run check:jank stay not verified.
- Three live depth planes upload cleanly on one WebGL 2 context: Live check 1 uploaded all three staggered
drawableplanes throughElementGL(578×342,500×276and394×169texels at 2.625 DPR) withgl.getError() === 0. At 412 px wide, the main canvas holds948×1036px (0.98 MP,7.86 MBlive,8.12 MBafter checks),0lost contexts,0open snapshots and0unasked readbacks. - Mipmapped 24-tap golden-angle bokeh stops letter ghosts: Sampling LOD 0 on 1 px button text across a 22 CSS px blur radius initially left 24 faint ghost copies of the letters. Generating trilinear mipmaps after each upload and sampling
textureLod()with per-pixel interleaved gradient noise rotation turned sharp typography into smooth optical blur while preserving circular bokeh discs on bright LEDs and chips. - Aperture f/1.4 vs f/11 and Far vs Near change measured edge sharpness: On isolated white step targets, the active plane held edge contrast at
248. Racking fromFocus FartoFocus Nearatf/1.4dropped far-plane edge contrast from248to14and lifted 10 px outside bokeh spill to189, while stopping down tof/11restored far edge contrast to231with spill down to7. - Hit testing tracks focus breathing across all three planes: Applying
geometryUnit()andupdateElementGeometry()with the1 + 0.015 * coCbreathing matrix kept controls on#rf-bg,#rf-mid(including the editable slate<input>) and#rf-fgclickable in bothFocus FarandFocus Near. Real CDP clicks toggledHDR Highlights, incrementedCUE 01toCUE 02, typed into#rf-slate-inputand triggeredAPPLIED #1whileselectstartstayed blocked on plane labels. - Reduced motion, 2D and plain-HTML fallbacks work at 412 px: Emulating
prefers-reduced-motion: reducecheckedReduced motion, keptCycle rackpaused and snappedFocus Nearin one frame. DeletingtexElementSubImage2Dran the 5-argumentdrawElementImage()depth stack (61.8%fixture ink,3.92 MB), syntheticwebglcontextlostandwebglcontextrestoredrecovered cleanly, and deletingdrawElementImagemoved all three planes into#plain-hostatscrollWidth === 412with zero CLS. - Not verified yet: real Android Chrome 159 touch scrolling and GPU frame rate, plus
npm run check:jank -- /rack-focus/and the build-timelearn.jsreduced-motion banner oncenpm run buildindexespage.json.