Skip to Content
๐ŸŽฒ Welcome to RandBox - Powerful JavaScript Random Data Generation Library! Learn More

LuckyWheel - Lucky Wheel

LuckyWheel is a Canvas-based lucky wheel component that provides a classic turntable lottery experience. It supports custom prizes, weights, colors configuration, animation effects, and many other rich features.

๐Ÿ“ฆ Import

import { LuckyWheel } from '@randbox/react'; import type { LuckyWheelProps, LuckyWheelResult } from '@randbox/react';

๐ŸŽฏ Type Definitions

LuckyWheelProps

interface LuckyWheelProps { // Required properties prizes: string[]; // Prize list // Optional properties weights?: number[]; // Prize weights colors?: string[]; // Custom colors animationDuration?: number; // Animation duration (ms), default 4000 buttonText?: string; // Button text, default "Start Lottery" // Base properties (inherited from BaseGameProps) className?: string; // CSS class name style?: React.CSSProperties; // Inline styles disabled?: boolean; // Whether disabled // Callback functions onGameStart?: () => void; // Game start callback onGameEnd?: (result: LuckyWheelResult) => void; // Game end callback onResult?: (result: LuckyWheelResult) => void; // Result callback }

LuckyWheelResult

interface LuckyWheelResult { prizeIndex: number; // Won prize index prize: string; // Won prize angle: number; // Final rotation angle round: number; // Round number }

LuckyWheelStats

interface LuckyWheelStats { totalSpins: number; // Total number of spins prizeHistory: Record<string, number>; // Prize history record }

๐Ÿš€ Basic Usage

import React from 'react'; import { LuckyWheel } from '@randbox/react'; function BasicLuckyWheel() { const prizes = ['First Prize', 'Second Prize', 'Third Prize', 'Fourth Prize', 'Fifth Prize', 'Sixth Prize']; const handleResult = (result) => { console.log('Lottery result:', result); alert(`Congratulations! You won: ${result.prize}`); }; return ( <LuckyWheel prizes={prizes} onResult={handleResult} /> ); }

๐ŸŽจ Advanced Usage

Weighted and Custom Colors

function CustomLuckyWheel() { const prizes = [ 'iPhone 15 Pro', 'iPad Air', 'AirPods Pro', 'Apple Watch', 'Coupon', 'Points', 'Voucher', 'Thank you for playing' ]; // Higher weights = higher probability const weights = [1, 3, 5, 8, 15, 20, 25, 23]; // Custom colors (blue-purple theme) const colors = [ '#A8B9F5', '#C9D5F7', '#A8B9F5', '#C9D5F7', '#A8B9F5', '#C9D5F7', '#A8B9F5', '#C9D5F7' ]; return ( <LuckyWheel prizes={prizes} weights={weights} colors={colors} animationDuration={5000} buttonText="Start Spin" onResult={(result) => { if (result.prizeIndex < 4) { alert(`๐ŸŽ‰ Congratulations! You won ${result.prize}!`); } }} /> ); }

๐Ÿ“‹ API Reference

LuckyWheelProps

PropertyTypeDefaultDescription
prizesstring[]RequiredPrize list, recommended 4-12 prizes
weightsnumber[]undefinedWeight array for controlling win probability
colorsstring[]Default gradientCustom sector colors, should match prize count
animationDurationnumber4000Animation duration (milliseconds)
buttonTextstring'Start Lottery'Center button text
classNamestring''CSS class name
styleReact.CSSProperties{}Inline styles
disabledbooleanfalseWhether disabled
onGameStart() => voidundefinedGame start callback
onGameEnd(result) => voidundefinedGame end callback
onResult(result) => voidundefinedResult callback

LuckyWheelResult Structure

{ prizeIndex: 0, // Won prize index (0-based) prize: "iPhone 15 Pro", // Won prize name angle: 12.566370614359172, // Final rotation angle (radians) round: 1 // Current round number }
Last updated on: