Create Scratch Card using JavaScript & HTML5 Canvas

Yet another code snippet to create scratch card using JavaScript and HTML5 canvas. This Vanilla JavaScript plugin allows you to hide your coupon code or other winning code that will show on scratch. You can place any number/text behind the scratch area. Users can scratch with cursor or touch swipe. Moreover, it shows a real-time scratching animation.

Scratch Card Plugin Overview and Preview

Plugin: scratch-card-with-canvas
Author: Prototype
Category: Others
Published: November 8, 2019
File Type: zip archive (HTML, CSS & JavaScript )
Package Size: 6 KB
Dependencies: No dependency!
Last Modified:
MIT
86 views

Code Rating

0
0.0 rating
0 out of 5 stars (based on 0 reviews)
Excellent0%
Very good0%
Average0%
Poor0%
Terrible0%

How to Create JavaScript Scratch Card

1. First of all create HTML canvas element and other necessary elements like below:

    <div class="card">
    <div class="base">Coupon Code: 123456789</div>
    <canvas id="scratch" width="300" height="60"></canvas>
    </div>

2. After that, style scratch card with CSS.

    .card{
         width: 300px;
         height: 60px;
         position: relative;
         box-shadow: 1px 2px 6px rgba(0, 0, 0, 0.2);
        }    
        
      .base, #scratch {
        cursor: default;
        height: 60px;
        width: 300px;
          position: absolute;
          top: 0;
          left: 0;
          cursor: grabbing;
      }
      .base {
        line-height: 60px;
        text-align: center;
      }
      #scratch {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0); 
        -webkit-touch-callout: none;
        -webkit-user-select: none;
      }

3. Finally, load the scratch card JS file into your HTML document and done.

<!-- scratch-card-with-canvas JS -->
<script src="js/scratch-card.js"></script>

Changelog

No data yet!

No Configuration Options!

There are no reviews yet. Be the first one to write one.

Submit Your Review