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.

Specimen 04 ยท Live HTML surface Editorial note & optical reticle

Select across these 12.5 px words to see the browser's live highlight and glyph hinting inside the glass lens above. Everything drawn here comes from a single texElementSubImage2D() texture upload with zero duplicate text nodes.

Inline buttons above stay clickable because updateElementGeometry() scaled by geometryUnit() holds the card hit box aligned 1:1 with the canvas.

col 24, ln 2

Fig. 1: 0.45 px hairline vernier scale and 8 px colophon note. Hover or scrub over this band to test barrel curvature, top meniscus specular highlight and chromatic dispersion along the bevel.

Track
Move loupe
Starting WebGL 2 loupe stage...
Show the code card markup, refractive glass shader, caret tracking, clamping and geometry sync
  1. 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.
  2. 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 uTex with radial barrel lensing, edge chromatic RGB aberration and a top meniscus highlight.
  3. 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.
  4. Measuring live caret and selection coordinates. Reads window.getSelection() ranges on editorial prose and uses textarea.createValueRange() with a hidden off-canvas mirror fallback for wrapped lines and empty lines.
  5. One WebGL 2 context via ElementGL. Uploads the card on paint events with texElementSubImage2D() and generates mipmaps for crisp 1:1 and magnified sampling.
  6. 1:1 hit testing and 2D fallback. Keeps buttons and the textarea clickable with updateElementGeometry() scaled by geometryUnit(), 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.