Skip to content

Files

Latest commit

 

History

History
72 lines (63 loc) · 2.08 KB

readme.md

File metadata and controls

72 lines (63 loc) · 2.08 KB

Scratchcard-js

ScratchCard is a js lib to simulate a scratchcard in browser with html5 and canvas.

Install

You can install ScratchCard with npm:

npm install --save scratchcard-js

or just copy the file scratchard.min.js

Import ScratchCard

import { ScratchCard, SCRATCH_TYPE } from 'scratchcard-js';
// or
import 'scratchcard-js'; 

Configuration

::: Tip The first argument of the ScratchCard instance can be a string or an HTMLElement. :::

const scContainer = document.getElementById('js--sc--container');
const sc = new ScratchCard(scContainer, {
    scratchType: SCRATCH_TYPE.LINE,
    containerWidth: scContainer.offsetWidth,
    containerHeight: 300,
    imageForwardSrc: '/images/scratchcard.jpg',
    imageBackgroundSrc: '/images/result.png',
    htmlBackground: '',
    clearZoneRadius: 20,
    nPoints: 0,
    pointSize: 0,
    callback: function () {}
})

See the SCRATCH_TYPES in action: Line, Spray, Circle, Brush

Name Type Default value Comment
scratchType SCRATCH_TYPE LINE Possibles values : LINE, SPRAY, CIRCLE, BRUSH
containerWidth number 100
containerHeight number 100
brushSrc string "" For SCRATCH_TYPE.BRUSH
imageForwardSrc string ""
imageBackgroundSrc string ""
htmlBackground string ""
`<p>Html-content<p>`
callback function function() { alert('done.'); }
clearZoneRadius number 0 For SCRATCH_TYPE.CIRCLE and SCRATCH_TYPE.LINE
nPoints number 30 For SCRATCH_TYPE.SPRAY
pointSize number 4 For SCRATCH_TYPE.SPRAY

Initialization method

sc.init().then(() => {
  // Do what you want
  // ex: listen scratch.move event
}).catch((error) => {
  // image not loaded
});

Event: scratch.move

sc.canvas.addEventListener('scratch.move', () => {
  let percent = sc.getPercent();
  // ...
});