Split-diopter two-plane focus lens
I wanted to know if one ElementGL WebGL 2 canvas could keep a close-up foreground HTML card and a distant background HTML panel razor sharp side-by-side across an adjustable optical seam, while blurring each defocused half with cinema bokeh and keeping controls in both planes clickable. Drag across the stage or move the sliders to sweep and tilt the glass edge, type and click inside either card, or turn on single-lens mode to see what a standard lens loses.
Starting canvas…
Show the code two drawable focal planes, Vogel bokeh and glass seam shader, pointer drag, geometry sync and 2D fallback
- Two live
drawableplanes in one canvas. Both#bg-card(distant 8.5 m plane) and#fg-card(close-up 0.42 m plane) live inside<canvas content="drawable" layoutsubtree>. - Split-diopter bokeh and glass-edge shader. Keeps the focused half of each element 1:1 sharp, applies a 24-tap Vogel disc bokeh with highlight bloom to the defocused half, and adds a refractive bevel band across
uSoftness. - Pointer drag and single-lens comparison controls. Lets you slide and tilt the optical boundary by dragging the stage or prism tabs while keeping text selection active inside
<input>. - Hit-box calibration with
geometryUnit(). Positions both cards viaupdateElementGeometry()and togglesdataset.hitTickso clicks and focus land on controls in both planes. - Five-argument 2D and plain-HTML fallbacks. Clips and blurs half-planes with
ctx.filterand 5-argumentdrawElementImage()without WebGL 2, or extracts both planes into plain HTML on context loss.
How it works
- A real split-diopter is a half-convex glass attachment bolted in front of half the camera lens so near foreground subjects and far background subjects focus sharply in the same frame, separated by a thin blurred optical border.
- Both
#bg-cardand#fg-cardstay as directdrawablechildren inside one<canvas content="drawable" layoutsubtree>. OneElementGLWebGL 2 context uploads changed DOM planes viagl.texElementSubImage2D()withUNPACK_PREMULTIPLY_ALPHA_WEBGLenabled, then draws both textures in one fragment pass. - For each pixel,
DIOPTER_FRAGmeasures the signed distance to the seam line defined byuSplitPosanduSplitAngle. OutsideuSoftness, the focused plane runs a single unblurred texture lookup so text and borders stay pixel-exact while the out-of-focus plane samples a 24-tap Vogel golden-angle bokeh disc with highlight bloom. InsideuSoftness, both planes refract slightly and soften under a translucent glass bevel. - Switching on Single lens (no diopter) removes the split attachment across the entire frame, letting you rack between the near cue card and the distant scene panel so you can see one plane smear into bokeh at a time.
- WebGL draws don't move Chromium's hit boxes automatically, so
syncPlaneGeometry()calibratesgeometryUnit()once per resize, callscanvas.updateElementGeometry()for both cards, and flipsdataset.hitTickto clear the hit-test cache.
Live checks
Each check builds an isolated fixture on the hidden test stage and retires its canvas when done, leaving the visible stage untouched.
| Result | Experiment | Detail |
|---|
Findings
Measured in headless Chrome 160.0.8188.0 canary on 2026-11-27 (412 px mobile at 2.625 DPR and 1024 px desktop at 1 DPR). Chrome 159 on Android isn't verified yet.
- Both focal planes render sharp at once: in split-diopter mode (
uLensMode = 0), live check 2 measured full255luminance contrast across 4 px stripes on both#fg-cardand#bg-card; in single-lens mode (uLensMode = 1), right-plane contrast dropped to47while the near plane stayed at255. - Controls in both planes stay clickable: calibrating
geometryUnit()afterstage.begin()sets the DPR backing store (keyed bycanvas.width x canvas.height) keepselementsFromPoint()and pointer clicks landing on#fg-cue-input,#fg-trigger-btnand#bg-action-btn. - Mipmapped Vogel bokeh avoids text stippling: generating mipmaps after
texElementSubImage2D()and samplingtextureLod()inside the 24-tap bokeh kernel removed dotted echoes on blurred typography while keeping focused zones at LOD 0. - Budget and context recovery: at 412 px wide and 2.625 DPR, the canvas stays at
927x1099(1.02 MP,8.2 MB, zero lost contexts, zero unasked readbacks), and syntheticwebglcontextlostrecovery returns both cards cleanly into plain HTML. - Not verified yet: touch drag feel across the stage and prism tabs, on-screen keyboard focus in
#fg-cue-inputon Chrome 159 for Android,npm run check:jank, and the 2D fallback in Chromium 153.