tween.module.js 23 KB

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