用canvas画的一个加载条

一个同学问的问题, 同样类型的问题不知道被问了多少变了, 这里写了一个例子,需要使用的同学可以自行复制粘贴,但是相关尺寸及内容请自行更改

<template>
  <div class="about">
    总课程数量<input type="text" v-model="m">
    已完成数量<input type="text" v-model="n">
    <button @click="finish">确认完成</button>
    <canvas width="400px" height="250px" id="canvas"></canvas>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ctx: '',
      m: 100,
      n: 0
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.draw(this.n, this.m)
    })
    
  },

  methods: {
    draw(n, m){
      var c = document.getElementById('canvas');
      var ctx = c.getContext("2d");
      // 每次开始画之前先清空之前的内容
      ctx.clearRect(0,0,c.width,c.height); 
      // 绘制底部的灰色园
      ctx.beginPath();
      // 灰色圆的宽度
      ctx.lineWidth = 10;
      // 灰色圆的颜色
      ctx.strokeStyle = "#ccc"
      // arc 方法 第一个参数是圆心的x轴坐标 第二个参数是圆心Y轴坐标 
      // 第三个参数是园的半径 第四个参数是 画圆时的起始位置 第五个参数是结束为止
      // 第六个参数表示方向 false表示顺时针方向画
      ctx.arc(100, 80, 60, 0, 2*Math.PI, false);
      ctx.stroke();
     
      // 绘制灰色园上的红色圆
      ctx.beginPath()
      // 红色圆的线条宽度
      ctx.lineWidth = 10;
      // 红色圆的线条颜色
      ctx.strokeStyle = 'red';
      // 设置红色环显示的角度范围为 120度到420度 共300度 用100% 表示
      // 用已经完成的课程数/总课程数乘以100% 表示课程完成的百分比
      // 我们这里用 n表示完成的课程数量 用 m表示所有的课程数量
      // 则完成n个课程表示的百分比是 
      // n/m*300 表示完成 n个课程要描绘的度数
      // Math.pI 表示 180度
      ctx.arc(100, 80, 60, 2*Math.PI/3, 2*Math.PI/3 + (Math.PI + 2*Math.PI/3) * (n/m), false)
      // 设置两端为圆角
      ctx.lineCap="round";
      ctx.stroke()

      // 插入文本
      ctx.font = "18px sans-serif"
      // 填充红色字体
      ctx.fillStyle = 'red'
      // fillText 第一个参数为填充的内容 第二个参数是填充内容的x轴位置 第三个参数是填充的内容的Y轴位置
      ctx.fillText("继续学习", 65, 70);
      // 文字间的分割线
      ctx.beginPath();
      // moveTo意思是画线的初始位置 第一个参数为x轴位置 第二个参数为Y轴位置
      ctx.moveTo(62, 80);
      // lineTo是连线到下一个位置 第一个参数是x轴的位置 第二个参数是y轴的位置
      ctx.lineTo(140, 80);
      ctx.lineWidth = 1;
      ctx.strokeStyle = "#ccc"
      ctx.stroke();

      // 第三行的文本
      ctx.font = "12px sans-serif"
      ctx.fillStyle = '#ccc'
      ctx.fillText(`已学习${this.n}/${this.m}课时`, 50, 100);
    },

    finish() {
      this.draw(this.n, this.m)
    }
  }
}
</script>
<style scoped>
  #canvas {
      background-color: #fff;
      margin: 0 auto;
  }
</style>

以下是用canvas一个流星的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Canvas 流星</title> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var x = 200; // 流星起点x坐标 var y = 0; // 流星起点y坐标 var speed = 3; // 流星速度 var length = 50; // 流星长度 var angle = Math.PI/4; // 流星角度 var tailLength = 20; // 流星尾巴长度 function drawMeteor() { ctx.clearRect(0, 0, canvas.width, canvas.height); // 清除布 ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + Math.cos(angle) * length, y + Math.sin(angle) * length); ctx.strokeStyle = "#fff"; ctx.lineWidth = 3; ctx.stroke(); ctx.moveTo(x + Math.cos(angle) * tailLength, y + Math.sin(angle) * tailLength); ctx.lineTo(x + Math.cos(angle) * length, y + Math.sin(angle) * length); ctx.strokeStyle = "#ff0"; ctx.lineWidth = 1; ctx.stroke(); x += Math.cos(angle) * speed; y += Math.sin(angle) * speed; if (y > canvas.height) { // 流星超出布,重新生成流星 x = Math.random() * canvas.width; y = 0; length = Math.random() * 50 + 20; angle = Math.PI/4 + Math.random() * Math.PI/4; tailLength = length / 4; } requestAnimationFrame(drawMeteor); // 不断重绘 } drawMeteor(); </script> </body> </html> ``` 解释一下代码: 首先,创建一个canvas元素和一个绘图上下文对象ctx。 然后,定义一些变量,包括流星起点坐标、速度、长度、角度和尾巴长度。 接下来,定义一个绘制流星的函数drawMeteor。在该函数中,先清除布,然后绘制流星的主体和尾巴,分别使用不同的颜色和线宽。接着,更新流星的坐标,并判断是否超出布,如果超出,则重新生成一个流星。最后,使用requestAnimationFrame函数不断重绘流星。 最后,在页面加载完成后,调用drawMeteor函数绘制流星。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值