tiny-dices
Version:
Tiny Dices adds a sprinkle of fun and a touch of magic to your dice-rolling adventures โ fully customizable with CSS and JavaScript to match your style, theme, or imagination! ๐ฒโจ
129 lines (83 loc) โข 3.17 kB
Markdown
## ๐ฒ Dice Management
Control and manage individual 3D dice, from creating and styling them to inserting or clearing them from the display area.
### ๐ข `addCubeId()`
Increments and returns the current internal cube ID used for z-index stacking.
```js
const id = dice.addCubeId();
```
Returns: `number` โ The current cube ID before incrementing.
๐ง Each new die receives a unique ID so it appears above older ones.
### โ `addElement(item)`
Adds a new dice element to the internal list.
```js
const success = dice.addElement({
faces: [face1, face2, face3, face4, face5, face6],
container: outerDiv,
wrapper: innerCube
});
```
| Param | Type | Description |
|-------|------|-------------|
| `item` | `{ faces: HTMLElement[], container: HTMLElement, wrapper: HTMLElement }` | The die element with 6 faces and container elements. |
Returns: `boolean` โ `true` if added successfully; otherwise `false`.
โ
This ensures each die has the correct structure.
### ๐จ `updateDiceFaceSkin(face)`
Applies current visual styles (color, image, etc.) to a single face of a die.
```js
dice.updateDiceFaceSkin(faceElement);
```
| Param | Type | Description |
|-------|------|-------------|
| `face` | `HTMLElement` | A single die face element. |
Returns: `void`
### ๐ฒ `updateDiceSkin(index)`
Updates the skin of a specific die by index (color, border, text, and image).
```js
const updated = dice.updateDiceSkin(0);
```
| Param | Type | Description |
|-------|------|-------------|
| `index` | `number \| string` | Index of the die to update. |
Returns: `boolean` โ `true` if the die was found and updated; otherwise `false`.
### ๐งฎ `updateDicesSkin()`
Updates the skin of **all** dice currently on screen.
```js
dice.updateDicesSkin();
```
Returns: `void`
๐ Uses the current active skin settings and reapplies them to every die.
### ๐ `insertDiceElement(result, max, canZero, rollInfinity)`
Creates and inserts a 3D dice into the display area.
```js
const sequence = dice.insertDiceElement(4, 6, false, true);
```
| Param | Type | Description |
|--------|-----------|-------------|
| `result` | `number` | Value shown on the front face of the die. |
| `max` | `number` | Maximum value (i.e. the "sides" of the die). |
| `canZero` | `boolean` | Whether face value can be 0. |
| `rollInfinity` | `boolean` | Whether the die rolls forever. |
Returns: `PreDiceResult` โ The randomized sequence of values assigned to the six faces.
```ts
{
sequence: number[] // array of 6 values for each face
reRollDice: () => number[], // Function that re-rolls the dice and returns the new sequence
stop: () => void, // Function that stops the dice rolling
stopTimeout: NodeJS.Timeout|null // Reference to the timeout controlling the dice stop, or null if not set.
}
```
๐ฒ Each die gets animated, rendered, and z-stacked dynamically.
### ๐งผ `clearDiceArea()`
Removes **all** dice from the display and resets internal IDs.
```js
dice.clearDiceArea();
```
Returns: `void`
๐งน Great for resetting the board or starting fresh.