Gamifying promotions with HTML5 canvas destination-out scratch-off reveals
Revealing promotional discounts through an interactive scratch-off lottery ticket is one of the highest-converting gamification patterns. In this lab, we construct an HTML5 canvas scratch card with dynamic reveal thresholds and coin physics.
Three Steps to Gamified Scratch Cards
Select Foil Surface
Pick from silver metallic lottery latex, luxury champagne gold glitter, prismatic holographic foil, or matte carbon slate.
Configure Prize & Threshold
Enter your promotional coupon code, discount percentage, and configure automatic full reveal upon crossing 50% scratched area.
Export Clean Canvas Code
Copy zero-dependency vanilla JS destination-out erasing logic and responsive CSS cards ready for promotional landing pages.
Features & CSS Best Practices
Native Destination-Out Erasing
Erases canvas pixels dynamically using HTML5 2D compositing without requiring third-party game engine libraries.
High-Performance Alpha Sampling
Inspects every 16th pixel in the ImageData buffer, avoiding CPU bottlenecks and ensuring high frame rates during rubbing.
Auto-Reveal Celebration State
Automatically triggers a celebratory reveal animation and fires custom events once the user clears the required threshold.
Touch & Mobile Optimized
Features touch-action prevention to deliver responsive, drag-free rubbing across iPhone, iPad, and Android mobile displays.
Frequently Asked Questions
How does canvas scratch-off erasing work under the hood?
The canvas utilizes 2D context compositing with globalCompositeOperation = 'destination-out'. When drawing arc circles along the pointer path, existing foil pixels are turned 100% transparent, revealing the underlying prize DOM element.
How is the percentage of cleared scratch area calculated without lagging?
The controller samples pixel alpha values in the canvas ImageData buffer at 16-pixel step intervals. This sparse sampling technique reduces CPU cycles by over 93% while delivering accurate percentage progress telemetry.
Does this scratch card support mobile touchscreens and desktop mice?
Yes! Both touch events (touchstart, touchmove, touchend) and mouse events (mousedown, mousemove, mouseup) are tracked with passive: false to prevent mobile screen scrolling during scratching.
What happens when the user clears more than the threshold percentage?
Once cleared pixels cross the configured threshold (e.g. 50%), an auto-reveal CSS transition dissolves the remaining foil, fires a scratch-complete CustomEvent, and presents the unmasked prize.