1.0.2 • Published 2 years ago

react-scratch-code v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-scratch-code

A scratch card component for React

Improvement ✨
  • Avoid preventDefault from passive event listeners

Install

npm install --save react-scratch-code

or

yarn add react-scratch-code

Usage

import React, { useRef }  from 'react';
import ScratchCard from 'react-scratch-code';

import * as IMG from './img.jpg';

const App = () => {

  const ref = useRef<ScratchCard>(null);

  const onClickReset = () => {
    ref.current && ref.current.reset();
  }

  return (
    <div>
      <button onClick={onClickReset}>Reset</button>
      <ScratchCard
        width={320}
        height={226}
        image={IMG}
        finishPercent={80}
        onComplete={() => console.log('complete')}
      >
        <div style={{
          display: 'flex',
          width: '100%',
          height: '100%',
          alignItems: 'center',
          justifyContent: 'center'
        }}
        >
          <h1>Scratch card</h1>
        </div>
      </ScratchCard>
    </div>
  );
};

Custom brush

const App = () => {
  return (
    <div>
      <ScratchCard
        width={320}
        height={226}
        image={IMG}
        finishPercent={80}
        customBrush={{
          image: require('./brush.img'),
          width: 15,
          height: 15
        }}
      >
        <h1>Scratch card</h1>
      </ScratchCard>
    </div>
  );
};

or you can use CUSTOM_BRUSH_PRESET object

import { CUSTOM_BRUSH_PRESET } from 'react-scratch-code';

const App = () => {
  return (
    <div>
      <ScratchCard
        width={320}
        height={226}
        image={IMG}
        finishPercent={80}
        customBrush={CUSTOM_BRUSH_PRESET}
      >
        <h1>Scratch card</h1>
      </ScratchCard>
    </div>
  )
}

Type

Props

nametypedefault
widthnumber
heightnumber
imageFile or Base64
finishPercent?number70
brushSize?number20
fadeOutOnComplete?booleantrue
onComplete?callback
customBrush?CustomBrush
customCheckZone?CustomCheckZone

CustomBrush

nametype
widthnumber
heightnumber
imageFile or Base64

CustomCheckZone

nametype
xnumber
ynumber
widthnumber
heightnumber

License

MIT © anujverma000