使用canvas实现环形进度条

news/2024/7/17 4:10:47

html代码:

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title></title>
 6     </head>
 7     <body>
 8         <canvas id="pro" width="400" height="300"></canvas>
 9     </body>
10 </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();

效果图如下:

第二步:

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);//执行这个函数

效果图如下:

第三步:让它动起来

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);

效果如下:

完整代码如下:

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6     </head>
 7     <body>
 8         <canvas id="pro" width="400" height="400"></canvas>
 9         <script>
10             (function(){
11                 var c=document.getElementById("pro"),
12                     pro=0,
13                     ctx=c.getContext("2d");
14                     
15                     //画灰色的圆
16                     ctx.beginPath();
17                     ctx.arc(200,200,80,0,Math.PI*2);
18                     ctx.closePath();
19                     ctx.fillStyle='#f6f6f6';
20                     ctx.fill();
21                     
22                     function animate(){
23                         requestAnimationFrame(function(){
24                             pro=pro+1;
25                             drawCricle(ctx,pro);
26                             if(pro<60){
27                                 animate();
28                             }
29                         });
30                     }
31                     
32                     function drawCricle(ctx,percent){
33                         //画进度环
34                         ctx.beginPath();
35                         ctx.moveTo(200,200);
36                         ctx.arc(200,200,80,Math.PI*0.8,Math.PI*(0.8+2*percent/200));
37                         ctx.closePath();
38                         ctx.fillStyle='#ff9600';
39                         ctx.fill();
40                         
41                         //画内填充圆
42                         ctx.beginPath();
43                         ctx.arc(200,200,75,0,Math.PI*2);
44                         ctx.closePath();
45                         ctx.fillStyle='#fff';
46                         ctx.fill();
47                         
48                           //填充文字  
49                         ctx.font = "bold 20pt Microsoft YaHei";   
50                         ctx.fillStyle = '#333';  
51                         ctx.textAlign = 'center';    
52                         ctx.textBaseline = 'middle';    
53                         ctx.moveTo(200, 200);    
54                         ctx.fillText(pro + '%', 200, 200);  
55                     }
56                     animate();
57             }())
58         </script>
59     </body>
60 </html>

 

转载于:https://www.cnblogs.com/Mrrabbit/p/7126838.html


http://www.niftyadmin.cn/n/1150516.html

相关文章

DoTween全解析

DoTween全解析 概述&#xff1a; DoTween&#xff0c;Itween&#xff0c;这些名字作为一个Unity开发人员听起来并不陌生&#xff0c;它们在动画方面表现出了令人折服的能力&#xff0c;今天我带着大家来一起认识一下这款插件。 首先我先给大家提前说明的一个概念就是&#xff0…

[Unity XLua]热更新XLua入门(一)-基础篇

Aladdin_XLua 前言 前段时间腾讯开源了一个内部热更框架XLua在Unity开发群里引起一阵热议&#xff0c;也受到广大开发者的热捧&#xff0c;然后我当然也抱着好奇的心去学习学习。后面也会将扩展之后的工程放在git上&#xff0c;大家一起学习交流&#xff01;在此感谢XLua作者创…

url 散列加密

1.hmvc.php 加密类 <?php /*** Created by PhpStorm.* User: Administrator* Date: 2017/7/6* Time: 9:14*/ class Crypt_HMVC {private $_func null;private $_ipad null;private $_opad null;function Crypt_HMVC($key, $method md5) {if(!in_array($method,array(sh…

iOS 基础:证书介绍

首先&#xff0c;打开developer.apple.com &#xff0c;在iOS Dev Center打开Certificates, Indentifiers & Profiles认识一下基本结构。列表就包含了开发、调试和发布iOS应用程序所需的所有内容&#xff1a;Certificates、Identifiers、Devices、Provisioning Profiles。 …

三层架构初识和搭建

一、是什么&#xff1f; 1.表现层&#xff08;UI&#xff09;&#xff1a;展现给用户的界面&#xff0c;用户在使用一个系统的时候他的所见所得。主要表示Web或WinForm方式&#xff0c;对用户的请求接受&#xff0c;以及数据的返回。为client提供应用程序的訪问。假设逻辑层相当…

Lua中的协程Coroutine

一、协程是什么&#xff1f;&#xff08;1&#xff09;线程首先复习一下多线程。我们都知道线程——Thread。每一个线程都代表一个执行序列。当我们在程序中创建多线程的时候&#xff0c;看起来&#xff0c;同一时刻多个线程是同时执行的&#xff0c;不过实质上多个线程是并发的…

js只能输入汉字

var reg new RegExp("[\\u4E00-\\u9FFF]","g");  if(reg.test(val)){ alert("不能输入汉字&#xff01;");

Unity3d的NavMesh信息导出与服务器端(KBEngine)的使用(一)

前言&#xff1a;这篇文档写了很久了&#xff0c;今天有群里朋友询问&#xff0c;干脆贴出来一起学习下吧。 一、在服务器中使用Recast Navigation进行导航的时候&#xff0c;可以使用Unity场景中的NavMesh信息&#xff0c;导出静态导航网格&#xff0c;提供给客户端或者服务器…