Refractive glass text magnifying loupe
I wanted to know if an iOS-style glass loupe can magnify live glyphs, selection highlights and a blinking or static caret from one texElementSubImage2D() upload without cloning any DOM nodes.
Type in the note field, select part of the paragraph, or switch to pointer or scrub mode to glide the 2.2x lens 58 px above the target and inspect the 8 px footnote and hairline vernier ticks below.
Show the code card markup, refractive glass shader, caret tracking, clamping and geometry sync
- The editable card inside one drawable canvas. One
<div id="loupe-card" drawable>holds the inline theme bar, selectable paragraphs, live<textarea>and 8 px hairline vernier diagram. - Single-texture refractive glass fragment shader. Draws the 1:1 base card, faint target reticle, outer drop shadow, and pill or round glass lens sampling
uTexwith radial barrel lensing, edge chromatic RGB aberration and a top meniscus highlight. - Placing the loupe 58 px above target with edge clamping. Keeps the lens clear of a finger or caret while clamping smoothly at the top canvas edge when
y < 60 px. - Measuring live caret and selection coordinates. Reads
window.getSelection()ranges on editorial prose and usestextarea.createValueRange()with a hidden off-canvas mirror fallback for wrapped lines and empty lines. - One WebGL 2 context via
ElementGL. Uploads the card on paint events withtexElementSubImage2D()and generates mipmaps for crisp 1:1 and magnified sampling. - 1:1 hit testing and 2D fallback. Keeps buttons and the textarea clickable with
updateElementGeometry()scaled bygeometryUnit(), and restores full hit testing after the 9-argument zoomed clip in 2D mode.
How it works
On iOS, a text magnifying loupe sits above your fingertip so you can see exactly where the caret will land.
If you build that on the web by cloning DOM nodes inside an overflow-clipped overlay, line heights, scroll offsets and form controls can drift apart, and browser selection state doesn't travel cleanly into the clone.
I built this experiment to test whether one ElementGL WebGL 2 context can keep a single live HTML card at 1:1 and draw the floating glass loupe directly from the same texElementSubImage2D() texture.
The card stays at (0, 0) inside one <canvas content="drawable" layoutsubtree>.
Whenever the DOM changes or the caret moves, Chromium fires a paint event on the canvas and stage.upload(card) copies the card's device-resolution pixels into an RGBA8 texture and regenerates mipmaps.
When you only glide the pointer or drag the loupe over static text, no texture upload is needed: the fragment shader redraws the cached texture with updated uTarget and uLoupeCenter uniforms.
To track the caret inside <textarea> without cloning visible DOM, the script checks textarea.createValueRange() first and falls back to an off-canvas inert measurer matching the field's computed font, padding and scroll offset so blank lines and end-of-line positions resolve accurately.
Selections inside the editorial paragraphs come straight from window.getSelection() and Range.getClientRects().
For hit testing, updateElementGeometry() multiplies the 1:1 matrix by geometryUnit() and increments card.dataset.hitTick, so clicking swatch buttons, dragging a selection or tapping into the textarea keeps native browser behaviour.
An IntersectionObserver pauses easing frames while off screen, and prefers-reduced-motion: reduce snaps the loupe immediately with no motion loop.
Without WebGL 2, a 2D fallback draws the card with five-argument drawElementImage(), draws the magnified lens inside a rounded clip with nine-argument drawElementImage(), and then issues a zero-area clipped five-argument draw so the narrow source rectangle doesn't shrink the card's hit box.
If HTML-in-canvas is missing or the WebGL context is lost, the card moves out into plain HTML with no loss of typed text.
Live checks
These checks wait for a tap so the page opens with one GPU context. Each check builds its own isolated fixture on the test stage and retires its canvas afterwards.
Findings
Measured in headless Chrome Canary 160 (160.0.8187.0, 412 px wide at device scale 2.625) on 2026-11-27. Physical phone hardware (Android Chrome 159) and npm run check:jank are not verified yet.
- One texture upload feeds both 1:1 text and the magnified loupe (measured, C160): all 6 live checks passed with
gl.getError() === 0, peak 2 WebGL contexts and a 1.22 MP canvas (927x1313at353x500CSS px). SamplinguTexwith analytic screen derivatives intextureGrad()kept both the base paragraph and the 2.2x lens interior crisp. textarea.createValueRange()gives exact wrapped caret coordinates (measured, C160): in Chrome Canary 160, collapsedcreateValueRange(pos, pos)inside the drawn<textarea>returned line-accurate bounding rectangles (and clicking and typing into the field updated the loupe at 1:1). An off-canvasinertmirror fallback handles blank lines and browsers withoutcreateValueRange().caret-animation: manualstops idle 2 Hz repaints: on a magnifying loupe, a blinking caret vanishes for half of every second and triggers two texture uploads a second even while nobody types. Settingcaret-animation: manualkeeps the caret steady inside the glass while idle.- Nine-argument
drawElementImage()narrows the 2D hit box (measured, C160 with--disable-webgl): in the 2D fallback, drawing the zoomed loupe clip with 9 arguments limits hit testing to that cropped source rectangle unless followed by a zero-clipped 5-argument draw of the full card. Adding the zero-clipped draw kept the Cobalt swatch button clickable. - Top-edge clamping keeps the reticle decoupled from the lens origin (measured, C160): targeting
y = 22 pxclamped the pill loupe centre toy = 32.0 px(top edge8.0 pxinside the canvas) whileuTargetstayed at the true coordinate. - Not verified yet on real hardware: Android Chrome 159 phone touch scrolling alongside
Loupe drag scrub, on-screen keyboard focus scrolling, high-DPI 2.625x visual sharpness of 8 px hairline text on a physical OLED screen, Chromium 157, andnpm run check:jank -- /text-loupe/.