LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Scratch Card Like Example For Captcha Or Coupons Using Plain JavaScript

Scratch Card Like Example For Captcha Or Coupons Using Plain JavaScript

Scratch-Card-Like-Example-For-Captcha-Or-Coupons-Using-Plain-JavaScript
Yet another code snippet to create a 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 a cursor or touch swipe. Moreover, it shows a real-time scratching animation.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add Scratch Card Like Example For Captcha Or Coupons Using Plain JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

Recommended For You:
How To Disable Copying Text From Blog And Website?

CSS:

<style type="text/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;
}
</style>

HTML:

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

JavaScript:

<!-- scratch-card-with-canvas JS -->
<script type="text/javascript">
(function(){

'use strict';

var canvas = document.getElementById('scratch'),
context = canvas.getContext('2d');

// default value
context.globalCompositeOperation = 'source-over';

//----------------------------------------------------------------------------

var x, y, radius;

x = y = radius = 150 / 2;

// fill circle
context.beginPath();
context.fillStyle = '#515151';
context.rect(0, 0, 300, 60);
context.fill();

//----------------------------------------------------------------------------

var isDrag = false;

function clearArc(x, y) {
context.globalCompositeOperation = 'destination-out';
context.beginPath();
context.arc(x, y, 10, 0, Math.PI * 2, false);
context.fill();
}

canvas.addEventListener('mousedown', function(event) {
isDrag = true;

clearArc(event.offsetX, event.offsetY);
judgeVisible();
}, false);

canvas.addEventListener('mousemove', function(event) {
if (!isDrag) {
return;
}

clearArc(event.offsetX, event.offsetY);
judgeVisible();
}, false);

canvas.addEventListener('mouseup', function(event) {
isDrag = false;
}, false);

canvas.addEventListener('mouseleave', function(event) {
isDrag = false;
}, false);

//----------------------------------------------------------------------------

canvas.addEventListener('touchstart', function(event) {
if (event.targetTouches.length !== 1) {
return;
}

event.preventDefault();

isDrag = true;

clearArc(event.touches[0].offsetX, event.touches[0].offsetY);
judgeVisible();
}, false);

canvas.addEventListener('touchmove', function(event) {
if (!isDrag || event.targetTouches.length !== 1) {
return;
}

event.preventDefault();

clearArc(event.touches[0].offsetX, event.touches[0].offsetY);
judgeVisible();
}, false);

canvas.addEventListener('touchend', function(event) {
isDrag = false;
}, false);

//----------------------------------------------------------------------------

function judgeVisible() {
var imageData = context.getImageData(0, 0, 150, 150),
pixels = imageData.data,
result = {},
i, len;

// count alpha values
for (i = 3, len = pixels.length; i < len; i += 4) {
result[pixels[i]] || (result[pixels[i]] = 0);
result[pixels[i]]++;
}

document.getElementById('gray-count').innerHTML = result[255];
document.getElementById('erase-count').innerHTML = result[0];
}

document.addEventListener('DOMContentLoaded', judgeVisible, false);

}());
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Recommended For You:
All About CSS3 With Features, Requirment And Information

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *