DmitrMakeev
commited on
Commit
•
94a5fb4
1
Parent(s):
a7bd6bd
Update koleso.html
Browse files- koleso.html +23 -8
koleso.html
CHANGED
@@ -78,13 +78,13 @@ body {
|
|
78 |
<script>
|
79 |
|
80 |
const sectors = [
|
81 |
-
{ color: '#f82', label: '
|
82 |
-
{ color: '#0bf', label: '10' },
|
83 |
-
{ color: '#fb0', label: '200' },
|
84 |
-
{ color: '#0fb', label: '50' },
|
85 |
-
{ color: '#b0f', label: '100' },
|
86 |
-
{ color: '#f0b', label: '5' },
|
87 |
-
{ color: '#bf0', label: '500' }
|
88 |
]
|
89 |
|
90 |
const rand = (m, M) => Math.random() * (M - m) + m
|
@@ -127,7 +127,7 @@ function drawSector(sector, i) {
|
|
127 |
function rotate() {
|
128 |
const sector = sectors[getIndex()]
|
129 |
ctx.canvas.style.transform = `rotate(${ang - PI / 2}rad)`
|
130 |
-
spinEl.textContent = !angVel ? '
|
131 |
spinEl.style.background = sector.color
|
132 |
}
|
133 |
|
@@ -157,12 +157,27 @@ function init() {
|
|
157 |
spinEl.addEventListener('click', () => {
|
158 |
if (!angVel && !hasSpun) {
|
159 |
angVel = rand(0.25, 0.45)
|
|
|
160 |
} else if (hasSpun) {
|
161 |
console.log('You have already spun the wheel.')
|
162 |
}
|
163 |
})
|
164 |
}
|
165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
init()
|
167 |
</script>
|
168 |
|
|
|
78 |
<script>
|
79 |
|
80 |
const sectors = [
|
81 |
+
{ color: '#f82', label: 'Stack', probability: 1 },
|
82 |
+
{ color: '#0bf', label: '10', probability: 20 },
|
83 |
+
{ color: '#fb0', label: '200', probability: 16 },
|
84 |
+
{ color: '#0fb', label: '50', probability: 26 },
|
85 |
+
{ color: '#b0f', label: '100', probability: 16 },
|
86 |
+
{ color: '#f0b', label: '5', probability: 16 },
|
87 |
+
{ color: '#bf0', label: '500', probability: 15 }
|
88 |
]
|
89 |
|
90 |
const rand = (m, M) => Math.random() * (M - m) + m
|
|
|
127 |
function rotate() {
|
128 |
const sector = sectors[getIndex()]
|
129 |
ctx.canvas.style.transform = `rotate(${ang - PI / 2}rad)`
|
130 |
+
spinEl.textContent = !angVel ? 'SPIN' : sector.label
|
131 |
spinEl.style.background = sector.color
|
132 |
}
|
133 |
|
|
|
157 |
spinEl.addEventListener('click', () => {
|
158 |
if (!angVel && !hasSpun) {
|
159 |
angVel = rand(0.25, 0.45)
|
160 |
+
spinWheel()
|
161 |
} else if (hasSpun) {
|
162 |
console.log('You have already spun the wheel.')
|
163 |
}
|
164 |
})
|
165 |
}
|
166 |
|
167 |
+
function spinWheel() {
|
168 |
+
const probabilities = sectors.map(sector => sector.probability)
|
169 |
+
const totalProbability = probabilities.reduce((a, b) => a + b, 0)
|
170 |
+
const random = Math.random() * totalProbability
|
171 |
+
let cumulativeProbability = 0
|
172 |
+
for (let i = 0; i < sectors.length; i++) {
|
173 |
+
cumulativeProbability += sectors[i].probability
|
174 |
+
if (random < cumulativeProbability) {
|
175 |
+
ang = (i + 0.5) * arc
|
176 |
+
break
|
177 |
+
}
|
178 |
+
}
|
179 |
+
}
|
180 |
+
|
181 |
init()
|
182 |
</script>
|
183 |
|