DIV CSS 佈局教程網

JavaScript 動畫緩動算法總整理:Tween 函數全解析
編輯:關於JavaScript     

做自製動畫時,最容易被忽略卻最影響觀感的就是「速度曲線」。位移量平均分配看起來機械而生硬,加上緩動函數後立刻有了重量感。這套 Tween 演算法由 Robert Penner 提出,幾乎成為業界標準,本文完整整理其分類、參數意義與實作程式碼。

十大緩動類型

· Linear:線性,無緩動效果,等速運動。

· Quadratic:二次方緩動。

· Cubic:三次方緩動。

· Quartic:四次方緩動。

· Quintic:五次方緩動。

· Sinusoidal:正弦曲線緩動。

· Exponential:指數曲線緩動。

· Circular:圓形曲線緩動。

· Elastic:具彈性的指數衰減正弦曲線,會超過終點再回彈。

· Back:先往反方向衝出去再拉回來的三次方緩動。

· Bounce:像球落地般指數衰減的反彈。

三種播放方式

每個效果都提供三種變體:

· easeIn:從 0 開始加速。

· easeOut:減速到 0 結束。

· easeInOut:前半段加速、後半段減速。

四個參數的含義

· t — current time,當前時間

· b — beginning value,初始值

· c — change in value,變化量

· d — duration,持續時間

函數回傳值就是「目前應該處於的路徑位置」。以位移動畫為例,把 b 設為起始 left、c 設為目標與起始的差、d 設為動畫總長,再用計時器持續把 t 遞增代入,就能得到平滑的運動軌跡。

Tween.js 完整實作

Tween = {
  Linear: function(t, b, c, d){
    return c * t / d + b;
  },

  Quad: {
    easeIn: function(t, b, c, d){
      return c * (t /= d) * t + b;
    },
    easeOut: function(t, b, c, d){
      return -c * (t /= d) * (t - 2) + b;
    },
    easeInOut: function(t, b, c, d){
      if ((t /= d / 2) < 1) return c / 2 * t * t + b;
      return -c / 2 * ((--t) * (t - 2) - 1) + b;
    }
  },

  Cubic: {
    easeIn: function(t, b, c, d){
      return c * (t /= d) * t * t + b;
    },
    easeOut: function(t, b, c, d){
      return c * ((t = t / d - 1) * t * t + 1) + b;
    },
    easeInOut: function(t, b, c, d){
      if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
      return c / 2 * ((t -= 2) * t * t + 2) + b;
    }
  },

  Quart: {
    easeIn: function(t, b, c, d){
      return c * (t /= d) * t * t * t + b;
    },
    easeOut: function(t, b, c, d){
      return -c * ((t = t / d - 1) * t * t * t - 1) + b;
    },
    easeInOut: function(t, b, c, d){
      if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
      return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
    }
  },

  Quint: {
    easeIn: function(t, b, c, d){
      return c * (t /= d) * t * t * t * t + b;
    },
    easeOut: function(t, b, c, d){
      return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
    },
    easeInOut: function(t, b, c, d){
      if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
      return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
    }
  },

  Sine: {
    easeIn: function(t, b, c, d){
      return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
    },
    easeOut: function(t, b, c, d){
      return c * Math.sin(t / d * (Math.PI / 2)) + b;
    },
    easeInOut: function(t, b, c, d){
      return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
    }
  },

  Expo: {
    easeIn: function(t, b, c, d){
      return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
    },
    easeOut: function(t, b, c, d){
      return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
    },
    easeInOut: function(t, b, c, d){
      if (t == 0) return b;
      if (t == d) return b + c;
      if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
      return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
    }
  },

  Circ: {
    easeIn: function(t, b, c, d){
      return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
    },
    easeOut: function(t, b, c, d){
      return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
    },
    easeInOut: function(t, b, c, d){
      if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
      return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
    }
  },

  Elastic: {
    // a 為振幅,p 為週期
    easeIn: function(t, b, c, d, a, p){
      if (t == 0) return b;
      if ((t /= d) == 1) return b + c;
      if (!p) p = d * .3;
      var s;
      if (!a || a < Math.abs(c)) { a = c; s = p / 4; }
      else { s = p / (2 * Math.PI) * Math.asin(c / a); }
      return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
    },
    easeOut: function(t, b, c, d, a, p){
      if (t == 0) return b;
      if ((t /= d) == 1) return b + c;
      if (!p) p = d * .3;
      var s;
      if (!a || a < Math.abs(c)) { a = c; s = p / 4; }
      else { s = p / (2 * Math.PI) * Math.asin(c / a); }
      return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
    },
    easeInOut: function(t, b, c, d, a, p){
      if (t == 0) return b;
      if ((t /= d / 2) == 2) return b + c;
      if (!p) p = d * (.3 * 1.5);
      var s;
      if (!a || a < Math.abs(c)) { a = c; s = p / 4; }
      else { s = p / (2 * Math.PI) * Math.asin(c / a); }
      if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
      return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
    }
  },

  Back: {
    // s 控制回過頭的幅度
    easeIn: function(t, b, c, d, s){
      if (s == undefined) s = 1.70158;
      return c * (t /= d) * t * ((s + 1) * t - s) + b;
    },
    easeOut: function(t, b, c, d, s){
      if (s == undefined) s = 1.70158;
      return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
    },
    easeInOut: function(t, b, c, d, s){
      if (s == undefined) s = 1.70158;
      if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
      return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
    }
  },

  Bounce: {
    easeIn: function(t, b, c, d){
      return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b;
    },
    easeOut: function(t, b, c, d){
      if ((t /= d) < (1 / 2.75)) {
        return c * (7.5625 * t * t) + b;
      } else if (t < (2 / 2.75)) {
        return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
      } else if (t < (2.5 / 2.75)) {
        return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
      } else {
        return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
      }
    },
    easeInOut: function(t, b, c, d){
      if (t < d / 2) return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
      else return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
    }
  }
};

怎麼用

function animate(ele, from, to, duration){
  var start = null;
  var change = to - from;

  function step(timestamp){
    if (start === null) start = timestamp;
    var t = timestamp - start;
    if (t >= duration) {
      ele.style.left = to + 'px';
      return;
    }
    // 三次方 easeOut,收尾柔和
    ele.style.left = Tween.Cubic.easeOut(t, from, change, duration) + 'px';
    requestAnimationFrame(step);
  }
  requestAnimationFrame(step);
}

選型建議

1. 進場用 easeOut,出場用 easeIn。元素出現時希望它「衝進來再減速停住」,用 easeOut;離開時希望「慢慢起步然後加速離開」,用 easeIn。

2. Cubic 是最安全的默認值。幅度溫和不易浮誇,多數 UI 過渡都用它。

3. Elastic 與 Bounce 要節制。彈跳次數多會讓人分心,通常只用於需要強調的單一元素,且 duration 不宜太長。

4. 搭配 requestAnimationFrame 而非 setInterval。前者會與螢幕刷新同步、在背景分頁自動暫停,效能與耗電都更好。

5. 現代專案若已有 CSS transition 或 Web Animations API,多數簡單動畫用 CSS 宣告即可,Tween 函數更适合需要依資料動態計算中間值的複雜場景。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved