Canvas实现小球自由下落的效果

效果图

requestAnimationFrame

利用这个API可以让动画在渲染时不那么卡顿,比setTimeOut以及setTimeInterval要好上不少。这里提供一个实现动画方法:参考链接

    animate (time /* s */, callback, easing = t => t) {
      let start = performance.now()

      const loop = () => {
        let passed = performance.now() - start // ms
        if (passed - time * 1000 >= 0) {
          cancelAnimationFrame(raf)
        } else {
          callback(easing(passed / 1000)) // 返回时间
          raf = requestAnimationFrame(loop)
        }
      }
      let raf = requestAnimationFrame(loop)
    }

canvas中实现自由下落

  • 画出一个小圆,利用arc函数
drawCircle (x, y, radius) {
      this.context.clearRect(0, 0, this.width, this.height)
      this.context.beginPath()
      this.context.arc(x, y, radius, 0, 2 * Math.PI, false)
      this.context.stroke()
      this.context.closePath()
    }

为了实现动画效果,每一次画圆都需要清除所有区域(视情况而定)

  • 利用自由落体公式
 // 实现小圆自由落体运动
    fall (height, a = 9.8) {
      let time = Math.sqrt(2 * height / a)
      this.animate(time, y => {
        this.drawCircle(this.x, this.radius + y, this.radius)
      }, passedTime => 1 / 2 * a * Math.pow(passedTime, 2))
    }

如果一个像素对应现实世界的一米的话,这个加速度取9.8会让动画变得比较慢,为了更直观我们在使用的时候将加速度调成98

  mounted () {
    this.init('#container')
    let height = this.height - this.radius * 2 // 小球从最高处落到地面的高度
    this.fall(height, 98) // 这里把加速度提升10倍获取直观的加速效果
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值