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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@randbox/react

v1.2.0

Published

Lightweight Canvas-based React game components: GridLottery, SlotMachine, ScratchCard, DiceGame, RockPaperScissors, CoinFlip, and LuckyWheel powered by RandBox

Downloads

22

Readme

@randbox/react

Lightweight, high-performance React game components library with Canvas rendering, powered by RandBox random number generator.

✨ Features

  • 🎮 Rich Component Collection: Grid lottery, slot machine, scratch card, dice game, rock-paper-scissors, and more
  • 🚀 High Performance: Canvas-based rendering with 60fps smooth animations
  • 📦 Lightweight: Optimized bundle size (ES module: ~109KB, UMD: ~45KB)
  • 🎨 Zero CSS Dependencies: All styles rendered via Canvas with no external dependencies
  • 📱 Responsive: Adaptive to different screen sizes
  • 🔧 TypeScript: Full type definition support
  • 🎲 True Randomness: High-quality random number generation based on RandBox
  • GPU Acceleration: Leverages browser hardware acceleration for optimal performance

📦 Installation

npm install @randbox/react randbox
# or
yarn add @randbox/react randbox
# or
pnpm add @randbox/react randbox

🚀 Quick Start

import { CanvasGridLottery, CanvasDiceGame, CanvasScratchCard } from '@randbox/react';

function App() {
  return (
    <div>
      {/* Grid Lottery */}
      <CanvasGridLottery
        prizes={['Prize 1', 'Prize 2', 'Prize 3', 'Try Again']}
        onResult={(result) => console.log('Winner:', result)}
      />

      {/* Dice Game */}
      <CanvasDiceGame
        diceCount={2}
        gameMode="sum"
        targetSum={7}
        onResult={(result) => console.log('Roll result:', result)}
      />

      {/* Scratch Card */}
      <CanvasScratchCard
        rows={3}
        cols={3}
        symbols={['🍎', '🍌', '🍇']}
        onScratch={(result) => console.log('Scratch result:', result)}
      />
    </div>
  );
}

🎮 Components

Canvas Components (Recommended)

High-performance Canvas-based components with advanced visual effects and smooth animations.

CanvasGridLottery

Classic 9-grid lottery with customizable prizes and weights.

<CanvasGridLottery
  prizes={string[]}              // Prize list
  weights={number[]}             // Weight configuration
  gridSize={9}                   // Grid size
  animationDuration={3000}       // Animation duration
  buttonText="Start"             // Button text
  onResult={(result) => {...}}   // Result callback
/>

Key Features:

  • Glowing highlight effects
  • Easing animation functions
  • Real-time progress display
  • Pixel-perfect rounded corners

CanvasScratchCard

Realistic scratch card experience with pixel-level scratch detection.

<CanvasScratchCard
  rows={3}                       // Number of rows
  cols={3}                       // Number of columns
  symbols={string[]}             // Symbol list
  winProbability={30}            // Win probability (%)
  onScratch={(result) => {...}}  // Scratch callback
/>

Key Features:

  • Dual-layer Canvas rendering
  • Pixel-level scratch detection
  • Real-time progress calculation
  • Mouse and touch support
  • Auto-reveal functionality

CanvasSlotMachine

Professional slot machine implementation with multi-reel animations.

<CanvasSlotMachine
  reels={string[][]}             // Reel configuration
  weights={number[][]}           // Weight configuration
  animationDuration={3000}       // Animation duration
  onResult={(result) => {...}}   // Result callback
/>

Key Features:

  • 5-reel independent animations
  • Payline visualization
  • Cascading stop effects
  • Jackpot detection
  • Symbol value display

CanvasDiceGame

3D-style dice game with multiple game modes.

<CanvasDiceGame
  diceCount={2}                  // Number of dice
  gameMode="sum"                 // Game mode
  targetSum={7}                  // Target sum
  onResult={(result) => {...}}   // Result callback
/>

Key Features:

  • 3D perspective rendering
  • Realistic shadow effects
  • Multiple game modes
  • Statistics tracking
  • Physics-based animation

HTML Components

Traditional HTML/CSS components for simple interactions and quick customization.

GridLottery

HTML-based 9-grid lottery component.

SlotMachine

HTML-based multi-reel slot machine.

ScratchCard

HTML-based scratch card component.

DiceGame

HTML-based dice game.

RockPaperScissors

Classic rock-paper-scissors game with multiple AI strategies and statistics.

🎨 Performance Comparison

| Feature | HTML Version | Canvas Version | |---------|-------------|----------------| | Rendering | DOM Manipulation | Canvas 2D API | | Animation | CSS Animation | 60fps requestAnimationFrame | | CPU Usage | Higher | Lower | | Memory Usage | Medium | Low | | Visual Effects | Limited | Rich | | Interaction Precision | Element-level | Pixel-level | | Mobile Performance | Good | Excellent |

🚀 Canvas Advantages

Performance Optimization

  • 60fps Smooth Animation: High-performance rendering based on requestAnimationFrame
  • GPU Acceleration: Leverages browser hardware acceleration
  • Low CPU Usage: More efficient rendering compared to DOM manipulation

Visual Effects

  • Advanced Effects: Support for gradients, shadows, glowing effects
  • Pixel-level Control: Precise graphics drawing and interaction detection
  • 3D Perspective: Realistic 3D rendering (e.g., 3D dice)

Interactive Experience

  • Precise Interaction: Pixel-level precision for mouse and touch events
  • Realistic Physics: Simulated real-world physics animation
  • Cross-platform: Perfect support for desktop and mobile

📱 Mobile Support

All Canvas components fully support mobile devices:

  • Auto-detection of touch events
  • Responsive Canvas sizing
  • High-DPI screen adaptation
  • Optimized touch interaction experience

🔧 Development

# Clone the repository
git clone https://github.com/your-username/randbox.git
cd randbox/packages/react

# Install dependencies
pnpm install

# Development mode
pnpm dev

# Build
pnpm build

# Test
pnpm test

🎯 Optimization

  • Tree-shaking: Supports on-demand imports, unused components won't be bundled
  • Code Minification: Automatically removes console.log and debug code in production
  • External Dependencies: React and RandBox as external dependencies to avoid duplicate bundling
  • Type Optimization: Type definitions generated only for core components

🛠 Best Practices

Component Selection Guide

  • Canvas Version: For scenarios requiring high animation performance and complex visual effects
  • HTML Version: For simple interactions and quick style customization

Development Tips

  1. Performance: Canvas components automatically optimize rendering, no extra configuration needed
  2. Responsive Design: Canvas automatically adapts to container size
  3. Event Handling: Canvas components handle all interaction logic internally
  4. Style Customization: Control appearance through component props, not CSS

Considerations

  • Canvas components don't support CSS style customization
  • Some browsers may require hardware acceleration support
  • Mobile devices need touch event handling

🐛 Troubleshooting

Common Issues

  1. Blurry Canvas: Check device pixel ratio settings
  2. Animation Lag: Ensure browser hardware acceleration is enabled
  3. Touch Not Responding: Check touch-action CSS property

Debugging Tips

  • Use browser DevTools Performance panel
  • Check actual Canvas element dimensions
  • Monitor requestAnimationFrame call frequency

📚 Documentation

Complete documentation and API reference: https://randbox.js.org

📄 License

MIT License - See LICENSE file for details.

🤝 Contributing

Contributions are welcome! Please check CONTRIBUTING.md for details.


Powered by ❤️ and RandBox