大转盘html5源码,html5 大转盘
时间: 2023-09-05 09:11:24 浏览: 484
您好,以下是一个简单的HTML5大转盘代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>HTML5大转盘</title>
<style>
body {
background-color: #f3f3f3;
}
#canvas {
margin: 0 auto;
display: block;
}
</style>
</head>
<body>
<canvas id="canvas" width="300" height="300"></canvas>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// 画圆
function drawSlice(degrees, color) {
ctx.beginPath();
ctx.fillStyle = color;
ctx.moveTo(canvas.width/2, canvas.height/2);
ctx.arc(canvas.width/2, canvas.height/2, canvas.width/2, degrees*Math.PI/180, (degrees+60)*Math.PI/180);
ctx.lineTo(canvas.width/2, canvas.height/2);
ctx.fill();
}
// 画文字
function drawText(degrees, text) {
ctx.save();
ctx.translate(canvas.width/2, canvas.height/2);
ctx.rotate(degrees*Math.PI/180);
ctx.font = "bold 20px Arial";
ctx.fillText(text, canvas.width/4, 0);
ctx.restore();
}
// 画转盘
var slices = [
{ degrees: 0, color: '#f44336', text: '1' },
{ degrees: 60, color: '#E91E63', text: '2' },
{ degrees: 120, color: '#9C27B0', text: '3' },
{ degrees: 180, color: '#673AB7', text: '4' },
{ degrees: 240, color: '#3F51B5', text: '5' },
{ degrees: 300, color: '#2196F3', text: '6' }
];
for (var i = 0; i < slices.length; i++) {
var slice = slices[i];
drawSlice(slice.degrees, slice.color);
drawText(slice.degrees+30, slice.text);
}
// 转盘旋转
var rotation = 0;
function rotate() {
rotation += 5;
if (rotation >= 360) {
rotation = 0;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < slices.length; i++) {
var slice = slices[i];
drawSlice(slice.degrees+rotation, slice.color);
drawText(slice.degrees+rotation+30, slice.text);
}
requestAnimationFrame(rotate);
}
requestAnimationFrame(rotate);
</script>
</body>
</html>
```
这个示例中,使用了HTML5的Canvas元素来绘制转盘,然后通过JavaScript来使转盘旋转。您可以根据需要修改代码来实现您自己的大转盘效果。
阅读全文