@tweenjs/tween.js 18.6.4 → 20.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tween.esm.js CHANGED
@@ -1,13 +1,22 @@
1
1
  /**
2
2
  * The Ease class provides a collection of easing functions for use with tween.js.
3
3
  */
4
- var Easing = {
5
- Linear: {
4
+ var Easing = Object.freeze({
5
+ Linear: Object.freeze({
6
6
  None: function (amount) {
7
7
  return amount;
8
8
  },
9
- },
10
- Quadratic: {
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({
11
20
  In: function (amount) {
12
21
  return amount * amount;
13
22
  },
@@ -20,8 +29,8 @@ var Easing = {
20
29
  }
21
30
  return -0.5 * (--amount * (amount - 2) - 1);
22
31
  },
23
- },
24
- Cubic: {
32
+ }),
33
+ Cubic: Object.freeze({
25
34
  In: function (amount) {
26
35
  return amount * amount * amount;
27
36
  },
@@ -34,8 +43,8 @@ var Easing = {
34
43
  }
35
44
  return 0.5 * ((amount -= 2) * amount * amount + 2);
36
45
  },
37
- },
38
- Quartic: {
46
+ }),
47
+ Quartic: Object.freeze({
39
48
  In: function (amount) {
40
49
  return amount * amount * amount * amount;
41
50
  },
@@ -48,8 +57,8 @@ var Easing = {
48
57
  }
49
58
  return -0.5 * ((amount -= 2) * amount * amount * amount - 2);
50
59
  },
51
- },
52
- Quintic: {
60
+ }),
61
+ Quintic: Object.freeze({
53
62
  In: function (amount) {
54
63
  return amount * amount * amount * amount * amount;
55
64
  },
@@ -62,19 +71,19 @@ var Easing = {
62
71
  }
63
72
  return 0.5 * ((amount -= 2) * amount * amount * amount * amount + 2);
64
73
  },
65
- },
66
- Sinusoidal: {
74
+ }),
75
+ Sinusoidal: Object.freeze({
67
76
  In: function (amount) {
68
- return 1 - Math.cos((amount * Math.PI) / 2);
77
+ return 1 - Math.sin(((1.0 - amount) * Math.PI) / 2);
69
78
  },
70
79
  Out: function (amount) {
71
80
  return Math.sin((amount * Math.PI) / 2);
72
81
  },
73
82
  InOut: function (amount) {
74
- return 0.5 * (1 - Math.cos(Math.PI * amount));
83
+ return 0.5 * (1 - Math.sin(Math.PI * (0.5 - amount)));
75
84
  },
76
- },
77
- Exponential: {
85
+ }),
86
+ Exponential: Object.freeze({
78
87
  In: function (amount) {
79
88
  return amount === 0 ? 0 : Math.pow(1024, amount - 1);
80
89
  },
@@ -93,8 +102,8 @@ var Easing = {
93
102
  }
94
103
  return 0.5 * (-Math.pow(2, -10 * (amount - 1)) + 2);
95
104
  },
96
- },
97
- Circular: {
105
+ }),
106
+ Circular: Object.freeze({
98
107
  In: function (amount) {
99
108
  return 1 - Math.sqrt(1 - amount * amount);
100
109
  },
@@ -107,8 +116,8 @@ var Easing = {
107
116
  }
108
117
  return 0.5 * (Math.sqrt(1 - (amount -= 2) * amount) + 1);
109
118
  },
110
- },
111
- Elastic: {
119
+ }),
120
+ Elastic: Object.freeze({
112
121
  In: function (amount) {
113
122
  if (amount === 0) {
114
123
  return 0;
@@ -140,15 +149,15 @@ var Easing = {
140
149
  }
141
150
  return 0.5 * Math.pow(2, -10 * (amount - 1)) * Math.sin((amount - 1.1) * 5 * Math.PI) + 1;
142
151
  },
143
- },
144
- Back: {
152
+ }),
153
+ Back: Object.freeze({
145
154
  In: function (amount) {
146
155
  var s = 1.70158;
147
- return amount * amount * ((s + 1) * amount - s);
156
+ return amount === 1 ? 1 : amount * amount * ((s + 1) * amount - s);
148
157
  },
149
158
  Out: function (amount) {
150
159
  var s = 1.70158;
151
- return --amount * amount * ((s + 1) * amount + s) + 1;
160
+ return amount === 0 ? 0 : --amount * amount * ((s + 1) * amount + s) + 1;
152
161
  },
153
162
  InOut: function (amount) {
154
163
  var s = 1.70158 * 1.525;
@@ -157,8 +166,8 @@ var Easing = {
157
166
  }
158
167
  return 0.5 * ((amount -= 2) * amount * ((s + 1) * amount + s) + 2);
159
168
  },
160
- },
161
- Bounce: {
169
+ }),
170
+ Bounce: Object.freeze({
162
171
  In: function (amount) {
163
172
  return 1 - Easing.Bounce.Out(1 - amount);
164
173
  },
@@ -182,40 +191,29 @@ var Easing = {
182
191
  }
183
192
  return Easing.Bounce.Out(amount * 2 - 1) * 0.5 + 0.5;
184
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
+ };
185
213
  },
186
- };
214
+ });
187
215
 
