tween.umd.js 35 KB

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