react-prize-wheel
Version:
A simple, performant React prize wheel component
79 lines • 3.16 kB
TypeScript
import type { WheelSegment } from '../types';
/**
* Selects a segment based on weighted probability distribution
*
* @description Uses weighted random selection to choose a segment from the provided array.
* Segments with higher weights have a proportionally higher chance of being selected.
* Automatically filters out disabled segments before selection.
*
* @since 2025-07-25
* @version 1.0.0
*
* @param segments - Array of wheel segments to choose from
* @returns Object containing the selected segment and its original index
* @throws {Error} When segments array is empty or no enabled segments exist
*
* @example
* ```typescript
* const segments = [
* { id: '1', text: 'Common', color: '#000', weight: 3 },
* { id: '2', text: 'Rare', color: '#000', weight: 1 }
* ];
* const result = getWeightedSegment(segments);
* // 'Common' has 75% chance, 'Rare' has 25% chance
* ```
*/
export declare function getWeightedSegment(segments: WheelSegment[]): {
segment: WheelSegment;
index: number;
};
/**
* Calculates the final rotation angle to land on a specific segment
*
* @description Computes the rotation angle needed for the wheel to stop at a target segment.
* Includes multiple full rotations plus a randomized offset within the target segment
* to make the result appear natural and unpredictable. Supports pointer positioning
* and predefined results by ID.
*
* @since 2025-07-25
* @version 1.0.0
*
* @param segments - Array of wheel segments
* @param targetIndexOrId - Index of the target segment or segment ID
* @param spins - Number of full rotations to perform (default: 5)
* @param pointerPosition - Position of the pointer ('top', 'right', 'bottom', 'left')
* @returns Total rotation angle in degrees
*
* @example
* ```typescript
* const angle = calculateTargetAngle(segments, 2, 3, 'top');
* // Returns angle for 3 full spins + rotation to segment 2 with top pointer
* ```
*/
export declare function calculateTargetAngle(segments: WheelSegment[], targetIndexOrId: number | string, spins?: number, pointerPosition?: 'top' | 'right' | 'bottom' | 'left'): number;
/**
* Determines which segment is positioned at a given rotation angle
*
* @description Calculates which segment is under the pointer at a specific wheel rotation.
* Takes into account the wheel's coordinate system and pointer position to determine
* the correct segment. Used to calculate final results after spinning.
*
* @since 2025-07-25
* @version 1.0.0
*
* @param segments - Array of wheel segments
* @param angle - Current rotation angle in degrees
* @param pointerPosition - Position of the pointer ('top', 'right', 'bottom', 'left')
* @returns Object containing the segment at the given angle and its index
*
* @example
* ```typescript
* const result = getSegmentAtAngle(segments, 45, 'top');
* // Returns the segment positioned at 45° rotation with top pointer
* ```
*/
export declare function getSegmentAtAngle(segments: WheelSegment[], angle: number, pointerPosition?: 'top' | 'right' | 'bottom' | 'left'): {
segment: WheelSegment;
index: number;
};
//# sourceMappingURL=selection.d.ts.map