gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
class Turntable { constructor(canvasId) { this.canvas =…
- User
class Turntable { constructor(canvasId) { this.canvas = document.getElementById(canvasId); this.ctx = this.canvas.getContext('2d');
codethis.radius = 200; // 转盘半径 this.centerX = this.canvas.width / 2; // 转盘中心点的 x 坐标 this.centerY = this.canvas.height / 2; // 转盘中心点的 y 坐标 this.sectorColors = ['#FF5733', '#FFC300', '#4CAF50', '#3498db', '#9B59B6', '#E74C3C', '#F39C12', '#2C3E50']; // 扇形的颜色 this.sectorPrizes = ['奖品1', '奖品2', '奖品3', '奖品4', '奖品5', '奖品6', '奖品7', '奖品8']; // 奖项 this.spinButton = document.getElementById('spinButton'); this.spinButton.addEventListener('click', () => { this.startSpin(); }); } drawWheel() { const numberOfSectors = this.sectorPrizes.length; const sectorAngle = (2 * Math.PI) / numberOfSectors; for (let i = 0; i < numberOfSectors; i++) { const startAngle = i * sectorAngle; const endAngle = (i + 1) * sectorAngle; this.ctx.beginPath(); this.ctx.moveTo(this.centerX, this.centerY); this.ctx.arc(this.centerX, this.centerY, this.radius, startAngle, endAngle); this.ctx.closePath(); this.ctx.fillStyle = this.sectorColors[i % this.sectorColors.length]; this.ctx.fill(); const text = this.sectorPrizes[i]; const textAngle = startAngle + sectorAngle / 2; const textX = this.centerX + Math.cos(textAngle) * this.radius * 0.5; const textY = this.centerY + Math.sin(textAngle) * this.radius * 0.5; this.ctx.save(); this.ctx.translate(textX, textY); this.ctx.rotate(textAngle + Math.PI / 2); this.ctx.textAlign = 'center'; this.ctx.textBaseline = 'middle'; this.ctx.font = '16px Arial'; this.ctx.fillStyle = '#fff'; this.ctx.fillText(text, 0, 0); this.ctx.restore(); } } startSpin() { const numberOfSpins = 2; // 旋转圈数 const targetSector = Math.floor(Math.random() * this.sectorPrizes.length); // 随机选择一个奖项作为目标扇形 const totalAngle = targetSector * (2 * Math.PI / this.sectorPrizes.length) + (2 * Math.PI) * numberOfSpins; // const radomangle = Math.random() * 360; // const totalAngle = radomangle + (2 * Math.PI) * numberOfSpins; let currentAngle = 0; const spinSpeed = 0.08; // 旋转速度 const spinAnimation = () => { currentAngle += spinSpeed; if (currentAngle >= totalAngle) { // 到达目标角度,停止旋转 return; } this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); this.ctx.save(); this.ctx.translate(this.centerX, this.centerY); this.ctx.rotate(currentAngle); this.ctx.translate(-this.centerX, -this.centerY); this.drawWheel(); this.ctx.restore(); requestAnimationFrame(spinAnimation); }; spinAnimation(); } - Assistant