Angular框架下通过canvas画布实现需求任务图中的动态进度条的绘制

下面展示的是需求任务中需要实现的一个动态的百分比进度条,接着我尝试使用canvas进行简单的绘制了一下。


图1.png
1、模板文件:

首先定义一个模板文件,#canvas给画布做上标识,宽高设置也是由自己动态的决定。

<canvas #canvas width="400" height="120"></canvas>
2、开始绘制:

首先通过标识获取canvas画布DOM元素

  @ViewChild("canvas", { static: true }) canvas: any; //获取DOM元素

封装一个方法drawGraphics,参数传入的是接口返回的百分占比,已经进行前期的准备工作处理,在绘制canvas时,不能将值写的太死,那样失去了灵活性,可以通过定义圆心位置(radius_x, radius_y)以及圆心半径radius来灵活绘制。

    var c = this.canvas.nativeElement;  // 获取到的canvas标识的元素
    const width = c.width, height = c.height; // canvas的宽高
    const radius = 40, radius_x = 50, radius_y = height / 2; //外环圆的半径以及坐标
    var ctx=c.getContext("2d");

由于是动态的展示,因此需要在接口传一次过来后,前端画布需要清除一次再进行重新绘制。

  // 每次重绘前清除一次画布
  this.clearDraw(ctx, width, height);

  // 清除canvas画布
  clearDraw(context, width, height) {
    context.clearRect(0, 0, width, height);
  }

开始使用canvas绘制外圆环以及内圆环,虚线绘制圆环等

    // 外层大圆
    ctx.beginPath();
    ctx.arc(radius_x,radius_y,radius,0.25*Math.PI,1.75*Math.PI); // x,y,r,起始角度,终点角度
    ctx.strokeStyle= '#488E87';
    ctx.stroke();
    // 内层小圆
    ctx.beginPath();
    ctx.arc(radius_x,radius_y,radius - 7,0*Math.PI,2*Math.PI);
    ctx.strokeStyle = '#70D4CA';
    ctx.stroke();
    // 虚线圆环
    this.drawDashRound(ctx, radius_x, radius_y, radius - 10);
    // 最内层小圆
    ctx.beginPath();
    ctx.arc(radius_x,radius_y,radius - 14,0*Math.PI,2*Math.PI);
    ctx.strokeStyle = '#488E87';
    ctx.font = "normal 16px '字体','字体','微软雅黑','宋体'"; //设置字体
    ctx.textBaseline = 'middle';
    ctx.fillText(percent.toFixed(1)*100+'%', radius_x / 2 + 5, radius_y);
    ctx.stroke();

虚线圆环在canvas中没有明确的api使用,因此需要手动的去计算,通过绘制弧长的方式绘制虚线圆

  // 绘制虚线圆环
  drawDashRound(context, x, y, radius, step = 5) {
    const count = Math.floor(360 / step);
    step = step / 180 * Math.PI * 2;
    for (let start = 0, e = step / 2; e <= 360; start += step, e += step) {
      context.beginPath()
      context.arc(x, y, radius, start, e);
      context.stroke();
    }
  }

