How to Build an Interactive Scratch-to-Reveal Coupon with HTML5 Canvas & Confetti Particles

The Quick Answer

An interactive scratch-off coupon renders an opaque metallic silver surface on an HTML5 canvas overlaying hidden coupon HTML. When the user drags across the canvas, `ctx.globalCompositeOperation = "destination-out"` cuts transparent circles through the surface, revealing the code underneath.

Interactive Demo Live Preview
🎉 You Won!
SAVE30
Use code at checkout
🪙 Rub cursor over card to scratch metallic foil

Step 1: Layer the Hidden Prize Behind the Canvas Overlay

Place the secret coupon code in an underlying HTML container and position the scratch canvas directly over it.

scratch-card.html
<div class="scratch-card-wrapper" id="scratchCard">
  <!-- Secret Prize Layer -->
  <div class="scratch-prize">
    <span class="prize-tag">FLASH SALE DISCOUNT</span>
    <span class="prize-code" id="couponCode">PLAYGROUND50</span>
    <span class="prize-desc">50% off all Pro developer tools</span>
    <button class="copy-coupon-btn" id="copyCouponBtn">Copy Coupon</button>
  </div>
  <!-- Interactive Silver Coating -->
  <canvas class="scratch-canvas" id="scratchCanvas" width="360" height="200"></canvas>
</div>

Step 2: Paint the Metallic Foil Overlay with Canvas Gradients

Fill the canvas with a patterned silver gradient and instructional teaser text before scratches begin.

scratch-setup.js
const canvas = document.getElementById('scratchCanvas');
const ctx = canvas.getContext('2d');

function initFoil() {
  const grad = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
  grad.addColorStop(0, '#94a3b8');
  grad.addColorStop(0.5, '#cbd5e1');
  grad.addColorStop(1, '#64748b');
  ctx.fillStyle = grad;
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  ctx.font = 'bold 15px sans-serif';
  ctx.fillStyle = '#1e293b';
  ctx.textAlign = 'center';
  ctx.fillText('✨ Scratch Here to Reveal Code ✨', canvas.width / 2, canvas.height / 2 + 5);
}
initFoil();

Step 3: Erase Pixels and Calculate Scratched Threshold

Erase pixels along pointer coordinates and trigger celebration when threshold passes 60%.

scratch-erase.js
let isScratching = false;

function erase(x, y) {
  ctx.globalCompositeOperation = 'destination-out';
  ctx.beginPath();
  ctx.arc(x, y, 22, 0, Math.PI * 2);
  ctx.fill();
  checkPercentage();
}

function checkPercentage() {
  const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  let clearPixels = 0;
  for (let i = 3; i < imgData.data.length; i += 16) { // sample every 4th pixel
    if (imgData.data[i] === 0) clearPixels++;
  }
  const pct = (clearPixels / (imgData.data.length / 16)) * 100;
  if (pct > 60) {
    canvas.style.transition = 'opacity 0.5s';
    canvas.style.opacity = '0';
    canvas.style.pointerEvents = 'none';
  }
}

canvas.addEventListener('pointerdown', (e) => { isScratching = true; erase(e.offsetX, e.offsetY); });
canvas.addEventListener('pointermove', (e) => { if (isScratching) erase(e.offsetX, e.offsetY); });
window.addEventListener('pointerup', () => { isScratching = false; });

Next-Level Addons: Elevating Your Implementation

Take this technique further by combining it with complementary design token studios and animation tools:

1. Canvas Destination-Out Foil Erasing

Explore in Scratch-to-Reveal Coupon Studio →

Use HTML5 Canvas globalCompositeOperation = "destination-out" to erase silver foil pixels transparently under touch gestures.

CLEARED: 62% [AUTO-REVEAL]

2. Percentage Cleared Threshold Detection

Explore in Scratch-to-Reveal Coupon Studio →

Sample alpha channels via getImageData() to automatically clear remaining foil when 50% of the coupon has been scratched.

3. Metallic Silver & Gold Shimmer Gradients

Explore in Scratch-to-Reveal Coupon Studio →

Render realistic metallic flake textures with multi-stop linear gradients and subtle procedural noise overlays.

Chaam
Chaam's Pro Tip

When building high-performance UI animations or particle systems, my golden rule is: never calculate layout triggers (like getBoundingClientRect() or offsetHeight) inside animation loops. Read measurements once upfront and use CSS transforms or Canvas for buttery 60fps.

Why I Built the Scratch-to-Reveal Coupon Studio

I got tired of guessing values and testing browser quirks in DevTools, so I built this tool to tweak and preview values in real time with 1-click code export.

Open in Scratch-to-Reveal Coupon Studio ↗

Frequently Asked Questions

Practical answers to common implementation questions, browser quirks, and performance tips.

How does globalCompositeOperation = "destination-out" work?

Destination-out retains existing canvas pixels where new drawing shapes DO NOT overlap, effectively erasing pixels and leaving transparency wherever you stroke.

Does sampling getImageData cause lag on mobile devices?

Iterating every pixel of a high-resolution canvas can cause frame drops. Stride sampling (checking every 4th or 8th pixel) reduces computation by 75–85% with zero perceptible loss in percentage accuracy.

How do you handle high-DPI (Retina) screens for scratch cards?

Multiply canvas.width and canvas.height by window.devicePixelRatio and scale the context via `ctx.scale(dpr, dpr)` to ensure crisp edges without blur.

Can the scratch card be refreshed to allow another attempt?

Yes, reset canvas opacity, clear destination-out composite mode, and invoke `initFoil()` again to restore the initial silver coating.

Is this scratch card accessible for users unable to use a mouse or touch?

Always provide an accessible fallback button (e.g. "Click to Reveal Voucher") that automatically triggers the reveal threshold for keyboard and screen reader users.

← Back to all blog guides Launch Generator Studio →