-
Notifications
You must be signed in to change notification settings - Fork 59
/
index.js
82 lines (63 loc) · 2.09 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {Wheel} from '../../../dist/spin-wheel-esm.js';
import {loadFonts, loadImages} from '../../../scripts/util.js';
import {props} from './props.js';
window.onload = async () => {
await loadFonts(props.map(i => i.itemLabelFont));
const wheel = new Wheel(document.querySelector('.wheel-wrapper'));
const dropdown = document.querySelector('select');
const images = [];
for (const p of props) {
// Initalise dropdown with the names of each example:
const opt = document.createElement('option');
opt.textContent = p.name;
dropdown.append(opt);
// Convert image urls into actual images:
images.push(initImage(p, 'image'));
images.push(initImage(p, 'overlayImage'));
for (const item of p.items) {
images.push(initImage(item, 'image'));
}
}
await loadImages(images);
// Show the wheel once everything has loaded
document.querySelector('.wheel-wrapper').style.visibility = 'visible';
// Handle dropdown change:
dropdown.onchange = () => {
wheel.init({
...props[dropdown.selectedIndex],
rotation: wheel.rotation, // Preserve value.
});
};
// Select default:
dropdown.options[0].selected = 'selected';
dropdown.onchange();
// Save object globally for easy debugging.
window.wheel = wheel;
const btnSpin = document.querySelector('button');
let modifier = 0;
window.addEventListener('click', (e) => {
// Listen for click event on spin button:
if (e.target === btnSpin) {
const {duration, winningItemRotaion} = calcSpinToValues();
wheel.spinTo(winningItemRotaion, duration);
}
});
function calcSpinToValues() {
const duration = 3000;
const winningItemRotaion = getRandomInt(360, 360 * 1.75) + modifier;
modifier += 360 * 1.75;
return {duration, winningItemRotaion};
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
function initImage(obj, pName) {
if (!obj[pName]) return null;
const i = new Image();
i.src = obj[pName];
obj[pName] = i;
return i;
}
};