上下的直线以及折现可以直接使用api中的moveTo以及lineTo去绘制。

    // 最上层的直线条
    ctx.beginPath();
    ctx.moveTo(radius_x + this.sqrt((radius*radius/2)),(height / 2) - this.sqrt((radius*radius/2)));
    ctx.lineTo(radius_x + this.sqrt((radius*radius/2)) + 200,(height / 2) - this.sqrt((radius*radius/2)));
    ctx.strokeStyle = '#488E87';
    ctx.lineWidth = 1;
    ctx.font = "normal 18px '字体','字体','微软雅黑','宋体'"; //设置字体
    ctx.textBaseline = 'bottom';
    ctx.fillText('盘库总进度', radius_x + this.sqrt((radius*radius/2)) + 100 ,(height / 2) - this.sqrt((radius*radius/2)));
    ctx.closePath();
    ctx.stroke();

    // 末端实心圆
    ctx.beginPath();
    ctx.arc(radius_x + this.sqrt((radius*radius/2)) + 200, (height / 2) - this.sqrt((radius*radius/2)), 3, 0*Math.PI,2*Math.PI);
    ctx.fillStyle = '#70D4CA';
    ctx.fill();
    ctx.stroke();

    // 下层折线
    ctx.beginPath();
    ctx.moveTo(radius_x + this.sqrt((radius*radius/2)), (height / 2) + this.sqrt((radius*radius/2)));
    ctx.lineTo(radius_x + this.sqrt((radius*radius/2)) + 50, (height / 2) + this.sqrt((radius*radius/2)));
    ctx.strokeStyle = '#488E87';
    ctx.lineWidth = 1;
    ctx.closePath();
    ctx.stroke();

    ctx.beginPath();
    ctx.moveTo(radius_x + this.sqrt((radius*radius/2)) + 50, (height / 2) + this.sqrt((radius*radius/2)));
    ctx.lineTo(radius_x + this.sqrt((radius*radius/2)) + 60, (height / 2) + this.sqrt((radius*radius/2)) - 10);
    ctx.strokeStyle = '#488E87';
    ctx.lineWidth = 1;
    ctx.closePath();
    ctx.stroke();

    ctx.beginPath();
    ctx.moveTo(radius_x + this.sqrt((radius*radius/2)) + 60, (height / 2) + this.sqrt((radius*radius/2)) - 10);
    ctx.lineTo(width, (height / 2) + this.sqrt((radius*radius/2)) - 10);
    ctx.strokeStyle = '#488E87';
    ctx.lineWidth = 1;
    ctx.closePath();
    ctx.stroke();

    ctx.beginPath();
    ctx.moveTo(width, (height / 2) + this.sqrt((radius*radius/2)) - 10);
    ctx.lineTo(width, (height / 2) + this.sqrt((radius*radius/2)) - 30);
    ctx.strokeStyle = '#488E87';
    ctx.lineWidth = 1;
    ctx.closePath();
    ctx.stroke();

    ctx.beginPath();
    ctx.moveTo(width, (height / 2) + this.sqrt((radius*radius/2)) - 30);
    ctx.lineTo(width - 15, (height / 2) + this.sqrt((radius*radius/2)) - 40);
    ctx.strokeStyle = '#488E87';
    ctx.lineWidth = 1;
    ctx.closePath();
    ctx.stroke();

    // 末端实心圆
    ctx.beginPath();
    ctx.arc(width - 15, (height / 2) + this.sqrt((radius*radius/2)) - 40, 3, 0*Math.PI, 2*Math.PI);
    ctx.fillStyle = '#70D4CA';
    ctx.fill();
    ctx.stroke();

在绘制折线以及直线的过程中,只需要理清楚不同的端点坐标进行计算即可。同时需要注意的是,这里需要使用平方根去计算一些数据。


图2.png
  // 二分法求平方根
  sqrt(num) {
    function sqrtWrapper(min, max) {
        let current = (min + max) / 2;
        let _min = min, _max = max;
        if (current * current > num) {
          _max = current;
        } else {
          _min = current;
        }
        if (min === _min && max === _max) {
            return current
        } else if (_max - _min < (1 / new Array(17).fill(10).reduce((a, b) => a * b, 1))) {
            return current;
        } else {
            return sqrtWrapper(_min, _max);
        }
    }
    return sqrtWrapper(0, num);
  }

接着,剩下中间的进度条实心块,这个的话就需要细心去计算了。
首先形参我通过传入完成的百分占比,canvas画布,进度条的起始x坐标,以及内部第一个小块左下角的x,y坐标。通过分成20个小块的进度条,进行动态绘制。

  // 中间的进度条块
  this.drawProcess(percent, ctx, radius_x , width - 50, radius_x - this.sqrt((radius*radius/2)) + 70, radius_y + 6);

  // 绘制进度块,根据传入的百分比进行绘制
  drawProcess(number, context, start, end, x, y) {
    const len = end - start, // 总长度
          count = len / 15; 
    // 开始绘制已完成的进度块
    const effective = Math.floor(count * number);
    // 根据长度绘制所有的进度块
    for (let i = 0; i < count ; i++) {
      context.beginPath();
      context.lineTo(x, y); 
      context.lineTo(x+5,y-15);
      context.lineTo(x+12,y-15);
      context.lineTo(x+7,y);
      context.closePath(); // 闭合图形
      if (i < effective) {
        context.fillStyle = '#2BFBE6';
      }else {
        context.fillStyle = '#196A88';
      }
      context.fill();
      context.lineWidth = 1;
      context.stroke();
      x += 13;
    }
  }

3、实现效果:
图3.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容