html5 抽奖效果,基于HTML5 canvas的简单抽奖轮盘
时间: 2023-08-03 21:31:34 浏览: 98
好的,下面是一个基于HTML5 canvas的简单抽奖轮盘的实现:
HTML 部分:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5抽奖轮盘</title>
<style>
#canvas {
margin: 0 auto;
display: block;
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
<button id="startBtn">开始抽奖</button>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="./js/lottery.js"></script>
</html>
```
JavaScript 部分:
```javascript
// 获取 canvas 和按钮元素
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var startBtn = document.getElementById('startBtn');
// 抽奖的奖项列表
var prizes = ['一等奖', '二等奖', '三等奖', '四等奖', '五等奖', '六等奖'];
// 抽奖轮盘的样式
var lotteryStyle = {
outerRadius: 200, // 轮盘的外半径
innerRadius: 50, // 轮盘的内半径
rotateAngle: 0, // 转盘的起始角度
fillStyle: ['#ff7f50', '#ff3e96', '#87cefa', '#da70d6', '#6495ed', '#ff69b4'], // 转盘的填充颜色
strokeStyle: '#fff', // 转盘的描边颜色
lineWidth: 2, // 转盘的描边宽度
prizes: prizes // 抽奖的奖项列表
};
// 绘制抽奖轮盘
function drawLottery() {
var angle = Math.PI * 2 / lotteryStyle.prizes.length;
for (var i = 0; i < lotteryStyle.prizes.length; i++) {
context.beginPath();
context.arc(canvas.width / 2, canvas.height / 2, lotteryStyle.outerRadius, lotteryStyle.rotateAngle, lotteryStyle.rotateAngle + angle);
context.arc(canvas.width / 2, canvas.height / 2, lotteryStyle.innerRadius, lotteryStyle.rotateAngle + angle, lotteryStyle.rotateAngle, true);
context.fillStyle = lotteryStyle.fillStyle[i % lotteryStyle.fillStyle.length];
context.fill();
context.strokeStyle = lotteryStyle.strokeStyle;
context.lineWidth = lotteryStyle.lineWidth;
context.stroke();
context.save();
context.beginPath();
context.fillStyle = '#fff';
context.font = 'bold 20px Arial';
context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(lotteryStyle.rotateAngle + angle / 2 + Math.PI / 2);
context.fillText(lotteryStyle.prizes[i], -context.measureText(lotteryStyle.prizes[i]).width / 2, -lotteryStyle.outerRadius + 30);
context.restore();
lotteryStyle.rotateAngle += angle;
}
}
// 抽奖的动画效果
function lotteryAnimation(prizeIndex) {
var angle = Math.PI * 2 / lotteryStyle.prizes.length;
var targetAngle = angle * prizeIndex;
var currentAngle = lotteryStyle.rotateAngle % (Math.PI * 2);
var speed = 0.3;
var count = 0;
var timer = setInterval(function() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawLottery();
count += speed;
if (count >= 3) {
speed *= 0.9;
}
if (count >= 3 && currentAngle + count >= targetAngle) {
clearInterval(timer);
startBtn.disabled = false;
alert('恭喜您获得了' + lotteryStyle.prizes[prizeIndex] + '!');
} else {
lotteryStyle.rotateAngle += speed;
currentAngle = lotteryStyle.rotateAngle % (Math.PI * 2);
}
}, 30);
}
// 点击抽奖按钮
startBtn.onclick = function() {
startBtn.disabled = true;
var prizeIndex = Math.floor(Math.random() * prizes.length);
lotteryAnimation(prizeIndex);
};
// 初始化抽奖轮盘
drawLottery();
```
这个实现中,我们使用 canvas 绘制了一个由多个扇形组成的轮盘,每个扇形代表一个奖项。当点击“开始抽奖”按钮时,随机生成一个奖项的索引,然后通过动画效果将轮盘旋转到指定的奖项位置。最终,弹出一个提示框,告诉用户获得了哪个奖项。
阅读全文