<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="wrap">
<canvas width="400" height="400" id="vas" style="background: gray;"></canvas>
</div>
<script>
var can = document.getElementById('vas');
var ctx = can.getContext('2d');
rad = Math.PI * 2 / 100;//将360度分成100份
// 画圆
function circle() {
ctx.beginPath();
ctx.arc(200, 200, 100, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fillStyle = 'green';
ctx.fill();
}
// 百分比
function text(n) {
ctx.save();
// ctx.strokeStyle = "orangered";
ctx.font = "40px Arial";
ctx.fillStyle = "#fff";
ctx.fillText(n + "%", 200 - 35, 200 + 10);
// ctx.fill();
ctx.restore();
}
// 画圆弧
var val = 1;
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.arc(200, 200, 100, 0, rad, false);
ctx.closePath();
ctx.fillStyle = 'red';
ctx.fill();
(function draw() {
val++;
circle()
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.arc(200, 200, 100, 0, val * rad, false);
ctx.closePath();
ctx.fillStyle = 'red';
ctx.fill();
// console.log(val);
text(val);
if (val >= 100) {
val = 0;
}
requestAnimationFrame(draw);
})();
</script>
</body>
</html>
canvas实现百分比&扇形效果
最新推荐文章于 2022-05-07 09:25:48 发布