188
- var now;
189
- // Include a performance.now polyfill.
190
- // In node.js, use process.hrtime.
191
- // eslint-disable-next-line
192
- // @ts-ignore
193
- if (typeof self === 'undefined' && typeof process !== 'undefined' && process.hrtime) {
194
- now = function () {
195
- // eslint-disable-next-line
196
- // @ts-ignore
197
- var time = process.hrtime();
198
- // Convert [seconds, nanoseconds] to milliseconds.
199
- return time[0] * 1000 + time[1] / 1000000;
200
- };
201
- }
202
- // In a browser, use self.performance.now if it is available.
203
- else if (typeof self !== 'undefined' && self.performance !== undefined && self.performance.now !== undefined) {
204
- // This must be bound, because directly assigning this function
205
- // leads to an invocation exception in Chrome.
206
- now = self.performance.now.bind(self.performance);
207
- }
208
- // Use Date.now if it is available.
209
- else if (Date.now !== undefined) {
210
- now = Date.now;
211
- }
212
- // Otherwise, use 'new Date().getTime()'.
213
- else {
214
- now = function () {
215
- return new Date().getTime();
216
- };
217
- }
218
- var now$1 = now;
216
+ var now = function () { return performance.now(); };
219
217
 
220
218
  /**
221
219
  * Controlling groups of tweens
@@ -246,7 +244,7 @@ var Group = /** @class */ (function () {
246
244
  delete this._tweensAddedDuringUpdate[tween.getId()];
247
245
  };
248
246
  Group.prototype.update = function (time, preserve) {
249
- if (time === void 0) { time = now$1(); }
247
+ if (time === void 0) { time = now(); }
250
248
  if (preserve === void 0) { preserve = false; }
251
249
  var tweenIds = Object.keys(this._tweens);
252
250
  if (tweenIds.length === 0) {
@@ -387,6 +385,7 @@ var Tween = /** @class */ (function () {
387
385
  this._valuesEnd = {};
388
386
  this._valuesStartRepeat = {};
389
387
  this._duration = 1000;
388
+ this._isDynamic = false;
390
389
  this._initialRepeat = 0;
391
390
  this._repeat = 0;
392
391
  this._yoyo = false;
@@ -396,10 +395,13 @@ var Tween = /** @class */ (function () {
396
395
  this._startTime = 0;
397
396
  this._easingFunction = Easing.Linear.None;
398
397
  this._interpolationFunction = Interpolation.Linear;
398
+ // eslint-disable-next-line
399
399
  this._chainedTweens = [];
400
400
  this._onStartCallbackFired = false;
401
+ this._onEveryStartCallbackFired = false;
401
402
  this._id = Sequence.nextId();
402
403
  this._isChainStopped = false;
404
+ this._propertiesAreSetUp = false;
403
405
  this._goToEnd = false;
404
406
  }
405
407
  Tween.prototype.getId = function () {
@@ -411,22 +413,28 @@ var Tween = /** @class */ (function () {
411
413
  Tween.prototype.isPaused = function () {
412
414
  return this._isPaused;
413
415
  };
414
- Tween.prototype.to = function (properties, duration) {
415
- // TODO? restore this, then update the 07_dynamic_to example to set fox
416
- // tween's to on each update. That way the behavior is opt-in (there's
417
- // currently no opt-out).
418
- // for (const prop in properties) this._valuesEnd[prop] = properties[prop]
419
- this._valuesEnd = Object.create(properties);
420
- if (duration !== undefined) {
421
- this._duration = duration;
422
- }
416
+ Tween.prototype.to = function (target, duration) {
417
+ if (duration === void 0) { duration = 1000; }
418
+ if (this._isPlaying)
419
+ throw new Error('Can not call Tween.to() while Tween is already started or paused. Stop the Tween first.');
420
+ this._valuesEnd = target;
421
+ this._propertiesAreSetUp = false;
422
+ this._duration = duration;
423
423
  return this;
424
424
  };
425
- Tween.prototype.duration = function (d) {
426
- this._duration = d;
425
+ Tween.prototype.duration = function (duration) {
426
+ if (duration === void 0) { duration = 1000; }
427
+ this._duration = duration;
427
428
  return this;
428
429
  };
429
- Tween.prototype.start = function (time) {
430
+ Tween.prototype.dynamic = function (dynamic) {
431
+ if (dynamic === void 0) { dynamic = false; }
432
+ this._isDynamic = dynamic;
433
+ return this;
434
+ };
435
+ Tween.prototype.start = function (time, overrideStartingValues) {
436
+ if (time === void 0) { time = now(); }
437
+ if (overrideStartingValues === void 0) { overrideStartingValues = false; }
430
438
  if (this._isPlaying) {
431
439
  return this;
432
440
  }
@@ -445,13 +453,27 @@ var Tween = /** @class */ (function () {
445
453
  this._isPlaying = true;
446
454
  this._isPaused = false;
447
455
  this._onStartCallbackFired = false;
456
+ this._onEveryStartCallbackFired = false;
448
457
  this._isChainStopped = false;
449
- this._startTime = time !== undefined ? (typeof time === 'string' ? now$1() + parseFloat(time) : time) : now$1();
458
+ this._startTime = time;
450
459
  this._startTime += this._delayTime;
451
- this._setupProperties(this._object, this._valuesStart, this._valuesEnd, this._valuesStartRepeat);
460
+ if (!this._propertiesAreSetUp || overrideStartingValues) {
461
+ this._propertiesAreSetUp = true;
462
+ // If dynamic is not enabled, clone the end values instead of using the passed-in end values.
463
+ if (!this._isDynamic) {
464
+ var tmp = {};
465
+ for (var prop in this._valuesEnd)
466
+ tmp[prop] = this._valuesEnd[prop];
467
+ this._valuesEnd = tmp;
468
+ }
469
+ this._setupProperties(this._object, this._valuesStart, this._valuesEnd, this._valuesStartRepeat, overrideStartingValues);
470
+ }
452
471
  return this;
453
472
  };
454
- Tween.prototype._setupProperties = function (_object, _valuesStart, _valuesEnd, _valuesStartRepeat) {
473
+ Tween.prototype.startFromCurrentValues = function (time) {
474
+ return this.start(time, true);
475
+ };
476
+ Tween.prototype._setupProperties = function (_object, _valuesStart, _valuesEnd, _valuesStartRepeat, overrideStartingValues) {
455
477
  for (var property in _valuesEnd) {
456
478
  var startValue = _object[property];
457
479
  var startValueIsArray = Array.isArray(startValue);
@@ -468,28 +490,46 @@ var Tween = /** @class */ (function () {
468
490
  if (endValues.length === 0) {
469
491
  continue;
470
492
  }
471
- // handle an array of relative values
472
- endValues = endValues.map(this._handleRelativeValue.bind(this, startValue));
473
- // Create a local copy of the Array with the start value at the front
474
- _valuesEnd[property] = [startValue].concat(endValues);
493
+ // Handle an array of relative values.
494
+ // Creates a local copy of the Array with the start value at the front
495
+ var temp = [startValue];
496
+ for (var i = 0, l = endValues.length; i < l; i += 1) {
497
+ var value = this._handleRelativeValue(startValue, endValues[i]);
498
+ if (isNaN(value)) {
499
+ isInterpolationList = false;
500
+ console.warn('Found invalid interpolation list. Skipping.');
501
+ break;
502
+ }
503
+ temp.push(value);
504
+ }
505
+ if (isInterpolationList) {
506
+ // if (_valuesStart[property] === undefined) { // handle end values only the first time. NOT NEEDED? setupProperties is now guarded by _propertiesAreSetUp.
507
+ _valuesEnd[property] = temp;
508
+ // }
509
+ }
475
510
  }
476
511
  // handle the deepness of the values
477
512
  if ((propType === 'object' || startValueIsArray) && startValue && !isInterpolationList) {
478
513
  _valuesStart[property] = startValueIsArray ? [] : {};
479
- // eslint-disable-next-line
480
- for (var prop in startValue) {
481
- // eslint-disable-next-line
482
- // @ts-ignore FIXME?
483
- _valuesStart[property][prop] = startValue[prop];
514
+ var nestedObject = startValue;
515
+ for (var prop in nestedObject) {
516
+ _valuesStart[property][prop] = nestedObject[prop];
484
517
  }
485
- _valuesStartRepeat[property] = startValueIsArray ? [] : {}; // TODO? repeat nested values? And yoyo? And array values?
486
- // eslint-disable-next-line
487
- // @ts-ignore FIXME?
488
- this._setupProperties(startValue, _valuesStart[property], _valuesEnd[property], _valuesStartRepeat[property]);
518
+ // TODO? repeat nested values? And yoyo? And array values?
519
+ _valuesStartRepeat[property] = startValueIsArray ? [] : {};
520
+ var endValues = _valuesEnd[property];
521
+ // If dynamic is not enabled, clone the end values instead of using the passed-in end values.
522
+ if (!this._isDynamic) {
523
+ var tmp = {};
524
+ for (var prop in endValues)
525
+ tmp[prop] = endValues[prop];
526
+ _valuesEnd[property] = endValues = tmp;
527
+ }
528
+ this._setupProperties(nestedObject, _valuesStart[property], endValues, _valuesStartRepeat[property], overrideStartingValues);
489
529
  }
490
530
  else {
491
- // Save the starting value, but only once.
492
- if (typeof _valuesStart[property] === 'undefined') {
531
+ // Save the starting value, but only once unless override is requested.
532
+ if (typeof _valuesStart[property] === 'undefined' || overrideStartingValues) {
493
533
  _valuesStart[property] = startValue;
494
534
  }
495
535
  if (!startValueIsArray) {
@@ -531,7 +571,7 @@ var Tween = /** @class */ (function () {
531
571
  return this;
532
572
  };
533
573
  Tween.prototype.pause = function (time) {
534
- if (time === void 0) { time = now$1(); }
574
+ if (time === void 0) { time = now(); }
535
575
  if (this._isPaused || !this._isPlaying) {
536
576
  return this;
537
577
  }
@@ -542,7 +582,7 @@ var Tween = /** @class */ (function () {
542
582
  return this;
543
583
  };
544
584
  Tween.prototype.resume = function (time) {
545
- if (time === void 0) { time = now$1(); }
585
+ if (time === void 0) { time = now(); }
546
586
  if (!this._isPaused || !this._isPlaying) {
547
587
  return this;
548
588
  }
@@ -560,14 +600,17 @@ var Tween = /** @class */ (function () {
560
600
  return this;
561
601
  };
562
602
  Tween.prototype.group = function (group) {
603
+ if (group === void 0) { group = mainGroup; }
563
604
  this._group = group;
564
605
  return this;
565
606
  };
566
607
  Tween.prototype.delay = function (amount) {
608
+ if (amount === void 0) { amount = 0; }
567
609
  this._delayTime = amount;
568
610
  return this;
569
611
  };
570
612
  Tween.prototype.repeat = function (times) {
613
+ if (times === void 0) { times = 0; }
571
614
  this._initialRepeat = times;
572
615
  this._repeat = times;
573
616
  return this;
@@ -577,17 +620,21 @@ var Tween = /** @class */ (function () {
577
620
  return this;
578
621
  };
579
622
  Tween.prototype.yoyo = function (yoyo) {
623
+ if (yoyo === void 0) { yoyo = false; }
580
624
  this._yoyo = yoyo;
581
625
  return this;
582
626
  };
583
627
  Tween.prototype.easing = function (easingFunction) {
628
+ if (easingFunction === void 0) { easingFunction = Easing.Linear.None; }
584
629
  this._easingFunction = easingFunction;
585
630
  return this;
586
631
  };
587
632
  Tween.prototype.interpolation = function (interpolationFunction) {
633
+ if (interpolationFunction === void 0) { interpolationFunction = Interpolation.Linear; }
588
634
  this._interpolationFunction = interpolationFunction;
589
635
  return this;
590
636
  };
637
+ // eslint-disable-next-line
591
638
  Tween.prototype.chain = function () {
592
639
  var tweens = [];
593
640
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -600,6 +647,10 @@ var Tween = /** @class */ (function () {
600
647
  this._onStartCallback = callback;
601
648
  return this;
602
649
  };
650
+ Tween.prototype.onEveryStart = function (callback) {
651
+ this._onEveryStartCallback = callback;
652
+ return this;
653
+ };
603
654
  Tween.prototype.onUpdate = function (callback) {
604
655
  this._onUpdateCallback = callback;
605
656
  return this;
@@ -622,7 +673,7 @@ var Tween = /** @class */ (function () {
622
673
  * it is still playing, just paused).
623
674
  */
624
675
  Tween.prototype.update = function (time, autoStart) {
625
- if (time === void 0) { time = now$1(); }
676
+ if (time === void 0) { time = now(); }
626
677
  if (autoStart === void 0) { autoStart = true; }
627
678
  if (this._isPaused)
628
679
  return true;
@@ -633,7 +684,7 @@ var Tween = /** @class */ (function () {
633
684
  if (time > endTime)
634
685
  return false;
635
686
  if (autoStart)
636
- this.start(time);
687
+ this.start(time, true);
637
688
  }
638
689
  this._goToEnd = false;
639
690
  if (time < this._startTime) {
@@ -645,6 +696,12 @@ var Tween = /** @class */ (function () {
645
696
  }
646
697
  this._onStartCallbackFired = true;
647
698
  }
699
+ if (this._onEveryStartCallbackFired === false) {
700
+ if (this._onEveryStartCallback) {
701
+ this._onEveryStartCallback(this._object);
702
+ }
703
+ this._onEveryStartCallbackFired = true;
704
+ }
648
705
  elapsed = (time - this._startTime) / this._duration;
649
706
  elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed;
650
707
  var value = this._easingFunction(elapsed);
@@ -683,6 +740,7 @@ var Tween = /** @class */ (function () {
683
740
  if (this._onRepeatCallback) {
684
741
  this._onRepeatCallback(this._object);
685
742
  }
743
+ this._onEveryStartCallbackFired = false;
686
744
  return true;
687
745
  }
688
746
  else {
@@ -692,7 +750,7 @@ var Tween = /** @class */ (function () {
692
750
  for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
693
751
  // Make the chained tweens start exactly at the time they should,
694
752
  // even if the `update()` method was called way past the duration of the tween
695
- this._chainedTweens[i].start(this._startTime + this._duration);
753
+ this._chainedTweens[i].start(this._startTime + this._duration, false);
696
754
  }
697
755
  this._isPlaying = false;
698
756
  return false;
@@ -738,9 +796,7 @@ var Tween = /** @class */ (function () {
738
796
  if (end.charAt(0) === '+' || end.charAt(0) === '-') {
739
797
  return start + parseFloat(end);
740
798
  }
741
- else {
742
- return parseFloat(end);
743
- }
799
+ return parseFloat(end);
744
800
  };
745
801
  Tween.prototype._swapEndStartRepeatValues = function (property) {
746
802
  var tmp = this._valuesStartRepeat[property];
@@ -756,7 +812,7 @@ var Tween = /** @class */ (function () {
756
812
  return Tween;
757
813
  }());
758
814
 
759
- var VERSION = '18.6.4';
815
+ var VERSION = '20.0.0';
760
816
 
761
817
  /**
762
818
  * Tween.js - Licensed under the MIT license
@@ -787,7 +843,7 @@ var exports = {
787
843
  Easing: Easing,
788
844
  Group: Group,
789
845
  Interpolation: Interpolation,
790
- now: now$1,
846
+ now: now,
791
847
  Sequence: Sequence,
792
848
  nextId: nextId,
793
849
  Tween: Tween,
@@ -799,5 +855,4 @@ var exports = {
799
855
  update: update,
800
856
  };
801
857
 
802
- export default exports;
803
- export { Easing, Group, Interpolation, Sequence, Tween, VERSION, add, getAll, nextId, now$1 as now, remove, removeAll, update };
858
+ export { Easing, Group, Interpolation, Sequence, Tween, VERSION, add, exports as default, getAll, nextId, now, remove, removeAll, update };