Changed around line 1
+ class LuckyDraw {
+ constructor() {
+ this.canvas = document.getElementById('wheelCanvas');
+ this.ctx = this.canvas.getContext('2d');
+ this.prizes = [];
+ this.isSpinning = false;
+ this.angle = 0;
+ this.spinSpeed = 0;
+
+ this.init();
+ }
+
+ init() {
+ this.drawButton = document.getElementById('drawButton');
+ this.prizeList = document.getElementById('prizeList');
+ this.winnersList = document.getElementById('winnersList');
+
+ this.drawButton.addEventListener('click', () => this.startDraw());
+ this.prizeList.addEventListener('input', () => this.updatePrizes());
+
+ this.updatePrizes();
+ this.drawWheel();
+ }
+
+ updatePrizes() {
+ this.prizes = this.prizeList.value
+ .split('\n')
+ .filter(prize => prize.trim())
+ .map(prize => prize.trim());
+ this.drawWheel();
+ }
+
+ drawWheel() {
+ const centerX = this.canvas.width / 2;
+ const centerY = this.canvas.height / 2;
+ const radius = Math.min(centerX, centerY) - 10;
+
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
+
+ if (this.prizes.length === 0) return;
+
+ const sliceAngle = (2 * Math.PI) / this.prizes.length;
+
+ this.prizes.forEach((prize, i) => {
+ const startAngle = i * sliceAngle + this.angle;
+ const endAngle = startAngle + sliceAngle;
+
+ // Draw slice
+ this.ctx.beginPath();
+ this.ctx.moveTo(centerX, centerY);
+ this.ctx.arc(centerX, centerY, radius, startAngle, endAngle);
+ this.ctx.closePath();
+ this.ctx.fillStyle = `hsl(${(360 / this.prizes.length) * i}, 70%, 60%)`;
+ this.ctx.fill();
+ this.ctx.stroke();
+
+ // Draw text
+ this.ctx.save();
+ this.ctx.translate(centerX, centerY);
+ this.ctx.rotate(startAngle + sliceAngle / 2);
+ this.ctx.textAlign = 'right';
+ this.ctx.fillStyle = 'white';
+ this.ctx.font = '14px Arial';
+ this.ctx.fillText(prize, radius - 20, 5);
+ this.ctx.restore();
+ });
+ }
+
+ startDraw() {
+ if (this.isSpinning || this.prizes.length === 0) return;
+
+ this.isSpinning = true;
+ this.spinSpeed = 0.2 + Math.random() * 0.1;
+ this.animate();
+ }
+
+ animate() {
+ this.angle += this.spinSpeed;
+ this.spinSpeed *= 0.99;
+ this.drawWheel();
+
+ if (this.spinSpeed > 0.001) {
+ requestAnimationFrame(() => this.animate());
+ } else {
+ this.isSpinning = false;
+ this.announceWinner();
+ }
+ }
+
+ announceWinner() {
+ const winningIndex = Math.floor(Math.random() * this.prizes.length);
+ const winner = this.prizes[winningIndex];
+
+ const li = document.createElement('li');
+ li.textContent = winner;
+ this.winnersList.prepend(li);
+
+ // Remove won prize
+ this.prizes.splice(winningIndex, 1);
+ this.prizeList.value = this.prizes.join('\n');
+ this.updatePrizes();
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', () => {
+ new LuckyDraw();
+ });