温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

html5中怎么用Canvas实现环形进度条

发布时间:2022-02-21 10:13:54 来源:亿速云 阅读:241 作者:iii 栏目:开发技术

本篇内容介绍了“html5中怎么用Canvas实现环形进度条”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、定义变量

定义半径,定义圆环厚度,定义圆心位置、定义默认填充颜色:

let radius = 75 let thickness= 10 let innerRadius = radius - thickness let x = 75 let y = 75 var canvas = document.getElementById('tutorial'); var ctx = canvas.getContext('2d'); ctx.fillStyle = "#f2d7d7";

二、画第一个圆弧

ctx.beginPath(); ctx.arc(x, y, radius, Math.PI * 1.5, Math.PI)

html5中怎么用Canvas实现环形进度条

注意 beginPath() 这个方法,生成路径的第一步。本质上,路径是由很多子路径构成,这些子路径都是在一个列表中,所有的子路径(线、弧形、等等)构成图形。而每次这个方法调用之后,列表清空重置,然后我们就可以重新绘制新的图形。
 

也就是说,这个方法可以用来给 Canvas图像 分组,绘制新的图形如果不调用此方法,那么新的图形会和前面的图形连接在一起

三、画第一个连接处

ctx.quadraticCurveTo((x - innerRadius) - thickness / 2, y - thickness, x - innerRadius, y)

html5中怎么用Canvas实现环形进度条

连接外是用二次贝塞尔曲线来画的,Canvas的 quadraticCurveTo(cp1x, cp1y, x, y) 方法接受4个参数,第一、二个参数为控制点,第三、四个参数为结束点官方文档
只需算出控制点和结束点,就可以画出一个圆弧

四、画第二个圆弧

ctx.arc(x, y, innerRadius, Math.PI, Math.PI * 1.5, true)

html5中怎么用Canvas实现环形进度条

注意方法后面最后一个参数,设置为true,代表逆时针绘制(默认是顺时针)

五、画第二个连接处

ctx.quadraticCurveTo(y - thickness, (x - innerRadius) - thickness / 2, x, y - innerRadius - thickness)

html5中怎么用Canvas实现环形进度条

这一步其实和第三步相差不大,简单的调换了下参数位置

六、填充

 ctx.fill();

html5中怎么用Canvas实现环形进度条

至此,一个简单的未闭合的圆环就完成了

画第二个进度条圆环

七、初始化

ctx.beginPath(); ctx.fillStyle = "#e87c7c";

beginPath 表示绘制新的图形,如果不调用此方法,那后面画的图形会和前面画的图形连在一起

八、绘制第二个进度条圆环

ctx.beginPath(); ctx.fillStyle = "#e87c7c"; ctx.arc(x, y, radius, Math.PI * 1.5, Math.PI * 2) ctx.quadraticCurveTo((x + innerRadius) + thickness / 2, y + thickness, x + innerRadius, y) ctx.arc(x, y, innerRadius, Math.PI * 2, Math.PI * 1.5, true) ctx.quadraticCurveTo(y - thickness, (x - innerRadius) - thickness / 2, x, y - innerRadius - thickness) ctx.fill();

html5中怎么用Canvas实现环形进度条

由于和第一个圆环绘制方式一模一样,就不在重复了,区别仅仅是圆的弧度

九、旋转 Canvas

transform: rotate(-135deg);

html5中怎么用Canvas实现环形进度条

由于css的旋转比较方便,也省去了角度的计算,所以本人使用的是css的transform来旋转的。当然 Canvas 也提供了旋转的方法

完整代码
<!DOCTYPE html> <html lang="cn"> <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>canvas</title>     <style>         .ring {             width: 150px;             height: 150px;             display: flex;             align-items: center;             justify-content: center;             flex-direction: column;             position: relative;         }         #tutorial {             transform: rotate(-135deg);             width: 150px;              height: 150px;         }         .fraction {             position: absolute;             font-size: 30px;             font-weight: bold;             color: red;         }         .small {             font-size: 12px;             font-weight: lighter;         }         .title {             color: red;             bottom: 0;             position: absolute;         }     </style> </head> <body>     <div class="ring">         <canvas id="tutorial" width="150" height="150"></canvas>         <span class="fraction">100 <span class="small">分</span> </span>         <span class="title">服务分</span>     </div>     <script>         let radius = 75         let thickness = 10         let innerRadius = radius - thickness         let x = 75         let y = 75         var canvas = document.getElementById('tutorial');         var ctx = canvas.getContext('2d');         ctx.fillStyle = "#f2d7d7";         ctx.beginPath();         ctx.arc(x, y, radius, Math.PI * 1.5, Math.PI)         ctx.quadraticCurveTo((x - innerRadius) - thickness/2 , y - thickness, x - innerRadius, y)         ctx.arc(x, y, innerRadius, Math.PI, Math.PI * 1.5, true)         ctx.quadraticCurveTo(y - thickness, (x - innerRadius) - thickness / 2, x, y - innerRadius - thickness)         ctx.fill();         ctx.beginPath();         ctx.fillStyle = "#e87c7c";         ctx.arc(x, y, radius, Math.PI * 1.5, Math.PI * 2)         ctx.quadraticCurveTo((x + innerRadius) + thickness / 2, y + thickness, x + innerRadius, y)         ctx.arc(x, y, innerRadius, Math.PI * 2, Math.PI * 1.5, true)         ctx.quadraticCurveTo(y - thickness, (x - innerRadius) - thickness / 2, x, y - innerRadius - thickness)         ctx.fill();     </script> </body> </html>

“html5中怎么用Canvas实现环形进度条”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI