-
Notifications
You must be signed in to change notification settings - Fork 8
/
script.js
100 lines (100 loc) · 3.25 KB
/
script.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* --------------- Spin Wheel --------------------- */
const spinWheel = document.getElementById("spinWheel");
const spinBtn = document.getElementById("spin_btn");
const text = document.getElementById("text");
/* --------------- Minimum And Maximum Angle For A value --------------------- */
const spinValues = [
{ minDegree: 61, maxDegree: 90, value: 100 },
{ minDegree: 31, maxDegree: 60, value: 200 },
{ minDegree: 0, maxDegree: 30, value: 300 },
{ minDegree: 331, maxDegree: 360, value: 400 },
{ minDegree: 301, maxDegree: 330, value: 500 },
{ minDegree: 271, maxDegree: 300, value: 600 },
{ minDegree: 241, maxDegree: 270, value: 700 },
{ minDegree: 211, maxDegree: 240, value: 800 },
{ minDegree: 181, maxDegree: 210, value: 900 },
{ minDegree: 151, maxDegree: 180, value: 1000 },
{ minDegree: 121, maxDegree: 150, value: 1100 },
{ minDegree: 91, maxDegree: 120, value: 1200 },
];
/* --------------- Size Of Each Piece --------------------- */
const size = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10];
/* --------------- Background Colors --------------------- */
var spinColors = [
"#E74C3C",
"#7D3C98",
"#2E86C1",
"#138D75",
"#F1C40F",
"#D35400",
"#138D75",
"#F1C40F",
"#b163da",
"#E74C3C",
"#7D3C98",
"#138D75",
];
/* --------------- Chart --------------------- */
/* --------------- Guide : https://chartjs-plugin-datalabels.netlify.app/guide/getting-started.html --------------------- */
let spinChart = new Chart(spinWheel, {
plugins: [ChartDataLabels],
type: "pie",
data: {
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
datasets: [
{
backgroundColor: spinColors,
data: size,
},
],
},
options: {
responsive: true,
animation: { duration: 0 },
plugins: {
tooltip: false,
legend: {
display: false,
},
datalabels: {
rotation: 90,
color: "#ffffff",
formatter: (_, context) => context.chart.data.labels[context.dataIndex],
font: { size: 24 },
},
},
},
});
/* --------------- Display Value Based On The Angle --------------------- */
const generateValue = (angleValue) => {
for (let i of spinValues) {
if (angleValue >= i.minDegree && angleValue <= i.maxDegree) {
text.innerHTML = `<p>Congratulations, You Have Won $${i.value} ! </p>`;
spinBtn.disabled = false;
break;
}
}
};
/* --------------- Spinning Code --------------------- */
let count = 0;
let resultValue = 101;
spinBtn.addEventListener("click", () => {
spinBtn.disabled = true;
text.innerHTML = `<p>Best Of Luck!</p>`;
let randomDegree = Math.floor(Math.random() * (355 - 0 + 1) + 0);
let rotationInterval = window.setInterval(() => {
spinChart.options.rotation = spinChart.options.rotation + resultValue;
spinChart.update();
if (spinChart.options.rotation >= 360) {
count += 1;
resultValue -= 5;
spinChart.options.rotation = 0;
} else if (count > 15 && spinChart.options.rotation == randomDegree) {
generateValue(randomDegree);
clearInterval(rotationInterval);
count = 0;
resultValue = 101;
}
}, 10);
});
/* --------------- End Spin Wheel --------------------- */