I recently added a Photo Nightlight tool to Tubox, my browser-local image toolbox. Drop in a portrait, landscape, or pet photo, preview how it looks both lit and unlit, and download a watertight binary STL file ready for a 3D printer slicer — all without uploading the photo to a server.
Here are a few practical details from building it in plain TypeScript, 2D Canvas, and a Web Worker.
Turning luminance into millimeters
A lithophane works because thinner white plastic lets more backlight through than thicker plastic. In Tubox, the plate thickness runs from 0.8 mm (brightest) to 3.2 mm (darkest), framed by a 2.5 mm border held at full thickness so the printed plate stays rigid.
Raw phone photos often look flat when mapped linearly to thickness, so the preprocessing step:
- Resamples the photo to a
384 pxworking long side and computes Rec.709 luminance. - Trims the bottom and top 2% of the histogram to stretch contrast gently.
- Caps the stretch factor at
1.55so low-contrast images don't amplify sensor noise. - Pads extreme aspect ratios to at least
1:4so ultra-narrow crops don't turn into fragile strips.
Backlit and unlit preview without WebGL
Shipping a 3D engine just to preview a flat lithophane plate felt unnecessary. Instead, the stage stacks two 2D canvases:
- Lit canvas: converts each pixel's thickness into approximate light transmission using an exponential Beer-Lambert curve (
exp(-0.58 * (mm - 0.8))) tinted toward a warm white LED. - Unlit canvas: takes horizontal and vertical thickness differences between neighbouring pixels to shade the pale relief surface.
Toggling the power switch simply cross-fades the two layers and adjusts the ambient halo. Adjusting the contrast slider (60%–160%) updates both canvases directly in the browser.
Building a watertight binary STL in a Web Worker
Slicers complain quickly if a mesh has open edges or inverted normals. To keep the geometry clean:
- The image area is sampled at
0.35 mmspacing using bilinear interpolation from the luminance buffer. - The flat back sits at
Z = 0, and the side walls and back fan share the exact perimeter vertices of the front relief grid. - Facet normals are computed with a cross product per triangle and written straight into an
ArrayBuffer(84-byte binary STL header + 50 bytes per triangle).
With the built-in 525 × 640 sleeping cat sample at 100 mm long side (83 × 100 mm plate; default is 80 mm), the mesh comes out to 128,322 triangles and 6.12 MB (6,416,184 bytes). Running the mesh builder inside a dedicated Web Worker keeps the UI responsive, and changing the photo or plate size mid-export cleanly terminates the previous worker.
Where it fits in Tubox
Tubox started around everyday local-only image tasks — starting with checking and stripping GPS coordinates, timestamps, and camera metadata in EXIF Viewer & Cleaner before sharing original photos, alongside batch compression, AI background removal, ID photos, collages, and GIF conversion. Adding a local lithophane STL generator was a fun extension of the same constraint: keep personal photos on the user's own device.
If you print lithophanes with white PLA, I'd love to hear what default thickness range or plate sizes work best for your setup.