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 @tofandel/scratchcard-js

Import ScratchCard

import ScratchCard from '@tofandel/scratchcard-js';

Configuration

See the types in action: Lineopen in new window, Sprayopen in new window, Circleopen in new window, Brushopen in new window

NameTypeDefault valueComment
scratchTypeSCRATCH_TYPELINEPossibles values : LINE, SPRAY, CIRCLE, BRUSH
percentToFinishnumber50The percentage at which the scratchcard will consider itself fully uncovered
containerWidthnumber100
containerHeightnumber100
brushSrcstring""For type.BRUSH
imageForwardSrcstring""
imageBackgroundSrcstring""
htmlBackgroundstring""
`<p>Html-content<p>`
callbackfunctionfunction() { alert('done.'); }
clearZoneRadiusnumber0For type.CIRCLE and type.LINE
nPointsnumber30For type.SPRAY
pointSizenumber4For 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();
  // ...
});
Last Updated:
Contributors: Tofandel, Masth0