DmitrMakeev
commited on
Commit
•
76f2705
1
Parent(s):
b1b29fc
Update koleso.html
Browse files- koleso.html +90 -0
koleso.html
CHANGED
@@ -72,5 +72,95 @@ body {
|
|
72 |
<canvas id="wheel" width="300" height="300"></canvas>
|
73 |
<div id="spin">Пуск</div>
|
74 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
</body>
|
76 |
</html>
|
|
|
72 |
<canvas id="wheel" width="300" height="300"></canvas>
|
73 |
<div id="spin">Пуск</div>
|
74 |
</div>
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
<script>
|
79 |
+
|
80 |
+
const sectors = [
|
81 |
+
{ color: '#f82', label: 'Stack' },
|
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
|
91 |
+
const tot = sectors.length
|
92 |
+
const spinEl = document.querySelector('#spin')
|
93 |
+
const ctx = document.querySelector('#wheel').getContext('2d')
|
94 |
+
const dia = ctx.canvas.width
|
95 |
+
const rad = dia / 2
|
96 |
+
const PI = Math.PI
|
97 |
+
const TAU = 2 * PI
|
98 |
+
const arc = TAU / sectors.length
|
99 |
+
|
100 |
+
const friction = 0.991 // 0.995=soft, 0.99=mid, 0.98=hard
|
101 |
+
let angVel = 0 // Angular velocity
|
102 |
+
let ang = 0 // Angle in radians
|
103 |
+
|
104 |
+
const getIndex = () => Math.floor(tot - (ang / TAU) * tot) % tot
|
105 |
+
|
106 |
+
function drawSector(sector, i) {
|
107 |
+
const ang = arc * i
|
108 |
+
ctx.save()
|
109 |
+
// COLOR
|
110 |
+
ctx.beginPath()
|
111 |
+
ctx.fillStyle = sector.color
|
112 |
+
ctx.moveTo(rad, rad)
|
113 |
+
ctx.arc(rad, rad, rad, ang, ang + arc)
|
114 |
+
ctx.lineTo(rad, rad)
|
115 |
+
ctx.fill()
|
116 |
+
// TEXT
|
117 |
+
ctx.translate(rad, rad)
|
118 |
+
ctx.rotate(ang + arc / 2)
|
119 |
+
ctx.textAlign = 'right'
|
120 |
+
ctx.fillStyle = '#fff'
|
121 |
+
ctx.font = 'bold 30px sans-serif'
|
122 |
+
ctx.fillText(sector.label, rad - 10, 10)
|
123 |
+
//
|
124 |
+
ctx.restore()
|
125 |
+
}
|
126 |
+
|
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 |
+
|
134 |
+
function frame() {
|
135 |
+
if (!angVel) return
|
136 |
+
angVel *= friction // Decrement velocity by friction
|
137 |
+
if (angVel < 0.002) angVel = 0 // Bring to stop
|
138 |
+
ang += angVel // Update angle
|
139 |
+
ang %= TAU // Normalize angle
|
140 |
+
rotate()
|
141 |
+
}
|
142 |
+
|
143 |
+
function engine() {
|
144 |
+
frame()
|
145 |
+
requestAnimationFrame(engine)
|
146 |
+
}
|
147 |
+
|
148 |
+
function init() {
|
149 |
+
sectors.forEach(drawSector)
|
150 |
+
rotate() // Initial rotation
|
151 |
+
engine() // Start engine
|
152 |
+
spinEl.addEventListener('click', () => {
|
153 |
+
if (!angVel) angVel = rand(0.25, 0.45)
|
154 |
+
})
|
155 |
+
}
|
156 |
+
|
157 |
+
init()
|
158 |
+
|
159 |
+
</script>
|
160 |
+
|
161 |
+
|
162 |
+
|
163 |
+
|
164 |
+
|
165 |
</body>
|
166 |
</html>
|