DEV Community

Cover image for A scratchcard component for React
Exaland App
Exaland App

Posted on • Edited on

A scratchcard component for React

react-scratchingcard

A scratchcard component for React

Original repo by Aleksik (not maintained)

https://github.com/aleksik/react-scratchcard

Improvement ✨
  • Resize the image using width and height props (in the original repo, the image was croped)
  • Smooth fade out animation on scratch complete
  • Add type definition (ts)
  • Change brush size through props
  • Use custom brush through props
  • Define a custom check zone through props

Demo

scratchcard-demo

Install

npm install --save react-scratchingcard
Enter fullscreen mode Exit fullscreen mode

or

yarn add react-scratchingcard
Enter fullscreen mode Exit fullscreen mode

Usage

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

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>
  );
};
Enter fullscreen mode Exit fullscreen mode

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>
  );
};
Enter fullscreen mode Exit fullscreen mode

or you can use CUSTOM_BRUSH_PRESET object

import { CUSTOM_BRUSH_PRESET } from 'react-scratchingcard';

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

Type

Props

name type default
width number
height number
image File \ string \
imageCrossOrigin ?'' \ 'anonymous' \
finishPercent ?number 70
brushSize ?number 20
fadeOutOnComplete ?boolean true
onComplete ?callback
customBrush ?CustomBrush
customCheckZone ?CustomCheckZone

Remote URL

<ScratchCard
  width={320}
  height={226}
  image='https://cdn.example.com/scratch-cover.jpg'
  imageCrossOrigin='anonymous'
>
  <h1>Scratch card</h1>
</ScratchCard>
Enter fullscreen mode Exit fullscreen mode

Note: pour calculer le pourcentage gratté (finishPercent) avec une image distante, le serveur distant doit autoriser CORS.

Le composant n'impose plus CORS par defaut pour une URL distante. imageCrossOrigin est utile seulement si ton serveur renvoie deja les en-tetes CORS voulus.

CustomBrush

name type
width number
height number
image File or Base64

CustomCheckZone

name type
x number
y number
width number
height number

License

MIT © exaland

Top comments (0)