Physical UI
Three ordinary forms on three materials. Grab one by its background and pull it about, then let go. Press a button or type and the material takes the hit. Scroll the page and they lag behind. The fields keep focus while this happens, and a click reaches the control that is drawn under the pointer.
<physical-ui material="jelly">
<physical-ui material="cloth" wind="0.12">
<physical-ui material="paper">
Show the code forms on jelly and cloth
- Wrap a form in
<physical-ui material="…">. The form is ordinary HTML and keeps working. - Load the element. Wind and gusts are optional.
allis the three elements. - Inside the element: the form becomes a texture that is drawn on a simulated mesh. This is the WebGL route; on WebGPU the call is
device.queue.drawElementImageToTexture({ source }, { texture }). - Inside the element: hit testing follows the mesh. Find the triangle under the pointer, work out the affine matrix that carries its flat shape to its bent shape, and hand that to
updateElementGeometry(). A click then lands on the control you see. - The whole element.
<script type="module" src="/lib/physical-ui.js"></script>
<physical-ui material="cloth" wind="0.1">
<form>…a real form…</form>
</physical-ui>
How it works
- The form is a texture on a mesh.
texElementSubImage2D()uploads the element whenever it repaints (typing, focus rings, the caret). The mesh is a grid of point masses joined by distance constraints, stepped with Verlet integration and lit from its surface normals. - It uses WebGPU, then WebGL. The mesh shader is written in both WGSL and GLSL. The element uses WebGPU when the browser has a real adapter that passes a self-test and accepts the first frame, and WebGL otherwise; the line under the wind control says which and why, and
backend="webgpu|webgl"names one by hand. The simulation and hit testing are the same code either way. - Materials are parameters. Jelly has soft links and a spring to home. Cloth has stiff links, a pinned top edge and a pendulum-like pull home that weakens down the sheet. Paper adds bending links so it folds instead of stretching.
- Hit testing uses the triangle under the pointer. An element only has one transform, and a wobbling mesh isn't one transform. But only the point under the cursor matters, so each frame the element's geometry is set to the affine transform of the mesh triangle the pointer is over. The control you see under the cursor is the one you click.
- At rest the mesh is the layout box, and the simulation stops until you touch it. With reduced motion on, it never moves.
- Keyboard and assistive tech see a normal form. Tab order, labels and focus are untouched; activating a control from the keyboard pushes the material too.
Live checks
| Result | Experiment | Detail |
|---|