4 Tactile Multiplayer Web Patterns on Cloudflare D1: Scratch-Off Canvas, 400-Byte Vector Doodles & Zero-MP3 Audio
Building tactile, multiplayer web experiences usually pulls in heavy dependencies: auth providers, WebSockets, cloud object buckets for user drawings, and audio asset CDNs. Over the past week, while adding a zero-login
Building tactile, multiplayer web experiences usually pulls in heavy dependencies: auth providers, WebSockets, cloud object buckets for user drawings, and audio asset CDNs.
Over the past week, while adding a zero-login Friend-Filled Mystery 3x3 mode to Anime3x3 and a 60-Second Daily Reflex Challenge to FontNumbers, I wanted to see how much tactile polish we could pack into plain HTML5 Canvas, Web Audio API, and Cloudflare D1 at $0 marginal cost.
Here are four self-contained patterns you can drop straight into any React, Next.js, or vanilla JS app.
1. Physical Metallic Scratch-Off Foil with destination-out
Instead of a plain "Click to Reveal" button when friends submit mystery recommendations, a physical scratch-off canvas creates genuine anticipation on mobile touchscreens.
The trick is layering an <img /> (the revealed card) underneath an <canvas> painted with a diagonal metallic linear gradient, then switching ctx.globalCompositeOperation = 'destination-out' during pointer drag:
export function paintMetallicFoil(
ctx: CanvasRenderingContext2D,
w: number,
h: number
) {
ctx.globalCompositeOperation = 'source-over';
const grad = ctx.createLinearGradient(0, 0, w, h);
grad.addColorStop(0, '#78350f');
grad.addColorStop(0.5, '#fde68a');
grad.addColorStop(1, '#b45309');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, w, h);
// Subtle diagonal sheen lines
ctx.strokeStyle = 'rgba(255,255,255,0.22)';
ctx.lineWidth = 1.5;
for (let i = -h; i < w + h; i += 18) {
ctx.beginPath();
ctx.moveTo(i, 0);
ctx.lineTo(i - h, h);
ctx.stroke();
}
}
export function scratchAt(ctx: CanvasRenderingContext2D, x: number, y: number) {
ctx.globalCompositeOperation = 'destination-out';
ctx.beginPath();
ctx.arc(x, y, 24, 0, Math.PI * 2);
ctx.fill();
}
Rather than calling ctx.getImageData() on every pointermove (which forces a GPU-to-CPU readback stall on mobile Safari), quantize pointer coordinates into an 8 × 8 boolean Set<string>. Once touchedCells.size >= 22 (~35% surface area), fade out the canvas and play a reveal chime.
2. Storing Handwritten Doodles in < 1.2 KB of SQLite JSON (Zero S3/R2 Uploads)
Uploading PNG blobs for tiny Polaroid signatures introduces storage buckets, CORS headers, and moderation headaches. Instead, normalize pointer coordinates into integers in [0..100] and decimate points closer than 3% Euclidean distance:
export type Stroke = number[]; // [x0, y0, x1, y1, ...] in 0..100
export function decimateStroke(raw: Stroke, minDist = 3): Stroke {
if (raw.length < 4) return [];
const out = [raw[0], raw[1]];
for (let i = 2; i + 1 < raw.length && out.length < 80; i += 2) {
const dx = raw[i] - out[out.length - 2];
const dy = raw[i + 1] - out[out.length - 1];
if (Math.hypot(dx, dy) >= minDist || i + 2 >= raw.length) {
out.push(Math.round(raw[i]), Math.round(raw[i + 1]));
}
}
return out;
}
A typical 5-stroke heart or signature serializes to 220–480 bytes of JSON ([[15,55,35,25,55,65,80,20]]), fitting effortlessly in a TEXT column in Cloudflare D1 and replaying crisply at any DPI as an <svg viewBox="0 0 100 100">.
3. Zero-Asset Web Audio Reveal Arpeggio (0 KB MP3s)
Synthesizing a warm 4-note triangle-wave arpeggio (C5 → E5 → G5 → C6) takes 18 lines of TypeScript, never blocks initial page load, and avoids audio licensing issues:
export function playRevealChime() {
const AudioCtx = window.AudioContext || (window as any).webkitAudioContext;
if (!AudioCtx) return;
const ctx = new AudioCtx();
[523.25, 659.25, 783.99, 1046.5].forEach((freq, i) => {
const t0 = ctx.currentTime + i * 0.065;
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'triangle';
osc.frequency.setValueAtTime(freq, t0);
gain.gain.setValueAtTime(0.001, t0);
gain.gain.exponentialRampToValueAtTime(0.11, t0 + 0.02);
gain.gain.exponentialRampToValueAtTime(0.0001, t0 + 0.36);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(t0);
osc.stop(t0 + 0.38);
});
}
4. Cross-WebView Ownership Recovery Without Accounts
Zero-login social tools hit a classic trap: a user creates a board inside Instagram or Discord's in-app WebView (localStorage isolated), then opens the link later in mobile Safari and loses host permissions.
Our compromise:
- Generate a 32-byte random
ownerSecretstored inlocalStorage, storing onlySHA-256(ownerSecret)in D1 (verified via constant-time comparison). - Also issue a memorable 2-word + 2-digit recovery key (e.g.,
sunset-beacon-23), hashed with SHA-256 in D1, rendered onto a downloadable Owner PassCard PNG so hosts can reclaim ownership in any browser without email OTPs.
Live Sandboxes
If you want to poke at either implementation in production:
- Canvas Scratch-Off + Vector Doodle + D1 Group Grid: anime3x3.app (click "Ask Friends to Fill My 3x3")
- Unicode Enclosed Glyphs + Edge Percentile Histogram: fontnumbers.com/daily-number-challenge
Happy to answer questions on D1 batching, WebView localStorage edge cases, or Canvas HiDPI export!
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.