• 利用canvas实现环形进度条


    前提:有时候在项目中会有用到进度条的情况,使用css3也可以实现,但是对于性能不好的设备,或者网络不好的情况下,卡顿现象非常明显,避免出现不流畅的尴尬情况,所以记录一下,使用canvas来实现的方法。

    效果图:  

    DOM中,首先定义canvas画板元素:  

    <canvas id="canvas" width="500" height="500" style="background:#F7F7F7;">
        <p>you browser not support canvas!</p>
      </canvas>

    对于不支持canvas的浏览器则会显示:you browser not support canvas!

    广州VI设计公司https://www.houdianzi.com

    接下来是js编写:
    定义canvas.js并在页面引入

    var canvas = document.getElementById('canvas'), //获取canvas元素
      context = canvas.getContext('2d'), //获取画图环境,指明为2d
      centerX = canvas.width / 2, //Canvas中心点x轴坐标
      centerY = canvas.height / 2, //Canvas中心点y轴坐标
      rad = Math.PI * 2 / 100, //将360度分成100份,那么每一份就是rad度
      speed = 0.1; //加载的快慢就靠它了
    
    //绘制蓝色外圈
    function blueCircle(n) {
      context.save();
      context.beginPath();
      context.strokeStyle = "#49f";
      context.lineWidth = 12;
      context.arc(centerX, centerY, 100, -Math.PI / 2, -Math.PI / 2 + n * rad, false);
      context.stroke();
      context.restore();
    }
    
    //绘制白色外圈
    function whiteCircle() {
      context.save();
      context.beginPath();
      context.strokeStyle = "#A5DEF1";
      context.lineWidth = 12;
      context.arc(centerX, centerY, 100, 0, Math.PI * 2, false);
      context.stroke();
      context.closePath();
      context.restore();
    }
    
    //百分比文字绘制
    function text(n) {
      context.save();
      context.fillStyle = "#F47C7C";
      context.font = "40px Arial";
      context.textAlign = "center";
      context.textBaseline = "middle";
      context.fillText(n.toFixed(0) + "%", centerX, centerY);
      context.restore();
    }
    
    //动画循环
    (function drawFrame() {
      window.requestAnimationFrame(drawFrame, canvas);
      context.clearRect(0, 0, canvas.width, canvas.height);
    
      whiteCircle();
      text(speed);
      blueCircle(speed);
    
      if (speed > 100) speed = 0;
      speed += 0.1;
    }());
    window.requestAnimationFrame(drawFrame, canvas);

    每行代码的注释标注非常清楚,如果还有不理解的可以去看canvas基础,应该就可以了。

  • 相关阅读:
    Atcoder D
    51nod 1201 整数划分(dp)
    Atcoder D
    Atcoder C
    codeforces 812 E. Sagheer and Apple Tree(树+尼姆博弈)
    codeforces 811 D. Vladik and Favorite Game(bfs水题)
    codeforces 811 E. Vladik and Entertaining Flags(线段树+并查集)
    codeforces 811 C. Vladik and Memorable Trip(dp)
    1449 砝码称重(思维)
    SQL大量数据查询的优化 及 非用like不可时的处理方案
  • 原文地址:https://www.cnblogs.com/qianxiaox/p/14041703.html
Copyright © 2020-2023  润新知