Git Product home page Git Product logo

scratchable's Introduction

scratchable

2023-07-23.6.54.58.mov

Overview

scratchable is a scratch card renderer using HTML Canvas. It renders a scratchable canvas element on your content, and provides percentage of scratched pixels.

Usage

First of all, you should pass container to Scratchable, which will be overlapped by a scratchable canvas.

const container = document.getElementById("container");

const scratchable = new Scratchable({
  container,
  /* ... */
});
<div id="container">/* CONTENT */</div>

Or in React,

const container = ref.current;

const scratchable = new Scratchable({
  container,
  /* ... */
});
<div ref={ref}>{/* CONTENT */}</div>

And then it will render the canvas on your /* CONTENT */, when you call Scratchable.render(). It returns Promise<void>.

await scratchable.render();

And you can also remove the rendered canvas.

scratchable.destroy();

This is the basic. Now let's see another required option background.

Background

You should pass background to Scratchable, which is the color of the rendered canvas.

It has three kinds of type, single, linear-gradient and image.

Single Background

new Scratchable({
  /* ... */
  background: {
    type: "single",
    color: "#FA58D0",
  },
});
2023-07-23.9.15.39.mov

Linear Gradient Background

new Scratchable({
  /* ... */
  background: {
    type: "linear-gradient",
    gradients: [
      { offset: 0, color: "#FA58D0" },
      { offset: 0.5, color: "#DA81F5" },
      { offset: 1, color: "#BE81F7" },
    ],
  },
});
2023-07-23.9.16.48.mov

Image Background

new Scratchable({
  /* ... */
  background: {
    type: "image",
    url: "karina.jpeg",
  },
});
2023-07-23.10.16.20.mov

Radius

You can set radius of a circle which is rendered when you scratch the canvas. Let's compare between 20 and 40.

20

new Scratchable({
  /* ... */
  radius: 20,
});
2023-07-23.9.20.28.mov

40

new Scratchable({
  /* ... */
  radius: 40,
});
2023-07-23.9.21.00.mov

Scratch Event

You can register a function which will be called when scratch event fires. The event fires when an user is scratching the canvas.

const handler = (e: ScratchableEvent) => {
  /* ... */
};

scratchable.addEventListener("scratch", handler);
scratchable.removeEventListener("scratch", handler);

Scratched Percentage

You can get percentage(0 ~ 1) from ScratchEvent above. The percentage is ratio of scratched area to all scratchable area.

const handler = (e: ScratchableEvent) => {
  if (e.percentage > 0.5) {
    scratchable.destroy();
  }
};

scratchable.addEventListener("scratch", handler);
2023-07-23.9.40.51.mov

scratchable's People

Contributors

hoseungme avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.