Street-level HTML overlays painted onto a tilted OpenStreetMap grid
I wanted to know if live interactive HTML signs, departure boards and route badges can lie flat in 3D perspective on a tilted OpenStreetMap road surface and keep accurate hit testing while you drive, rotate and pitch the view. Drag across Central London to pan, adjust the heading and pitch sliders, press Next step on the painted turn card, change the bus line filter, toggle Avoid tolls or edit the checkpoint note directly on the street.
Drag to pan across Whitehall and Embankment. Tap Next step, pick a bus route, toggle Avoid tolls or edit the checkpoint note.
Show the code zoom-16 OpenStreetMap tile atlas, single-context ElementGL road shader, projective flat and upright matrices
- Four interactive street overlays inside the canvas. Turn-by-turn directions, a transit departure board, a speed and toll plate and an editable checkpoint note sit as
drawablechildren inside<canvas content="drawable" layoutsubtree>. - Zoom-16 OpenStreetMap tile atlas with offline street fallback. A plain 1024 by 1024 offscreen canvas draws a procedural Central London street layout first, then paints each CORS-enabled OpenStreetMap tile into its slot as it loads from memory or network.
- Road plane, glowing route polyline and asphalt-blended card shaders. The road shader renders the pitched tile grid with horizon fog, the route shader animates forward chevrons and card drop shadows, and the card fragment shader blends subtle tarmac grain onto flat painted cards.
- Singularity-free camera and projective hit-test matrices. An analytical heading-and-pitch basis avoids gimbal lock at 0 degrees top-down, builds clip and canvas matrices for both flat painted overlays and upright street signs, and updates
updateElementGeometry()withgeometryUnit(). - One ElementGL WebGL 2 context for map, route and DOM. The
painthandler uploads dirty cards viastage.upload(), draws the tile road plane and route polyline onstage.gl, restoresElementGL's shader state and draws depth-sorted cards. - Texture upload in
element-gl.js.texElementSubImage2D()copies each live HTML overlay into a WebGL 2 texture sized togetElementImageDefaultSize().
How it works
I took the visual idea from in-car AR navigation and painted road markings in Apple Maps, Google Maps Live View and Mapbox Navigation, and built it over live OpenStreetMap zoom-16 tiles around Trafalgar Square, Northumberland Avenue, Victoria Embankment and Whitehall.
The projective updateElementGeometry() hit-testing approach follows /three-form/ and /relief-map/ on this site.
A plain detached 1024 by 1024 canvas holds a 4 by 4 grid of zoom-16 OpenStreetMap tiles (x = 32743..32746, y = 21791..21794) fetched with crossOrigin = 'anonymous' and cached in memory.
Before those tiles arrive, or if the device is offline, a procedural fallback paints the same Central London streets, River Thames curve and embankment parks into the atlas using the exact Web Mercator projection, so the road surface and route polyline align either way.
Everything on screen runs inside one WebGL 2 context from new ElementGL(canvas, ...).
After stage.begin(), I save ElementGL's active program and vertex array object, upload any newly loaded OpenStreetMap tiles to the road texture with mipmaps, draw the pitched road plane, then draw the glowing route polyline and soft ground shadows on stage.gl.
Restoring ElementGL's program and VAO lets stage.quad() draw the four depth-sorted HTML overlays straight onto the tarmac.
In Painted on road mode, each card lies horizontally just above the asphalt and aligns to its street bearing so perspective foreshortening narrows the far edge as you raise the camera pitch toward 68 degrees.
Switching to Upright sign stands each card vertically on a post facing the camera's horizontal heading.
In both modes, the exact 4 by 4 projective matrix mapping CSS pixels (0..w, 0..h) to canvas coordinates is scaled by geometryUnit() and passed to canvas.updateElementGeometry(), toggling dataset.hitTick whenever the transform shifts so Chromium's hit-test cache stays fresh.
If WebGL 2 HTML-in-canvas isn't available, the page falls back to a 2D affine projection using five-argument drawElementImage(), or to plain HTML cards below a 2D road map if HTML-in-canvas is turned off or the context is lost.
Live checks
These checks run on isolated fixtures in a hidden corner stage and retire their canvases after each row, leaving the road map above untouched.
| Result | Check | Detail |
|---|
Findings
- Building the camera basis analytically from road heading and pitch angle avoids the
lookAtup-vector singularity at 0 degrees top-down, so sweeping the pitch slider smoothly from a flat plan view to a 68-degree driver's-eye angle keeps both the road plane and projective hit boxes stable. - Using one
ElementGLWebGL 2 context for the OpenStreetMap tile plane, animated route ribbon, ground shadows andtexElementSubImage2D()DOM quads works by saving and restoringCURRENT_PROGRAMandVERTEX_ARRAY_BINDINGaround the custom passes, keeping the page to a single GPU context. - Composite map tiles in a plain offscreen canvas before uploading to WebGL with mipmaps (and anisotropic filtering where supported) prevents flickering gaps while tiles download and gives a deterministic offline street layout at the same Web Mercator coordinates.
- Credits: street map tiles come from OpenStreetMap (© OpenStreetMap contributors); painted road-surface navigation overlays draw on in-car AR navigation and painted road markings in Apple Maps, Google Maps Live View and Mapbox Navigation; projective
updateElementGeometry()hit testing builds on /three-form/ and /relief-map/. - Not verified yet: live browser runs in headless Chromium 157 and on Chrome 159 for Android (412 px touch panning, tapping Next step on a foreshortened flat road card, opening the native
<select>line filter at 68 degrees pitch, typing into the painted checkpoint<input>with the on-screen keyboard, and CORS tile loading over a mobile network). Running the live checks above and testing on a phone will verify those paths.