three.js in a static site (bundling, viewers, snapshots)
threejswebglesbuild3d-viewersnapshotstouch
Notes from using three.js (r186) without a framework on 3d.jhampton.io and on justinahampton.com pages (the orrery's 3D view, the planet globe).
Getting the library
- three r186 on npm ships no
.min.jsand no fonts. Bundle once with esbuild:npx esbuild build/three.module.js --bundle --minify --format=esm, and vendor the result underassets/vendor/with an immutable cache rule. Fetch fonts (Helvetiker JSON) from the GitHub repo at the matching tag. 3MFLoaderalso needslibs/fflate.module.jsandutils/BufferGeometryUtils.js.- Copy the vendored folder between sites rather than re-bundling.
Viewer details
- Show size in mm, triangle count and volume; users of print files care about all three.
- Recompute normals for STLs (exporters often write zeros, which renders black).
- Replace a loader's materials carefully: three's 3MFLoader path lost the file's colors when the viewer forced one material. A custom reader that builds one mesh per color is more faithful (3MF files and the Bambu Studio project layout).
- On touch,
touch-action: noneon the canvas makes the page impossible to scroll past the viewer. Usepan-y: one finger scrolls, two fingers orbit/zoom. - "See the back" of a flat object: rotate the camera 180 degrees about the horizontal axis through the target, which matches turning a page over.
No-build alternative: import map from a CDN
For a single-file toy, skip bundling: an <script type="importmap"> mapping three to
https://unpkg.com/three@0.160.0/build/three.module.js (and three/addons/ to the examples
folder) lets import * as THREE from 'three' work in a plain HTML file opened from any static
server. Turbo Circuit 3D (a Trackmania-style racer, 2026-08) was built this way, with procedural
low-poly meshes and no asset files. Give each variant its own localStorage key so a 2D and a 3D
version of the same game do not overwrite each other's best laps.
Orienting a flat object across a path
To stand a banner (a start gate) across a track at a point with ground-plane normal nrm (the
across-track direction, in x/z): local +X under rotation.y = θ maps to world
(cos θ, 0, -sin θ), so θ = -Math.atan2(nrm.z, nrm.x) aligns the banner's width with the normal
exactly. An extra + Math.PI/2 (a common reflex) aligns it with the tangent instead and the gate
renders edge-on as a sliver along the road. Derive rotations by mapping a local axis to world
coordinates rather than by trial and error.
Snapshots
- A WebGL canvas cannot be read back a frame later (the drawing buffer is cleared after compositing unless
preserveDrawingBufferis set). Render, then calltoBlobin the same task. - The 3d site's thumbnails are WebP blobs snapshotted in the browser and uploaded, so no server-side renderer is needed.
Headless rendering
WebGL in headless Chrome needs SwiftShader: --use-gl=angle --use-angle=swiftshader --enable-unsafe-swiftshader. A full model page screenshot works with plain --screenshot and --virtual-time-budget=15000. Software GL frame rates say nothing about a real GPU; add a quality safety valve (the planet drops fine relief after 12 slow frames in a row) and say it is untested on hardware.
See also
- Headless Chrome testing gotchas
- Generating watertight print meshes in code (three.js, earcut, manifold)
- 3d.jhampton.io (print library and parametric template maker)