tween.module.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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.getDuration = function () {
  411. return this._duration;
  412. };
  413. Tween.prototype.to = function (target, duration) {
  414. if (duration === void 0) { duration = 1000; }
  415. if (this._isPlaying)
  416. throw new Error('Can not call Tween.to() while Tween is already started or paused. Stop the Tween first.');
  417. this._valuesEnd = target;
  418. this._propertiesAreSetUp = false;
  419. this._duration = duration < 0 ? 0 : duration;
  420. return this;
  421. };
  422. Tween.prototype.duration = function (duration) {
  423. if (duration === void 0) { duration = 1000; }
  424. this._duration = duration < 0 ? 0 : duration;
  425. return this;
  426. };
  427. Tween.prototype.dynamic = function (dynamic) {
  428. if (dynamic === void 0) { dynamic = false; }
  429. this._isDynamic = dynamic;
  430. return this;
  431. };
  432. Tween.prototype.start = function (time, overrideStartingValues) {
  433. if (time === void 0) { time = now(); }
  434. if (overrideStartingValues === void 0) { overrideStartingValues = false; }
  435. if (this._isPlaying) {
  436. return this;
  437. }
  438. // eslint-disable-next-line
  439. this._group && this._group.add(this);
  440. this._repeat = this._initialRepeat;
  441. if (this._reversed) {
  442. // If we were reversed (f.e. using the yoyo feature) then we need to
  443. // flip the tween direction back to forward.
  444. this._reversed = false;
  445. for (var property in this._valuesStartRepeat) {
  446. this._swapEndStartRepeatValues(property);
  447. this._valuesStart[property] = this._valuesStartRepeat[property];
  448. }
  449. }
  450. this._isPlaying = true;
  451. this._isPaused = false;
  452. this._onStartCallbackFired = false;
  453. this._onEveryStartCallbackFired = false;
  454. this._isChainStopped = false;
  455. this._startTime = time;
  456. this._startTime += this._delayTime;
  457. if (!this._propertiesAreSetUp || overrideStartingValues) {
  458. this._propertiesAreSetUp = true;
  459. // If dynamic is not enabled, clone the end values instead of using the passed-in end values.
  460. if (!this._isDynamic) {
  461. var tmp = {};
  462. for (var prop in this._valuesEnd)
  463. tmp[prop] = this._valuesEnd[prop];
  464. this._valuesEnd = tmp;
  465. }
  466. this._setupProperties(this._object, this._valuesStart, this._valuesEnd, this._valuesStartRepeat, overrideStartingValues);
  467. }
  468. return this;
  469. };
  470. Tween.prototype.startFromCurrentValues = function (time) {
  471. return this.start(time, true);
  472. };
  473. Tween.prototype._setupProperties = function (_object, _valuesStart, _valuesEnd, _valuesStartRepeat, overrideStartingValues) {
  474. for (var property in _valuesEnd) {
  475. var startValue = _object[property];
  476. var startValueIsArray = Array.isArray(startValue);
  477. var propType = startValueIsArray ? 'array' : typeof startValue;
  478. var isInterpolationList = !startValueIsArray && Array.isArray(_valuesEnd[property]);
  479. // If `to()` specifies a property that doesn't exist in the source object,
  480. // we should not set that property in the object
  481. if (propType === 'undefined' || propType === 'function') {
  482. continue;
  483. }
  484. // Check if an Array was provided as property value
  485. if (isInterpolationList) {
  486. var endValues = _valuesEnd[property];
  487. if (endValues.length === 0) {
  488. continue;
  489. }
  490. // Handle an array of relative values.
  491. // Creates a local copy of the Array with the start value at the front
  492. var temp = [startValue];
  493. for (var i = 0, l = endValues.length; i < l; i += 1) {
  494. var value = this._handleRelativeValue(startValue, endValues[i]);
  495. if (isNaN(value)) {
  496. isInterpolationList = false;
  497. console.warn('Found invalid interpolation list. Skipping.');
  498. break;
  499. }
  500. temp.push(value);
  501. }
  502. if (isInterpolationList) {
  503. // if (_valuesStart[property] === undefined) { // handle end values only the first time. NOT NEEDED? setupProperties is now guarded by _propertiesAreSetUp.
  504. _valuesEnd[property] = temp;
  505. // }
  506. }
  507. }
  508. // handle the deepness of the values
  509. if ((propType === 'object' || startValueIsArray) && startValue && !isInterpolationList) {
  510. _valuesStart[property] = startValueIsArray ? [] : {};
  511. var nestedObject = startValue;
  512. for (var prop in nestedObject) {
  513. _valuesStart[property][prop] = nestedObject[prop];
  514. }
  515. // TODO? repeat nested values? And yoyo? And array values?
  516. _valuesStartRepeat[property] = startValueIsArray ? [] : {};
  517. var endValues = _valuesEnd[property];
  518. // If dynamic is not enabled, clone the end values instead of using the passed-in end values.
  519. if (!this._isDynamic) {
  520. var tmp = {};
  521. for (var prop in endValues)
  522. tmp[prop] = endValues[prop];
  523. _valuesEnd[property] = endValues = tmp;
  524. }
  525. this._setupProperties(nestedObject, _valuesStart[property], endValues, _valuesStartRepeat[property], overrideStartingValues);
  526. }
  527. else {
  528. // Save the starting value, but only once unless override is requested.
  529. if (typeof _valuesStart[property] === 'undefined' || overrideStartingValues) {
  530. _valuesStart[property] = startValue;
  531. }
  532. if (!startValueIsArray) {
  533. // eslint-disable-next-line
  534. // @ts-ignore FIXME?
  535. _valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
  536. }
  537. if (isInterpolationList) {
  538. // eslint-disable-next-line
  539. // @ts-ignore FIXME?
  540. _valuesStartRepeat[property] = _valuesEnd[property].slice().reverse();
  541. }
  542. else {
  543. _valuesStartRepeat[property] = _valuesStart[property] || 0;
  544. }
  545. }
  546. }
  547. };
  548. Tween.prototype.stop = function () {
  549. if (!this._isChainStopped) {
  550. this._isChainStopped = true;
  551. this.stopChainedTweens();
  552. }
  553. if (!this._isPlaying) {
  554. return this;
  555. }
  556. // eslint-disable-next-line
  557. this._group && this._group.remove(this);
  558. this._isPlaying = false;
  559. this._isPaused = false;
  560. if (this._onStopCallback) {
  561. this._onStopCallback(this._object);
  562. }
  563. return this;
  564. };
  565. Tween.prototype.end = function () {
  566. this._goToEnd = true;
  567. this.update(Infinity);
  568. return this;
  569. };
  570. Tween.prototype.pause = function (time) {
  571. if (time === void 0) { time = now(); }
  572. if (this._isPaused || !this._isPlaying) {
  573. return this;
  574. }
  575. this._isPaused = true;
  576. this._pauseStart = time;
  577. // eslint-disable-next-line
  578. this._group && this._group.remove(this);
  579. return this;
  580. };
  581. Tween.prototype.resume = function (time) {
  582. if (time === void 0) { time = now(); }
  583. if (!this._isPaused || !this._isPlaying) {
  584. return this;
  585. }
  586. this._isPaused = false;
  587. this._startTime += time - this._pauseStart;
  588. this._pauseStart = 0;
  589. // eslint-disable-next-line
  590. this._group && this._group.add(this);
  591. return this;
  592. };
  593. Tween.prototype.stopChainedTweens = function () {
  594. for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
  595. this._chainedTweens[i].stop();
  596. }
  597. return this;
  598. };
  599. Tween.prototype.group = function (group) {
  600. if (group === void 0) { group = mainGroup; }
  601. this._group = group;
  602. return this;
  603. };
  604. Tween.prototype.delay = function (amount) {
  605. if (amount === void 0) { amount = 0; }
  606. this._delayTime = amount;
  607. return this;
  608. };
  609. Tween.prototype.repeat = function (times) {
  610. if (times === void 0) { times = 0; }
  611. this._initialRepeat = times;
  612. this._repeat = times;
  613. return this;
  614. };
  615. Tween.prototype.repeatDelay = function (amount) {
  616. this._repeatDelayTime = amount;
  617. return this;
  618. };
  619. Tween.prototype.yoyo = function (yoyo) {
  620. if (yoyo === void 0) { yoyo = false; }
  621. this._yoyo = yoyo;
  622. return this;
  623. };
  624. Tween.prototype.easing = function (easingFunction) {
  625. if (easingFunction === void 0) { easingFunction = Easing.Linear.None; }
  626. this._easingFunction = easingFunction;
  627. return this;
  628. };
  629. Tween.prototype.interpolation = function (interpolationFunction) {
  630. if (interpolationFunction === void 0) { interpolationFunction = Interpolation.Linear; }
  631. this._interpolationFunction = interpolationFunction;
  632. return this;
  633. };
  634. // eslint-disable-next-line
  635. Tween.prototype.chain = function () {
  636. var tweens = [];
  637. for (var _i = 0; _i < arguments.length; _i++) {
  638. tweens[_i] = arguments[_i];
  639. }
  640. this._chainedTweens = tweens;
  641. return this;
  642. };
  643. Tween.prototype.onStart = function (callback) {
  644. this._onStartCallback = callback;
  645. return this;
  646. };
  647. Tween.prototype.onEveryStart = function (callback) {
  648. this._onEveryStartCallback = callback;
  649. return this;
  650. };
  651. Tween.prototype.onUpdate = function (callback) {
  652. this._onUpdateCallback = callback;
  653. return this;
  654. };
  655. Tween.prototype.onRepeat = function (callback) {
  656. this._onRepeatCallback = callback;
  657. return this;
  658. };
  659. Tween.prototype.onComplete = function (callback) {
  660. this._onCompleteCallback = callback;
  661. return this;
  662. };
  663. Tween.prototype.onStop = function (callback) {
  664. this._onStopCallback = callback;
  665. return this;
  666. };
  667. /**
  668. * @returns true if the tween is still playing after the update, false
  669. * otherwise (calling update on a paused tween still returns true because
  670. * it is still playing, just paused).
  671. */
  672. Tween.prototype.update = function (time, autoStart) {
  673. var _this = this;
  674. var _a;
  675. if (time === void 0) { time = now(); }
  676. if (autoStart === void 0) { autoStart = true; }
  677. if (this._isPaused)
  678. return true;
  679. var property;
  680. var endTime = this._startTime + this._duration;
  681. if (!this._goToEnd && !this._isPlaying) {
  682. if (time > endTime)
  683. return false;
  684. if (autoStart)
  685. this.start(time, true);
  686. }
  687. this._goToEnd = false;
  688. if (time < this._startTime) {
  689. return true;
  690. }
  691. if (this._onStartCallbackFired === false) {
  692. if (this._onStartCallback) {
  693. this._onStartCallback(this._object);
  694. }
  695. this._onStartCallbackFired = true;
  696. }
  697. if (this._onEveryStartCallbackFired === false) {
  698. if (this._onEveryStartCallback) {
  699. this._onEveryStartCallback(this._object);
  700. }
  701. this._onEveryStartCallbackFired = true;
  702. }
  703. var elapsedTime = time - this._startTime;
  704. var durationAndDelay = this._duration + ((_a = this._repeatDelayTime) !== null && _a !== void 0 ? _a : this._delayTime);
  705. var totalTime = this._duration + this._repeat * durationAndDelay;
  706. var calculateElapsedPortion = function () {
  707. if (_this._duration === 0)
  708. return 1;
  709. if (elapsedTime > totalTime) {
  710. return 1;
  711. }
  712. var timesRepeated = Math.trunc(elapsedTime / durationAndDelay);
  713. var timeIntoCurrentRepeat = elapsedTime - timesRepeated * durationAndDelay;
  714. // TODO use %?
  715. // const timeIntoCurrentRepeat = elapsedTime % durationAndDelay
  716. var portion = Math.min(timeIntoCurrentRepeat / _this._duration, 1);
  717. if (portion === 0 && elapsedTime === _this._duration) {
  718. return 1;
  719. }
  720. return portion;
  721. };
  722. var elapsed = calculateElapsedPortion();
  723. var value = this._easingFunction(elapsed);
  724. // properties transformations
  725. this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
  726. if (this._onUpdateCallback) {
  727. this._onUpdateCallback(this._object, elapsed);
  728. }
  729. if (this._duration === 0 || elapsedTime >= this._duration) {
  730. if (this._repeat > 0) {
  731. var completeCount = Math.min(Math.trunc((elapsedTime - this._duration) / durationAndDelay) + 1, this._repeat);
  732. if (isFinite(this._repeat)) {
  733. this._repeat -= completeCount;
  734. }
  735. // Reassign starting values, restart by making startTime = now
  736. for (property in this._valuesStartRepeat) {
  737. if (!this._yoyo && typeof this._valuesEnd[property] === 'string') {
  738. this._valuesStartRepeat[property] =
  739. // eslint-disable-next-line
  740. // @ts-ignore FIXME?
  741. this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property]);
  742. }
  743. if (this._yoyo) {
  744. this._swapEndStartRepeatValues(property);
  745. }
  746. this._valuesStart[property] = this._valuesStartRepeat[property];
  747. }
  748. if (this._yoyo) {
  749. this._reversed = !this._reversed;
  750. }
  751. this._startTime += durationAndDelay * completeCount;
  752. if (this._onRepeatCallback) {
  753. this._onRepeatCallback(this._object);
  754. }
  755. this._onEveryStartCallbackFired = false;
  756. return true;
  757. }
  758. else {
  759. if (this._onCompleteCallback) {
  760. this._onCompleteCallback(this._object);
  761. }
  762. for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
  763. // Make the chained tweens start exactly at the time they should,
  764. // even if the `update()` method was called way past the duration of the tween
  765. this._chainedTweens[i].start(this._startTime + this._duration, false);
  766. }
  767. this._isPlaying = false;
  768. return false;
  769. }
  770. }
  771. return true;
  772. };
  773. Tween.prototype._updateProperties = function (_object, _valuesStart, _valuesEnd, value) {
  774. for (var property in _valuesEnd) {
  775. // Don't update properties that do not exist in the source object
  776. if (_valuesStart[property] === undefined) {
  777. continue;
  778. }
  779. var start = _valuesStart[property] || 0;
  780. var end = _valuesEnd[property];
  781. var startIsArray = Array.isArray(_object[property]);
  782. var endIsArray = Array.isArray(end);
  783. var isInterpolationList = !startIsArray && endIsArray;
  784. if (isInterpolationList) {
  785. _object[property] = this._interpolationFunction(end, value);
  786. }
  787. else if (typeof end === 'object' && end) {
  788. // eslint-disable-next-line
  789. // @ts-ignore FIXME?
  790. this._updateProperties(_object[property], start, end, value);
  791. }
  792. else {
  793. // Parses relative end values with start as base (e.g.: +10, -3)
  794. end = this._handleRelativeValue(start, end);
  795. // Protect against non numeric properties.
  796. if (typeof end === 'number') {
  797. // eslint-disable-next-line
  798. // @ts-ignore FIXME?
  799. _object[property] = start + (end - start) * value;
  800. }
  801. }
  802. }
  803. };
  804. Tween.prototype._handleRelativeValue = function (start, end) {
  805. if (typeof end !== 'string') {
  806. return end;
  807. }
  808. if (end.charAt(0) === '+' || end.charAt(0) === '-') {
  809. return start + parseFloat(end);
  810. }
  811. return parseFloat(end);
  812. };
  813. Tween.prototype._swapEndStartRepeatValues = function (property) {
  814. var tmp = this._valuesStartRepeat[property];
  815. var endValue = this._valuesEnd[property];
  816. if (typeof endValue === 'string') {
  817. this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(endValue);
  818. }
  819. else {
  820. this._valuesStartRepeat[property] = this._valuesEnd[property];
  821. }
  822. this._valuesEnd[property] = tmp;
  823. };
  824. return Tween;
  825. }());
  826. var VERSION = '23.1.1';
  827. /**
  828. * Tween.js - Licensed under the MIT license
  829. * https://github.com/tweenjs/tween.js
  830. * ----------------------------------------------
  831. *
  832. * See https://github.com/tweenjs/tween.js/graphs/contributors for the full list of contributors.
  833. * Thank you all, you're awesome!
  834. */
  835. var nextId = Sequence.nextId;
  836. /**
  837. * Controlling groups of tweens
  838. *
  839. * Using the TWEEN singleton to manage your tweens can cause issues in large apps with many components.
  840. * In these cases, you may want to create your own smaller groups of tweens.
  841. */
  842. var TWEEN = mainGroup;
  843. // This is the best way to export things in a way that's compatible with both ES
  844. // Modules and CommonJS, without build hacks, and so as not to break the
  845. // existing API.
  846. // https://github.com/rollup/rollup/issues/1961#issuecomment-423037881
  847. var getAll = TWEEN.getAll.bind(TWEEN);
  848. var removeAll = TWEEN.removeAll.bind(TWEEN);
  849. var add = TWEEN.add.bind(TWEEN);
  850. var remove = TWEEN.remove.bind(TWEEN);
  851. var update = TWEEN.update.bind(TWEEN);
  852. var exports = {
  853. Easing: Easing,
  854. Group: Group,
  855. Interpolation: Interpolation,
  856. now: now,
  857. Sequence: Sequence,
  858. nextId: nextId,
  859. Tween: Tween,
  860. VERSION: VERSION,
  861. getAll: getAll,
  862. removeAll: removeAll,
  863. add: add,
  864. remove: remove,
  865. update: update,
  866. };
  867. export { Easing, Group, Interpolation, Sequence, Tween, VERSION, add, exports as default, getAll, nextId, now, remove, removeAll, update };