A scratch card in the browser: destination-out, ragged edges and knowing when to stop
Scratch cards are a fun way to hand out a discount: the visitor does something, so the reveal feels earned. I built Scratch Card, a free MIT package (about 20 KB, no dependencies) that shows a foil ticket you scratch wit
Scratch cards are a fun way to hand out a discount: the visitor does something, so the reveal feels earned. I built Scratch Card, a free MIT package (about 20 KB, no dependencies) that shows a foil ticket you scratch with the mouse or a finger. It can also cover elements on your own page, like mystery prices.
Install
<script src="https://cdn.jsdelivr.net/npm/@fluritheme/scratch-card@1/dist/scratch-card.global.js" data-auto></script>
import { createScratchCard } from "@fluritheme/scratch-card";
createScratchCard({ prize: "15% OFF", code: "WELCOME15", foil: "gold" });
// or put foil over your own elements:
createScratchCard({ target: ".mystery-price", ticket: false });
Erasing with destination-out
The foil is a canvas over the prize. Scratching is drawing with globalCompositeOperation = "destination-out", which removes pixels instead of adding them. A plain round-capped line looks like an eraser tool, though, not a coin. Real scratching leaves a ragged edge, so along each segment I punch a few small random circles near the edge of the brush:
c.globalCompositeOperation = "destination-out";
c.lineCap = "round";
c.lineWidth = brush * 0.8;
c.beginPath(); c.moveTo(a.x, a.y); c.lineTo(b.x, b.y); c.stroke();
// Ragged edges, like a coin catching the foil.
for (let i = 0; i <= n; i++) {
for (let k = 0; k < 3; k++) {
const ang = Math.random() * Math.PI * 2, rr = brush * 0.42 * rand(0.75, 1.1);
c.beginPath();
c.arc(px + Math.cos(ang) * rr, py + Math.sin(ang) * rr, rand(1.2, brush * 0.16), 0, Math.PI * 2);
c.fill();
}
}
Little foil shavings fly off in the direction you're scratching, drawn on a separate overlay canvas.
Knowing when to stop
Nobody wants to scratch every last pixel. The card reveals itself once enough is gone (45% by default). To measure that, read the alpha channel with getImageData and count transparent pixels. Reading every pixel on every move would be slow, so it samples every 7th pixel and checks at most every 140 ms:
const data = c.getImageData(0, 0, canvas.width, canvas.height).data;
let clear = 0, all = 0;
for (let i = 3; i < data.length; i += 4 * 7) { // alpha of every 7th pixel
all++;
if (data[i] < 60) clear++;
}
progress = clear / all;
Then the foil fades away and a little confetti pops.
The foil itself
The foil is painted once: a metallic gradient, a few soft sheen bands like light on crinkled foil, fine grain, a faint diamond pattern and an embossed label. Silver, gold, holographic or your brand colour.
Accessibility and the small stuff
- The foil is a button: Enter reveals it, and there's a "Just show me" link for anyone who can't or doesn't want to scratch.
- On Android phones there's a tiny
navigator.vibrate(4)buzz while scratching, throttled to every 70 ms. - A reveal fires
fluri-scratch-reveal, and the result is remembered so returning visitors see their code again.
A note on codes: everything runs in the browser, so codes are visible in the page source. Use codes you're happy to share, or validate on your server.
It's one of the free, MIT-licensed packages behind Fluri, my collection of interactive website effects. If you run WordPress, Shopify or Wix there's a no-code version with live settings.
Would you scratch a mystery price on a product page, or is that too much?
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.