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

cyber-dice

v2.0.1

Published

Dice animation library

Downloads

2,242

Readme

cyber-dice

React dice animation library with Typescript

GitHub package.json version (subfolder of monorepo)

Installation

npm:

npm install cyber-dice

yarn:

yarn add cyber-dice

Demo with examples

https://evgenia-cyber.github.io/dice/

< Dice /> props

| Name | Type | Prop type | Default | Description | | ------------ | :------: | :-------: | :-----: | -------------------------------------------------- | | randomNumber | required | number | - | The number rolled on the dice - number from 1 to 6 | | size | optional | number | 80 | Dice size in px |

< DiceWithAnimation /> props

| Name | Type | Prop type | Default | Description | | ------------------- | :------: | :--------------: | :-----: | ---------------------------------------------------------------------------------------------------------------------------------------- | | randomNumber | required | number | - | The side rolled on the dice - number from 1 to 6 | | isAnimation | required | boolean | - | Animation control flag | | animationEndHandler | required | function | - | Callback that will be executed when the animation ends. | | size | optional | number | 80 | Dice size in px | | faces | optional | array of strings | - | Custom faces for the dice. Strings can only have the following values: "first" or "second" or "third" or "fourth" or "fifth" or "sixth"; |

< CubeWithAnimation /> props

| Name | Type | Prop type | Default | Description | | ------------------- | :------: | :--------------: | :-----: | ------------------------------------------------------- | | randomNumber | required | number | - | The side rolled on the cube - number from 1 to 6 | | isAnimation | required | boolean | - | Animation control flag | | animationEndHandler | required | function | - | Callback that will be executed when the animation ends. | | size | optional | number | 80 | Dice size in px | | sides | required | array of objects | - | Custom sides for the cube. |

This object has type:

{
  text: string;
  textColor?: string;
  fontSize?: string;
  bgColor?: string;
}

Examples

1. Dice without animation

import React from "react";
import { Dice } from "cyber-dice";

const Component = () => <Dice randomNumber={5} />;

export default Component;

2. Dice without animation with Typescript

import React, { FC } from "react";
import { Dice } from "cyber-dice";

const Component: FC = () => <Dice randomNumber={5} />;

export default Component;

3. Custom size dice

import React from "react";
import { Dice } from "cyber-dice";

const Component = () => <Dice randomNumber={5} size={100} />;

export default Component;

4. Dice with animation

import React from "react";
import { DiceWithAnimation } from "cyber-dice";

const Component = () => {
  const [randomNumber, setRandomNumber] = React.useState(1);
  const [isAnim, setIsAnim] = React.useState(true);

  const clickHandler = () => {
    setIsAnim(true);
    const newRandomNumber = Math.floor(Math.random() * 6) + 1;
    setRandomNumber(newRandomNumber);
  };

  const animationEndHandler = () => {
    setIsAnim(false);
    console.log("Animation end");
  };

  return (
    <div>
      {randomNumber}
      <button type="button" onClick={clickHandler}>
        Click
      </button>
      <DiceWithAnimation
        randomNumber={randomNumber}
        isAnimation={isAnim}
        animationEndHandler={animationEndHandler}
      />
    </div>
  );
};

export default Component;

5. Custom dice with animation

To do this, you need to pass optional props faces - array of six strings. Strings can only have the following values: "first" or "second" or "third" or "fourth" or "fifth" or "sixth". This example uses custom dice with only one or two points.

import React from "react";
import { DiceWithAnimation } from "cyber-dice";

const Component = () => {
  const [randomNumber, setRandomNumber] = React.useState(1);
  const [isAnim, setIsAnim] = React.useState(true);

  const clickHandler = () => {
    setIsAnim(true);
    const newRandomNumber = Math.floor(Math.random() * 6) + 1;
    setRandomNumber(newRandomNumber);
  };

  const animationEndHandler = () => {
    setIsAnim(false);
    console.log("Animation end");
  };

  return (
    <div>
      {randomNumber}
      <button type="button" onClick={clickHandler}>
        Click
      </button>
      <DiceWithAnimation
        faces={["first", "second", "first", "second", "first", "second"]}
        randomNumber={randomNumber}
        isAnimation={isAnim}
        animationEndHandler={animationEndHandler}
      />
    </div>
  );
};

export default Component;

6. Custom colors for dice and dice points

Change background-color in css classes .dice__color and .point__color

7. Custom cube with animation

To do this, you need to pass optional props sides - array of six objects.

Object has type:

{
  text: string;
  textColor?: string;
  fontSize?: string;
  bgColor?: string;
}

This example uses custom cube with letters and numbers in different colors.

import React from "react";
import { CubeWithAnimation } from "cyber-dice";

const Component = () => {
  const [randomNumber, setRandomNumber] = React.useState(1);
  const [isAnim, setIsAnim] = React.useState(true);

  const clickHandler = () => {
    setIsAnim(true);
    const newRandomNumber = Math.floor(Math.random() * 6) + 1;
    setRandomNumber(newRandomNumber);
  };

  const animationEndHandler = () => {
    setIsAnim(false);
    console.log("Animation end");
  };

  return (
    <div>
      {randomNumber}
      <button type="button" onClick={clickHandler}>
        Click
      </button>
      <CubeWithAnimation
        sides={[
          { text: "A", textColor: "red", bgColor: "black" },
          { text: "V", textColor: "green", bgColor: "black" },
          { text: "C", textColor: "blue", bgColor: "white" },
          { text: "7", textColor: "#7bff00", bgColor: "black" },
          { text: "9", textColor: "black", bgColor: "#d607ff" },
          { text: "11", textColor: "#d607ff", bgColor: "black" },
        ]}
        randomNumber={randomNumber}
        isAnimation={isAnim}
        animationEndHandler={animationEndHandler}
      />
    </div>
  );
};

export default Component;

8. Custom styles for cube

Add custom styles for the every side of the cube - use css classes: .side__front, .side__back, .side__left, .side__right, .side__top, .side__bottom.

Add custom styles for the text of the cube - use css class .side__text.