基于requestAnimationFrame的动画控制器

最近学习threejs了解到了一个window的api:requestAnimationFrame,于是封装了一个控制器类,用来控制和实现自定义的动画效果。

window.requestAnimationFrame() 告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。该方法需要传入一个回调函数作为参数,该回调函数会在浏览器下一次重绘之前执行

回调函数会被传入DOMHighResTimeStamp参数,DOMHighResTimeStamp指示当前被 requestAnimationFrame() 排序的回调函数被触发的时间。在同一个帧中的多个回调函数,它们每一个都会接受到一个相同的时间戳,即使在计算上一个回调函数的工作负载期间已经消耗了一些时间。该时间戳是一个十进制数,单位毫秒,最小精度为1ms(1000μs)。

语法

window.requestAnimationFrame(callback);

参数

callback

下一次重绘之前更新动画帧所调用的函数(即上面所说的回调函数)。该回调函数会被传入DOMHighResTimeStamp参数,该参数与performance.now()的返回值相同,它表示requestAnimationFrame() 开始去执行回调函数的时刻。

返回值

一个 long 整数,请求 ID ,是回调列表中唯一的标识。是个非零值,没别的意义。你可以传这个值给 window.cancelAnimationFrame() 以取消回调函数。

动画控制器代码如下:

/**
 * 动画控制器
 * @author Su Jiantao
 */
export class AnimControls {
  /**
   * 动画控制器
   * @param {Number} duration 总时长 default:1000 ms
   * @param {Function} render 每一帧的回调,主要动作
   * @param {Function} endCall 动画结束回调
   * @param {Function} easeFunc 缓动函数
   * @param {Number} speed 播放倍速 default:1
   */
  constructor(duration = 10000, render = (t, v) => { }, endCall = () => { }, easeFunc = t => t, speed = 1) {
    this.duration = duration
    this.easeFunc = easeFunc
    this.render = render
    this.endCall = endCall
    this.speed = speed
    this.v = 0; // 动画进度
    this.inPause = true
    this.atEnd = false
    this.reuqestId = null
  }

  play() {
    if (this.atEnd) {
      this.reset();
    }
    this.inPause = false

    let frameFunc = (time) => {

      if (this.inPause) {
        this.v = v;
        cancelAnimationFrame(this.requestId)
        return false;
      }
      let v = (this.v + (time - startTime) / this.duration) * this.speed;
      if (this.atEnd || v >= 1) {
        const t = this.easeFunc(1)
        this.render(t, 1);
        this.endCall();
        return true;
      } else {
        const t = this.easeFunc(v)
        this.render(t, v);
        this.reuqestId = requestAnimationFrame(frameFunc.bind(this));
      }
    }
    let startTime = performance.now();
    this.reuqestId = requestAnimationFrame(frameFunc.bind(this));
  }

  reset() {
    this.inPause = true
    this.atEnd = false
    this.v = 0
    this.render(0, 0)
  }

  pause() {
    this.inPause = true
  }

  toEnd() {
    this.atEnd = true
    if (this.inPause) {
      this.render(t, 1)
      this.endCall()
    }
  }

  dispose() {
    this.pause();
    this.duration = null
    this.easeFunc = null
    this.render = null
    this.endCall = null
    this.speed = null
    this.v = null;
    this.inPause = null
    this.atEnd = null
  }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值