Foil cards and world-locked dithering
I wanted to see if one live HTML playing card could switch smoothly between Balatro-style Foil, Holographic, Polychrome and Negative shaders, plus Return of the Obra Dinn 3D sphere-locked 1-bit dithering, without losing clickable controls when tilted. Drag the card to tilt it in 3D, type into its title box, tap the chip and mult counters, then switch to Obra Dinn mode and toggle between Screen-space and Sphere-locked to watch why the 3D view sphere stops dither swimming.
Show the code card markup, 5 optical shaders, 3D view-sphere dithering, spring tilt and hit sync
- The live playing card inside one drawable canvas. The editable title
<input>, counter buttons, suit toggle, seal toggle and shaded SVG medallion sit inside one<canvas content="drawable" layoutsubtree>. - Toroidal 32x32 blue-noise generator. Ulichney's void-and-cluster pass builds a tileable rank table once on startup and uploads it to texture unit 1.
- Five optical editions in one WebGL 2 fragment shader. Switching
uEditionswaps between anisotropic Foil, prismatic Holographic scan-bars, thin-film Polychrome iridescence, solarized Negative edges and Obra Dinn 1-bit dithering without recompiling. - 3D view-sphere angular grid vs screen-space coordinates. Quantizing equirectangular angles from
normalize(rayDir)keeps Bayer 8x8 plus blue-noise dots anchored to the card in 3D space instead of sliding acrossgl_FragCoord.xy. - 3D perspective projection, spring physics and hit testing. One
DOMMatrixdrives WebGL clip space andcanvas.updateElementGeometry()scaled bygeometryUnit(), togglingdataset.hitTickwhen the pose moves. - 2D
drawElementImage()fallback and plain-HTML recovery. Falls back to 5-argumentdrawElementImage()if WebGL 2 is absent, or moves#collector-cardinto plain HTML if the context is lost.
How it works
CSS mix-blend-mode and static gradients can approximate a sheen over flat DOM nodes, though they can't drive anisotropic diffraction gratings from a 3D surface normal or re-index an ordered dither matrix onto a 3D sphere.
Here one ElementGL WebGL 2 context uploads #collector-card via gl.texElementSubImage2D() whenever the input, counters, suit or seal change, then draws the card as a single perspective-tilted quad.
Switching editions sets a uniform integer uEdition instead of recompiling shaders, so Foil, Holographic, Polychrome, Negative and Obra Dinn 1-bit switch in zero milliseconds.
Foil modulates an anisotropic brushed-metal specular lobe along embossed borders; Holographic sweeps prismatic cosine diffraction bars across the surface; Polychrome evaluates a thin-film optical path difference with a Fresnel rim boost; Negative inverts into a darkroom plate with Sobel-like emerald and magenta edge halos.
In Obra Dinn 1-bit mode, I wanted to show directly why Lucas Pope projected his dither pattern onto a 3D sphere instead of flat screen coordinates.
In Screen-space mode, the 8x8 ordered Bayer plus 32x32 blue-noise threshold is sampled at gl_FragCoord.xy, so tilting or orbiting the card drags its shaded gradients across a stationary screen door.
In Sphere-locked mode, each fragment constructs a 3D unit vector rayDir = normalize(vec3(localPx, -uSphereRadius)), converts it to equirectangular angles (theta, phi), and quantizes those angular coordinates to select the Bayer and blue-noise cell before outputting strictly two binary colours (such as #333319 and #e5ffff).
To keep the card clickable and typeable while tilted, the same 3D perspective DOMMatrix passed to WebGL clip space (with clip Z zeroed so near and far planes never clip) is scaled by geometryUnit(canvas, cardEl, cardW) from /lib/surface.js and passed to canvas.updateElementGeometry().
Whenever the rounded pose key changes, cardEl.dataset.hitTick toggles so Chromium clears its hit-test cache, and under prefers-reduced-motion: reduce spring release snaps instantaneously without running an animation loop.
Live checks
These checks wait for a tap so the page opens with one GPU context. Each check runs on its own isolated fixture on the test stage and retires its canvas when finished.
Findings
Not verified in a live browser yet. Run the checks above in Chromium with #canvas-draw-element enabled to measure this device.
- Single-shader edition switching: keeping all five optical modes in one
ElementGLfragment shader withuEditionavoids shader recompilation pauses on mobile GPUs while keeping one WebGL 2 context for the entire page. - Texture re-upload gating: only calling
stage.upload(cardEl)whencardDirtyis set ore.changedElementsincludes the card lets pointer light sweeps and 3D spring tilting run without re-runningtexElementSubImage2D()every frame. - Strict 1-bit binary output: clipping card corners at
tex.a < 0.5instead of applying border anti-aliasing after dithering keeps Obra Dinn mode strictly two-tone inside the card so no mid-grey edge pixels leak in. - Sphere-locked vs screen-space: indexing the 8x8 Bayer plus blue-noise grid by equirectangular
(theta, phi)angles fromnormalize(rayDir)locks every halftone dot onto the tilting 3D card surface instead of swimming overgl_FragCoord.xy. - Not verified: live WebGL 2 shader compilation and pixel readbacks in headless Chromium 157, layout stability under
npm run check:jank -- /card-editions/, and real touch tilt plus on-screen keyboard typing on Chrome 159 for Android at 412 px and DPR 2.625.