react-poker
React component for dealing cards
Last updated 2 years ago by therewillbecode .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install react-poker 
SYNC missed versions from official npm registry.

Flurry

A High Level React Library For Playing Card Animations

Click here for a demo

react-poker

getting started

install the module inside your project root

npm install react-poker --save

The Deck component takes a board prop which is the array of cards that are dealt and facing up on the community board.

Internally react motion is used to animate card deals when the board prop changes.

import Deck from "react-poker";
import "react-poker/styles.css"

const YourComponent = props =>
    <Deck
      board={["3s", "Qh", "As"]} // array of cards to render on board
      boardXoffset={375} // X axis pixel offset for dealing board
      boardYoffset={200} // Y axis pixel offset for dealing board
      size={200} // card height in pixels
    />

example

The example below includes the logic to shuffle a deck of cards. A new board prop is passed to the Deck component everytime a new card(s) is drawn from the deck or there is a new round.

import Deck from "react-poker";
import "react-poker/styles.css"

const range = (start, count) =>
  Array.apply(0, Array(count)).map((element, index) => {
    return index + start;
  });

function shuffle(array) {
  const copy = [];
  let n = array.length;
  let i;
  // While there remain elements to shuffle…
  while (n) {
    // Pick a remaining element…
    i = Math.floor(Math.random() * array.length);

    // If not already shuffled, move it to the new array.
    if (i in array) {
      copy.push(array[i]);
      delete array[i];
      n--;
    }
  }

  return copy;
}

const suits = ["d", "c", "h", "s"];
const ranks = [
  "A",
  "2",
  "3",
  "4",
  "5",
  "6",
  "7",
  "8",
  "9",
  "10",
  "J",
  "Q",
  "K"
];

const getDeck = () =>
  shuffle(
    ranks
      .map(r => suits.map(s => r + s))
      .reduce((prev, curr) => prev.concat(curr))
  );

class AppContainer extends Component {
  constructor(props) {
    super(props);
    this.state = { board: [], deck: getDeck() };
    this.progressDeal = this.progressDeal.bind(this);
  }

  newRound() {
    const { deck, board } = this.state;

    const newDeck = getDeck();
    this.setState(Object.assign({}, { board: [], deck: newDeck }));
  }

  dealFlop() {
    const { deck, board } = this.state;
    const flop = range(0, 3).map(e => deck.pop());

    this.setState(Object.assign({}, { board: flop, deck }));
  }

  dealCard() {
    const { deck, board } = this.state;
    const card = deck.pop();

    this.setState(Object.assign({}, { deck, board: board.concat(card) }));
  }

  progressDeal() {
    const { deck, board } = this.state;

    if (board.length === 0) {
      this.dealFlop();
      return;
    }

    if (board.length === 5) {
      this.newRound();
    } else {
      this.dealCard();
    }
  }

  render() {
    const { board } = this.state;

    return (
      <div style={{ left: "10vw", top: "10vh", position: "absolute" }}>
        <button
          style={{ padding: "1.5em", margin: "2em" }}
          onClick={this.progressDeal}
        >
          Deal
        </button>
        <Deck
          board={["3s", "Qh", "As"]}
          boardXoffset={375} // X axis pixel offset for dealing board
          boardYoffset={200} // Y axis pixel offset for dealing board
          size={200} // card height in pixels
        />
      </div>
    );
  }
}

export default AppContainer;

TODO

  • Generalise library for any card game - i.e Pokemon, Magic The Gathering
  • Demos
    • Add a Pokemon demo.
    • For the poker demo add a player deal event
  • Make the API more declarative:
  • Pass functions for determining spring settings and card positioning/animation
  • Abstract out functions for different events i.e flop, turn river that define animations (essentially removing the "poker" related assumptions about the game.
  • Permit Custom Card Images Design more attractive set of custom poker cards

Current Tags

  • 0.0.77                                ...           latest (2 years ago)

15 Versions

  • 0.0.77                                ...           2 years ago
  • 0.0.76                                ...           2 years ago
  • 0.0.75                                ...           3 years ago
  • 0.0.74                                ...           3 years ago
  • 0.0.73                                ...           3 years ago
  • 0.0.72                                ...           3 years ago
  • 0.0.71                                ...           3 years ago
  • 0.0.7                                ...           3 years ago
  • 0.0.61                                ...           3 years ago
  • 0.0.6                                ...           3 years ago
  • 0.0.5                                ...           3 years ago
  • 0.0.4                                ...           3 years ago
  • 0.0.3                                ...           3 years ago
  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (11)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |