File size: 4,072 Bytes
04d1c4d
 
 
 
5d356ca
 
 
 
b1b29fc
 
 
 
 
 
3d81750
b1b29fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d356ca
 
 
 
 
 
 
76f2705
 
 
 
 
a7bd6bd
b94e502
94a5fb4
06f0aeb
 
 
b94e502
 
76f2705
 
 
 
 
 
 
 
 
 
 
 
 
 
06f0aeb
76f2705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b94e502
76f2705
 
 
 
 
 
 
 
b94e502
76f2705
 
 
 
 
 
a7bd6bd
 
 
06f0aeb
a7bd6bd
 
76f2705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7bd6bd
 
94a5fb4
a7bd6bd
 
 
76f2705
 
 
94a5fb4
 
 
 
 
 
 
 
 
 
 
 
 
 
76f2705
 
 
 
 
 
 
5d356ca
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<head>
    <title>Spin N Win</title>
  
    <link rel="stylesheet" href="https://huggingface.co/spaces/DMTuit/psy4/resolve/main/css/main.css">
    <script src="https://huggingface.co/spaces/DMTuit/psy4/resolve/main/js/main.js"></script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: grid;
  place-items: center;
}

#wheelOfFortune {
  display: inline-block;
  position: relative;
  overflow: hidden;
}

#wheel {
  display: block;
}

#spin {
  font: 1.5em/0 sans-serif;
  user-select: none;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 30%;
  height: 30%;
  margin: -15%;
  background: #fff;
  color: #fff;
  box-shadow: 0 0 0 8px currentColor, 0 0px 15px 5px rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  transition: 0.8s;
}

#spin::after {
  content: '';
  position: absolute;
  top: -17px;
  border: 10px solid transparent;
  border-bottom-color: currentColor;
  border-top: none;
}



  
</style>


  
  </head>

  <body>
    <div id="wheelOfFortune">
      <canvas id="wheel" width="300" height="300"></canvas>
      <div id="spin">Пуск</div>
    </div>



<script>

const sectors = [
  { color: '#f82', label: 'Stack', probability: 10 },
  { color: '#0bf', label: '10', probability: 20 },
  { color: '#fb0', label: '200', probability: 15 },
  { color: '#0fb', label: '50', probability: 20 },
  { color: '#b0f', label: '100', probability: 15 },
  { color: '#f0b', label: '5', probability: 10 },
  { color: '#bf0', label: '500', probability: 10 }
]

const rand = (m, M) => Math.random() * (M - m) + m
const tot = sectors.length
const spinEl = document.querySelector('#spin')
const ctx = document.querySelector('#wheel').getContext('2d')
const dia = ctx.canvas.width
const rad = dia / 2
const PI = Math.PI
const TAU = 2 * PI
const arc = TAU / sectors.length
const friction = 0.991 // 0.995=soft, 0.99=mid, 0.98=hard
let angVel = 0 // Angular velocity
let ang = 0 // Angle in radians
let hasSpun = false

const getIndex = () => Math.floor(tot - (ang / TAU) * tot) % tot

function drawSector(sector, i) {
  const ang = arc * i
  ctx.save()
  // COLOR
  ctx.beginPath()
  ctx.fillStyle = sector.color
  ctx.moveTo(rad, rad)
  ctx.arc(rad, rad, rad, ang, ang + arc)
  ctx.lineTo(rad, rad)
  ctx.fill()
  // TEXT
  ctx.translate(rad, rad)
  ctx.rotate(ang + arc / 2)
  ctx.textAlign = 'right'
  ctx.fillStyle = '#fff'
  ctx.font = 'bold 39px sans-serif' // Увеличенный размер шрифта на 30%
  ctx.fillText(sector.label, rad - 10, 10)
  //
  ctx.restore()
}

function rotate() {
  const sector = sectors[getIndex()]
  ctx.canvas.style.transform = `rotate(${ang - PI / 2}rad)`
  spinEl.textContent = !angVel ? 'SPIN' : sector.label
  spinEl.style.background = sector.color
}

function frame() {
  if (!angVel) return
  angVel *= friction // Decrement velocity by friction
  if (angVel < 0.002) {
    angVel = 0 // Bring to stop
    const sector = sectors[getIndex()]
    hasSpun = true
    console.log('Result:', sector.label)
  }
  ang += angVel // Update angle
  ang %= TAU // Normalize angle
  rotate()
}

function engine() {
  frame()
  requestAnimationFrame(engine)
}

function init() {
  sectors.forEach(drawSector)
  rotate() // Initial rotation
  engine() // Start engine
  spinEl.addEventListener('click', () => {
    if (!angVel && !hasSpun) {
      angVel = rand(0.25, 0.45)
      spinWheel()
    } else if (hasSpun) {
      console.log('You have already spun the wheel.')
    }
  })
}

function spinWheel() {
  const probabilities = sectors.map(sector => sector.probability)
  const totalProbability = probabilities.reduce((a, b) => a + b, 0)
  const random = Math.random() * totalProbability
  let cumulativeProbability = 0
  for (let i = 0; i < sectors.length; i++) {
    cumulativeProbability += sectors[i].probability
    if (random < cumulativeProbability) {
      ang = (i + 0.5) * arc
      break
    }
  }
}

init()
</script>




    
  </body>
</html>