Magnified ruler picker and rolling digits
I rebuilt Nitish Khagwal's Ruler Picker inside one ElementGL WebGL 2 canvas. Drag the scale, press the arrow keys, or switch units and presets: a cylindrical glass lens bends and magnifies nearby <datalist> ticks while the <output> digits roll like an odometer drum.
Starting...
Show the code drawable instrument card, cylindrical lens shader, rolling odometer and geometry sync
- Live ruler card inside one canvas. Putting unit buttons, rolling
<output>drums,<input type="range">,<datalist>and preset buttons in onedrawablecard keeps them in a single texture upload. - Cylindrical lens and drum fragment shader. Refracts scale UVs around
uLensXwith barrel arch, Snell rim compression, chromatic RGB fringe, reflection bar and mechanical drum shading over<output>. - Damped velocity spring and rolling digit reels. Tracks the thumb or pointer hover along the scale and rolls each digit wheel continuously across
9to0wraps, snapping under reduced motion. - Hit-box calibration with
updateElementGeometry(). Scales the transform bygeometryUnit()and flipsdataset.hitTickso dragging the slider and tapping buttons keep working. - Five-argument 2D strip fallback. Draws the card once with 5-argument
drawElementImage(), copies it into a plain buffer for the lens strips and keeps hit testing at 1:1.
How it works
This builds on Ruler Picker by Nitish Khagwal (listed on Demos elsewhere). Earlier on /elastic-range/, I mocked up a 2D ruler in strips without rolling digits. Here the whole precision instrument sits inside one <canvas content="drawable" layoutsubtree> backed by a single ElementGL WebGL 2 context from /lib/element-gl.js.
One drawable card (#ruler-card) holds the unit switcher buttons, the <output id="ruler-out" for="ruler-range"> odometer window, the real <input id="ruler-range" type="range">, its <datalist id="ruler-ticks"> options, a visual tick strip synced to the thumb inset and four preset buttons. Putting aria-hidden="true" on the visual 14-digit reels while updating a visually hidden text span inside <output> stops a screen reader from reciting every digit on every strip while keeping native live-region announcements intact.
On each paint event, stage.begin() sizes the WebGL buffer first and stage.upload(cardEl) uploads the card with gl.texElementSubImage2D(). Inside RULER_FRAG, fragments inside uRulerRect map lateral distance from uLensX through s / zoom + (1 - 1 / zoom) * s^3.6 so ticks swell up to 2.4x (2.15x at rest) directly under the thumb without moving the thumb centre, bow outward with vertical barrel arch, split subtly into R, G and B near the steep Snell rim and pick up a darkened glass reflection bar. Fragments inside uOdoRect get cylindrical drum shading and top/bottom bezel vignette, while buttons stay untouched at 1:1.
Two damped velocity springs drive the lens focal coordinate and each digit reel, stopping the requestAnimationFrame loop once settled and pausing completely off screen through IntersectionObserver. Under prefers-reduced-motion: reduce, both snap straight to target. Because WebGL uploads don't move hit boxes on their own, syncGeometry() passes a geometryUnit()-scaled matrix to canvas.updateElementGeometry() and toggles dataset.hitTick. Without WebGL 2, draw2DFallback() renders with 5-argument drawElementImage() plus clipped lens strips from a plain copy, and on webglcontextlost the card moves into plain HTML so every control stays usable.
Live checks
Each check builds an isolated fixture on testStage() without scrolling or focusing the demo above. Press Run to test this browser.
Findings
- Measured in headless Chrome 160 Canary (2026-11-27, 412 px viewport at DPR 2.625): all 6 live checks pass, backing store stays at
0.86 MP(924x929) and6.9 MB,0errors,0lost contexts, and.motion-warnfires when reduced motion toggles on. - Passing an explicit
nowclock intosetTargetValue()keeps deterministic unit-test stepping aligned withboostUntilspring hold-off delays. - Real Chrome 159 for Android touch dragging, hardware frame rates,
npm run check:jank(Playwright absent) and 2D fallback visuals stay not verified until tested on device.