Curved 3D packaging customiser with live DOM labels
I wanted to see if a live HTML product label could wrap round a 3D coffee tin or vinyl sleeve in WebGL so you can spin the object, edit text and controls right on the curved surface, and unroll it into a flat print-ready PNG. Most 3D product configurators (such as three.js packaging and sneaker customisers) bake label text onto a 2D canvas first, which gives up native inputs, focus and accessibility; here I reused the curved-surface updateElementGeometry() tangent-plane hit-testing idea from /three-form/ on a single ElementGL context.
Drag empty space (or switch to Spin object on a phone) to turn the tin or sleeve. Tap any text field, menu, colour swatch or tasting note on the curved label to edit it live. Auto-orbit pauses while a field has focus.
Show the code ElementGL 3D packaging shader, tangent-plane hit testing and PNG export
- One drawable canvas holding the product label form. The label is a 720×360 two-panel grid with native inputs, a
<select>, theme buttons, checkboxes and an<output>. - One WebGL 2 context through
ElementGL. On everypaintevent,stage.upload(label)runsgl.texElementSubImage2D()into one texture and regenerates mipmaps so text stays legible around the curve. - One 3D mesh shader for the tin, vinyl sleeve and unrolled flat net. Switching to Flat print net animates the cylinder curvature or sleeve fold angle down to zero while blending studio lighting toward flat print illumination.
- Curved-surface hit testing with
updateElementGeometry(). On pointer move or tap, I raycast against the live mesh triangles, compute the local UV tangent-plane Jacobian matrix, place the element there and toggledataset.hitTickto clear Chromium's hit-test cache. - Print-ready flat PNG export on tap. In WebGL 2 I attach the flat uploaded label texture to a temporary framebuffer for one
gl.readPixels()call into a plain 2D canvas; in 2D mode I draw acaptureElementImage()snapshot, copy it to a plain canvas and close the snapshot straight away.
How it works
Most 3D packaging customisers on the web draw product text into an offscreen 2D canvas and upload that bitmap as a texture, which means you edit controls in a separate sidebar instead of on the object itself. Here the label (#pack-label) is a drawable child of a single <canvas content="drawable" layoutsubtree>, laid out as two 360×360 square panels side by side (720×360 CSS pixels).
The page opens one WebGL 2 context through new ElementGL(canvas, …) from /lib/element-gl.js and compiles a single 3D mesh shader on stage.gl. Every browser paint event uploads #pack-label via gl.texElementSubImage2D() with mipmaps. On the coffee tin, the full u ∈ [0, 1] width wraps 360° around a brushed-metal cylinder; on the vinyl sleeve, u ∈ [0, 0.5] covers the front jacket and u ∈ [0.5, 1] covers the back jacket beside a peeking grooved disc. Picking Flat print net smoothly animates the cylinder curvature (or the sleeve's back fold) to zero so both halves lie flat as one 2:1 sheet.
No single DOMMatrix can map a flat DOM rectangle onto a whole cylinder at once, so I follow the tangent-plane approach from /three-form/. On pointermove and pointerdown, I unproject the pointer through the inverse model-view-projection matrix and raycast the live triangles in JS. When the closest hit is a front-facing label triangle, I project one-pixel steps along the triangle's local UV tangent vectors to build an affine DOMMatrix that is exact at the hit point, pass it to canvas.updateElementGeometry(label, { canvasTransform }) and toggle label.dataset.hitTick so Chromium flushes its hit-test cache. When the ray hits the metal lid, the vinyl disc or empty space, or when you switch to Spin object, the label's hit box is parked off-canvas (unless a field inside it has keyboard focus) so dragging orbits cleanly.
Pressing Export flat label PNG runs only on your tap. Because the WebGL texture holds the unshaded flat DOM picture already, reading it through a temporary framebuffer with one gl.readPixels() call into a plain non-drawable 2D canvas produces a crisp flat PNG even while the 3D tin is curved and spinning. If WebGL 2 texElementSubImage2D() is missing, the page falls back to a 2D cylinder-slice renderer with 5-argument drawElementImage() calls; if HTML-in-canvas isn't available or the WebGL context is lost, #pack-label moves out into plain HTML so the form stays usable.
Live checks
| Result | Check | Detail |
|---|
Findings
- Credit and prior art. Curved-surface tangent-plane
updateElementGeometry()hit testing builds directly on/three-form/on this site. The packaging workflow responds to browser 3D product configurators (such as three.js packaging and sneaker customisers) that currently bake canvas text into textures instead of hosting live accessible DOM controls on the product itself. - One
ElementGLcontext is enough for a full 3D mesh shader. Reusingstage.glandstage.upload(label)keeps the page to one WebGL 2 context (within the mobile GPU budget) while letting custom 3D vertex/fragment shaders handle curved cylinders, rolled metallic lids, vinyl grooves and smooth unroll morphs. - A 2:1 two-panel label unifies cylinder, box and flat print net. Splitting the 720×360 label into front (
u ∈ [0, 0.5]) and back (u ∈ [0.5, 1]) lets the same DOM wrap 360° around a coffee tin, fold cleanly onto front and back vinyl jacket faces, and unroll straight into a flat print layout with zero DOM duplication. - Tangent-plane hit testing needs a touch-friendly orbit switch. On desktop, hovering updates the tangent plane before a click lands; on a 412 px phone there's no hover, so
pointerdownupdates the geometry in the capture phase and an explicit Spin object toggle lets you turn the tin without accidentally focusing an input. - Cross-canvas
ElementImageisolation affects flat PNG export. Chromium refuses drawing acaptureElementImage()snapshot onto a different canvas. Reading the flat uploaded WebGL texture via a one-shot FBOgl.readPixels()on user tap avoids both cross-canvas rejection and extra drawable canvases, and never runs in a frame loop. - Browser verification status. Source checks pass locally; headless Chromium and Chrome on Android testing of the live checks, touch orbit and FBO export are not verified yet (see
findings/pack-3d.md).