Now showing · live DOM, 9 metres away
A real form in a three.js scene
Three HTML documents on three kinds of surface: a laptop screen, a far-away billboard and a curved wall. Type, tab, select text,
drag the slider, scroll the list. They're ordinary DOM elements inside the <canvas>, uploaded as textures on every
paint, and the browser's hit testing follows them around the scene.
Drag empty space to orbit, wheel or pinch to zoom. Everything on a screen takes input. Hover states, the caret and the clock repaint the texture.
Show the code HTML on three.js meshes
- A drawable canvas, and ordinary HTML for each screen. The canvas is the three.js canvas. The billboard's document is shown here; the form and the wall are the same idea.
- Make the renderer on that canvas and attach each document to a mesh.
surfaces.add(element, mesh)moves the element into the canvas and gives the mesh a texture of it.{ hit: 'mesh' }is for curved meshes. - Call
surfaces.update()before each render. It keeps hit testing in step with the camera. These lines are inside the animation callback. - Inside the library: DOM to texture. On every canvas
paintevent, each element that changed is uploaded withgl.texElementSubImage2D(…, element)into its own sRGB texture with mipmaps. - Inside the library: tell the browser where the element is on screen. For a flat mesh one perspective matrix (element pixels to canvas pixels) goes to
canvas.updateElementGeometry(), and clicks, the caret and Tab then work natively. - The whole library.
Guest book
| # | From | Message | When |
|---|
Notes contenteditable
This wall is a 100° arc of a cylinder. No single matrix maps a rectangle onto it, so the element's geometry follows your pointer instead. Select this text, edit it, paste into it.
How is this hit tested?
On every pointer move the scene is raycast. The triangle and UV under the pointer give a small affine map that is exact at that spot, and updateElementGeometry() puts the element there.
Controls
A real link: all experiments ↗Live checks
| Result | Experiment | Detail |
|---|
How it is put together
import * as THREE from 'three';
import { HTMLSurfaces } from '/lib/html3d.js';
const renderer = new THREE.WebGLRenderer({ canvas }); // <canvas content="drawable">
const surfaces = new HTMLSurfaces(renderer, camera, { scene });
surfaces.add(form, laptopScreen); // flat: one projective matrix
surfaces.add(guest, curvedWall, { hit: 'mesh' }); // curved: geometry follows the pointer
renderer.setAnimationLoop(() => { surfaces.update(); renderer.render(scene, camera); });
Findings
- The method names changed. three.js r187 ships
HTMLTexture, but it callsgl.texElementImage2Dandqueue.copyElementImageToTexture. Chromium 157 hastexElementSubImage2DanddrawElementImageToTexture, so stock three draws nothing until a small shim is installed.html3d.jsfeature-detects both spellings. - Hit testing needs
updateElementGeometry(). three'sInteractionManagerpositions elements with a CSSmatrix3d. In this build a CSS transform on a WebGL-drawn element doesn't make it hit-testable; a perspectiveDOMMatrixpassed toupdateElementGeometry()does, and bounding rects, focus rings and the caret follow it. - Curved surfaces work if the geometry follows the pointer. A rectangle can't be mapped onto a cylinder with one matrix, but a mouse hit test only looks at one point. Raycast, take the triangle's UV Jacobian, and re-place the element on every pointer move.
- Occlusion is yours to handle. The DOM has no idea a drone is in front of the screen. The same raycast parks a hidden surface's geometry off-canvas so clicks can't pass through scene objects.
- Touch is the weak spot for curved surfaces. A tap has no pointer move before it, so the hit test runs against the flat fallback placement. Flat surfaces are exact for touch.
- The billboard needs mipmaps. Uploading into an immutable sRGB texture of the page's own and calling
generateMipmap()keeps the billboard readable from across the scene. three's stock path uses a single level.