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.

Roastery tin // live DOM Lot 042
Notes: Bergamot, Dark Cocoa
250 g · £14.50 Front panel
Colourway & notes 360° wrap
HTML-IN-CANVAS
720×360 PRINT NET
starting…

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
  1. 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>.
  2. One WebGL 2 context through ElementGL. On every paint event, stage.upload(label) runs gl.texElementSubImage2D() into one texture and regenerates mipmaps so text stays legible around the curve.
  3. 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.
  4. 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 toggle dataset.hitTick to clear Chromium's hit-test cache.
  5. 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 a captureElementImage() 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

ResultCheckDetail

Findings