Angular-canvas实现圆角圆环

一、示例图

 

 

 

 

 

 

 

二、实现步骤

  html:  

1 <canvas #canvasDom width="{{width}}" height="{{height}}" #canvas>
2     您的浏览器不支持画布
3 </canvas>

  1.设置背景圆环

    

 1 // 背景圆环
 2   drawArc() {
 3     // 设置宽高
 4     this.context = this.canvasDom.nativeElement.getContext('2d');
 5     const context = this.context;
 6     this.canvasDom.nativeElement.width = this.width;
 7     this.canvasDom.nativeElement.height = this.height;
 8     // 外层圆环
 9     context.beginPath();
10     context.strokeStyle = '#ebebeb';  // 颜色
11     context.arc(100, 100, 80, (Math.PI / 180) * 270, (Math.PI / 180) * - 90, false);
12     context.lineWidth = 20;
13     context.closePath();
14     context.stroke();
15    // 设置着色圆环
16     this.frameArc();
17   }

 

  2.设置内层圆环-运动效果

 1 // 动态圆环
 2   frameArc() {
 3     const speed = 20; // 速度
 4     let stepDeg = 0;  // 初始值
 5 
 6     let timer = setInterval(() => {
 7       stepDeg += 1; // 以1为间距
 8       // 若当前数值已大于传入数值,则清除定时器
 9       if (stepDeg >= this.rate) {
10         clearInterval(timer);
11       }
12       // 内层圆环
13       const context = this.context;
14       context.beginPath();
15       // 设置渐变色
16       const gradient = context.createLinearGradient(0, 20, 0, 120);
17       gradient.addColorStop('0', '#ffaa03');
18       gradient.addColorStop('1.0', '#ff4514');
19       context.strokeStyle = gradient;
20       context.lineCap = 'round';  // 使其形状为圆弧
21       context.arc(100, 100, 80, (Math.PI / 180) * 270, (Math.PI / 180) * (270 + (stepDeg / 100 * 360)), false);
22       context.lineWidth = 20; // 圆环的宽度
23       context.stroke();
24       context.closePath();
25     }, speed);
26   }

  3.设置文字

1 // 文字
2   drawText() {
3     const cans = this.canvasDom.nativeElement.getContext('2d');
4     cans.font = 'bold 44px Arial';  // 字体
5     cans.fillStyle = '#676767'; // 字体颜色
6     cans.textAlign = 'center';  // 使文字居中
7     cans.fillText(this.rate + '%', this.width / 2, this.height / 2 + 15);
8   }

 

转载于:https://www.cnblogs.com/yaq-lh/p/9637272.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值