Touch-friendly Scratch Card With Pure JavaScript – ScratchCard

Category: Javascript , Others | September 13, 2018
Author:Masth0
Views Total:21,146 views
Official Page:Go to website
Last Update:September 13, 2018
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.

scratchType: SCRATCH_TYPE, // BRUSH, SPRAY, or CIRCLE
containerWidth: number,
containerHeight: number,
imageForwardSrc: string,
imageBackgroundSrc: string,
htmlBackground: string,
clearZoneRadius: number,
nPoints: number,
pointSize: number,
percentToFinish: number
callback ?: () => void,
brushSrc: string,
cursor: {
  cur: string,
  png: string,
  poosition: number[]
},
enabledPercentUpdate: boolean

Changelog:

v1.4.6 (09/13/2018)

  • Bugfix

You Might Be Interested In:


4 thoughts on “Touch-friendly Scratch Card With Pure JavaScript – ScratchCard

  1. Anuj Gupta

    HI, I want to use it in my ionic 2 app. can u guide me for this.

    Reply
  2. Suraj Raj Bhandari

    This is really good, Is it possible to create full screen scratch card using this library.

    Reply

Leave a Reply