tween.module.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /**
  2. * The Ease class provides a collection of easing functions for use with tween.js.
  3. */
  4. var Easing = Object.freeze({
  5. Linear: Object.freeze({
  6. None: function (amount) {
  7. return amount;
  8. },
  9. In: function (amount) {
  10. return this.None(amount);
  11. },
  12. Out: function (amount) {
  13. return this.None(amount);
  14. },
  15. InOut: function (amount) {
  16. return this.None(amount);
  17. },
  18. }),
  19. Quadratic: Object.freeze({
  20. In: function (amount) {
  21. return amount * amount;
  22. },
  23. Out: function (amount) {
  24. return amount * (2 - amount);
  25. },
  26. InOut: function (amount) {
  27. if ((amount *= 2) < 1) {
  28. return 0.5 * amount * amount;
  29. }
  30. return -0.5 * (--amount * (amount - 2) - 1);
  31. },
  32. }),
  33. Cubic: Object.freeze({
  34. In: function (amount) {
  35. return amount * amount * amount;
  36. },
  37. Out: function (amount) {
  38. return --amount * amount * amount + 1;
  39. },
  40. InOut: function (amount) {
  41. if ((amount *= 2) < 1) {
  42. return 0.5 * amount * amount * amount;
  43. }
  44. return 0.5 * ((amount -= 2) * amount * amount + 2);
  45. },
  46. }),
  47. Quartic: Object.freeze({
  48. In: function (amount) {
  49. return amount * amount * amount * amount;
  50. },
  51. Out: function (amount) {
  52. return 1 - --amount * amount * amount * amount;
  53. },
  54. InOut: function (amount) {
  55. if ((amount *= 2) < 1) {
  56. return 0.5 * amount * amount * amount * amount;
  57. }
  58. return -0.5 * ((amount -= 2) * amount * amount * amount - 2);
  59. },
  60. }),
  61. Quintic: Object.freeze({
  62. In: function (amount) {
  63. return amount * amount * amount * amount * amount;
  64. },
  65. Out: function (amount) {
  66. return --amount * amount * amount * amount * amount + 1;
  67. },
  68. InOut: function (amount) {
  69. if ((amount *= 2) < 1) {
  70. return 0.5 * amount * amount * amount * amount * amount;
  71. }
  72. return 0.5 * ((amount -= 2) * amount * amount * amount * amount + 2);
  73. },
  74. }),
  75. Sinusoidal: Object.freeze({
  76. In: function (amount) {
  77. return 1 - Math.sin(((1.0 - amount) * Math.PI) / 2);
  78. },
  79. Out: function (amount) {
  80. return Math.sin((amount * Math.PI) / 2);
  81. },
  82. InOut: function (amount) {
  83. return 0.5 * (1 - Math.sin(Math.PI * (0.5 - amount)));
  84. },
  85. }),
  86. Exponential: Object.freeze({
  87. In: function (amount) {
  88. return amount === 0 ? 0 : Math.pow(1024, amount - 1);
  89. },
  90. Out: function (amount) {
  91. return amount === 1 ? 1 : 1 - Math.pow(2, -10 * amount);
  92. },
  93. InOut: function (amount) {
  94. if (amount === 0) {
  95. return 0;
  96. }
  97. if (amount === 1) {
  98. return 1;
  99. }
  100. if ((amount *= 2) < 1) {
  101. return 0.5 * Math.pow(1024, amount - 1);
  102. }
  103. return 0.5 * (-Math.pow(2, -10 * (amount - 1)) + 2);
  104. },
  105. }),
  106. Circular: Object.freeze({
  107. In: function (amount) {
  108. return 1 - Math.sqrt(1 - amount * amount);
  109. },
  110. Out: function (amount) {
  111. return Math.sqrt(1 - --amount * amount);
  112. },
  113. InOut: function (amount) {
  114. if ((amount *= 2) < 1) {
  115. return -0.5 * (Math.sqrt(1 - amount * amount) - 1);
  116. }
  117. return 0.5 * (Math.sqrt(1 - (amount -= 2) * amount) + 1);
  118. },
  119. }),
  120. Elastic: Object.freeze({
  121. In: function (amount) {
  122. if (amount === 0) {
  123. return 0;
  124. }
  125. if (amount === 1) {
  126. return 1;
  127. }
  128. return -Math.pow(2, 10 * (amount - 1)) * Math.sin((amount - 1.1) * 5 * Math.PI);
  129. },
  130. Out: function (amount) {
  131. if (amount === 0) {
  132. return 0;
  133. }
  134. if (amount === 1) {
  135. return 1;
  136. }
  137. return Math.pow(2, -10 * amount) * Math.sin((amount - 0.1) * 5 * Math.PI) + 1;
  138. },
  139. InOut: function (amount) {
  140. if (amount === 0) {
  141. return 0;
  142. }
  143. if (amount === 1) {
  144. return 1;
  145. }
  146. amount *= 2;
  147. if (amount < 1) {
  148. return -0.5 * Math.pow(2, 10 * (amount - 1)) * Math.sin((amount - 1.1) * 5 * Math.PI);
  149. }
  150. return 0.5 * Math.pow(2, -10 * (amount - 1)) * Math.sin((amount - 1.1) * 5 * Math.PI) + 1;
  151. },
  152. }),
  153. Back: Object.freeze({
  154. In: function (amount) {
  155. var s = 1.70158;
  156. return amount === 1 ? 1 : amount * amount * ((s + 1) * amount - s);
  157. },
  158. Out: function (amount) {
  159. var s = 1.70158;
  160. return amount === 0 ? 0 : --amount * amount * ((s + 1) * amount + s) + 1;
  161. },
  162. InOut: function (amount) {
  163. var s = 1.70158 * 1.525;
  164. if ((amount *= 2) < 1) {
  165. return 0.5 * (amount * amount * ((s + 1) * amount - s));
  166. }
  167. return 0.5 * ((amount -= 2) * amount * ((s + 1) * amount + s) + 2);
  168. },
  169. }),
  170. Bounce: Object.freeze({
  171. In: function (amount) {
  172. return 1 - Easing.Bounce.Out(1 - amount);
  173. },
  174. Out: function (amount) {
  175. if (amount < 1 / 2.75) {
  176. return 7.5625 * amount * amount;
  177. }
  178. else if (amount < 2 / 2.75) {
  179. return 7.5625 * (amount -= 1.5 / 2.75) * amount + 0.75;
  180. }
  181. else if (amount < 2.5 / 2.75) {
  182. return 7.5625 * (amount -= 2.25 / 2.75) * amount + 0.9375;
  183. }
  184. else {
  185. return 7.5625 * (amount -= 2.625 / 2.75) * amount + 0.984375;
  186. }
  187. },
  188. InOut: function (amount) {
  189. if (amount < 0.5) {
  190. return Easing.Bounce.In(amount * 2) * 0.5;
  191. }
  192. return Easing.Bounce.Out(amount * 2 - 1) * 0.5 + 0.5;
  193. },
  194. }),
  195. generatePow: function (power) {
  196. if (power === void 0) { power = 4; }
  197. power = power < Number.EPSILON ? Number.EPSILON : power;
  198. power = power > 10000 ? 10000 : power;
  199. return {
  200. In: function (amount) {
  201. return Math.pow(amount, power);
  202. },
  203. Out: function (amount) {
  204. return 1 - Math.pow((1 - amount), power);
  205. },
  206. InOut: function (amount) {
  207. if (amount < 0.5) {
  208. return Math.pow((amount * 2), power) / 2;
  209. }
  210. return (1 - Math.pow((2 - amount * 2), power)) / 2 + 0.5;
  211. },
  212. };
  213. },
  214. });
  215. var now = function () { return performance.now(); };
  216. /**
  217. * Controlling groups of tweens
  218. *
  219. * Using the TWEEN singleton to manage your tweens can cause issues in large apps with many components.
  220. * In these cases, you may want to create your own smaller groups of tween
  221. */
  222. var Group = /** @class */ (function () {
  223. function Group() {
  224. this._tweens = {};
  225. this._tweensAddedDuringUpdate = {};
  226. }
  227. Group.prototype.getAll = function () {
  228. var _this = this;
  229. return Object.keys(this._tweens).map(function (tweenId) {
  230. return _this._tweens[tweenId];
  231. });
  232. };
  233. Group.prototype.removeAll = function () {
  234. this._tweens = {};
  235. };
  236. Group.prototype.add = function (tween) {
  237. this._tweens[tween.getId()] = tween;
  238. this._tweensAddedDuringUpdate[tween.getId()] = tween;
  239. };
  240. Group.prototype.remove = function (tween) {
  241. delete this._tweens[tween.getId()];
  242. delete this._tweensAddedDuringUpdate[tween.getId()];
  243. };
  244. Group.prototype.update = function (time, preserve) {
  245. if (time === void 0) { time = now(); }
  246. if (preserve === void 0) { preserve = false; }
  247. var tweenIds = Object.keys(this._tweens);
  248. if (tweenIds.length === 0) {
  249. return false;
  250. }
  251. // Tweens are updated in "batches". If you add a new tween during an
  252. // update, then the new tween will be updated in the next batch.
  253. // If you remove a tween during an update, it may or may not be updated.
  254. // However, if the removed tween was added during the current batch,
  255. // then it will not be updated.
  256. while (tweenIds.length > 0) {
  257. this._tweensAddedDuringUpdate = {};
  258. for (var i = 0; i < tweenIds.length; i++) {
  259. var tween = this._tweens[tweenIds[i]];
  260. var autoStart = !preserve;
  261. if (tween && tween.update(time, autoStart) === false && !preserve) {
  262. delete this._tweens[tweenIds[i]];
  263. }
  264. }
  265. tweenIds = Object.keys(this._tweensAddedDuringUpdate);
  266. }
  267. return true;
  268. };
  269. return Group;
  270. }());
  271. /**
  272. *
  273. */
  274. var Interpolation = {
  275. Linear: function (v, k) {
  276. var m = v.length - 1;
  277. var f = m * k;
  278. var i = Math.floor(f);
  279. var fn = Interpolation.Utils.Linear;
  280. if (k < 0) {
  281. return fn(v[0], v[1], f);
  282. }
  283. if (k > 1) {
  284. return fn(v[m], v[m - 1], m - f);
  285. }
  286. return fn(v[i], v[i + 1 > m ? m : i + 1], f - i);
  287. },
  288. Bezier: function (v, k) {
  289. var b = 0;
  290. var n = v.length - 1;
  291. var pw = Math.pow;
  292. var bn = Interpolation.Utils.Bernstein;
  293. for (var i = 0; i <= n; i++) {
  294. b += pw(1 - k, n - i) * pw(k, i) * v[i] * bn(n, i);
  295. }
  296. return b;
  297. },
  298. CatmullRom: function (v, k) {
  299. var m = v.length - 1;
  300. var f = m * k;
  301. var i = Math.floor(f);
  302. var fn = Interpolation.Utils.CatmullRom;
  303. if (v[0] === v[m]) {
  304. if (k < 0) {
  305. i = Math.floor((f = m * (1 + k)));
  306. }
  307. return fn(v[(i - 1 + m) % m], v[i], v[(i + 1) % m], v[(i + 2) % m], f - i);
  308. }
  309. else {
  310. if (k < 0) {
  311. return v[0] - (fn(v[0], v[0], v[1], v[1], -f) - v[0]);
  312. }
  313. if (k > 1) {
  314. return v[m] - (fn(v[m], v[m], v[m - 1], v[m - 1], f - m) - v[m]);
  315. }
  316. 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);
  317. }
  318. },
  319. Utils: {
  320. Linear: function (p0, p1, t) {
  321. return (p1 - p0) * t + p0;
  322. },
  323. Bernstein: function (n, i) {
  324. var fc = Interpolation.Utils.Factorial;
  325. return fc(n) / fc(i) / fc(n - i);
  326. },
  327. Factorial: (function () {
  328. var a = [1];
  329. return function (n) {
  330. var s = 1;
  331. if (a[n]) {
  332. return a[n];
  333. }
  334. for (var i = n; i > 1; i--) {
  335. s *= i;
  336. }
  337. a[n] = s;
  338. return s;
  339. };
  340. })(),
  341. CatmullRom: function (p0, p1, p2, p3, t) {
  342. var v0 = (p2 - p0) * 0.5;
  343. var v1 = (p3 - p1) * 0.5;
  344. var t2 = t * t;
  345. var t3 = t * t2;
  346. return (2 * p1 - 2 * p2 + v0 + v1) * t3 + (-3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
  347. },
  348. },
  349. };
  350. /**
  351. * Utils
  352. */
  353. var Sequence = /** @class */ (function () {
  354. function Sequence() {
  355. }
  356. Sequence.nextId = function () {
  357. return Sequence._nextId++;
  358. };
  359. Sequence._nextId = 0;
  360. return Sequence;
  361. }());
  362. var mainGroup = new Group();
  363. /**
  364. * Tween.js - Licensed under the MIT license
  365. * https://github.com/tweenjs/tween.js
  366. * ----------------------------------------------
  367. *
  368. * See https://github.com/tweenjs/tween.js/graphs/contributors for the full list of contributors.
  369. * Thank you all, you're awesome!
  370. */
  371. var Tween = /** @class */ (function () {
  372. function Tween(_object, _group) {
  373. if (_group === void 0) { _group = mainGroup; }
  374. this._object = _object;
  375. this._group = _group;
  376. this._isPaused = false;
  377. this._pauseStart = 0;
  378. this._valuesStart = {};
  379. this._valuesEnd = {};
  380. this._valuesStartRepeat = {};
  381. this._duration = 1000;
  382. this._isDynamic = false;
  383. this._initialRepeat = 0;
  384. this._repeat = 0;
  385. this._yoyo = false;
  386. this._isPlaying = false;
  387. this._reversed = false;
  388. this._delayTime = 0;
  389. this._startTime = 0;
  390. this._easingFunction = Easing.Linear.None;
  391. this._interpolationFunction = Interpolation.Linear;
  392. // eslint-disable-next-line
  393. this._chainedTweens = [];
  394. this._onStartCallbackFired = false;
  395. this._onEveryStartCallbackFired = false;
  396. this._id = Sequence.nextId();
  397. this._isChainStopped = false;
  398. this._propertiesAreSetUp = false;
  399. this._goToEnd = false;
  400. }
  401. Tween.prototype.getId = function () {
  402. return this._id;
  403. };
  404. Tween.prototype.isPlaying = function () {
  405. return this._isPlaying;
  406. };
  407. Tween.prototype.isPaused = function () {
  408. return this._isPaused;
  409. };
  410. Tween.prototype.to = function (target, duration) {
  411. if (duration === void 0) { duration = 1000; }
  412. if (this._isPlaying)
  413. throw new Error('Can not call Tween.to() while Tween is already started or paused. Stop the Tween first.');
  414. this._valuesEnd = target;
  415. this._propertiesAreSetUp = false;
  416. this._duration = duration;
  417. return this;
  418. };
  419. Tween.prototype.duration = function (duration) {
  420. if (duration === void 0) { duration = 1000; }
  421. this._duration = duration;
  422. return this;
  423. };
  424. Tween.prototype.dynamic = function (dynamic) {
  425. if (dynamic === void 0) { dynamic = false; }
  426. this._isDynamic = dynamic;
  427. return this;
  428. };
  429. Tween.prototype.start = function (time, overrideStartingValues) {
  430. if (time === void 0) { time = now(); }
  431. if (overrideStartingValues === void 0) { overrideStartingValues = false; }
  432. if (this._isPlaying) {
  433. return this;
  434. }
  435. // eslint-disable-next-line
  436. this._group && this._group.add(this);
  437. this._repeat = this._initialRepeat;
  438. if (this._reversed) {
  439. // If we were reversed (f.e. using the yoyo feature) then we need to
  440. // flip the tween direction back to forward.
  441. this._reversed = false;
  442. for (var property in this._valuesStartRepeat) {
  443. this._swapEndStartRepeatValues(property);
  444. this._valuesStart[property] = this._valuesStartRepeat[property];
  445. }
  446. }
  447. this._isPlaying = true;
  448. this._isPaused = false;
  449. this._onStartCallbackFired = false;
  450. this._onEveryStartCallbackFired = false;
  451. this._isChainStopped = false;
  452. this._startTime = time;
  453. this._startTime += this._delayTime;
  454. if (!this._propertiesAreSetUp || overrideStartingValues) {
  455. this._propertiesAreSetUp = true;
  456. // If dynamic is not enabled, clone the end values instead of using the passed-in end values.
  457. if (!this._isDynamic) {
  458. var tmp = {};
  459. for (var prop in this._valuesEnd)
  460. tmp[prop] = this._valuesEnd[prop];
  461. this._valuesEnd = tmp;
  462. }
  463. this._setupProperties(this._object, this._valuesStart, this._valuesEnd, this._valuesStartRepeat, overrideStartingValues);
  464. }
  465. return this;
  466. };
  467. Tween.prototype.startFromCurrentValues = function (time) {
  468. return this.start(time, true);
  469. };
  470. Tween.prototype._setupProperties = function (_object, _valuesStart, _valuesEnd, _valuesStartRepeat, overrideStartingValues) {
  471. for (var property in _valuesEnd) {
  472. var startValue = _object[property];
  473. var startValueIsArray = Array.isArray(startValue);
  474. var propType = startValueIsArray ? 'array' : typeof startValue;
  475. var isInterpolationList = !startValueIsArray && Array.isArray(_valuesEnd[property]);
  476. // If `to()` specifies a property that doesn't exist in the source object,
  477. // we should not set that property in the object
  478. if (propType === 'undefined' || propType === 'function') {
  479. continue;
  480. }
  481. // Check if an Array was provided as property value
  482. if (isInterpolationList) {
  483. var endValues = _valuesEnd[property];
  484. if (endValues.length === 0) {
  485. continue;
  486. }
  487. // Handle an array of relative values.
  488. // Creates a local copy of the Array with the start value at the front
  489. var temp = [startValue];
  490. for (var i = 0, l = endValues.length; i < l; i += 1) {
  491. var value = this._handleRelativeValue(startValue, endValues[i]);
  492. if (isNaN(value)) {
  493. isInterpolationList = false;
  494. console.warn('Found invalid interpolation list. Skipping.');
  495. break;
  496. }
  497. temp.push(value);
  498. }
  499. if (isInterpolationList) {
  500. // if (_valuesStart[property] === undefined) { // handle end values only the first time. NOT NEEDED? setupProperties is now guarded by _propertiesAreSetUp.
  501. _valuesEnd[property] = temp;
  502. // }
  503. }
  504. }
  505. // handle the deepness of the values
  506. if ((propType === 'object' || startValueIsArray) && startValue && !isInterpolationList) {
  507. _valuesStart[property] = startValueIsArray ? [] : {};
  508. var nestedObject = startValue;
  509. for (var prop in nestedObject) {
  510. _valuesStart[property][prop] = nestedObject[prop];
  511. }
  512. // TODO? repeat nested values? And yoyo? And array values?
  513. _valuesStartRepeat[property] = startValueIsArray ? [] : {};
  514. var endValues = _valuesEnd[property];
  515. // If dynamic is not enabled, clone the end values instead of using the passed-in end values.
  516. if (!this._isDynamic) {
  517. var tmp = {};
  518. for (var prop in endValues)
  519. tmp[prop] = endValues[prop];
  520. _valuesEnd[property] = endValues = tmp;
  521. }
  522. this._setupProperties(nestedObject, _valuesStart[property], endValues, _valuesStartRepeat[property], overrideStartingValues);
  523. }
  524. else {
  525. // Save the starting value, but only once unless override is requested.
  526. if (typeof _valuesStart[property] === 'undefined' || overrideStartingValues) {
  527. _valuesStart[property] = startValue;
  528. }
  529. if (!startValueIsArray) {
  530. // eslint-disable-next-line
  531. // @ts-ignore FIXME?
  532. _valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
  533. }
  534. if (isInterpolationList) {
  535. // eslint-disable-next-line
  536. // @ts-ignore FIXME?
  537. _valuesStartRepeat[property] = _valuesEnd[property].slice().reverse();
  538. }
  539. else {
  540. _valuesStartRepeat[property] = _valuesStart[property] || 0;
  541. }
  542. }
  543. }
  544. };
  545. Tween.prototype.stop = function () {
  546. if (!this._isChainStopped) {
  547. this._isChainStopped = true;
  548. this.stopChainedTweens();
  549. }
  550. if (!this._isPlaying) {
  551. return this;
  552. }
  553. // eslint-disable-next-line
  554. this._group && this._group.remove(this);
  555. this._isPlaying = false;
  556. this._isPaused = false;
  557. if (this._onStopCallback) {
  558. this._onStopCallback(this._object);
  559. }
  560. return this;
  561. };
  562. Tween.prototype.end = function () {
  563. this._goToEnd = true;
  564. this.update(Infinity);
  565. return this;
  566. };
  567. Tween.prototype.pause = function (time) {
  568. if (time === void 0) { time = now(); }
  569. if (this._isPaused || !this._isPlaying) {
  570. return this;
  571. }
  572. this._isPaused = true;
  573. this._pauseStart = time;
  574. // eslint-disable-next-line
  575. this._group && this._group.remove(this);
  576. return this;
  577. };
  578. Tween.prototype.resume = function (time) {
  579. if (time === void 0) { time = now(); }
  580. if (!this._isPaused || !this._isPlaying) {
  581. return this;
  582. }
  583. this._isPaused = false;
  584. this._startTime += time - this._pauseStart;
  585. this._pauseStart = 0;
  586. // eslint-disable-next-line
  587. this._group && this._group.add(this);
  588. return this;
  589. };
  590. Tween.prototype.stopChainedTweens = function () {
  591. for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
  592. this._chainedTweens[i].stop();
  593. }
  594. return this;
  595. };
  596. Tween.prototype.group = function (group) {
  597. if (group === void 0) { group = mainGroup; }
  598. this._group = group;
  599. return this;
  600. };
  601. Tween.prototype.delay = function (amount) {
  602. if (amount === void 0) { amount = 0; }
  603. this._delayTime = amount;
  604. return this;
  605. };
  606. Tween.prototype.repeat = function (times) {
  607. if (times === void 0) { times = 0; }
  608. this._initialRepeat = times;
  609. this._repeat = times;
  610. return this;
  611. };
  612. Tween.prototype.repeatDelay = function (amount) {
  613. this._repeatDelayTime = amount;
  614. return this;
  615. };
  616. Tween.prototype.yoyo = function (yoyo) {
  617. if (yoyo === void 0) { yoyo = false; }
  618. this._yoyo = yoyo;
  619. return this;
  620. };
  621. Tween.prototype.easing = function (easingFunction) {
  622. if (easingFunction === void 0) { easingFunction = Easing.Linear.None; }
  623. this._easingFunction = easingFunction;
  624. return this;
  625. };
  626. Tween.prototype.interpolation = function (interpolationFunction) {
  627. if (interpolationFunction === void 0) { interpolationFunction = Interpolation.Linear; }
  628. this._interpolationFunction = interpolationFunction;
  629. return this;
  630. };
  631. // eslint-disable-next-line
  632. Tween.prototype.chain = function () {
  633. var tweens = [];
  634. for (var _i = 0; _i < arguments.length; _i++) {
  635. tweens[_i] = arguments[_i];
  636. }
  637. this._chainedTweens = tweens;
  638. return this;
  639. };
  640. Tween.prototype.onStart = function (callback) {
  641. this._onStartCallback = callback;
  642. return this;
  643. };
  644. Tween.prototype.onEveryStart = function (callback) {
  645. this._onEveryStartCallback = callback;
  646. return this;
  647. };
  648. Tween.prototype.onUpdate = function (callback) {
  649. this._onUpdateCallback = callback;
  650. return this;
  651. };
  652. Tween.prototype.onRepeat = function (callback) {
  653. this._onRepeatCallback = callback;
  654. return this;
  655. };
  656. Tween.prototype.onComplete = function (callback) {
  657. this._onCompleteCallback = callback;
  658. return this;
  659. };
  660. Tween.prototype.onStop = function (callback) {
  661. this._onStopCallback = callback;
  662. return this;
  663. };
  664. /**
  665. * @returns true if the tween is still playing after the update, false
  666. * otherwise (calling update on a paused tween still returns true because
  667. * it is still playing, just paused).
  668. */
  669. Tween.prototype.update = function (time, autoStart) {
  670. if (time === void 0) { time = now(); }
  671. if (autoStart === void 0) { autoStart = true; }
  672. if (this._isPaused)
  673. return true;
  674. var property;
  675. var elapsed;
  676. var endTime = this._startTime + this._duration;
  677. if (!this._goToEnd && !this._isPlaying) {
  678. if (time > endTime)
  679. return false;
  680. if (autoStart)
  681. this.start(time, true);
  682. }
  683. this._goToEnd = false;
  684. if (time < this._startTime) {
  685. return true;
  686. }
  687. if (this._onStartCallbackFired === false) {
  688. if (this._onStartCallback) {
  689. this._onStartCallback(this._object);
  690. }
  691. this._onStartCallbackFired = true;
  692. }
  693. if (this._onEveryStartCallbackFired === false) {
  694. if (this._onEveryStartCallback) {
  695. this._onEveryStartCallback(this._object);
  696. }
  697. this._onEveryStartCallbackFired = true;
  698. }
  699. elapsed = (time - this._startTime) / this._duration;
  700. elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed;
  701. var value = this._easingFunction(elapsed);
  702. // properties transformations
  703. this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
  704. if (this._onUpdateCallback) {
  705. this._onUpdateCallback(this._object, elapsed);
  706. }
  707. if (elapsed === 1) {
  708. if (this._repeat > 0) {
  709. if (isFinite(this._repeat)) {
  710. this._repeat--;
  711. }
  712. // Reassign starting values, restart by making startTime = now
  713. for (property in this._valuesStartRepeat) {
  714. if (!this._yoyo && typeof this._valuesEnd[property] === 'string') {
  715. this._valuesStartRepeat[property] =
  716. // eslint-disable-next-line
  717. // @ts-ignore FIXME?
  718. this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property]);
  719. }
  720. if (this._yoyo) {
  721. this._swapEndStartRepeatValues(property);
  722. }
  723. this._valuesStart[property] = this._valuesStartRepeat[property];
  724. }
  725. if (this._yoyo) {
  726. this._reversed = !this._reversed;
  727. }
  728. if (this._repeatDelayTime !== undefined) {
  729. this._startTime = time + this._repeatDelayTime;
  730. }
  731. else {
  732. this._startTime = time + this._delayTime;
  733. }
  734. if (this._onRepeatCallback) {
  735. this._onRepeatCallback(this._object);
  736. }
  737. this._onEveryStartCallbackFired = false;
  738. return true;
  739. }
  740. else {
  741. if (this._onCompleteCallback) {
  742. this._onCompleteCallback(this._object);
  743. }
  744. for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
  745. // Make the chained tweens start exactly at the time they should,
  746. // even if the `update()` method was called way past the duration of the tween
  747. this._chainedTweens[i].start(this._startTime + this._duration, false);
  748. }
  749. this._isPlaying = false;
  750. return false;
  751. }
  752. }
  753. return true;
  754. };
  755. Tween.prototype._updateProperties = function (_object, _valuesStart, _valuesEnd, value) {
  756. for (var property in _valuesEnd) {
  757. // Don't update properties that do not exist in the source object
  758. if (_valuesStart[property] === undefined) {
  759. continue;
  760. }
  761. var start = _valuesStart[property] || 0;
  762. var end = _valuesEnd[property];
  763. var startIsArray = Array.isArray(_object[property]);
  764. var endIsArray = Array.isArray(end);
  765. var isInterpolationList = !startIsArray && endIsArray;
  766. if (isInterpolationList) {
  767. _object[property] = this._interpolationFunction(end, value);
  768. }
  769. else if (typeof end === 'object' && end) {
  770. // eslint-disable-next-line
  771. // @ts-ignore FIXME?
  772. this._updateProperties(_object[property], start, end, value);
  773. }
  774. else {
  775. // Parses relative end values with start as base (e.g.: +10, -3)
  776. end = this._handleRelativeValue(start, end);
  777. // Protect against non numeric properties.
  778. if (typeof end === 'number') {
  779. // eslint-disable-next-line
  780. // @ts-ignore FIXME?
  781. _object[property] = start + (end - start) * value;
  782. }
  783. }
  784. }
  785. };
  786. Tween.prototype._handleRelativeValue = function (start, end) {
  787. if (typeof end !== 'string') {
  788. return end;
  789. }
  790. if (end.charAt(0) === '+' || end.charAt(0) === '-') {
  791. return start + parseFloat(end);
  792. }
  793. return parseFloat(end);
  794. };
  795. Tween.prototype._swapEndStartRepeatValues = function (property) {
  796. var tmp = this._valuesStartRepeat[property];
  797. var endValue = this._valuesEnd[property];
  798. if (typeof endValue === 'string') {
  799. this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(endValue);
  800. }
  801. else {
  802. this._valuesStartRepeat[property] = this._valuesEnd[property];
  803. }
  804. this._valuesEnd[property] = tmp;
  805. };
  806. return Tween;
  807. }());
  808. var VERSION = '21.0.0';
  809. /**
  810. * Tween.js - Licensed under the MIT license
  811. * https://github.com/tweenjs/tween.js
  812. * ----------------------------------------------
  813. *
  814. * See https://github.com/tweenjs/tween.js/graphs/contributors for the full list of contributors.
  815. * Thank you all, you're awesome!
  816. */
  817. var nextId = Sequence.nextId;
  818. /**
  819. * Controlling groups of tweens
  820. *
  821. * Using the TWEEN singleton to manage your tweens can cause issues in large apps with many components.
  822. * In these cases, you may want to create your own smaller groups of tweens.
  823. */
  824. var TWEEN = mainGroup;
  825. // This is the best way to export things in a way that's compatible with both ES
  826. // Modules and CommonJS, without build hacks, and so as not to break the
  827. // existing API.
  828. // https://github.com/rollup/rollup/issues/1961#issuecomment-423037881
  829. var getAll = TWEEN.getAll.bind(TWEEN);
  830. var removeAll = TWEEN.removeAll.bind(TWEEN);
  831. var add = TWEEN.add.bind(TWEEN);
  832. var remove = TWEEN.remove.bind(TWEEN);
  833. var update = TWEEN.update.bind(TWEEN);
  834. var exports = {
  835. Easing: Easing,
  836. Group: Group,
  837. Interpolation: Interpolation,
  838. now: now,
  839. Sequence: Sequence,
  840. nextId: nextId,
  841. Tween: Tween,
  842. VERSION: VERSION,
  843. getAll: getAll,
  844. removeAll: removeAll,
  845. add: add,
  846. remove: remove,
  847. update: update,
  848. };
  849. export { Easing, Group, Interpolation, Sequence, Tween, VERSION, add, exports as default, getAll, nextId, now, remove, removeAll, update };