not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

758 lines
13 KiB

  1. /**
  2. * @author sole / http://soledadpenades.com
  3. * @author mrdoob / http://mrdoob.com
  4. * @author Robert Eisele / http://www.xarg.org
  5. * @author Philippe / http://philippe.elsass.me
  6. * @author Robert Penner / http://www.robertpenner.com/easing_terms_of_use.html
  7. * @author Paul Lewis / http://www.aerotwist.com/
  8. * @author lechecacharro
  9. * @author Josh Faul / http://jocafa.com/
  10. * @author egraether / http://egraether.com/
  11. * @author endel / http://endel.me
  12. * @author Ben Delarre / http://delarre.net
  13. */
  14. // HACK: window.performance not supported on iOS
  15. if (typeof window.performance === 'undefined') {
  16. window.performance = {};
  17. }
  18. if (!window.performance.now){
  19. var nowOffset = Date.now();
  20. if (performance.timing && performance.timing.navigationStart){
  21. nowOffset = performance.timing.navigationStart
  22. }
  23. window.performance.now = function now(){
  24. return Date.now() - nowOffset;
  25. }
  26. }
  27. // Date.now shim for (ahem) Internet Explo(d|r)er
  28. if ( Date.now === undefined ) {
  29. Date.now = function () {
  30. return new Date().valueOf();
  31. };
  32. }
  33. var TWEEN = TWEEN || ( function () {
  34. var _tweens = [];
  35. return {
  36. REVISION: '12',
  37. getAll: function () {
  38. return _tweens;
  39. },
  40. removeAll: function () {
  41. _tweens = [];
  42. },
  43. add: function ( tween ) {
  44. _tweens.push( tween );
  45. },
  46. remove: function ( tween ) {
  47. var i = _tweens.indexOf( tween );
  48. if ( i !== -1 ) {
  49. _tweens.splice( i, 1 );
  50. }
  51. },
  52. update: function ( time ) {
  53. if ( _tweens.length === 0 ) return false;
  54. var i = 0;
  55. time = time !== undefined ? time : ( typeof window !== 'undefined' && window.performance !== undefined && window.performance.now !== undefined ? window.performance.now() : Date.now() );
  56. while ( i < _tweens.length ) {
  57. if ( _tweens[ i ].update( time ) ) {
  58. i++;
  59. } else {
  60. _tweens.splice( i, 1 );
  61. }
  62. }
  63. return true;
  64. }
  65. };
  66. } )();
  67. TWEEN.Tween = function ( object ) {
  68. var _object = object;
  69. var _valuesStart = {};
  70. var _valuesEnd = {};
  71. var _valuesStartRepeat = {};
  72. var _duration = 1000;
  73. var _repeat = 0;
  74. var _yoyo = false;
  75. var _isPlaying = false;
  76. var _reversed = false;
  77. var _delayTime = 0;
  78. var _startTime = null;
  79. var _easingFunction = TWEEN.Easing.Linear.None;
  80. var _interpolationFunction = TWEEN.Interpolation.Linear;
  81. var _chainedTweens = [];
  82. var _onStartCallback = null;
  83. var _onStartCallbackFired = false;
  84. var _onUpdateCallback = null;
  85. var _onCompleteCallback = null;
  86. // Set all starting values present on the target object
  87. for ( var field in object ) {
  88. _valuesStart[ field ] = parseFloat(object[field], 10);
  89. }
  90. this.to = function ( properties, duration ) {
  91. if ( duration !== undefined ) {
  92. _duration = duration;
  93. }
  94. _valuesEnd = properties;
  95. return this;
  96. };
  97. this.start = function ( time ) {
  98. TWEEN.add( this );
  99. _isPlaying = true;
  100. _onStartCallbackFired = false;
  101. _startTime = time !== undefined ? time : ( typeof window !== 'undefined' && window.performance !== undefined && window.performance.now !== undefined ? window.performance.now() : Date.now() );
  102. _startTime += _delayTime;
  103. for ( var property in _valuesEnd ) {
  104. // check if an Array was provided as property value
  105. if ( _valuesEnd[ property ] instanceof Array ) {
  106. if ( _valuesEnd[ property ].length === 0 ) {
  107. continue;
  108. }
  109. // create a local copy of the Array with the start value at the front
  110. _valuesEnd[ property ] = [ _object[ property ] ].concat( _valuesEnd[ property ] );
  111. }
  112. _valuesStart[ property ] = _object[ property ];
  113. if( ( _valuesStart[ property ] instanceof Array ) === false ) {
  114. _valuesStart[ property ] *= 1.0; // Ensures we're using numbers, not strings
  115. }
  116. _valuesStartRepeat[ property ] = _valuesStart[ property ] || 0;
  117. }
  118. return this;
  119. };
  120. this.stop = function () {
  121. if ( !_isPlaying ) {
  122. return this;
  123. }
  124. TWEEN.remove( this );
  125. _isPlaying = false;
  126. this.stopChainedTweens();
  127. return this;
  128. };
  129. this.stopChainedTweens = function () {
  130. for ( var i = 0, numChainedTweens = _chainedTweens.length; i < numChainedTweens; i++ ) {
  131. _chainedTweens[ i ].stop();
  132. }
  133. };
  134. this.delay = function ( amount ) {
  135. _delayTime = amount;
  136. return this;
  137. };
  138. this.repeat = function ( times ) {
  139. _repeat = times;
  140. return this;
  141. };
  142. this.yoyo = function( yoyo ) {
  143. _yoyo = yoyo;
  144. return this;
  145. };
  146. this.easing = function ( easing ) {
  147. _easingFunction = easing;
  148. return this;
  149. };
  150. this.interpolation = function ( interpolation ) {
  151. _interpolationFunction = interpolation;
  152. return this;
  153. };
  154. this.chain = function () {
  155. _chainedTweens = arguments;
  156. return this;
  157. };
  158. this.onStart = function ( callback ) {
  159. _onStartCallback = callback;
  160. return this;
  161. };
  162. this.onUpdate = function ( callback ) {
  163. _onUpdateCallback = callback;
  164. return this;
  165. };
  166. this.onComplete = function ( callback ) {
  167. _onCompleteCallback = callback;
  168. return this;
  169. };
  170. this.update = function ( time ) {
  171. var property;
  172. if ( time < _startTime ) {
  173. return true;
  174. }
  175. if ( _onStartCallbackFired === false ) {
  176. if ( _onStartCallback !== null ) {
  177. _onStartCallback.call( _object );
  178. }
  179. _onStartCallbackFired = true;
  180. }
  181. var elapsed = ( time - _startTime ) / _duration;
  182. elapsed = elapsed > 1 ? 1 : elapsed;
  183. var value = _easingFunction( elapsed );
  184. for ( property in _valuesEnd ) {
  185. var start = _valuesStart[ property ] || 0;
  186. var end = _valuesEnd[ property ];
  187. if ( end instanceof Array ) {
  188. _object[ property ] = _interpolationFunction( end, value );
  189. } else {
  190. // Parses relative end values with start as base (e.g.: +10, -3)
  191. if ( typeof(end) === "string" ) {
  192. end = start + parseFloat(end, 10);
  193. }
  194. // protect against non numeric properties.
  195. if ( typeof(end) === "number" ) {
  196. _object[ property ] = start + ( end - start ) * value;
  197. }
  198. }
  199. }
  200. if ( _onUpdateCallback !== null ) {
  201. _onUpdateCallback.call( _object, value );
  202. }
  203. if ( elapsed == 1 ) {
  204. if ( _repeat > 0 ) {
  205. if( isFinite( _repeat ) ) {
  206. _repeat--;
  207. }
  208. // reassign starting values, restart by making startTime = now
  209. for( property in _valuesStartRepeat ) {
  210. if ( typeof( _valuesEnd[ property ] ) === "string" ) {
  211. _valuesStartRepeat[ property ] = _valuesStartRepeat[ property ] + parseFloat(_valuesEnd[ property ], 10);
  212. }
  213. if (_yoyo) {
  214. var tmp = _valuesStartRepeat[ property ];
  215. _valuesStartRepeat[ property ] = _valuesEnd[ property ];
  216. _valuesEnd[ property ] = tmp;
  217. _reversed = !_reversed;
  218. }
  219. _valuesStart[ property ] = _valuesStartRepeat[ property ];
  220. }
  221. _startTime = time + _delayTime;
  222. return true;
  223. } else {
  224. if ( _onCompleteCallback !== null ) {
  225. _onCompleteCallback.call( _object );
  226. }
  227. for ( var i = 0, numChainedTweens = _chainedTweens.length; i < numChainedTweens; i++ ) {
  228. _chainedTweens[ i ].start( time );
  229. }
  230. return false;
  231. }
  232. }
  233. return true;
  234. };
  235. };
  236. TWEEN.Easing = {
  237. Linear: {
  238. None: function ( k ) {
  239. return k;
  240. }
  241. },
  242. Quadratic: {
  243. In: function ( k ) {
  244. return k * k;
  245. },
  246. Out: function ( k ) {
  247. return k * ( 2 - k );
  248. },
  249. InOut: function ( k ) {
  250. if ( ( k *= 2 ) < 1 ) return 0.5 * k * k;
  251. return - 0.5 * ( --k * ( k - 2 ) - 1 );
  252. }
  253. },
  254. Cubic: {
  255. In: function ( k ) {
  256. return k * k * k;
  257. },
  258. Out: function ( k ) {
  259. return --k * k * k + 1;
  260. },
  261. InOut: function ( k ) {
  262. if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k;
  263. return 0.5 * ( ( k -= 2 ) * k * k + 2 );
  264. }
  265. },
  266. Quartic: {
  267. In: function ( k ) {
  268. return k * k * k * k;
  269. },
  270. Out: function ( k ) {
  271. return 1 - ( --k * k * k * k );
  272. },
  273. InOut: function ( k ) {
  274. if ( ( k *= 2 ) < 1) return 0.5 * k * k * k * k;
  275. return - 0.5 * ( ( k -= 2 ) * k * k * k - 2 );
  276. }
  277. },
  278. Quintic: {
  279. In: function ( k ) {
  280. return k * k * k * k * k;
  281. },
  282. Out: function ( k ) {
  283. return --k * k * k * k * k + 1;
  284. },
  285. InOut: function ( k ) {
  286. if ( ( k *= 2 ) < 1 ) return 0.5 * k * k * k * k * k;
  287. return 0.5 * ( ( k -= 2 ) * k * k * k * k + 2 );
  288. }
  289. },
  290. Sinusoidal: {
  291. In: function ( k ) {
  292. return 1 - Math.cos( k * Math.PI / 2 );
  293. },
  294. Out: function ( k ) {
  295. return Math.sin( k * Math.PI / 2 );
  296. },
  297. InOut: function ( k ) {
  298. return 0.5 * ( 1 - Math.cos( Math.PI * k ) );
  299. }
  300. },
  301. Exponential: {
  302. In: function ( k ) {
  303. return k === 0 ? 0 : Math.pow( 1024, k - 1 );
  304. },
  305. Out: function ( k ) {
  306. return k === 1 ? 1 : 1 - Math.pow( 2, - 10 * k );
  307. },
  308. InOut: function ( k ) {
  309. if ( k === 0 ) return 0;
  310. if ( k === 1 ) return 1;
  311. if ( ( k *= 2 ) < 1 ) return 0.5 * Math.pow( 1024, k - 1 );
  312. return 0.5 * ( - Math.pow( 2, - 10 * ( k - 1 ) ) + 2 );
  313. }
  314. },
  315. Circular: {
  316. In: function ( k ) {
  317. return 1 - Math.sqrt( 1 - k * k );
  318. },
  319. Out: function ( k ) {
  320. return Math.sqrt( 1 - ( --k * k ) );
  321. },
  322. InOut: function ( k ) {
  323. if ( ( k *= 2 ) < 1) return - 0.5 * ( Math.sqrt( 1 - k * k) - 1);
  324. return 0.5 * ( Math.sqrt( 1 - ( k -= 2) * k) + 1);
  325. }
  326. },
  327. Elastic: {
  328. In: function ( k ) {
  329. var s, a = 0.1, p = 0.4;
  330. if ( k === 0 ) return 0;
  331. if ( k === 1 ) return 1;
  332. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  333. else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
  334. return - ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
  335. },
  336. Out: function ( k ) {
  337. var s, a = 0.1, p = 0.4;
  338. if ( k === 0 ) return 0;
  339. if ( k === 1 ) return 1;
  340. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  341. else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
  342. return ( a * Math.pow( 2, - 10 * k) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) + 1 );
  343. },
  344. InOut: function ( k ) {
  345. var s, a = 0.1, p = 0.4;
  346. if ( k === 0 ) return 0;
  347. if ( k === 1 ) return 1;
  348. if ( !a || a < 1 ) { a = 1; s = p / 4; }
  349. else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
  350. if ( ( k *= 2 ) < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) );
  351. return a * Math.pow( 2, -10 * ( k -= 1 ) ) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1;
  352. }
  353. },
  354. Back: {
  355. In: function ( k ) {
  356. var s = 1.70158;
  357. return k * k * ( ( s + 1 ) * k - s );
  358. },
  359. Out: function ( k ) {
  360. var s = 1.70158;
  361. return --k * k * ( ( s + 1 ) * k + s ) + 1;
  362. },
  363. InOut: function ( k ) {
  364. var s = 1.70158 * 1.525;
  365. if ( ( k *= 2 ) < 1 ) return 0.5 * ( k * k * ( ( s + 1 ) * k - s ) );
  366. return 0.5 * ( ( k -= 2 ) * k * ( ( s + 1 ) * k + s ) + 2 );
  367. }
  368. },
  369. Bounce: {
  370. In: function ( k ) {
  371. return 1 - TWEEN.Easing.Bounce.Out( 1 - k );
  372. },
  373. Out: function ( k ) {
  374. if ( k < ( 1 / 2.75 ) ) {
  375. return 7.5625 * k * k;
  376. } else if ( k < ( 2 / 2.75 ) ) {
  377. return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
  378. } else if ( k < ( 2.5 / 2.75 ) ) {
  379. return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
  380. } else {
  381. return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
  382. }
  383. },
  384. InOut: function ( k ) {
  385. if ( k < 0.5 ) return TWEEN.Easing.Bounce.In( k * 2 ) * 0.5;
  386. return TWEEN.Easing.Bounce.Out( k * 2 - 1 ) * 0.5 + 0.5;
  387. }
  388. }
  389. };
  390. TWEEN.Interpolation = {
  391. Linear: function ( v, k ) {
  392. var m = v.length - 1, f = m * k, i = Math.floor( f ), fn = TWEEN.Interpolation.Utils.Linear;
  393. if ( k < 0 ) return fn( v[ 0 ], v[ 1 ], f );
  394. if ( k > 1 ) return fn( v[ m ], v[ m - 1 ], m - f );
  395. return fn( v[ i ], v[ i + 1 > m ? m : i + 1 ], f - i );
  396. },
  397. Bezier: function ( v, k ) {
  398. var b = 0, n = v.length - 1, pw = Math.pow, bn = TWEEN.Interpolation.Utils.Bernstein, i;
  399. for ( i = 0; i <= n; i++ ) {
  400. b += pw( 1 - k, n - i ) * pw( k, i ) * v[ i ] * bn( n, i );
  401. }
  402. return b;
  403. },
  404. CatmullRom: function ( v, k ) {
  405. var m = v.length - 1, f = m * k, i = Math.floor( f ), fn = TWEEN.Interpolation.Utils.CatmullRom;
  406. if ( v[ 0 ] === v[ m ] ) {
  407. if ( k < 0 ) i = Math.floor( f = m * ( 1 + k ) );
  408. return fn( v[ ( i - 1 + m ) % m ], v[ i ], v[ ( i + 1 ) % m ], v[ ( i + 2 ) % m ], f - i );
  409. } else {
  410. if ( k < 0 ) return v[ 0 ] - ( fn( v[ 0 ], v[ 0 ], v[ 1 ], v[ 1 ], -f ) - v[ 0 ] );
  411. if ( k > 1 ) return v[ m ] - ( fn( v[ m ], v[ m ], v[ m - 1 ], v[ m - 1 ], f - m ) - v[ m ] );
  412. return fn( v[ i ? i - 1 : 0 ], v[ i ], v[ m < i + 1 ? m : i + 1 ], v[ m < i + 2 ? m : i + 2 ], f - i );
  413. }
  414. },
  415. Utils: {
  416. Linear: function ( p0, p1, t ) {
  417. return ( p1 - p0 ) * t + p0;
  418. },
  419. Bernstein: function ( n , i ) {
  420. var fc = TWEEN.Interpolation.Utils.Factorial;
  421. return fc( n ) / fc( i ) / fc( n - i );
  422. },
  423. Factorial: ( function () {
  424. var a = [ 1 ];
  425. return function ( n ) {
  426. var s = 1, i;
  427. if ( a[ n ] ) return a[ n ];
  428. for ( i = n; i > 1; i-- ) s *= i;
  429. return a[ n ] = s;
  430. };
  431. } )(),
  432. CatmullRom: function ( p0, p1, p2, p3, t ) {
  433. var v0 = ( p2 - p0 ) * 0.5, v1 = ( p3 - p1 ) * 0.5, t2 = t * t, t3 = t * t2;
  434. return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  435. }
  436. }
  437. };