canvas 仪表盘
时间: 2023-12-05 16:39:15 浏览: 30
以下是使用HTML5 Canvas绘制仪表盘的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Canvas Gauge</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="gauge" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById("gauge");
var ctx = canvas.getContext("2d");
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var radius = 80;
// 绘制背景圆
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
ctx.fillStyle = "#f2f2f2";
ctx.fill();
// 绘制进度圆
var progress = 0.7; // 进度值
ctx.beginPath();
ctx.arc(centerX, centerY, radius, -Math.PI / 2, -Math.PI / 2 + 2 * Math.PI * progress);
ctx.lineTo(centerX, centerY);
ctx.fillStyle = "#66ccff";
ctx.fill();
// 绘制指针
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.lineTo(centerX + radius * Math.cos(-Math.PI / 2 + 2 * Math.PI * progress), centerY + radius * Math.sin(-Math.PI / 2 + 2 * Math.PI * progress));
ctx.strokeStyle = "#ff0000";
ctx.lineWidth = 5;
ctx.stroke();
</script>
</body>
</html>
```
该示例代码绘制了一个半径为80像素的仪表盘,进度值为0.7,指针为红色。你可以根据需要修改进度值和指针颜色等参数。