vue 中使用canvas画简单的线条动画效果

vue 中使用canvas画简单的线条动画效果;
问题:1.没有写X轴或者Y轴不变的逻辑判断;2.不会画弧度,不知道弧线怎么去单个像素的画。

method中定义方法:

getCanvas(start,end){
        return new Promise(resolve=>{
        let movingDistance_x = end.x - start.x; //x轴需要移动的距离
        let movingDistance_y = end.y - start.y; //y轴需要移动的距离
        let ratio = movingDistance_y / movingDistance_x; // 斜率
        // 画线的方法
        let draw_lineTo = (start_draw,end_draw)=>{
          this.ctx.beginPath()
          this.ctx.lineCap="round";
          this.ctx.lineJoin="round";
          this.ctx.miterLimit=1;
          this.ctx.moveTo(start_draw.x,start_draw.y)
          this.ctx.lineTo(end_draw.x,end_draw.y)
          this.ctx.lineWidth = 4
          this.ctx.strokeStyle = 'rgb(32,83,131)'
          this.ctx.stroke()
          if(Math.abs(end_draw.x - end.x) == 0 ){
            resolve()
            return
          }
          start_position = end_draw
          end_position = getEnd_position(start_position)
          console.log(Math.abs(end_draw.x - end.x),start_position,end_position)
          setTimeout(() => {
            draw_lineTo(start_position,end_position)
          }, 10);
        }
        // 获取开始坐标
        let start_position = { x : start.x, y : start.y}
        // 获取结束坐标的方法
        let getEnd_position = (start_position)=>{
          let x = end.x > start.x ? (start_position.x + 1) : (start_position.x - 1)
          let y =
            end.x > start.x
              ? start_position.y + ratio
              : start_position.y - ratio;
          return { x : x , y : y }
        }
        // 获取结束坐标
        let end_position = getEnd_position(start_position)
        // 画线
        draw_lineTo(start_position,end_position)
        })
      }

mounted中调用

this.ctx = this.$refs.myCanvas.getContext('2d')
      this.$nextTick(()=>{
        (async () =>{
          await this.getCanvas({x:706,y:402},{x:660,y:375})
          await this.getCanvas({x:598,y:374},{x:534,y:410})
          await this.getCanvas({x:534,y:410},{x:468,y:374})
          // 分叉
          this.getCanvas({x:468,y:374},{x:554,y:321}) 
            .then(()=>{
              this.getCanvas({x:554,y:321},{x:518,y:296})
            })
          await this.getCanvas({x:468,y:374},{x:241,y:503})
          await this.getCanvas({x:241,y:503},{x:23,y:377})
          this.getCanvas({x:23,y:377},{x:62,y:352})
        })()
      })
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值