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

scratchable

v0.0.4

Published

A scratch card renderer using HTML Canvas

Downloads

13

Readme

scratchable

https://github.com/HoseungJang/scratchable/assets/39669819/5e3e5e4b-ce97-47b5-877c-d04d8375e843

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",
  },
});

https://github.com/HoseungJang/scratchable/assets/39669819/7915c2af-8bbe-43d8-9169-631fd7124b91

Linear Gradient Background

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

https://github.com/HoseungJang/scratchable/assets/39669819/bef24ef0-2860-4150-9133-35a922950936

Image Background

new Scratchable({
  /* ... */
  background: {
    type: "image",
    url: "karina.jpeg",
  },
});

https://github.com/HoseungJang/scratchable/assets/39669819/8fd80f49-bb3c-4582-af82-b57273e6a8c2

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,
});

https://github.com/HoseungJang/scratchable/assets/39669819/44c38fac-a130-427d-8c8e-874f03e132f1

40

new Scratchable({
  /* ... */
  radius: 40,
});

https://github.com/HoseungJang/scratchable/assets/39669819/b8421e3b-f79e-4114-a002-0f8c066e1c95

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);

https://github.com/HoseungJang/scratchable/assets/39669819/877e7224-5311-443e-84d1-be24632f5d21