国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

tween.js緩動補間動畫算法示例

2019-11-19 14:21:10
字體:
來源:轉載
供稿:網友

一、理解tween.js

如果看到上面的已經理解了,可以跳過下面的部分.下面為對Tween.js的解釋 下面就介紹如何使用這個Tween了,首先b、c、d三個參數(即初始值,變化量,持續時間)在緩動開始前,是需要先確定好的。 首先引入一個概念就補間動畫 Flash做動畫時會用到Tween類,利用它可以做很多動畫效果,例如緩動、彈簧等等。 tween.js在Flash中可以解釋為補間動畫. 那么問題來了,什么是補間動畫呢?

相信學過Flash的都知道補間動畫是flash主要的非常重要的表現手段之一.補間動畫有動作補間動畫與形狀補間動畫兩種,但是在js中卻不需要了解這么多. 好了,廢話不多說,先看看百度百科關于補間動畫給出的定義: 補間動畫:做flash動畫時,在兩個關鍵幀中間需要做“補間動畫”,才能實現圖畫的運動; 插入補間動畫后兩個關鍵幀之間的插補幀是由計算機自動運算而得到的

那么什么是關鍵幀呢? 舉個栗子: 先科普一下,平常所看的電影,動畫都是24幀的,24幀為一秒.在人眼可以捕捉的范圍內.可以想象兩個點之間有有22個點,形成一條直線或者曲線.而每一個點就代表一幀,幀――就是動畫中最小單位的單幅影像畫面,而單幅影像畫面就可以看做是一個對象(一切皆對象,除去值類型)了.而這條線就代表對象的運動軌跡.

二、四個參數

  1. t: current time-->代表第一個點,也就是第一幀,也就是一個動畫開始的地方。
  2. b: beginning value-->代表初始值,也就是開始量,我們看電影或者動畫一般都不會看序幕把,那么跳過開頭部分,選擇第一幀和最后一幀之間你要開始看位置,而此位置就是初始值。
  3. c: change in value-->代表的就是最后一幀減去初始值就是變化量,
  4. d: duration-->代表最后一幀,1s的結束,也是動畫的結束。

tween.js的使用 1.下載 2.引入 3.使用tween.js語法

Tween.緩動函數名.緩動效果名(t,b,c,d);

注意:當開始步數增加到與結束步數相等時,整個運動結束. 注注意:只有當t增加到與d相等時才會結束運動;如果不等,運動不會停止.

三、tween文件代碼

/* * Tween.js * t: current time(當前時間); * b: beginning value(初始值); * c: change in value(變化量); * d: duration(持續時間)。*/var 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: { //指數衰減正弦曲線緩動函數    easeIn: function(t, b, c, d, a, p) { //加速      var s;      if (t==0) return b;      if ((t /= d) == 1) return b + c;      if (typeof p == "undefined") p = d * .3;      if (!a || a < Math.abs(c)) {        s = p / 4;        a = c;      } 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) { //減速      var s;      if (t==0) return b;      if ((t /= d) == 1) return b + c;      if (typeof p == "undefined") p = d * .3;      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) { //先加速后減速      var s;      if (t==0) return b;      if ((t /= d / 2) == 2) return b+c;      if (typeof p == "undefined") p = d * (.3 * 1.5);      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: { //超過范圍的三次方的緩動函數    easeIn: function(t, b, c, d, s) {      if (typeof s == "undefined") s = 1.70158;      return c * (t /= d) * t * ((s + 1) * t - s) + b;    },    easeOut: function(t, b, c, d, s) {      if (typeof 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 (typeof 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;      }    }  }}Math.tween = Tween;

四、舉個栗子

<!DOCTYPE html><html>  <head>    <meta charset="UTF-8">    <title></title>    <script src="https://cdn.jsdelivr.net/npm/vue"></script>    <script src="Tween/tween.js"></script>    <style>      *{margin: 0;padding: 0;}      .out{width: 800px;height: 500px;background: #e5e5e5;position: relative;padding: 20px;text-align: center;}      .inner{width: 50px;height: 50px;border-radius: 50%;background: #FF0000; position: absolute;left: 50px;top: 50px;}    </style>  </head>  <body>    <div id="app" class="out">      <div class="inner" id="ball"></div>      <button id="start" @click="start()">start</button>    </div>  </body>  <script type="text/javascript">    var app = new Vue({      el: '#app',      data: {        t: 0,        b: 50,        c: 500,        d: 1500,      },      methods:{        start(){          var t = this.t;          const b = this.b;          const c = this.c;          const d = this.d;          const setInt = setInterval(()=>{            t++;            console.log(t)            if(t==300){clearInterval(setInt)}            console.log(t);            const ballLeft = Tween.Linear(t,b,c,d)+"px";            ball.style.left = ballLeft;          },20)        }      }    })  </script></html>

五、個人體會

tween的優勢在于tween實現效果是依據算法,不是某種語言的語法,因此可以運用的地方很廣泛,一次學習終身受益。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 特克斯县| 阿图什市| 古交市| 阿城市| 卫辉市| 桑植县| 荣成市| 柞水县| 华容县| 长寿区| 马鞍山市| 博爱县| 阳信县| 揭阳市| 英超| 五寨县| 微山县| 方正县| 天津市| 高青县| 辽阳市| 如皋市| 苏尼特左旗| 合水县| 丹棱县| 常熟市| 儋州市| 镇赉县| 布尔津县| 建始县| 屯留县| 星座| 阜康市| 南皮县| 汝城县| 永安市| 延安市| 阳谷县| 大邑县| 福海县| 高安市|