Touch-friendly Scratch Card With Pure JavaScript – ScratchCard

Category: Javascript , Others | October 5, 2016
Author: Masth0
Views Total: 699
Official Page: Go to website
Last Update: October 5, 2016
License: MIT

Preview:

Touch-friendly Scratch Card With Pure JavaScript – ScratchCard

Description:

ScratchCard is a zero-dependency JavaScript library which takes advantage of HTML5 canvas to generate a touch-friendly, highly customizable scratch it card on the page.

How to use it:

Build the html for the scratch card.

<div class="scratch_container">
  <div class="scratch_viewport">
    <!-- result picture -->
    <canvas id="js-scratch-canvas"></canvas>
  </div>
</div>

The main CSS styles.

.scratch_container {
  position: relative;
  margin: 0 auto;
  max-width: 1024px;
}

.scratch_viewport {
  position: relative;
  width: 250px;
  height: 250px;
  margin: 0 auto;
  z-index: 0;
}

.scratch_picture-under {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  z-index: -1;
}

.scratch_container canvas {
  position: relative;
  width: 100%;
  height: auto;
  z-index: 1;
}

Create a new Scratch object and pass the options as an object like this:

var scratch = new Scratch({
    canvasId: 'js-scratch-canvas',
    imageBackground: 'loose.jpg',
    pictureOver: 'foreground.jpg',
    cursor: {
        png: 'piece.png',
        cur: 'piece.cur',
        x: '20',
        y: '17'
    },
    radius: 20,
    nPoints: 100,
    percent: 50,
    callback: function () {
      alert('I am Callback.');
    },
    pointSize: { x: 3, y: 3}
});

All default settings.

canvasId: '', // Canvas id
imageBackground: '', // Path [src]
pictureOver: '', // Path [src]
cursor: this.cursor, // Custom pointer
sceneWidth: 250, // Canvas width
sceneHeight: 250, // Canvas height
radius: 40, // Radius of scratch zone
nPoints: 10, // n points for clear canvas
pointSize: this.pointSize,
percent: null,
callback: null