@jingx/plinko-threejs
v1.0.1
Published
Three.js plinko table animation packaged as a React component for Next.js.
Readme
@jingx/plinko-threejs
An interactive 3D Plinko board game built with Three.js rendering and cannon-es physics. Packaged as a React component for Next.js / React applications as well as a standalone Vanilla JavaScript API.
Features
- 3D Physics & Rendering: Accurate rigid-body collisions powered by
cannon-esand rendered withThree.js. - Dynamic Spot Bounce Feedback: Glowing emissive bounce animations on collision spots.
- Flat Numeric Slot Badges: Rendered with numeric pocket numbers
5, 4, 3, 2, 1, 1, 2, 3, 4, 5from left to right. - Deterministic Result Routing: Control exactly which pocket the ball lands in using canonical result keys (e.g.
'5_LEFT','1_RIGHT') or numeric slot indices. - Release Event Handling:
onAfterReleasecallback triggers as soon as the ball settles into the target slot pocket. - Synthesized Audio: Built-in Web Audio API synthesizer for peg bounces, drops, and win celebrations without external asset dependencies.
- TypeScript Support: Full TypeScript declarations included out-of-the-box (
dist/index.d.ts).
Installation
npm install @jingx/plinko-threejs three cannon-esNote:
threeandcannon-esare required dependencies. If you are using React,react(>=18.0.0) is a peer dependency.
Slot Pocket Layout
For the default 9-row board (10 pockets), the layout from left to right is:
| Index | Canonical Name | Badge Label | Description |
| :---: | :---: | :---: | :--- |
| 0 | 5_LEFT | 5 | Outermost left pocket |
| 1 | 4_LEFT | 4 | Left inner pocket 4 |
| 2 | 3_LEFT | 3 | Left inner pocket 3 |
| 3 | 2_LEFT | 2 | Left inner pocket 2 |
| 4 | 1_LEFT | 1 | Center-left pocket 1 |
| 5 | 1_RIGHT | 1 | Center-right pocket 1 |
| 6 | 2_RIGHT | 2 | Right inner pocket 2 |
| 7 | 3_RIGHT | 3 | Right inner pocket 3 |
| 8 | 4_RIGHT | 4 | Right inner pocket 4 |
| 9 | 5_RIGHT | 5 | Outermost right pocket |
Quick Start
1. React / Next.js Component
import React, { useState } from "react";
import PlinkoGame from "@jingx/plinko-threejs";
export function GameView() {
const [result, setResult] = useState<string | null>("5_LEFT");
const handleRelease = () => {
console.log("Ball successfully landed in target pocket!");
};
return (
<div style={{ width: "100%", height: "600px", position: "relative" }}>
<PlinkoGame
rows={9}
result={result}
isMuted={false}
onAfterRelease={handleRelease}
/>
</div>
);
}
export default GameView;2. Vanilla JavaScript / DOM
import { createPlinkoGame } from "@jingx/plinko-threejs";
const container = document.getElementById("plinko-container");
const game = createPlinkoGame({
container,
rows: 9,
result: "5_RIGHT",
isMuted: false,
onAfterRelease: () => {
console.log("Ball reached slot pocket!");
},
onWin: ({ slot, slotName, slotLabel, multiplier, payout }) => {
console.log(`Landed in ${slotName} (${slotLabel}) - Multiplier: ${multiplier}x, Payout: $${payout}`);
}
});
// Drop a ball with a new target dynamically:
game.setResult("1_LEFT");
// Clean up when destroying the view
// game.destroy();API Reference
<PlinkoGame /> (React Component)
| Prop | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| rows | number | 9 | Number of peg rows (8 to 16 supported). |
| result | string \| number \| null | null | Target slot pocket key (e.g. '5_LEFT', '2_RIGHT') or slot index (0..9). |
| onAfterRelease | () => void | undefined | Callback fired when the ball reaches and settles into the target slot pocket. |
| isMuted | boolean | false | Whether synthesizer audio is muted. |
| currentCountdown | number \| null | null | Optional round countdown timer. |
| spinningSeconds | number | undefined | Duration hint for animation / countdown timing. |
| className | string | undefined | CSS class for container <div>. |
| style | CSSProperties | undefined | Inline styles for container <div>. |
createPlinkoGame(options) (Vanilla JS)
Options:
container?: HTMLElement- DOM element to mount the Three.js canvas into.rows?: number- Row count (default9).result?: string | number | null- Target slot pocket result.onAfterRelease?: () => void- Callback fired when the ball settles in target pocket.onWin?: (result: PlinkoWinResult) => void- Win event callback.isMuted?: boolean- Mute audio effects (defaultfalse).initialBalance?: number- Initial virtual balance (default1000.00).initialBet?: number- Initial bet amount (default10.00).initialRisk?: 'Low' | 'Medium' | 'High'- Risk multiplier tier.
Instance Methods:
dropBall(laneX?: number)- Drops a ball from the top indicator or specified lane.setResult(result: string | number | null)- Sets target pocket and triggers a drop.getResult(): string | null- Retrieves current target pocket string.setOnAfterRelease(callback: () => void)- Registers / updates release callback.setRows(rows: number)- Reconfigures row count and rebuilds the board geometry.setIsMuted(isMuted: boolean)- Toggles audio mute state.getIsMuted(): boolean- Gets current audio mute state.getSlotName(index: number): string | null- Returns canonical name (e.g.'5_LEFT').getSlotLabel(index: number): string- Returns badge digit (e.g.'5','4').parseResultToSlotIndex(res: string | number): number | null- Resolves result string to slot index.getState(): PlinkoGameState- Gets current state snapshot.setState(state: Partial<...>)- Updates state properties.destroy()- Disposes Three.js scene, physics bodies, event listeners, and DOM elements.
Exported Utilities
import {
getSlotName,
getSlotLabel,
parseResultToSlotIndex
} from "@jingx/plinko-threejs";
getSlotName(0); // "5_LEFT"
getSlotName(9); // "5_RIGHT"
getSlotLabel(0); // "5"
getSlotLabel(4); // "1"
getSlotLabel(5); // "1"
getSlotLabel(9); // "5"
parseResultToSlotIndex("5_LEFT"); // 0
parseResultToSlotIndex("5_RIGHT"); // 9Development
# Install dependencies
npm install
# Start development server
npm run dev
# Build library bundle (ESM + CJS + Types)
npm run build
# Build demo application
npm run build:demoLicense
MIT © SPACX
