Open laijbin opened 9 years ago
让绘画过程被用户感知
<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="400" height="200" style="border: 1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var l ,speed,cur,end;
function drawArc(color, x, y, r, s, e,init) {
if (init) {
end = e;
l = e - s;
speed = l/20;
cur = s+speed;
drawArc(color, x, y, r, s, cur)
}else{
if (cur<=end) {
ctx.clearRect(0,0,400,200)
ctx.strokeStyle = color;
ctx.beginPath();
ctx.arc(x,y,r,s,e,false);
ctx.stroke()
cur = cur+speed;
setTimeout(function(){
drawArc(color, x, y, r, s, cur)
},17)
} else {
ctx.clearRect(0,0,400,200)
ctx.strokeStyle = color;
ctx.beginPath();
ctx.arc(x,y,r,s,end,false);
ctx.stroke()
}
}
}
// function drawArc(color, x, y, r, s, e) {
// ctx.strokeStyle = color;
// ctx.beginPath();
// ctx.arc(x,y,r,s,e,false);
// ctx.stroke()
// }
var c = document.getElementById('myCanvas'),
ctx = c.getContext('2d'),
p = Math.PI;
ctx.lineWidth = 5;
drawArc('#163B62',70,70,40,0,2*p,true)
// drawArc('#000',160,70,40,0,2*p)
// drawArc('#BF0628',250,70,40,0,2*p)
// drawArc('#EBC41F',110,110,40,0,2*p)
// drawArc('#198E4A',200,110,40,0,2*p)
// drawArc('#163B62',70,70,40,1.9*p,2.1*p)
// drawArc('#000',160,70,40,0.9*p,2.1*p)
// drawArc('#BF0628',250,70,40,0.9*p,1.1*p)
</script>
</body>
</html>
效果图:
实现代码: