3D relief map with live DOM pins and weather cards
I wanted to know if a procedural WebGL 2 relief map with real-time sun shading could hold live HTML weather cards in 3D space without breaking clicks or typing. Drag to orbit the island, pinch or scroll to zoom, sweep the sun azimuth to shift shadows across the ridges, or edit the field note and temperature units directly inside any card.
Drag to orbit, wheel or pinch to zoom. Tap any button, select or field note inside a station card.
Show the code procedural relief terrain, single-context ElementGL, 3D card projection and hit boxes
- Four station cards inside the canvas. Each weather card is a
drawablechild of<canvas content="drawable" layoutsubtree>with buttons, a unit<select>and an editable text<input>. - Procedural alpine-and-coast heightfield. Deterministic five-octave noise with ridged peaks builds a 64 by 64 grid plus a plinth skirt with no network map tiles.
- Relief hillshading and card shaders. One shader colours elevation bands, anti-aliased contours, water glint and directional sun shading. The card fragment shader smooths tilted quad edges and adds depth haze.
- 3D quad projection and hit-test matrix. Both billboard and flat-on-map cards share one world-to-clip matrix for drawing and one element-to-canvas matrix for
updateElementGeometry(). - One WebGL 2 context for terrain and DOM. The
painthandler uploads changed cards throughElementGL, draws the relief mesh, terrain-conformed ground shadows and pin stems onstage.gl, then restoresElementGL's shader state to draw depth-sorted cards. - Texture upload in
element-gl.js.texElementSubImage2D()copies each card into a texture sized togetElementImageDefaultSize().
How it works
I took the idea from shader-driven relief maps like Mapbox GL and Tangram terrain hillshading, and from Three.js procedural topography experiments, then combined it with the 3D updateElementGeometry() hit-test pattern from /three-form/.
Instead of loading external map tiles or a 3D engine, a deterministic noise function generates a 64 by 64 island heightfield with a central peak, an eastern granite ridge, a western harbour bay and a southern headland.
Everything renders on one WebGL 2 context created by new ElementGL(canvas, ...). On each paint event, the terrain mesh draws first with depth testing on. The vertex shader stores raw height and unscaled slope gradients once in the buffer, so dragging the elevation scale slider updates peaks and surface normals on the GPU without rebuilding geometry.
Next, soft drop-shadow ellipses conformed to the local terrain height and screen-aligned pin stems draw over the slopes, shifting direction as you drag the sun azimuth slider.
After restoring ElementGL's shader program and vertex array object, the four station cards are sorted back-to-front by camera distance and drawn with stage.quad().
Each card's element-to-canvas projective matrix goes to canvas.updateElementGeometry(), scaled by geometryUnit() for high-DPI screens, and its data-hit-tick attribute toggles whenever the matrix changes to clear Chromium's hit-test cache.
If WebGL 2 HTML-in-canvas isn't available, the page falls back to a 2D isometric relief projection with five-argument drawElementImage(), or to plain HTML cards below the 2D relief canvas when HTML-in-canvas is disabled.
Live checks
These 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
- Drawing custom 3D WebGL 2 geometry and live DOM textures inside one
ElementGLcontext works by savingCURRENT_PROGRAMandVERTEX_ARRAY_BINDINGafterstage.begin(), rendering the terrain, conformed ground shadows and stems onstage.gl, and restoring both bindings before callingstage.quad(). This keeps the page to one WebGL context without modifyingelement-gl.js. - Sorting cards back-to-front by view-space depth and matching each card's
zIndexonly when the sort order flips aligns visual occlusion with DOM pointer hit-testing where two cards overlap. - In flat-on-map mode, keeping the camera pitch above 18 degrees keeps the top face of every horizontal card front-facing from all 360 degrees of camera yaw, so the cards stay readable and interactive as you orbit around them.
- Credits: terrain hillshading and hypsometric contour bands follow shader-driven relief map techniques from Mapbox GL, Tangram and Three.js procedural topography work; the projective
updateElementGeometry()pattern builds on /three-form/. - Not verified yet: live browser runs in headless Chromium 157 and on Chrome 159 for Android (412 px touch orbit, pinch zoom, native
<select>picker over a 3D card, and on-screen keyboard typing into a projected<input>). Running the live checks above and testing touch input on a device will verify those paths.