-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (47 loc) · 1.62 KB
/
Copy pathscript.js
File metadata and controls
56 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
let rollCount = 0;
function rollDice() {
const btn = document.getElementById('roll-btn');
const d20 = document.getElementById('d20');
const numberEl = document.getElementById('dice-number');
const label = document.getElementById('result-label');
const historyList = document.getElementById('history-list');
btn.disabled = true;
// Animate
d20.classList.remove('rolling');
void d20.offsetWidth; // reflow to restart animation
d20.classList.add('rolling');
// Flash numbers during roll
let flashes = 0;
const flashInterval = setInterval(() => {
numberEl.textContent = Math.ceil(Math.random() * 20);
flashes++;
if (flashes >= 10) clearInterval(flashInterval);
}, 50);
setTimeout(() => {
const result = Math.ceil(Math.random() * 20);
numberEl.textContent = result;
label.className = 'result-label';
if (result === 20) {
label.textContent = 'Natural 20! Critical Hit!';
label.classList.add('nat20');
} else if (result === 1) {
label.textContent = 'Natural 1! Critical Fail!';
label.classList.add('nat1');
} else {
label.textContent = `You rolled a ${result}`;
}
// Add to history
rollCount++;
const li = document.createElement('li');
if (result === 20) li.classList.add('nat20');
if (result === 1) li.classList.add('nat1');
li.innerHTML = `
<span class="roll-label">Roll #${rollCount}</span>
<span class="roll-num">${result}</span>
`;
historyList.prepend(li);
btn.disabled = false;
}, 650);
}
// Also allow clicking the dice itself
document.getElementById('d20').addEventListener('click', rollDice);