Scratch-off foil and thermal paper
I wanted to know if an R8 mask texture in one ElementGL canvas can coat a live HTML card in brushed metallic foil, gate pointer hits until you scratch past 35% of a control, and flip to heat-reactive thermal receipt paper. Drag to scratch the foil, or switch to thermal paper and rub to develop the ink.
0% scratched · 0 of 6 controls open
Show the code R8 mask texSubImage2D, anisotropic foil and thermal shaders, CPU hit gating, ElementGL paint
- One drawable wrapper holding both cards. Both the prize card and the thermal receipt sit inside one
<canvas content="drawable" layoutsubtree>, with covered controls marked bydata-gate. - Anisotropic foil and thermal paper fragment shader. Draws brushed metallic streaks, holographic diffraction and torn flake shadows in foil mode, or converts dark DOM luminance into thermal-carbon ink surrounded by warm amber halos in thermal mode.
- Selective R8 mask upload. Stamps jagged coin strokes or thermal stylus heat into a 256 by 192
Uint8Arrayand uploads only the dirty rectangle viagl.texSubImage2D()withUNPACK_ROW_LENGTH. - CPU shadow mask hit-test gating. Measures each control's cleared fraction against the 256 by 192 buffer and keeps
pointer-events: noneuntil cleared past 35%, then togglesdata-hit-tickto flush Chromium's hit-test cache. - Single ElementGL paint path and 2D fallback. Uploads the live DOM with
texElementSubImage2D(), aligns hit geometry withupdateElementGeometry(), and falls back todestination-out+ 5-argumentdrawElementImage()or plain HTML.
How it works
I built this experiment around a single ElementGL context from /lib/element-gl.js and a
256 by 192 single-channel Uint8Array uploaded as an R8 WebGL 2 texture. Each pointer stroke
updates only the dirty bounding box in that CPU array and calls gl.texSubImage2D() with
UNPACK_ROW_LENGTH, UNPACK_SKIP_PIXELS and UNPACK_SKIP_ROWS so no pixels are read
back from the GPU.
In scratch-off mode, the fragment shader combines directional brushed streaks, cell-based metallic flake normals,
pointer-tilt specular lighting and serrated edge noise to peel back the foil and cast a rim shadow onto the bright
HTML card beneath. Switching to thermal paper keeps the same R8 texture as a heat field: holding the
stylus down pours heat into the buffer, blooming dark receipt text and borders into carbon black inside warm amber
scorch halos while paper areas stay pale and waxy.
Hit-test gating runs straight off the CPU shadow mask. Covered controls start with pointer-events: none and
tabIndex = -1 so dragging across a button or input scratches the canvas instead of triggering taps or
opening the phone keyboard. Once more than 35% of a control's cached layout box reaches the cleared threshold, the
gating pass flips data-gated="open", toggles a data-hit-tick attribute after
canvas.updateElementGeometry() to clear Chromium's hit-test cache, and lets taps and typing land inside
the scratched window.
Live checks
These wait for a tap so the demo starts with no extra WebGL contexts running. Each check builds a small fixture in the corner of the window and retires it afterwards.
Findings
- Keeping the authoritative scratch and heat state in a 256 by 192
Uint8Arraylets the page stream dirty rectangles viagl.texSubImage2D()and compute per-control cleared fractions without a singlereadPixels()call during interaction. - Putting
pointer-events: noneon the drawable wrapper and unlocking individual[data-gate]controls only above 35% mask clearance avoids focusing hidden<input>fields or triggering text selection when a finger starts dragging directly on top of a covered control. - After switching a control from gated to open, toggling a
data-hit-tickattribute alongsidecanvas.updateElementGeometry()keeps Chromium's hit-test cache current soelementsFromPoint()sees the uncovered control straight away. - Using one drawable wrapper holding both
#sf-foil-cardand#sf-receiptkeepsElementGL.upload()tied to a single texture and avoids reallocating GPU storage when toggling between scratch foil and thermal paper. - Measured in HeadlessChrome 160 (2026-11-27, scale factor 2.625): all four live checks pass, touch dragging scratches openings past 35% to expose
Claim +1and#sf-name-inputwhile leaving untouched controls blocked, and switching to thermal paper develops dark carbon ink over warm amber halos withopenSnapshots: 0andunasked readbacks: 0. Not verified yet:npm run check:jank -- /scratch-foil/in headless Chromium 157, real touch hardware on Chrome 159 for Android, and the 2Ddestination-outfallback on a non-WebGL browser.