zoukankan      html  css  js  c++  java
  • canvas绘制百分比圆环进度条

      开发项目,PM会跟踪项目进度;完成某个事情,也可以设置一个完成的进度。

      这里用canvas绘制一个简单百分比圆环进度条。

      看下效果:

      1. 动画方式

     

      2. 静默方式

     

      

       贴上代码,仅供参考 

    /**
     * LBS drawRing 
     * Date: 2015-04-24
     * ==================================
     * opts.parent 插入到哪里 一个JS元素对象
     * opts.width 宽度 = 2* (半径+弧宽)  
     * opts.radius 半径
     * opts.arc 弧宽
     * opts.perent 百分比 
     * opts.color 弧渲染颜色 [底色,进度色]
     * opts.textColor 文字渲染颜色
     * opts.textSize 文字渲染大小
     * opts.animated 是否以动画的方式绘制 默认false
     * opts.after 绘制完成时执行函数
     * ==================================
     **/
    
    function drawRing(opts) {
        var _opts = {
            parent: document.body,
             100,
            radius: 45,
            arc: 5,
            perent: 100,
            color: ['#ccc', '#042b61'],
            textColor: '#000',
            textSize: '14px',
            animated: false,
            after: function() {}
        }, k;
        for (k in opts) _opts[k] = opts[k];
        
        var parent = _opts.parent,
            width = _opts.width,
            radius = _opts.radius,
            arc = _opts.arc,
            perent = parseFloat(_opts.perent),
            color = _opts.color,
            textSize = _opts.textSize,
            textColor = _opts.textColor,
            c = document.createElement('canvas'),
            ctx = null,
            x = 0,
            animated = _opts.animated,
            after = _opts.after;
    
        parent.appendChild(c);
        ctx = c.getContext("2d");
        ctx.canvas.width = width;
        ctx.canvas.height = width;
    
        function clearFill() {
            ctx.clearRect(0, 0, width, width);
        }
    
        function fillBG() {
            ctx.beginPath();
            ctx.lineWidth = arc;
            ctx.strokeStyle = color[0];
            ctx.arc(width / 2, width / 2, radius, 0, 2 * Math.PI);
            ctx.stroke();
        }
    
        function fillArc(x) {
            ctx.beginPath();
            ctx.lineWidth = arc;
            ctx.strokeStyle = color[1];
            ctx.arc(width / 2, width / 2, radius, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180);
            ctx.stroke();
        }
    
        function fillText(x) {
            ctx.font = textSize + ' Arial';
            ctx.fillStyle = textColor;
            ctx.textBaseline = "middle";
            ctx.textAlign = 'center';
            ctx.fillText(x.toFixed(1) + '%', width / 2, width / 2);
        }
    
        function fill(x) {
            fillBG();
            fillArc(x);
            fillText(x);
        }
    
        if (!animated) return fill(perent);
    
        fill(x);
        !function animate() {
            if (++x > perent) return after && after();
            setTimeout(animate, 10);
            clearFill();
            fill(x);
        }();
    }
    View Code

       很简单的一段代码

      先创建一个canvas画布对象,设置宽高。 

    var c = document.createElement('canvas');
    document.body.appendChild(c);
    var ctx = c.getContext("2d");
    ctx.canvas.width = width;
    ctx.canvas.height = width;

      圆环由两部分组成,底部灰色完整的环,根据百分比变动的环。

      先绘制底部完整的环。

    //起始一条路径
    ctx.beginPath();
    //设置当前线条的宽度
    ctx.lineWidth = 10; //10px
    //设置笔触的颜色
    ctx.strokeStyle = '#ccc';
    //arc() 方法创建弧/曲线(用于创建圆或部分圆) 
    ctx.arc(100, 100, 90, 0, 2 * Math.PI);
    //绘制已定义的路径
    ctx.stroke();

      重点理解:canvas arc() 方法 :HTML5 canvas arc() 方法

      arc方法默认是从3点钟方向(90度)开始绘制的,一般要把这个开始处设置平常习惯的0点方向。

      也需要理解弧度和角度的互相转换。

    degrees = radians * 180/Math.PI 
    角度等于弧度乘于180再除于PI
    
    radians = degrees * Math.PI/180
    弧度等于角度度乘于PI再除于180

       绘制根据百分比变动的环。

    ctx.beginPath();
    ctx.lineWidth = 10;
    ctx.strokeStyle = '#f30';
    //设置开始处为0点钟方向(-90 * Math.PI / 180)
    //x为百分比值(0-100)
    ctx.arc(100, 100, 90, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180);  
    ctx.stroke();

      绘制中间的文字

    ctx.font = '40px Arial';
    ctx.fillStyle = '#f30';
    ctx.textBaseline = 'middle';
    ctx.textAlign = 'center';
    ctx.fillText('50.0%', 100, 100);

      到此一个静止的百分比圆环进度条就绘制完成了。

        不满足于绘制静态的,动态的更生动有趣一些。

      canvas动态绘制就是在一段时间内:绘制一个区域的内容,清除这个区域内容,再重新绘制内容,重复这个过程(绘制-清除-绘制)。

      有兴趣可以去研究一下,上面的代码也可以参考,如果结合动画函数和运动公式可以绘制更生动的百分比圆环进度条哦。

      --------------------------------------------

      参考: 

      HTML 5 Canvas 参考手册

  • 相关阅读:
    [HTML]安卓下<video>无法点击播放
    [JSP]解决Maven创建项目失败
    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis)
    [转]Express入门教程:一个简单的博客
    [转][译]关于CSS中的float和position和z-index
    [CSS]多浏览器兼容的垂直居中,兼容多个IE
    [转]非常实用的15款开源PHP类库
    [PHP]使用PHPMailer发送带附件并支持HTML内容的邮件
    ROS中阶笔记(一):机器人系统设计—ROS系统下连接外部传感器
    ROS入门笔记(十三):分布式通信
  • 原文地址:https://www.cnblogs.com/eyeear/p/5278092.html
Copyright © 2011-2022 走看看