首页 技术 正文
技术 2022年11月20日
0 收藏 956 点赞 2,864 浏览 1887 个字

html代码:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<canvas id="pro" width="400" height="300"></canvas>
</body>
</html>

js代码:

第一步:

var c=document.getElementById("pro"),
pro=0,
ctx=c.getContext("2d");
//画灰色的圆
ctx.beginPath();
ctx.arc(200,200,190,0,Math.PI*2);
ctx.closePath();
ctx.fillStyle='#e3eaf2';
ctx.fill();

效果图如下:

使用canvas实现环形进度条

第二步:

function drawCricle(ctx,percent){
//画进度环
ctx.beginPath();
ctx.moveTo(200,200);
ctx.arc(200,200,190,Math.PI*0.8,Math.PI*(0.8+2*percent/200));
ctx.closePath();
ctx.fillStyle='#ff4b88';
ctx.fill(); //画内填充圆
ctx.beginPath();
ctx.arc(200,200,175,0,Math.PI*2);
ctx.closePath();
ctx.fillStyle='#fff';
ctx.fill();
}
drawCricle(ctx,60);//执行这个函数

效果图如下:

使用canvas实现环形进度条

第三步:让它动起来

function animate(){
requestAnimationFrame(function(){
pro=pro+1;
drawCricle(ctx,pro);
if(pro<60){
animate();
}
});
}

如果需加入百分比文字:

//将这段代码加到drawCricle函数里面
ctx.font = "bold 20pt Microsoft YaHei";
ctx.fillStyle = '#333';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.moveTo(200, 200);
ctx.fillText(process + '%', 200, 200);

效果如下:

使用canvas实现环形进度条

完整代码如下:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<canvas id="pro" width="400" height="400"></canvas>
<script>
(function(){
var c=document.getElementById("pro"),
pro=0,
ctx=c.getContext("2d"); //画灰色的圆
ctx.beginPath();
ctx.arc(200,200,80,0,Math.PI*2);
ctx.closePath();
ctx.fillStyle='#f6f6f6';
ctx.fill(); function animate(){
requestAnimationFrame(function(){
pro=pro+1;
drawCricle(ctx,pro);
if(pro<60){
animate();
}
});
} function drawCricle(ctx,percent){
//画进度环
ctx.beginPath();
ctx.moveTo(200,200);
ctx.arc(200,200,80,Math.PI*0.8,Math.PI*(0.8+2*percent/200));
ctx.closePath();
ctx.fillStyle='#ff9600';
ctx.fill(); //画内填充圆
ctx.beginPath();
ctx.arc(200,200,75,0,Math.PI*2);
ctx.closePath();
ctx.fillStyle='#fff';
ctx.fill(); //填充文字
ctx.font = "bold 20pt Microsoft YaHei";
ctx.fillStyle = '#333';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.moveTo(200, 200);
ctx.fillText(pro + '%', 200, 200);
}
animate();
}())
</script>
</body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,990
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,132
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,843