npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactjs-scratchcard

v1.0.6

Published

simple scractchcard library

Downloads

9

Readme

reactjs-scratchcard

A simple scratchcard component for React under ~8kB

Original repo by Aleksik (not maintained)

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

Converted this repo from class based components to function based components

https://github.com/dopey2/react-scratchcard-v2

Improvements over older versions

  • Functional Components
  • Fully Type Safe
  • Added relative sizing (rem, em , vh, vw, etc) over static sizing (px)
  • Custom Transition Props for animating completion
  • More functions to be added soon

Demo

Demo Gif

How to install

npm install --save reactjs-scratchcard

or

yarn add reactjs-scratchcard

Usage

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

const App = () => {

    const handleComplete = () => {
    // do some stuff here
    }

  return (
    <div>
      <ScratchCard
        brushSize         = {40}
        fadeOutOnComplete = {true}
        finishPercent     = {60}
        height            = {"20rem"}
        image             = {src}
        transitionProps   = {{ timeout: 200 }}
        onComplete        = {handleComplete}
        width             = {"20rem"}
      >
        <span
          style={{
            width         : "100%",
            height        : "100%",
            textAlign     : "center",
            display       : "flex",
            alignItems    : "center",
            justifyContent: "center"
          }}>
          hehehehehe
        </span>
      </ScratchCard>
    </div>
  );
};

ScratchCardProps

export type ScratchCardProps = {
    width: CSSLengthUnit
    height: CSSLengthUnit
    image: string
    finishPercent?: number
    onComplete: () => void
    brushSize?: number
    fadeOutOnComplete: boolean
    children: React.ReactNode
    customBrush?: CustomBrush
    customCheckZone?: CustomCheckZone
    transitionProps?: TransitionProps
}

The ScratchCardProps type represents the props (properties) expected by a scratch card component. It has the following properties:

  • width: Specifies the width of the scratch card.
  • height: Specifies the height of the scratch card.
  • image: Specifies the image used as the background of the scratch card, provided as a string.
  • finishPercent (optional): Specifies the percentage of the scratch card that needs to be completed for the onComplete callback to be triggered. If not provided, the default value is 70%.
  • onComplete: Specifies the callback function to be called when the scratch card is completely scratched.
  • brushSize (optional): Specifies the size of the brush used for scratching, represented as a number. If not provided, the default value is 20.
  • fadeOutOnComplete: Specifies whether the scratch card should fade out when it is completed, represented as a boolean value.
  • customBrush (optional): Specifies a custom brush to be used instead of the default brush. It is of type CustomBrush and allows customizing the brush image, width, and height.
  • customCheckZone (optional): Specifies a custom check zone for determining the completion of the scratch card.
  • transitionProps (optional): Specifies the transition properties for animating the completion of the scratchcard. It has a timeout of 1s by default

Types

CSSLengthUnit

export type CSSLengthUnit = Unit<RelativeLengthUnitSuffix>

The CSSLengthUnit type represents a CSS relative length unit, like "rem" | "em" | "vw" | "vh" | "vmin" | "vmax"

CustomBrush

export type CustomBrush = {
    image: any
    width: CSSLengthUnit
    height: CSSLengthUnit
}

The CustomBrush type represents a custom brush used in a scratch card component. It has the following properties:

  • image: Specifies the brush image, which can be of any type.
  • width: Defines the width of the brush using the CSSLengthUnit type.
  • height: Defines the height of the brush using the CSSLengthUnit type.

CustomCheckZone

export type CustomCheckZone = {
    x: number
    y: number
    width: CSSLengthUnit
    height: CSSLengthUnit
}

The CustomCheckZone type represents a custom check zone in a scratch card component. It defines an area where the scratch card completion is checked. It has the following properties:

  • x: Specifies the x-coordinate of the check zone as a number.
  • y: Specifies the y-coordinate of the check zone as a number.
  • width: Defines the width of the check zone using the CSSLengthUnit type.
  • height: Defines the height of the check zone using the CSSLengthUnit type.

TransitionProps

export type TransitionProps = {
    timeout: number
}

The TransitionProps type represents the properties used for defining transitions in components. It has the following property:

  • timeout: Specifies the duration of the transition in seconds.

Caveats

  • Currently, there is an issue with the Scratchcard's sizing, as it cannot be adjusted to occupy the entire width and height of its wrapper component by using percentage units. For proper functionality, it is important to ensure that both the wrapper and Scratchcard component have identical height and width dimensions. I am actively working on resolving this matter and will implement the necessary changes promptly.

License

MIT © vimalkmr816