-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheel.js
361 lines (293 loc) · 10.6 KB
/
wheel.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// wheel.js
// Get canvas element and context
var canvas = document.getElementById('wheelCanvas');
var ctx = canvas.getContext('2d');
// Variables for the wheel
var names = [];
var startAngle = 0;
var arc = 0;
var spinTimeout = null;
var spinTime = 0;
var spinTimeTotal = 0;
// Variables for spinning animation
var totalRotation = 0;
var initialStartAngle = 0;
// Rigged winner variables
var isRigging = false;
var riggedWinner = '';
var riggedInput = '';
let triggerMode = false;
let triggerNumber = '';
let isInputFocused = false;
// Add event listeners for input focus and blur
document.getElementById('namesInput').addEventListener('focus', () => {
isInputFocused = true;
});
document.getElementById('namesInput').addEventListener('blur', () => {
isInputFocused = false;
});
// Modify the keydown event listener
document.addEventListener('keydown', (event) => {
if (!isInputFocused) {
if (event.code === 'Space') {
event.preventDefault(); // Prevent default space bar behavior
triggerMode = !triggerMode;
triggerNumber = '';
console.log(triggerMode ? 'Trigger mode activated' : 'Trigger mode deactivated');
} else if (triggerMode && event.key >= '0' && event.key <= '9') {
triggerNumber += event.key;
console.log('Current trigger number:', triggerNumber);
}
}
});
// Set canvas dimensions based on viewport
function setCanvasSize() {
var containerWidth = canvas.parentElement.clientWidth;
var containerHeight = window.innerHeight;
var size = Math.min(containerWidth, containerHeight * 0.8); // Increased from 0.7
canvas.width = size;
canvas.height = size;
console.log('Canvas size:', size);
}
// Function to update the wheel with names
function updateWheel() {
var input = document.getElementById('namesInput').value;
names = input.split(',').map(function(name) {
return name.trim();
}).filter(function(name) {
return name !== '';
});
startAngle = 0; // Reset the start angle
setCanvasSize(); // Update canvas size
drawWheel();
// Save names to localStorage
localStorage.setItem('wheelNames', JSON.stringify(names));
console.log('Wheel updated with names:', names);
}
// Function to draw the wheel
function drawWheel() {
// Adjusted to use dynamic canvas size
var outsideRadius = canvas.width / 2 - 20;
var textRadius = outsideRadius - 30;
var insideRadius = 0;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (names.length === 0) {
// If no names are available, display a message
ctx.font = 'bold 24px Arial';
ctx.fillStyle = 'black';
ctx.fillText('No names available!', canvas.width / 2 - ctx.measureText('No names available!').width / 2, canvas.height / 2);
return;
}
var numSegments = names.length;
arc = 2 * Math.PI / numSegments;
for (var i = 0; i < numSegments; i++) {
var angle = startAngle + i * arc;
var color = getColor(i, numSegments);
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(canvas.width / 2, canvas.height / 2);
ctx.arc(canvas.width / 2, canvas.height / 2, outsideRadius, angle, angle + arc, false);
ctx.lineTo(canvas.width / 2, canvas.height / 2);
ctx.fill();
// Convert HSL to RGB for text color calculation
var rgb = hslToRgb(i * (360 / numSegments) / 360, 1, 0.5);
var rgbString = 'rgb(' + Math.round(rgb.r * 255) + ',' + Math.round(rgb.g * 255) + ',' + Math.round(rgb.b * 255) + ')';
var textColor = getContrastingTextColor(rgbString);
ctx.fillStyle = textColor;
ctx.save();
ctx.translate(
canvas.width / 2 + Math.cos(angle + arc / 2) * textRadius,
canvas.height / 2 + Math.sin(angle + arc / 2) * textRadius
);
ctx.rotate(angle + arc / 2 + Math.PI / 2);
var text = names[i];
ctx.font = 'bold ' + Math.max(12, outsideRadius / 15) + 'px Arial';
ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
ctx.restore();
}
// Draw arrow pointing down at the top of the wheel
ctx.fillStyle = "black";
ctx.beginPath();
ctx.moveTo(canvas.width / 2 - 10, canvas.height / 2 - (outsideRadius + 20));
ctx.lineTo(canvas.width / 2 + 10, canvas.height / 2 - (outsideRadius + 20));
ctx.lineTo(canvas.width / 2, canvas.height / 2 - (outsideRadius + 10));
ctx.closePath();
ctx.fill();
}
// Function to generate colors for the wheel segments
function getColor(item, maxitem) {
var hue = item * (360 / maxitem);
return 'hsl(' + hue + ', 100%, 50%)';
}
// Function to determine the contrasting text color
function getContrastingTextColor(backgroundColor) {
// Extract RGB values from the background color string
var rgb = backgroundColor.match(/\d+/g).map(Number);
// Calculate relative luminance
var luminance = (0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]) / 255;
// Return black for light backgrounds and white for dark backgrounds
return luminance > 0.5 ? 'black' : 'white';
}
// Function to convert HSL to RGB
function hslToRgb(h, s, l) {
var r, g, b;
if (s === 0) {
r = g = b = l; // achromatic
} else {
var hue2rgb = function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
};
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return {
r: r,
g: g,
b: b
};
}
// Function to start spinning the wheel
function spin() {
if (names.length === 0) {
alert("No names available to spin!");
return;
}
spinTime = 0;
spinTimeTotal = 5000; // Total spin time in milliseconds
initialStartAngle = startAngle;
var rotations = Math.floor(Math.random() * 3) + 3; // 3 to 5 rotations
if (triggerMode && triggerNumber !== '') {
const index = parseInt(triggerNumber) - 1;
if (index >= 0 && index < names.length) {
console.log('Triggering spin to:', names[index]);
var desiredAngle = (names.length - index) * arc - (arc / 2);
desiredAngle = desiredAngle % (2 * Math.PI);
var currentAngle = (startAngle + Math.PI / 2) % (2 * Math.PI); // Adjust for arrow at 90 degrees
var angleDifference = (desiredAngle - currentAngle + 2 * Math.PI) % (2 * Math.PI);
totalRotation = rotations * 2 * Math.PI + angleDifference;
} else {
console.log('Invalid trigger number, spinning randomly');
totalRotation = rotations * 2 * Math.PI + Math.random() * 2 * Math.PI;
}
triggerMode = false;
triggerNumber = '';
} else {
totalRotation = rotations * 2 * Math.PI + Math.random() * 2 * Math.PI;
}
// Disable spin button
document.getElementById('spinBtn').disabled = true;
document.getElementById('result').classList.remove('show');
rotateWheel();
}
// Function to rotate the wheel
function rotateWheel() {
spinTime += 30;
if (spinTime >= spinTimeTotal) {
startAngle = initialStartAngle + totalRotation;
startAngle = startAngle % (2 * Math.PI); // Normalize the angle
stopRotateWheel();
return;
}
var t = spinTime / spinTimeTotal;
var easedT = easeOut(t);
startAngle = initialStartAngle + easedT * totalRotation;
drawWheel();
spinTimeout = setTimeout(rotateWheel, 30);
}
// Function to stop the wheel and display the selected name
function stopRotateWheel() {
console.log('Stopping wheel rotation');
clearTimeout(spinTimeout);
var degrees = startAngle * 180 / Math.PI + 90;
var arcd = arc * 180 / Math.PI;
var index = Math.floor((360 - (degrees % 360)) / arcd) % names.length;
var text = names[index];
document.getElementById('result').innerText = "Congratulations! The winner is " + text + "!";
document.getElementById('result').classList.add('show');
// Clear the rigged winner after spinning
riggedWinner = '';
// Enable spin button
document.getElementById('spinBtn').disabled = false;
console.log('Starting fireworks');
startFireworks();
// Redraw the wheel after a delay
setTimeout(() => {
console.log('Redrawing wheel after fireworks');
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawWheel();
}, 3000);
}
// Easing function for spin animation
function easeOut(t) {
return 1 - Math.pow(1 - t, 3);
}
// Load names from localStorage on page load
window.addEventListener('load', function() {
setCanvasSize();
var storedNames = localStorage.getItem('wheelNames');
if (storedNames) {
names = JSON.parse(storedNames);
document.getElementById('namesInput').value = names.join(', ');
startAngle = 0;
drawWheel();
} else {
// Initial draw if no names are in localStorage
drawWheel();
}
});
// Adjust canvas size on window resize
window.addEventListener('resize', function() {
setCanvasSize();
drawWheel();
});
// Make sure the event listener is properly set
document.getElementById('updateWheelBtn').addEventListener('click', updateWheel);
document.getElementById('spinBtn').addEventListener('click', spin);
// Function to create a firework
function createFirework(x, y) {
const firework = document.createElement('div');
firework.className = 'firework';
firework.style.left = `${x}px`;
firework.style.top = `${y}px`;
const colors = ['#ff0', '#ff3', '#f90', '#f09', '#90f', '#0ff', '#f0f', '#0f0'];
for (let i = 0; i < 80; i++) { // Tăng số lượng hạt từ 50 lên 80
const particle = document.createElement('div');
particle.className = 'particle';
const color = colors[Math.floor(Math.random() * colors.length)];
particle.style.backgroundColor = color;
const angle = Math.random() * Math.PI * 2;
const speed = Math.random() * 80 + 80; // Tăng tốc độ và khoảng cách
const translateY = Math.sin(angle) * speed;
const translateX = Math.cos(angle) * speed;
particle.style.setProperty('--translateY', `${translateY}px`);
particle.style.setProperty('--translateX', `${translateX}px`);
firework.appendChild(particle);
}
document.getElementById('fireworks-container').appendChild(firework);
setTimeout(() => {
firework.remove();
}, 1500); // Tăng thời gian hiển thị
}
// Function to start fireworks
function startFireworks() {
const container = document.querySelector('.col-md-8');
const containerRect = container.getBoundingClientRect();
const fireworksInterval = setInterval(() => {
for (let i = 0; i < 3; i++) { // Tạo 3 pháo hoa mỗi lần
const x = Math.random() * containerRect.width;
const y = Math.random() * containerRect.height;
createFirework(x, y);
}
}, 200); // Giảm khoảng thời gian giữa các đợt pháo hoa
setTimeout(() => {
clearInterval(fireworksInterval);
}, 3000);
}