旋转的唱片hmtl
时间: 2024-02-12 21:06:11 浏览: 30
你可以使用HTML5中的<canvas>元素来绘制旋转的唱片。以下是一个示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>旋转的唱片</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
// 绘制黑色圆盘
context.beginPath();
context.arc(100, 100, 80, 0, 2 * Math.PI);
context.fillStyle = "black";
context.fill();
// 绘制唱针
context.beginPath();
context.moveTo(100, 100);
context.lineTo(120, 60);
context.lineWidth = 5;
context.strokeStyle = "white";
context.stroke();
// 定义旋转函数
var rotateDegree = 0;
function rotate() {
rotateDegree += 5;
context.save();
context.clearRect(0, 0, canvas.width, canvas.height);
context.translate(100, 100);
context.rotate(rotateDegree * Math.PI / 180);
context.translate(-100, -100);
context.beginPath();
context.arc(100, 100, 80, 0, 2 * Math.PI);
context.fillStyle = "black";
context.fill();
context.beginPath();
context.moveTo(100, 100);
context.lineTo(120, 60);
context.lineWidth = 5;
context.strokeStyle = "white";
context.stroke();
context.restore();
requestAnimationFrame(rotate);
}
// 启动旋转
rotate();
</script>
</body>
</html>
```
这个示例中,我们使用<canvas>元素绘制了一个黑色圆盘和一个唱针。然后定义了一个旋转函数,每次旋转5度,并且使用requestAnimationFrame函数定时调用该函数。在旋转函数中,我们使用context.save()和context.restore()保存和恢复画布状态,这样就不会影响到其他元素的绘制。通过使用context.translate()和context.rotate()函数,我们让画布绕着中心点旋转。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)