DmitrMakeev commited on
Commit
38c03ce
·
verified ·
1 Parent(s): 52eb55e

Update koleso.html

Browse files
Files changed (1) hide show
  1. koleso.html +64 -63
koleso.html CHANGED
@@ -85,104 +85,105 @@ const sectors = [
85
  { color: '#b0f', label: '100', probability: 15 },
86
  { color: '#f0b', label: '5', probability: 10 },
87
  { color: '#bf0', label: '500', probability: 10 }
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
- const friction = 0.991 // 0.995=soft, 0.99=mid, 0.98=hard
100
- let angVel = 0 // Angular velocity
101
- let ang = 0 // Angle in radians
102
-
103
- const getIndex = () => Math.floor(tot - (ang / TAU) * tot) % tot
104
 
105
  function drawSector(sector, i) {
106
- const ang = arc * i
107
- ctx.save()
108
  // COLOR
109
- ctx.beginPath()
110
- ctx.fillStyle = sector.color
111
- ctx.moveTo(rad, rad)
112
- ctx.arc(rad, rad, rad, ang, ang + arc)
113
- ctx.lineTo(rad, rad)
114
- ctx.fill()
115
  // TEXT
116
- ctx.translate(rad, rad)
117
- ctx.rotate(ang + arc / 2)
118
- ctx.textAlign = 'right'
119
- ctx.fillStyle = '#fff'
120
- ctx.font = 'bold 21px sans-serif' // Уменьшенный размер шрифта на 30%
121
- ctx.fillText(sector.label, rad - 10, 10)
122
  //
123
- ctx.restore()
124
  }
125
 
126
  function rotate() {
127
- const sector = sectors[getIndex()]
128
- ctx.canvas.style.transform = `rotate(${ang - PI / 2}rad)`
129
- spinEl.textContent = !angVel ? 'SPIN' : sector.label
130
- spinEl.style.background = sector.color
131
  }
132
 
133
  function frame() {
134
- if (!angVel) return
135
- angVel *= friction // Decrement velocity by friction
136
  if (angVel < 0.002) {
137
- angVel = 0 // Bring to stop
138
- const sector = sectors[getIndex()]
139
- localStorage.setItem('hasSpun', 'true')
140
- console.log('Result:', sector.label)
141
  }
142
- ang += angVel // Update angle
143
- ang %= TAU // Normalize angle
144
- rotate()
145
  }
146
 
147
  function engine() {
148
- frame()
149
- requestAnimationFrame(engine)
150
  }
151
 
152
  function init() {
153
  if (!localStorage.getItem('hasSpun')) {
154
- localStorage.setItem('hasSpun', 'false')
155
  }
156
 
157
- sectors.forEach(drawSector)
158
- rotate() // Initial rotation
159
- engine() // Start engine
160
  spinEl.addEventListener('click', () => {
161
  if (localStorage.getItem('hasSpun') === 'false') {
162
- localStorage.setItem('hasSpun', 'true')
163
- angVel = rand(0.25, 0.45)
164
- spinWheel()
165
  } else {
166
- console.log('You have already spun the wheel.')
167
  }
168
- })
169
  }
170
 
171
  function spinWheel() {
172
- const probabilities = sectors.map(sector => sector.probability)
173
- const totalProbability = probabilities.reduce((a, b) => a + b, 0)
174
- const random = Math.random() * totalProbability
175
- let cumulativeProbability = 0
176
  for (let i = 0; i < sectors.length; i++) {
177
- cumulativeProbability += sectors[i].probability
178
  if (random < cumulativeProbability) {
179
- ang = (i + 0.5) * arc
180
- break
181
  }
182
  }
183
  }
184
 
185
- window.onload = init
 
186
  </script>
187
 
188
 
 
85
  { color: '#b0f', label: '100', probability: 15 },
86
  { color: '#f0b', label: '5', probability: 10 },
87
  { color: '#bf0', label: '500', probability: 10 }
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
+ const friction = 0.991; // 0.995=soft, 0.99=mid, 0.98=hard
100
+ let angVel = 0; // Angular velocity
101
+ let ang = 0; // Angle in radians
102
+
103
+ const getIndex = () => Math.floor(tot - (ang / TAU) * tot) % tot;
104
 
105
  function drawSector(sector, i) {
106
+ const ang = arc * i;
107
+ ctx.save();
108
  // COLOR
109
+ ctx.beginPath();
110
+ ctx.fillStyle = sector.color;
111
+ ctx.moveTo(rad, rad);
112
+ ctx.arc(rad, rad, rad, ang, ang + arc);
113
+ ctx.lineTo(rad, rad);
114
+ ctx.fill();
115
  // TEXT
116
+ ctx.translate(rad, rad);
117
+ ctx.rotate(ang + arc / 2);
118
+ ctx.textAlign = 'right';
119
+ ctx.fillStyle = '#fff';
120
+ ctx.font = 'bold 21px sans-serif'; // Уменьшенный размер шрифта на 30%
121
+ ctx.fillText(sector.label, rad - 10, 10);
122
  //
123
+ ctx.restore();
124
  }
125
 
126
  function rotate() {
127
+ const sector = sectors[getIndex()];
128
+ ctx.canvas.style.transform = `rotate(${ang - PI / 2}rad)`;
129
+ spinEl.textContent = !angVel ? 'SPIN' : sector.label;
130
+ spinEl.style.background = sector.color;
131
  }
132
 
133
  function frame() {
134
+ if (!angVel) return;
135
+ angVel *= friction; // Decrement velocity by friction
136
  if (angVel < 0.002) {
137
+ angVel = 0; // Bring to stop
138
+ const sector = sectors[getIndex()];
139
+ localStorage.setItem('hasSpun', 'true');
140
+ console.log('Result:', sector.label); // Вывод значения в консоль
141
  }
142
+ ang += angVel; // Update angle
143
+ ang %= TAU; // Normalize angle
144
+ rotate();
145
  }
146
 
147
  function engine() {
148
+ frame();
149
+ requestAnimationFrame(engine);
150
  }
151
 
152
  function init() {
153
  if (!localStorage.getItem('hasSpun')) {
154
+ localStorage.setItem('hasSpun', 'false');
155
  }
156
 
157
+ sectors.forEach(drawSector);
158
+ rotate(); // Initial rotation
159
+ engine(); // Start engine
160
  spinEl.addEventListener('click', () => {
161
  if (localStorage.getItem('hasSpun') === 'false') {
162
+ localStorage.setItem('hasSpun', 'true');
163
+ angVel = rand(0.25, 0.45);
164
+ spinWheel();
165
  } else {
166
+ console.log('You have already spun the wheel.');
167
  }
168
+ });
169
  }
170
 
171
  function spinWheel() {
172
+ const probabilities = sectors.map(sector => sector.probability);
173
+ const totalProbability = probabilities.reduce((a, b) => a + b, 0);
174
+ const random = Math.random() * totalProbability;
175
+ let cumulativeProbability = 0;
176
  for (let i = 0; i < sectors.length; i++) {
177
+ cumulativeProbability += sectors[i].probability;
178
  if (random < cumulativeProbability) {
179
+ ang = (i + 0.5) * arc;
180
+ break;
181
  }
182
  }
183
  }
184
 
185
+ window.onload = init;
186
+
187
  </script>
188
 
189