Submerged page
I wanted to know if a live HTML form stays readable and clickable beneath a 2D wave-equation water surface with Snell refraction, optical caustics and a movable shoreline meniscus, without reading pixels back from the GPU or breaking touch scroll. Tap or drag on the water, type in the log box, slide the waterline from dry to submerged, and try the buttons underwater.
Show the code shallow-water PDE, Snell refraction and Jacobian caustics shader, ElementGL paint and hit-test offset
- One drawable form in one canvas. The live tide-station form sits inside a single
<canvas content="drawable" layoutsubtree>. - 2D shallow-water heightfield PDE. A 128 by 128 typed-array wave grid steps on the CPU with aspect-corrected Laplacian weights and packs height and gradients into an
RGBA8texture. - Snell refraction, Jacobian caustics and meniscus. The WebGL 2 fragment shader bends R, G and B through the water normal map, computes
1 / det(J)caustics across the live HTML texture and splits dry from wet at the undulating shoreline. - Single-context paint and geometry sync.
ElementGLuploads the DOM texture only when the form changes, uploads the 64 KB water texture each frame and keeps hit boxes synced withupdateElementGeometry().
How it works
The entire tide-station form sits as one drawable child inside one WebGL 2 canvas managed by
ElementGL from /lib/element-gl.js. Inside the paint handler,
stage.upload() calls gl.texElementSubImage2D() only when the form changes (typing,
toggling a checkbox, moving a slider, focus or resize). While ripples animate across calm DOM, the element texture
stays on the GPU with zero re-uploads and zero readback.
The water surface is a 128 by 128 2D damped wave equation in WaterField. I ran the PDE on
Float32Array buffers on the CPU and packed height, dh/dx, dh/dy and
curvature into one 64 KB RGBA8 texture uploaded with gl.texSubImage2D() each frame. That
avoids floating-point framebuffer extensions that can fail on mobile GPUs, gives hardware bilinear filtering
everywhere, and lets JavaScript read the exact surface gradient under the pointer with zero GPU readback.
In the fragment shader, the shoreline height undulates with the live wave field. Above the waterline the form renders
unrefracted. Below it, the shader computes a surface normal from the heightfield gradient plus subtle capillary
ripples, refracts the view ray with Snell's law (n = 1.333) and slight RGB dispersion, and takes finite
differences of the refracted UV map to form its 2 by 2 Jacobian determinant det(J). Where wave crests
converge rays (det(J) < 1), light scales up as 1 / det(J) to project bright optical
caustics straight over the live HTML text and controls.
Standard updateElementGeometry() leaves the form's hit box at its unrefracted CSS coordinates. Turning
on Refraction hit-test offset evaluates the local Snell displacement at the pointer from the CPU heightfield
and translates the element's canvasTransform matrix (toggling dataset.hitTick to clear
Chromium's hit-test cache) so the control under the pointer tracks its refracted image. The stage keeps
touch-action: pan-y and uses passive pointer listeners so vertical swipes on a phone scroll the page
normally while taps reach the underwater controls. An IntersectionObserver pauses the simulation loop
when the stage scrolls off screen.
Live checks
These run on their own small fixtures in the corner of the window and leave the demo above untouched.
Findings
- Running the 128 by 128 shallow-water PDE on CPU typed arrays and uploading one 64 KB
RGBA8texture avoids float framebuffer compatibility issues, keepsElementGLon a single WebGL 2 context, and lets hit-test compensation read wave slopes with zero GPU readback. updateElementGeometry()accepts oneDOMMatrixper drawable element. That lets me shift the form's hit box to match the local Snell refraction directly under the pointer, but it can't represent a non-linear per-pixel wave warp across several controls at once unless each control is split into its own drawable child.- Using passive pointer listeners and
touch-action: pan-yon the stage lets vertical touch swipes scroll the page while taps still trigger underwater buttons, checkboxes, sliders and splashes. - Not verified: live rendering, caustic clarity, hit-test accuracy, frame rate and touch scrolling in Chromium 157 (headless) and Chrome 159 on Android. Open this page in a browser with HTML-in-canvas enabled and tap Run on the live checks above to verify on your device.