A lens over live DOM
Drag across the card. Each lens draws the patch of page under it once, at the lens's own magnification, and a shader bends it, so 4px text comes out sharp. What's under the lens is still the page, and you can click and type through it.
Specimen sheet
Regular text, then the small print. Everything here is ordinary markup inside a <dom-lens> element.
The quick brown fox jumps over the lazy dog. Sphinx of black quartz, judge my vow. Pack my box with five dozen liquor jugs. How vexingly quick daft zebras jump. The five boxing wizards jump quickly. Jackdaws love my big sphinx of quartz. Terms apply. Past performance isn't a guide to future results. Contains small parts. Objects in the lens are closer than they appear. Don't operate heavy machinery while reading 7px type. Void where prohibited. Batteries not included. Some assembly required.
This line is set at four pixels. If you can read it without the lens, the lens is on. HTML-in-canvas replays paint records, so scale is free.
Click the button through the lens, type in the field, or press Tab and the lens follows focus. On a touch screen the lens rides above your finger and the page doesn't scroll from the card (switch the lens off, or pin it, to scroll from the card).
<script type="module" src="/lib/dom-lens.js"></script>
<dom-lens mode="glass" zoom="2.4" radius="120">
<section>…your markup, unchanged…</section>
</dom-lens>
Measure it on this device
The fisheye used to be a mesh of clipped triangles: the element's paint record was replayed once per triangle, up to 199 times a frame. I rebuilt it so that every lens is two replays and one shader pass, on WebGPU where it works and WebGL where it does not. This sweeps the lens across the card each way and times the frames on this device. “Patch px” is the side of the square drawn for the lens: if frames come slowly while the lens moves it is drawn smaller, then sharpened when the lens rests.
| Lens and route | frames/s | ms/frame | worst 5% | draws | patch px |
|---|
Show the code a lens over real markup
- Wrap any markup in
<dom-lens>. The markup isn't changed. - Load the element and set attributes.
mode,zoom,radius,off, andx,ywithpinnedto hold the lens still.$isgetElementById. - Inside: the markup is slotted into a drawable canvas by this shadow root.
- Inside: draw the patch of page under the lens, magnified, then run it through a shader.
drawElementImage()under a scale makes the browser draw the text again at that size, so it's sharp. That square goes to WebGPU as a texture, or to WebGL where WebGPU is missing, the lens shader bends it, and the result is drawn back over the page. - Inside: the patch becomes a texture. It is copied straight out of the 2D canvas. This is the WebGL route; the WebGPU one is
upload()in /lib/lens-gpu.js. - A lens is one GLSL function. This is the plain magnifier; the glass, fisheye and other lenses are in the same file.
- The whole element. The shaders are in /lib/lens-gl.js (GLSL) and /lib/lens-gpu.js (WGSL).
How it works
- The content is slotted. Your markup stays a child of
<dom-lens>. A shadow root holds a canvas with one drawable wrapper around a<slot>, sized to the content. - One magnified draw.
drawElementImage()replays the element's paint record, so drawing it scaled re-rasterizes text and vectors. Each frame the square of page under the lens is drawn once, clipped, at the lens's magnification, into a patch of the same canvas. - One shader. The patch is copied into a texture (
copyExternalImageToTexture()on WebGPU,texImage2D()on WebGL) and a fragment shader, written in both WGSL and GLSL, decides, for every pixel of the lens, which point of the patch to show and what to do to its colour. The result is drawn back withdrawImage(). All the lenses share one device or context. Then the patch is painted over with the page at rest, clipped to that neighbourhood, so the rest of the canvas is never redrawn while the lens moves. - Real glass is ray traced: a ray enters the top spherical face, bends by Snell's law, crosses the glass, bends again leaving the bottom face and runs on to the page. Red, green and blue use slightly different refractive indices, which gives the colour fringes, and the edge distortion is the spherical aberration every simple lens has. The zoom slider moves the lens nearer to or further from the page.
- Fisheye is the same pipeline with a smooth radial mapping. Black hole uses the point-mass lens equation, so the page forms an Einstein ring. Water drop refracts through a spherical cap with a travelling ripple.
- X-ray: the layout box of every element is outlined onto the patch (interactive ones in amber) before the shader runs. It works like a DevTools overlay that you point at part of the page.
- Hit testing: the pointer is the fixed point of the magnification, so the element under the cursor is the one you see there. For a pinned lens the element's geometry is switched to the magnified transform while the pointer is inside it.
- Routes. WebGPU is used when it has a real (not software) adapter and passes a self-test, then WebGL 2, then plain 2D.
pipeline="webgpu|webgl|2d"names one by hand. On the 2D route the magnifier and x-ray are two plain draws and the fisheye falls back to the triangle mesh.
Live checks
| Result | Experiment | Detail |
|---|