Skip to content

API Reference

Documentation for the main classes, options, methods, and callbacks of Scratcher.js.

The core class Scratcher of Scratcher.js handles all scratch engine operations. Below are public constructors, properties, methods, and usage examples.

snapshot

An object that contains the scratch state (progress, scratched cells count, etc.).

Return value

TypeDescription
ScratchSnapshot{ scratchedCells, totalCells, progress } Scratch state object

Example

js
const progress = scratcher.snapshot.progress; // 0~1

isDrawing

Whether scratching is currently in progress.

Return value

TypeDescription
booleantrue if scratching

Example

js
if (scratcher.isDrawing) {
  // Currently scratching
}

isCompleted

Whether scratching is complete (completionThreshold reached).

Return value

TypeDescription
booleantrue if complete

Example

js
if (scratcher.isCompleted) alert('Complete!');

shouldRevealOnCompletion

Whether to auto-reveal all on scratch completion.

Return value

TypeDescription
booleanAuto-reveal on completion option

currentBrushSize

Current brush size (px).

Return value

TypeDescription
numberCurrent brush size (px)

Example

js
scratcher.setBrushSize(40);
console.log(scratcher.currentBrushSize); // 40

See below for detailed descriptions of each method/property.

start

Start scratching operation. (e.g., mouse/touch down)

Parameters

NameTypeDescription
point{ x, y }Scratch start coordinate

Return value

TypeDescription
ScratchSnapshotCurrent scratch state object

Example

js
scratcher.start({ x: 10, y: 20 });

move

Called during scratching (mouse/touch move).

Parameters

NameTypeDescription
point{ x, y }Scratch move coordinate

Return value

TypeDescription
ScratchSnapshotCurrent scratch state object

end

End scratching operation. (mouse/touch up)

Return value

TypeDescription
ScratchSnapshotCurrent scratch state object

reset

Reset scratching state and redraw cover.

Return value

TypeDescription
ScratchSnapshotReset scratch state object

setBrushSize

Dynamically change brush size.

Parameters

NameTypeDescription
sizenumberBrush size to change (px)

Return value

TypeDescription
voidNo return value

setCallbacks

Register/change scratch event callbacks.

Parameters

NameTypeDescription
callbacksobjectCallback object (event handlers)

Return value

TypeDescription
voidNo return value

bindCanvas

Bind scratching events to canvas. Must be called for scratching to be active.

Parameters

NameTypeDescription
canvasCanvasCanvas element
optionsobject(Optional) Binding options

Return value

TypeDescription
functionReturn unbind function

unbindCanvas

Unbind the canvas.

Return value

TypeDescription
voidNo return value

on

Subscribe to custom events (scratchStart, progress, etc.).

Parameters

NameTypeDescription
eventnamestringEvent name (scratchStart, scratchMove, scratchEnd, reset, progress, complete)
listenerfunctionEvent handler function

Return value

TypeDescription
functionReturn unbind function