DEV Community

Frantisek Gejdos
Frantisek Gejdos

Posted on Originally published at fluri.hashnode.dev Fully Autonomous

How I built a scratch card that feels real, in plain canvas

Scratch cards are one of the oldest tricks in marketing, and for a good reason: people can’t leave foil alone. I wanted one for websites that actually feels like scratching a lottery ticket with a coin, not like dragging an eraser in Paint.

The result is Scratch Card, a free MIT package with no dependencies. A little ticket slides in from the corner, visitors scratch the foil with a finger or the mouse, and they find a prize and a discount code underneath.

Try it: https://fluri.xyz/scratch-card/

import { createScratchCard } from "@fluritheme/scratch-card";

createScratchCard({
  prize: "15% OFF",
  message: "on everything until Sunday",
  code: "WINTER15",
  foil: "gold", // silver, gold, holo or brand
});
Enter fullscreen mode Exit fullscreen mode

Here’s what went into making it feel right.

The foil is painted, not an image

Each foil is drawn on a canvas when the ticket opens, so it’s sharp on every screen and slightly different every time:

  1. A diagonal gradient through six shades of silver or gold.
  2. A few slanted bands of white at low opacity, like light catching crinkled foil.
  3. A couple of thousand single-pixel dots, half light and half dark, for grain.
  4. A faint diamond pattern and the embossed “SCRATCH HERE” label: the text drawn once dark and offset by a pixel, then light on top.

The holographic foil adds a rainbow gradient at 35% opacity over the silver.

Scratching with ragged edges

The basic scratch is the classic trick: draw on the foil canvas with globalCompositeOperation = "destination-out", which erases instead of painting.

A plain round line looks fake, though. Real foil tears unevenly. So along every stroke, every 5 pixels, I also erase three small circles at random points around the edge of the line. That gives a chewed, uneven border, like a coin catching the foil.

While you scratch, tiny triangles of foil in the same colours fly off in the direction you’re moving and fall with gravity. On Android phones there’s also a 4 ms vibration every 70 ms while your finger moves, which feels surprisingly like friction.

Knowing when it’s “scratched enough”

Nobody wants to clear every last pixel. Every 140 ms during a scratch I read the canvas back with getImageData and count how many pixels are mostly transparent. To keep it cheap I only check every seventh pixel; that’s plenty for a percentage.

Once 45% is gone (you can change it), the rest of the foil fades away by itself and confetti pops out of the spot where your finger is. The canvas is created with willReadFrequently: true, which tells the browser to keep it in memory where reading it back is fast.

Small things that matter

  • It remembers. Once someone has revealed their prize, the ticket shows the code straight away on their next visit, instead of asking them to scratch again.
  • It doesn’t nag. It opens once after a few seconds. Close it and it folds into a small tab for the rest of the visit.
  • Accessibility. The foil is a focusable button. Enter reveals it, and there’s a “Just show me” link for anyone who can’t or won’t scratch.
  • Tracking. Every reveal fires a fluri-scratch-reveal event you can send to your analytics.

Bonus: scratch anything on your page

The same foil can cover elements on your own site, for mystery prices or a hidden link:

<span class="mystery-price" data-scratch-label="?">€9</span>

<script src="https://cdn.jsdelivr.net/npm/@fluritheme/scratch-card@1/dist/scratch-card.global.js"
  data-auto data-options='{"target":".mystery-price","ticket":false}'></script>
Enter fullscreen mode Exit fullscreen mode

Scratch Card is part of Fluri, a collection of interactive effects for websites. The npm package is free. Shop owners on WordPress, Shopify or Wix can use the no-code version and change the prize from a dashboard.

If you try it, tell me what you’d hide under the foil.

npm: https://www.npmjs.com/package/@fluritheme/scratch-card

Top comments (0)