js实现抽奖转盘效果
实现抽奖转盘效果
抽奖转盘通常由旋转动画和结果判定两部分组成。以下是一个基于Canvas和JavaScript的实现方案:

初始化Canvas和转盘绘制
const canvas = document.getElementById('wheel');
const ctx = canvas.getContext('2d');
const segments = ['一等奖', '二等奖', '三等奖', '谢谢参与', '再来一次'];
const colors = ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF'];
const center = { x: canvas.width/2, y: canvas.height/2 };
const radius = Math.min(canvas.width, canvas.height)/2 - 10;
function drawWheel() {
const arcAngle = (2 * Math.PI) / segments.length;
segments.forEach((segment, index) => {
ctx.beginPath();
ctx.fillStyle = colors[index];
ctx.moveTo(center.x, center.y);
ctx.arc(center.x, center.y, radius,
index * arcAngle, (index + 1) * arcAngle);
ctx.lineTo(center.x, center.y);
ctx.fill();
// 绘制文本
ctx.save();
ctx.translate(center.x, center.y);
ctx.rotate(index * arcAngle + arcAngle/2);
ctx.textAlign = 'right';
ctx.fillStyle = '#fff';
ctx.font = '16px Arial';
ctx.fillText(segment, radius - 10, 5);
ctx.restore();
});
}
实现旋转动画
let currentRotation = 0;
let isSpinning = false;
function spin() {
if(isSpinning) return;
isSpinning = true;
const spinTime = 3000; // 旋转总时间
const startTime = Date.now();
const targetRotation = currentRotation + 10 + Math.random() * 20;
function animate() {
const elapsed = Date.now() - startTime;
const progress = Math.min(elapsed / spinTime, 1);
const easeOut = 1 - Math.pow(1 - progress, 3);
currentRotation = easeOut * targetRotation;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.translate(center.x, center.y);
ctx.rotate(currentRotation);
ctx.translate(-center.x, -center.y);
drawWheel();
ctx.restore();
if(progress < 1) {
requestAnimationFrame(animate);
} else {
isSpinning = false;
determineResult();
}
}
requestAnimationFrame(animate);
}
function determineResult() {
const normalizedRotation = currentRotation % (2 * Math.PI);
const segmentAngle = (2 * Math.PI) / segments.length;
const winningIndex = Math.floor(
(2 * Math.PI - normalizedRotation) / segmentAngle
) % segments.length;
alert(`恭喜获得: ${segments[winningIndex]}`);
}
HTML结构
<canvas id="wheel" width="400" height="400"></canvas>
<button onclick="spin()">开始抽奖</button>
CSS样式
#wheel {
display: block;
margin: 0 auto;
background: #f5f5f5;
border-radius: 50%;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
button {
display: block;
margin: 20px auto;
padding: 10px 20px;
font-size: 16px;
background: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
完整实现步骤
- 初始化Canvas并设置尺寸
- 定义奖项和对应颜色
- 绘制转盘扇形区域和文字
- 实现旋转动画使用requestAnimationFrame
- 添加缓动函数使旋转更自然
- 计算最终停止位置确定获奖结果
- 添加开始按钮触发旋转
优化建议
- 添加指针指示器
- 实现多次旋转惯性效果
- 增加音效增强体验
- 使用图片替代纯色扇形
- 添加开始/停止动画的过渡效果
这个实现使用了Canvas绘制和JavaScript动画基础,可以根据需要调整转盘大小、奖项数量和旋转时间等参数。






