Multi-layer X-ray lens over OpenStreetMap
I wanted to know if a draggable circular shader lens over an OpenStreetMap street map could reveal a second map layer underneath alongside live HTML neighbourhood cards that only exist inside the lens. Drag the circular lens over the orange district markers to reveal each card, switch between night transit, thermal density and blueprint contour views, or pin the lens so tapping a checkbox, button or text input on a phone is easy.
Drag inside the brass ring to move the lens, drag outside to pan the map, or press Pin lens before tapping a card control.
Show the code OSM/CARTO tile textures, dual-layer bevel shader, circular card clipping and hit-box gating
- Four neighbourhood cards inside the canvas. Each card is a
drawablechild of<canvas content="drawable" layoutsubtree>with a transit filter checkbox, a save button and an editable district tag input. - Web Mercator projection and two tile caches. Live daytime tiles from OpenStreetMap and dark tiles from CARTO DarkMatter paint into two plain offscreen canvases, with a deterministic procedural street grid filling any slot that is still loading or offline.
- Dual-layer X-ray lens and circular card aperture shaders. The map shader blends daytime tiles outside the brass bezel with night transit, thermal density or Sobel blueprint edges inside, adding curved glass bevel refraction near the rim. The card shader clips each DOM quad to the same circle.
- Hit boxes clipped to the lens with
canvasClipandinert. Cards outside the circle are markedinert = truewith a zero-sizecanvasClip; cards inside compute the overlapping visible rectangle in element coordinates, scalecanvasTransformwithgeometryUnit()and toggledata-hit-tick. - One WebGL 2 context for both map layers and live DOM cards. The
painthandler uploads dirty cards viaElementGL, draws the fullscreen lens shader onstage.gl, then restoresElementGL's shader and VAO to draw the clipped cards. - Texture upload in
element-gl.js.texElementSubImage2D()copies each card into a texture sized togetElementImageDefaultSize().
How it works
The idea goes back to Bier et al.'s Magic Lenses paper at Xerox PARC (1993), combined with the live DOM lens experiments in /lens/ and /translate-lens/ on this site.
Here the lens sits over live slippy-map tiles from OpenStreetMap (daytime base) and CARTO DarkMatter (night transit layer).
Both tile layers composite into two plain offscreen 2D canvases (never drawable canvases) and upload to WebGL 2 textures only when a tile finishes loading or the map pans or zooms. While a tile is fetching or if you are offline, a deterministic street grid fills that tile slot so the shaders still have real lines to work on.
Everything on screen runs inside one WebGL 2 context from new ElementGL(canvas, ...). On each paint event, the map pass draws a fullscreen quad: outside the bevelled brass ring it samples the daytime OpenStreetMap texture and paints four district beacons; inside the ring it applies curved glass bevel refraction near the rim, optional optical zoom (1.0x to 1.8x) and the chosen lens mode.
Night + Transit blends CARTO DarkMatter tiles (or a dark nocturnal fallback transform while tiles load) with glowing district transit rings controlled by each card's Transit checkbox. Thermal maps local street contrast and district activity plumes onto a five-stop false-colour ramp. Blueprint runs a 3 by 3 Sobel edge filter over the daytime tile luminance to pull out roads, parks and building edges on a drafting grid.
After restoring ElementGL's program and vertex array object, the four drawable neighbourhood cards draw on top. Each card's fragment shader checks the fragment's canvas CSS coordinates against the lens centre and radius, discarding anything outside the circular aperture so cards slide cleanly out from under the brass rim as you scan across a city.
Hit testing follows the same aperture: cards outside the circle get inert = true and a zero-size canvasClip so hidden cards never intercept taps or keyboard focus, while cards inside pass their visible overlap rectangle to canvas.updateElementGeometry(card, { canvasTransform, canvasClip }) scaled by geometryUnit().
If WebGL 2 HTML-in-canvas isn't available, the page falls back to a 2D circular ctx.clip() path with five-argument drawElementImage(), or moves the cards into a plain HTML grid below the 2D map if HTML-in-canvas is disabled or the WebGL context is lost.
Live checks
These six checks run on isolated fixtures in a hidden corner stage and retire their canvases after each row, leaving the map above untouched.
| Result | Check | Detail |
|---|
Findings
- Keeping both map tile canvases offscreen and re-uploading them to WebGL 2 only when tiles arrive or the map moves keeps lens drags to uniform updates plus four small card quads, with zero tile recompositing during a drag.
- Clipping live DOM cards in
CARD_FRAGMENTagainst the exact lens centre and radius gives a crisp circular reveal behind the brass bezel, while pairinginert = truewith a zero-sizecanvasClipstops off-lens cards from stealing taps or Tab focus. - Because Chromium's
canvasCliptakes a single axis-aligned rectangle (measured on /toolkit-occlusion/), hit testing on a partially revealed card approximates the circular bezel with the chord-clamped visible rectangle rather than a true circle. - Adding a
Pin lenstoggle makes a big difference on touch screens: once you drag the lens over a neighbourhood card, locking the ring prevents a thumb tap near a checkbox or text input from dragging the lens away. - Credits: map data is © OpenStreetMap contributors, dark basemap tiles are © CARTO, and the multi-layer toolglass concept builds on Bier et al.'s 1993 Xerox PARC paper alongside /lens/ and /translate-lens/ on this site.
- Not verified yet: live browser runs in headless Chromium 157 and on Chrome 159 for Android (412 px touch drag, live OSM/CARTO CORS tile loads, lens pinning, checkbox/button taps and on-screen keyboard typing inside a revealed card). Running the live checks above and testing on a phone will verify those paths.