class Turntable { constructor(canvasId) { this.canvas =…
- User
class Turntable { constructor(canvasId) { this.canvas = document.getElementById(canvasId); this.ctx = this.canvas.getContext('2d');
this.radius = 200; this.centerX = this.canvas.width / 2; this.centerY = this.canvas.height / 2; this.sectorColors = ['#FF5733', '#FFC300', '#4CAF50', '#3498db', '#9B59B6', '#E74C3C', '#F39C12', '#2C3E50']; this.sectorPrizes = ['手机', '电脑', '谢谢惠顾', '拍立得', '谢谢惠顾', '空调', '购物卡', '视频VIP', '护肤品', '蓝牙耳机', '谢谢惠顾', '空气炸锅']; // 奖项 this.sectorImages = [ '手机.png', '云端电脑.png', '谢谢参与.png', '相机.png', '谢谢参与.png', '空调.png', '购物好礼场景.png', '暂无会员卡.png', '美妆护肤.png', '耳机.png', '谢谢参与.png', '空气炸锅.png']; this.spinButton = document.getElementById('spinButton'); this.spinButton.addEventListener('click', () => { this.startSpin(); const ring1 = document.querySelector(".ring1"); const ring2 = document.querySelector(".ring2"); ring1.classList.add("scale-animation"); ring2.classList.add("scale-animation"); }); this.oppositespinButton = document.getElementById('oppositespinButton'); this.oppositespinButton.addEventListener('click', () => { this.startOppositeSpin(); }); this.sectorImagesData = []; this.loaded = false; this.preloadImages(); } preloadImages() { let imagesToLoad = this.sectorImages.length; this.sectorImages.forEach((src, index) => { const img = new Image(); img.onload = () => { this.sectorImagesData[index] = img; imagesToLoad--; if (imagesToLoad === 0) { this.loaded = true; //所有图片加载完毕 this.drawWheel(); } }; img.src = src; }); } drawWheel(currentAngle = 0) { this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); if (!this.loaded) { return; } const numSectors = this.sectorColors.length; const sectorAngle = (2 * Math.PI) / numSectors; const usedNumbers = []; //跟踪已经生成的随机数 for (let i = 0; i < numSectors; 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 prizenum = Math.floor(Math.random() * 12); // const text = this.sectorPrizes[prizenum]; let prizenum; let text; do { prizenum = Math.floor(Math.random() * 12); } while (usedNumbers.includes(prizenum)); //检查随机数 usedNumbers.push(prizenum); text = this.sectorPrizes[prizenum]; const textAngle = startAngle + sectorAngle / 2; const textX = this.centerX + Math.cos(textAngle) * this.radius * 0.8; const textY = this.centerY + Math.sin(textAngle) * this.radius * 0.8; 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 = '20px fantasy'; this.ctx.fillStyle = '#fff'; this.ctx.fillText(text, 0, 0); this.ctx.restore(); const image = this.sectorImagesData[usedNumbers[i]]; //从预加载的图片数组中获取图像 const imageX = this.centerX + Math.cos(textAngle) * this.radius * 0.55; const imageY = this.centerY + Math.sin(textAngle) * this.radius * 0.55; const imageWidth = 60; const imageHeight = 60; this.ctx.save(); this.ctx.translate(imageX, imageY); this.ctx.rotate(textAngle + Math.PI / 2); this.ctx.drawImage(image, -imageWidth / 2, -imageHeight / 2, imageWidth, imageHeight); this.ctx.restore(); } } startSpin() { const numSpins = 2; const targetSector = Math.floor(Math.random() * this.sectorPrizes.length); // 随机选择一个奖项作为目标扇形 const totalAngle = targetSector * (2 * Math.PI / this.sectorPrizes.length) + (2 * Math.PI) * numSpins; let currentAngle = 0; let spinSpeed = 0.08; let isPaused = false; const spinAnimation = () => { if (isPaused) { return; } currentAngle += spinSpeed; if (currentAngle >= totalAngle) { const winningSectorIndex = Math.floor(targetSector + numSpins) % this.sectorPrizes.length; const winningText = this.sectorPrizes[winningSectorIndex]; const winningImage = this.sectorImagesData[winningSectorIndex]; if (winningText === '谢谢惠顾') { alert('很遗憾!您没有中奖'); } else { alert(`恭喜您中奖!将获得 ${winningText} 一台/张/副`); } 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); }; requestAnimationFrame(spinAnimation); spinAnimation(); const stopButton = document.getElementById("stopButton"); stopButton.addEventListener("click", () => { isPaused = !isPaused; }); const speedButton = document.getElementById("speedButton"); speedButton.addEventListener("click", () => { spinSpeed = 0.15; }); } startOppositeSpin() { const numSpins = 2; const targetSector = Math.floor(Math.random() * this.sectorPrizes.length); //随机选择一个奖项作为目标扇形 const totalAngle = targetSector * (2 * Math.PI / this.sectorPrizes.length) + (2 * Math.PI) * numSpins; let currentAngle = 0; let spinSpeed = 0.08; let isPaused = false; const oppositespinAnimation = () => { if (isPaused) { return; } currentAngle -= spinSpeed; if (currentAngle <= -totalAngle) { const winningSectorIndex = Math.floor(targetSector + numSpins) % this.sectorPrizes.length; const winningText = this.sectorPrizes[winningSectorIndex]; const winningImage = this.sectorImagesData[winningSectorIndex]; if (winningText === '谢谢惠顾') { alert('很遗憾!您没有中奖'); } else { alert(`恭喜您中奖!将获得 ${winningText} 一台/张/副`); } 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(oppositespinAnimation); }; oppositespinAnimation(); const stopButton = document.getElementById("stopButton"); stopButton.addEventListener("click", () => { isPaused = !isPaused; }); const speedButton = document.getElementById("oppositespeedButton"); speedButton.addEventListener("click", () => { spinSpeed = 0.15; }); } - Assistant
在您的
startSpin和startOppositeSpin方法中,计算中奖部分的代码有一些问题。尤其是这一行:const winningSectorIndex = Math.floor(targetSector + numSpins) % this.sectorPrizes.length;这行代码试图确定赌盘停下时,指针指向的奖项,但存在以下几个问题:
targetSector是您随机选择的奖项的扇形索引,但它不应该直接与numSpins相加。numSpins是指转盘旋转的完整圈数,所以这里不应该添加完成的旋转圈数。- 转盘停止时,
currentAngle已经包含了targetSector扇形相对于起始位置的旋转角度以及整个旋转过程中的角度。但实际的中奖扇形,应该是currentAngle对应的扇形,且取决于转盘的总旋转角度和扇形的数目。
要修复这个问题,您应该基于总旋转角度
totalAngle和扇形角度来计算中奖扇形的索引。这里是一个修复该过程的示例:if (currentAngle >= totalAngle) { // 由于转盘总是逆时针转动而起始位置在图像的正上方 // 所以计算实际角度需要从 Math.PI / 2 (即90度) 开始减去总角度 const normalizedAngle = (Math.PI / 2 - totalAngle) % (2 * Math.PI); const winningSectorIndex = Math.floor(numSectors - normalizedAngle / sectorAngle) % numSectors; const winningText = this.sectorPrizes[winningSectorIndex]; // ... }同样,对于
startOppositeSpin函数,也需要进行类似的计算,但逻辑需要适应其是顺时针旋转。注意,您可能需要调整这里的计算以适应您的特定设置(例如,转盘的起始位置,旋转方向等)。重要的是,您要确保在弹窗显示中奖文字时,
winningSectorIndex要根据指针相对于扇形的实际停止位置来准确计算。 - User
- Assistant
要确保弹出窗口中的奖项文本与转盘上指针所指的扇区文本匹配,必须正确计算出中奖扇区的索引。以下是修改后的
startSpin和startOppositeSpin方法,其中包括用于计算中奖扇区索引的正确代码:首先,为了简化代码并避免重复,我们创建一个单独的方法来计算中奖扇区:
calculateWinningSectorIndex(currentAngle, numSectors, sectorAngle) { const offsetAngle = Math.PI / 2; // 偏移量,因为我们从垂直位置开始 let adjustedAngle = (currentAngle + offsetAngle) % (2 * Math.PI); // 将当前角度调整为从正上方开始 adjustedAngle = (2 * Math.PI) - adjustedAngle; // 逆时针旋转,所以要取相反数 const winningSectorIndex = Math.floor(adjustedAngle / sectorAngle) % numSectors; return winningSectorIndex; }