Giter Club home page Giter Club logo

react-dice-complete's Introduction

React Dice ๐ŸŽฒ

npm version contributions welcome

Library for creating multisided dice and rolling them. Check out the demo here.

sample dice roll

Installation

npm install react-dice-complete

Usage

import React, { useRef } from 'react'
import ReactDice, { ReactDiceRef } from 'react-dice-complete'

const MyDiceApp = () => {

  const reactDice = useRef<ReactDiceRef>(null)

  const rollDone = (totalValue: number, values: number[]) => {
    console.log('individual die values array:', values)
    console.log('total dice value:', totalValue)
  }

  const rollAll = () => {
    reactDice.current?.rollAll()
  }

  return (
    <ReactDice
      numDice={2}
      ref={reactDice}
      rollDone={rollDone}
    />
  )

}

Available Options

Name Type Default Description
defaultRoll {Number} 6 The number you want displayed before a roll
dieCornerRadius {Number} 5 Rounded radius to use for each die
dieSize {Number} 60 px width/height of each dice face
disableIndividual {Bool} false disable clicks on die to roll each individually
disabbleRandom {Bool} false disable random die chance and reuses current value
dotColor {String} #1eff00 hex color code for the dots on the die
faceColor {String} #ff00ac hex color code for the face of the die
margin {Number} 15 margin between each die
numDice {Number} 4 The number of dice you wish to have
outline {Bool} false Show a 1px outline for each face of the die
outlineColor {String} #000000 hex color code for outline color if outline is true
rollDone {String/Function} null callback returns total & individual values from dice roll
rollTime {Number} 2 time in seconds for the roll animation

Provided functions

rollAll([values]): rolls all die and calls rollDone with total from roll. You can preset the values you would like the outcome to be by passing array of numbers, if not result will be random.

react-dice-complete's People

Contributors

adamtyler avatar dependabot[bot] avatar elenaverdy avatar infernalslam avatar paulatkins88 avatar robertjmason avatar serexx 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar

react-dice-complete's Issues

RollDone() doesnt run after rolling the dices for a second time

Hi Adam!

I'm having an issue where, after I decrease the numDice number and roll the dices a second time, it doesnt run the RollDone() callback. You can test it in the demo.

Steps:
-Choose a dice number and roll it
-Decrease the dice number and roll it again

You will see that the rollDone() callback wont be called

chrome-capture-2024-5-15

[enhancement]Increasing Number of Sides

Hi, great project!

How could I go about increasing the number of sides on the dice..? Perhaps to 10
Would it be a matter of increasing the number of f(num)Style and changing sides: to e.g 10
Thanks in advance.

Setting up next rolled value

Is there any way to set up different value before every roll? I want to get random value from server and send it to react component.

change default value from 6

I would like to change the default (unrolled) value of the dice from 6 to something else via props (and leave it to 6 if no default value is present in props). demo of my game

an even nicer feature would be to change the default face of the dice to something custom (question mark, text 'push', clicking finger, ...) so that users are pushed to click it.

do you plan such enhancement? thanks

Typescript support

Hi Guys,

Thanks for maintaining this component. is there any change you guys add Typescript support?

Related issue: #66

Thanks...

Blinking colors across entire page issue

I get the colors of the dice face flashing all across the page in rays while the dice are rolling. This is only in Chrome 87...I'm unable to replicate in Firefox. Ubuntu 20.04 for both.

Here is an Imgur link to a GIF I made. I rolled the dice 4 times, and it only did it on the second and fourth rolls. It seems to be intermittent.

https://imgur.com/H9Ute1Y

Support React 18

Hi, I've just upgraded my project to React 18 and found the dice has broken. Everything works except the value returned on the rollDone callback doesn't match the value of the face of the dice. Any possibility this could get fixed? Thanks.

Edit: it looks like the value being returned is the previous rolled value.

react-dice-complete vs react-dice

npm install react-dice-complete
or
npm install react-dice

What's the difference?
Caused me bit of confusion before. Unblocked myself but thought it would be worth submitting some feedback!

PropTypes Deprecation

Greetings, thanks for the work on this, made life easier :-)

React is complaining about the deprecated internal PropTypes (replaced by the separate 'prop-types' package) in Die.js and ReactDice.js

I'd rather not fork it for my current project but will if you don't want to change the master?

Can't resolve 'react-dice-complete/dist/react-dice-complete.css

Hi Guys,

Thanks for working on the changes for React 18 and Typescript. I was testing version 2.1.0 and seems like the package is failing when compiling the app.

`
import React, { useRef } from 'react'
import ReactDice, { ReactDiceRef } from 'react-dice-complete'

const MyDiceApp = () => {

const reactDice = useRef(null)

const rollDone = (totalValue: number, values: number[]) => {
console.log('individual die values array:', values)
console.log('total dice value:', totalValue)
}

const rollAll = () => {
reactDice.current?.rollAll()
}

render() {
return (




)
}

}

`
React: 18.2.0
Typescript: 4.9.5

Error:

`Module not found: Error: Can't resolve 'react-dice-complete/dist/react-dice-complete.css'

Thanks...

rollDoneCallback called on render of dice

I've noticed that the rollDoneCallback is called on render of the dice at the default state (6).

With the example in the readme

  rollDoneCallback(num) {
    console.log(`You rolled a ${num}`)
  }

screen shot 2019-01-14 at 23 19 00

Upon refreshing, I noticed that I received a 'You rolled a 12'

Is this behaviour expected?

Preventing default roll

Hi,
is it possible to prevent the default roll of the dice when first renders, and just keep the roll-on-click behavior?

thanks,
Nektarios

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.