2021SC@SDUSC山东大学软件学院软件工程应用与实践--quark renderer代码分析 第六篇 动画系统分析(5):时间轴

2021SC@SDUSC

今天来看看时间线。时间线,其作用是用来计算元素上的某个属性在指定时间点上的数值,以便于动画的稳定有序播放,也便于处理动画过程中的操作。

首先是创建时间线。

function Timeline(options) {
    _classCallCheck(this, Timeline);

    this.element = options.element;
    this.lifeTime = options.lifeTime || 1000;
    this.delay = options.delay || 0;
    this.loop = options.loop == null ? false : options.loop;
    this.gap = options.gap || 0;
    this.easing = options.easing || 'Linear';
    this.onframe = options.onframe;
    this.ondestroy = options.ondestroy;
    this.onrestart = options.onrestart;
    this._initialized = false;
    this._pausedTime = 0;
    this._paused = false;
  }

这里是时间轴的构造,包括了时间轴的基本元素,时间轴的生命周期(默认值1000),时间轴的延迟,时间轴是否循环,时间线的空隙,时间轴的缓动,帧,时间线的销毁和重建。

_createClass(Timeline, [{
    key: "nextFrame",
    value: function nextFrame(globalTime, deltaTime) {
      // Set startTime on first frame, or _startTime may has milleseconds different between clips
      // PENDING
      if (!this._initialized) {
        this._startTime = globalTime + this.delay;
        this._initialized = true;
      }

      if (this._paused) {
        this._pausedTime += deltaTime;
        return;
      }

      var percent = (globalTime - this._startTime - this._pausedTime) / this.lifeTime;

      if (percent < 0) {
        return;
      }

      percent = mathMin(percent, 1);
      var easing = this.easing;
      var easingFunc = typeof easing === 'string' ? easingFuncs[easing] : easing;
      var schedule = typeof easingFunc === 'function' ? easingFunc(percent) : percent;

      if (percent === 1) {
        if (this.loop) {
          this.restart(globalTime);
          return 'restart';
        }

        return 'destroy';
      } else {
        this.fire('frame', schedule);
        return percent;
      }
    }

这里的作用是进入下一帧,也就是时间线最主要的作用,是利用当前时间{Number} globalTime加时间偏移量{Number} deltaTime来实现。里面还包括了时间线初始化和暂停后重新开始的情况。当没有下一帧的时候,时间轴destory自动摧毁。

key: "restart",
    value: function restart(globalTime) {
      var remainder = (globalTime - this._startTime - this._pausedTime) % this.lifeTime;
      this._startTime = globalTime - remainder + this.gap;
      this._pausedTime = 0;

当重启动画的时候,利用原时间减开始时间减暂停时间和生命周期取余,获得重启时间轴位置。

key: "fire",
    value: function fire(eventType, arg) {
      eventType = 'on' + eventType;

      if (this[eventType]) {
        this[eventType](this.element, arg);
      }
    }
 {
    key: "pause",
    value: function pause() {
      this._paused = true;
    }
{
    key: "resume",
    value: function resume() {
      this._paused = false;
    }

后面的开始暂停和恢复就大同小异没什么了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云亦纵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值