canvas环形进度条

近日倒腾了canvas,整了个环形进度条了 ,不得不说canvas是真的无所不能,极其强大。那么废话不多说,先上效果图。
在这里插入图片描述
canvas 拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。canvas 元素本身是没有绘图能力的,可以看成一个画图的容器。所有的绘制工作必须在 JavaScript 内部完成:getContext(“2d”) 对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。

html代码

<template>
  <div>
    <canvas id="myCanvas" :width="width" :height="height"></canvas>
  </div>
</template>

绘制底色圆环

 mounted() {
    let percent = "70";
    this.drawCircle(percent);
  },
  methods: {
    drawCircle(percent) {
      this.canvas = document.getElementById("myCanvas");
      this.context = this.canvas.getContext("2d");
      this.context.beginPath();
      this.context.arc(100, 100, 50, 0, 2 * Math.PI, false);
      this.context.lineWidth = 10;
      this.context.strokeStyle = "rgba(208, 232, 254, 1)";
      this.context.stroke();
      this.writeCircle(percent);
    },

绘制有色圆环

  writeCircle(percent) {
      var startAngle = (3 / 2) * Math.PI; //开始位置弧度 数学里的12点方向
      var diffAngle = (percent / 100) * Math.PI * 2; //完成进度弧度值
      this.context.beginPath();
      this.context.arc(100, 100, 50, startAngle, diffAngle + startAngle, false);
      this.context.strokeStyle = "rgba(68, 163, 252, 1";
      this.context.lineCap = "round";
      this.context.stroke();
    }

这样就得到简单的环形进度条啦~

完整代码也附上,数据是假数据,替换一下即可成为动态的环形进度条哦。

<template>
  <div>
    <canvas id="myCanvas" :width="width" :height="height"></canvas>
  </div>
</template>

<script>
export default {
  props: {
    width: {
      type: Number,
      default: 200
    },
    height: {
      type: Number,
      default: 200
    }
  },
  data() {
    return {
      context: "",
      canvas: "",
      R:100
    };
  },
  mounted() {
    let percent = "70";
    this.drawCircle(percent);
  },
  methods: {
    drawCircle(percent) {
      this.canvas = document.getElementById("myCanvas");
      this.context = this.canvas.getContext("2d");
      this.context.beginPath();
      this.context.arc(100, 100, 50, 0, 2 * Math.PI, false);
      this.context.lineWidth = 10;
      this.context.strokeStyle = "rgba(208, 232, 254, 1)";
      this.context.stroke();
      this.writeCircle(percent);
    },

    writeCircle(percent) {
      var startAngle = (3 / 2) * Math.PI; //开始位置弧度 数学里的12点方向
      var diffAngle = (percent / 100) * Math.PI * 2; //完成进度弧度值
      this.context.beginPath();
      this.context.arc(100, 100, 50, startAngle, diffAngle + startAngle, false);
      this.context.strokeStyle = "rgba(68, 163, 252, 1";
      this.context.lineCap = "round";
      this.context.stroke();
    }
  },
};
</script>

<style>
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值