@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.umd.js CHANGED
@@ -2,18 +2,27 @@
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
3
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TWEEN = {}));
5
- }(this, (function (exports) { 'use strict';
5
+ })(this, (function (exports) { 'use strict';
6
6
 
7
7
  /**
8
8
  * The Ease class provides a collection of easing functions for use with tween.js.
9
9
  */
10
- var Easing = {
11
- Linear: {
10
+ var Easing = Object.freeze({
11
+ Linear: Object.freeze({
12
12
  None: function (amount) {
13
13
  return amount;
14
14
  },
15
- },
16
- Quadratic: {
15
+ In: function (amount) {
16
+ return this.None(amount);
17
+ },
18
+ Out: function (amount) {
19
+ return this.None(amount);
20
+ },
21
+ InOut: function (amount) {
22
+ return this.None(amount);
23
+ },
24
+ }),
25
+ Quadratic: Object.freeze({
17
26
  In: function (amount) {
18
27
  return amount * amount;
19
28
  },
@@ -26,8 +35,8 @@
26
35
  }
27
36
  return -0.5 * (--amount * (amount - 2) - 1);
28
37
  },
29
- },
30
- Cubic: {
38
+ }),
39
+ Cubic: Object.freeze({
31
40
  In: function (amount) {
32
41
  return amount * amount * amount;
33
42
  },
@@ -40,8 +49,8 @@
40
49
  }
41
50
  return 0.5 * ((amount -= 2) * amount * amount + 2);
42
51
  },
43
- },
44
- Quartic: {
52
+ }),
53
+ Quartic: Object.freeze({
45
54
  In: function (amount) {
46
55
  return amount * amount * amount * amount;
47
56
  },
@@ -54,8 +63,8 @@
54
63
  }
55
64
  return -0.5 * ((amount -= 2) * amount * amount * amount - 2);
56
65
  },
57
- },
58
- Quintic: {
66
+ }),
67
+ Quintic: Object.freeze({
59
68
  In: function (amount) {
60
69
  return amount * amount * amount * amount * amount;
61
70
  },
@@ -68,19 +77,19 @@
68
77
  }
69
78
  return 0.5 * ((amount -= 2) * amount * amount * amount * amount + 2);
70
79
  },
71
- },
72
- Sinusoidal: {
80
+ }),
81
+ Sinusoidal: Object.freeze({
73
82
  In: function (amount) {
74
- return 1 - Math.cos((amount * Math.PI) / 2);
83
+ return 1 - Math.sin(((1.0 - amount) * Math.PI) / 2);
75
84
  },
76
85
  Out: function (amount) {
77
86
  return Math.sin((amount * Math.PI) / 2);
78
87
  },
79
88
  InOut: function (amount) {
80
- return 0.5 * (1 - Math.cos(Math.PI * amount));
89
+ return 0.5 * (1 - Math.sin(Math.PI * (0.5 - amount)));
81
90
  },
82
- },
83
- Exponential: {
91
+ }),
92
+ Exponential: Object.freeze({
84
93
  In: function (amount) {
85
94
  return amount === 0 ? 0 : Math.pow(1024, amount - 1);
86
95
  },
@@ -99,8 +108,8 @@
99
108
  }
100
109
  return 0.5 * (-Math.pow(2, -10 * (amount - 1)) + 2);
101
110
  },
102
- },
103
- Circular: {
111
+ }),
112
+ Circular: Object.freeze({
104
113
  In: function (amount) {
105
114
  return 1 - Math.sqrt(1 - amount * amount);
106
115
  },
@@ -113,8 +122,8 @@
113
122
  }
114
123
  return 0.5 * (Math.sqrt(1 - (amount -= 2) * amount) + 1);
115
124
  },
116
- },
117
- Elastic: {
125
+ }),
126
+ Elastic: Object.freeze({
118
127
  In: function (amount) {
119
128
  if (amount === 0) {
120
129
  return 0;
@@ -146,15 +155,15 @@
146
155
  }
147
156
  return 0.5 * Math.pow(2, -10 * (amount - 1)) * Math.sin((amount - 1.1) * 5 * Math.PI) + 1;
148
157
  },
149
- },
150
- Back: {
158
+ }),
159
+ Back: Object.freeze({
151
160
  In: function (amount) {
152
161
  var s = 1.70158;
153
- return amount * amount * ((s + 1) * amount - s);
162
+ return amount === 1 ? 1 : amount * amount * ((s + 1) * amount - s);
154
163
  },
155
164
  Out: function (amount) {
156
165
  var s = 1.70158;
157
- return --amount * amount * ((s + 1) * amount + s) + 1;
166
+ return amount === 0 ? 0 : --amount * amount * ((s + 1) * amount + s) + 1;
158
167
  },
159
168
  InOut: function (amount) {
160
169
  var s = 1.70158 * 1.525;
@@ -163,8 +172,8 @@
163
172
  }
164
173
  return 0.5 * ((amount -= 2) * amount * ((s + 1) * amount + s) + 2);
165
174
  },
166
- },
167
- Bounce: {
175
+ }),
176
+ Bounce: Object.freeze({
168
177
  In: function (amount) {
169
178
  return 1 - Easing.Bounce.Out(1 - amount);
170
179
  },
@@ -188,40 +197,29 @@
188
197
  }
189
198
  return Easing.Bounce.Out(amount * 2 - 1) * 0.5 + 0.5;
190
199
  },
200
+ }),
201
+ generatePow: function (power) {
202
+ if (power === void 0) { power = 4; }
203
+ power = power < Number.EPSILON ? Number.EPSILON : power;
204
+ power = power > 10000 ? 10000 : power;
205
+ return {
206
+ In: function (amount) {
207
+ return Math.pow(amount, power);
208
+ },
209
+ Out: function (amount) {
210
+ return 1 - Math.pow((1 - amount), power);
211
+ },
212
+ InOut: function (amount) {
213
+ if (amount < 0.5) {
214
+ return Math.pow((amount * 2), power) / 2;
215
+ }
216
+ return (1 - Math.pow((2 - amount * 2), power)) / 2 + 0.5;
217
+ },
218
+ };
191
219
  },
192
- };
220
+ });
193
221
 
194
- var now;
195
- // Include a performance.now polyfill.
196
- // In node.js, use process.hrtime.
197
- // eslint-disable-next-line
198
- // @ts-ignore
199
- if (typeof self === 'undefined' && typeof process !== 'undefined' && process.hrtime) {
200
- now = function () {
201
- // eslint-disable-next-line
202
- // @ts-ignore
203
- var time = process.hrtime();
204
- // Convert [seconds, nanoseconds] to milliseconds.
205
- return time[0] * 1000 + time[1] / 1000000;
206
- };
207
- }
208
- // In a browser, use self.performance.now if it is available.
209
- else if (typeof self !== 'undefined' && self.performance !== undefined && self.performance.now !== undefined) {
210
- // This must be bound, because directly assigning this function
211
- // leads to an invocation exception in Chrome.
212
- now = self.performance.now.bind(self.performance);
213
- }
214
- // Use Date.now if it is available.
215
- else if (Date.now !== undefined) {
216
- now = Date.now;
217
- }
218
- // Otherwise, use 'new Date().getTime()'.
219
- else {
220
- now = function () {
221
- return new Date().getTime();
222
- };
223
- }
224
- var now$1 = now;
222
+ var now = function () { return performance.now(); };
225
223
 
226
224
  /**
227
225
  * Controlling groups of tweens
@@ -252,7 +250,7 @@
252
250
  delete this._tweensAddedDuringUpdate[tween.getId()];
253
251
  };
254
252
  Group.prototype.update = function (time, preserve) {
255
- if (time === void 0) { time = now$1(); }
253
+ if (time === void 0) { time = now(); }
256
254
  if (preserve === void 0) { preserve = false; }
257
255
  var tweenIds = Object.keys(this._tweens);
258
256
  if (tweenIds.length === 0) {
@@ -393,6 +391,7 @@
393
391
  this._valuesEnd = {};
394
392
  this._valuesStartRepeat = {};
395
393
  this._duration = 1000;
394
+ this._isDynamic = false;
396
395
  this._initialRepeat = 0;
397
396
  this._repeat = 0;
398
397
  this._yoyo = false;
@@ -402,10 +401,13 @@
402
401
  this._startTime = 0;
403
402
  this._easingFunction = Easing.Linear.None;
404
403
  this._interpolationFunction = Interpolation.Linear;
404
+ // eslint-disable-next-line
405
405
  this._chainedTweens = [];
406
406
  this._onStartCallbackFired = false;
407
+ this._onEveryStartCallbackFired = false;
407
408
  this._id = Sequence.nextId();
408
409
  this._isChainStopped = false;
410
+ this._propertiesAreSetUp = false;
409
411
  this._goToEnd = false;
410
412
  }
411
413
  Tween.prototype.getId = function () {
@@ -417,22 +419,28 @@
417
419
  Tween.prototype.isPaused = function () {
418
420
  return this._isPaused;
419
421
  };
420
- Tween.prototype.to = function (properties, duration) {
421
- // TODO? restore this, then update the 07_dynamic_to example to set fox
422
- // tween's to on each update. That way the behavior is opt-in (there's
423
- // currently no opt-out).
424
- // for (const prop in properties) this._valuesEnd[prop] = properties[prop]
425
- this._valuesEnd = Object.create(properties);
426
- if (duration !== undefined) {
427
- this._duration = duration;
428
- }
422
+ Tween.prototype.to = function (target, duration) {
423
+ if (duration === void 0) { duration = 1000; }
424
+ if (this._isPlaying)
425
+ throw new Error('Can not call Tween.to() while Tween is already started or paused. Stop the Tween first.');
426
+ this._valuesEnd = target;
427
+ this._propertiesAreSetUp = false;
428
+ this._duration = duration;
429
429
  return this;
430
430
  };
431
- Tween.prototype.duration = function (d) {
432
- this._duration = d;
431
+ Tween.prototype.duration = function (duration) {
432
+ if (duration === void 0) { duration = 1000; }
433
+ this._duration = duration;
433
434
  return this;
434
435
  };
435
- Tween.prototype.start = function (time) {
436
+ Tween.prototype.dynamic = function (dynamic) {
437
+ if (dynamic === void 0) { dynamic = false; }
438
+ this._isDynamic = dynamic;
439
+ return this;
440
+ };
441
+ Tween.prototype.start = function (time, overrideStartingValues) {
442
+ if (time === void 0) { time = now(); }
443
+ if (overrideStartingValues === void 0) { overrideStartingValues = false; }
436
444
  if (this._isPlaying) {
437
445
  return this;
438
446
  }
@@ -451,13 +459,27 @@
451
459
  this._isPlaying = true;
452
460
  this._isPaused = false;
453
461
  this._onStartCallbackFired = false;
462
+ this._onEveryStartCallbackFired = false;
454
463
  this._isChainStopped = false;
455
- this._startTime = time !== undefined ? (typeof time === 'string' ? now$1() + parseFloat(time) : time) : now$1();
464
+ this._startTime = time;
456
465
  this._startTime += this._delayTime;
457
- this._setupProperties(this._object, this._valuesStart, this._valuesEnd, this._valuesStartRepeat);
466
+ if (!this._propertiesAreSetUp || overrideStartingValues) {
467
+ this._propertiesAreSetUp = true;
468
+ // If dynamic is not enabled, clone the end values instead of using the passed-in end values.
469
+ if (!this._isDynamic) {
470
+ var tmp = {};
471
+ for (var prop in this._valuesEnd)
472
+ tmp[prop] = this._valuesEnd[prop];
473
+ this._valuesEnd = tmp;
474
+ }
475
+ this._setupProperties(this._object, this._valuesStart, this._valuesEnd, this._valuesStartRepeat, overrideStartingValues);
476
+ }
458
477
  return this;
459
478
  };
460
- Tween.prototype._setupProperties = function (_object, _valuesStart, _valuesEnd, _valuesStartRepeat) {
479
+ Tween.prototype.startFromCurrentValues = function (time) {
480
+ return this.start(time, true);
481
+ };
482
+ Tween.prototype._setupProperties = function (_object, _valuesStart, _valuesEnd, _valuesStartRepeat, overrideStartingValues) {
461
483
  for (var property in _valuesEnd) {
462
484
  var startValue = _object[property];
463
485
  var startValueIsArray = Array.isArray(startValue);
@@ -474,28 +496,46 @@
474
496
  if (endValues.length === 0) {
475
497
  continue;
476
498
  }
477
- // handle an array of relative values
478
- endValues = endValues.map(this._handleRelativeValue.bind(this, startValue));
479
- // Create a local copy of the Array with the start value at the front
480
- _valuesEnd[property] = [startValue].concat(endValues);
499
+ // Handle an array of relative values.
500
+ // Creates a local copy of the Array with the start value at the front
501
+ var temp = [startValue];
502
+ for (var i = 0, l = endValues.length; i < l; i += 1) {
503
+ var value = this._handleRelativeValue(startValue, endValues[i]);
504
+ if (isNaN(value)) {
505
+ isInterpolationList = false;
506
+ console.warn('Found invalid interpolation list. Skipping.');
507
+ break;
508
+ }
509
+ temp.push(value);
510
+ }
511
+ if (isInterpolationList) {
512
+ // if (_valuesStart[property] === undefined) { // handle end values only the first time. NOT NEEDED? setupProperties is now guarded by _propertiesAreSetUp.
513
+ _valuesEnd[property] = temp;
514
+ // }
515
+ }
481
516
  }
482
517
  // handle the deepness of the values
483
518
  if ((propType === 'object' || startValueIsArray) && startValue && !isInterpolationList) {
484
519
  _valuesStart[property] = startValueIsArray ? [] : {};
485
- // eslint-disable-next-line
486
- for (var prop in startValue) {
487
- // eslint-disable-next-line
488
- // @ts-ignore FIXME?
489
- _valuesStart[property][prop] = startValue[prop];
520
+ var nestedObject = startValue;
521
+ for (var prop in nestedObject) {
522
+ _valuesStart[property][prop] = nestedObject[prop];
490
523
  }
491
- _valuesStartRepeat[property] = startValueIsArray ? [] : {}; // TODO? repeat nested values? And yoyo? And array values?
492
- // eslint-disable-next-line
493
- // @ts-ignore FIXME?
494
- this._setupProperties(startValue, _valuesStart[property], _valuesEnd[property], _valuesStartRepeat[property]);
524
+ // TODO? repeat nested values? And yoyo? And array values?
525
+ _valuesStartRepeat[property] = startValueIsArray ? [] : {};
526
+ var endValues = _valuesEnd[property];
527
+ // If dynamic is not enabled, clone the end values instead of using the passed-in end values.
528
+ if (!this._isDynamic) {
529
+ var tmp = {};
530
+ for (var prop in endValues)
531
+ tmp[prop] = endValues[prop];
532
+ _valuesEnd[property] = endValues = tmp;
533
+ }
534
+ this._setupProperties(nestedObject, _valuesStart[property], endValues, _valuesStartRepeat[property], overrideStartingValues);
495
535
  }
496
536
  else {
497
- // Save the starting value, but only once.
498
- if (typeof _valuesStart[property] === 'undefined') {
537
+ // Save the starting value, but only once unless override is requested.
538
+ if (typeof _valuesStart[property] === 'undefined' || overrideStartingValues) {
499
539
  _valuesStart[property] = startValue;
500
540
  }
501
541
  if (!startValueIsArray) {
@@ -537,7 +577,7 @@
537
577
  return this;
538
578
  };
539
579
  Tween.prototype.pause = function (time) {
540
- if (time === void 0) { time = now$1(); }
580
+ if (time === void 0) { time = now(); }
541
581
  if (this._isPaused || !this._isPlaying) {
542
582
  return this;
543
583
  }
@@ -548,7 +588,7 @@
548
588
  return this;
549
589
  };
550
590
  Tween.prototype.resume = function (time) {
551
- if (time === void 0) { time = now$1(); }
591
+ if (time === void 0) { time = now(); }
552
592
  if (!this._isPaused || !this._isPlaying) {
553
593
  return this;
554
594
  }
@@ -566,14 +606,17 @@
566
606
  return this;
567
607
  };
568
608
  Tween.prototype.group = function (group) {
609
+ if (group === void 0) { group = mainGroup; }
569
610
  this._group = group;
570
611
  return this;
571
612
  };
572
613
  Tween.prototype.delay = function (amount) {
614
+ if (amount === void 0) { amount = 0; }
573
615
  this._delayTime = amount;
574
616
  return this;
575
617
  };
576
618
  Tween.prototype.repeat = function (times) {
619
+ if (times === void 0) { times = 0; }
577
620
  this._initialRepeat = times;
578
621
  this._repeat = times;
579
622
  return this;
@@ -583,17 +626,21 @@
583
626
  return this;
584
627
  };
585
628
  Tween.prototype.yoyo = function (yoyo) {
629
+ if (yoyo === void 0) { yoyo = false; }
586
630
  this._yoyo = yoyo;
587
631
  return this;
588
632
  };
589
633
  Tween.prototype.easing = function (easingFunction) {
634
+ if (easingFunction === void 0) { easingFunction = Easing.Linear.None; }
590
635
  this._easingFunction = easingFunction;
591
636
  return this;
592
637
  };
593
638
  Tween.prototype.interpolation = function (interpolationFunction) {
639
+ if (interpolationFunction === void 0) { interpolationFunction = Interpolation.Linear; }
594
640
  this._interpolationFunction = interpolationFunction;
595
641
  return this;
596
642
  };
643
+ // eslint-disable-next-line
597
644
  Tween.prototype.chain = function () {
598
645
  var tweens = [];
599
646
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -606,6 +653,10 @@
606
653
  this._onStartCallback = callback;
607
654
  return this;
608
655
  };
656
+ Tween.prototype.onEveryStart = function (callback) {
657
+ this._onEveryStartCallback = callback;
658
+ return this;
659
+ };
609
660
  Tween.prototype.onUpdate = function (callback) {
610
661
  this._onUpdateCallback = callback;
611
662
  return this;
@@ -628,7 +679,7 @@
628
679
  * it is still playing, just paused).
629
680
  */
630
681
  Tween.prototype.update = function (time, autoStart) {
631
- if (time === void 0) { time = now$1(); }
682
+ if (time === void 0) { time = now(); }
632
683
  if (autoStart === void 0) { autoStart = true; }
633
684
  if (this._isPaused)
634
685
  return true;
@@ -639,7 +690,7 @@
639
690
  if (time > endTime)
640
691
  return false;
641
692
  if (autoStart)
642
- this.start(time);
693
+ this.start(time, true);
643
694
  }
644
695
  this._goToEnd = false;
645
696
  if (time < this._startTime) {
@@ -651,6 +702,12 @@
651
702
  }
652
703
  this._onStartCallbackFired = true;
653
704
  }
705
+ if (this._onEveryStartCallbackFired === false) {
706
+ if (this._onEveryStartCallback) {
707
+ this._onEveryStartCallback(this._object);
708
+ }
709
+ this._onEveryStartCallbackFired = true;
710
+ }
654
711
  elapsed = (time - this._startTime) / this._duration;
655
712
  elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed;
656
713
  var value = this._easingFunction(elapsed);
@@ -689,6 +746,7 @@
689
746
  if (this._onRepeatCallback) {
690
747
  this._onRepeatCallback(this._object);
691
748
  }
749
+ this._onEveryStartCallbackFired = false;
692
750
  return true;
693
751
  }
694
752
  else {
@@ -698,7 +756,7 @@
698
756
  for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
699
757
  // Make the chained tweens start exactly at the time they should,
700
758
  // even if the `update()` method was called way past the duration of the tween
701
- this._chainedTweens[i].start(this._startTime + this._duration);
759
+ this._chainedTweens[i].start(this._startTime + this._duration, false);
702
760
  }
703
761
  this._isPlaying = false;
704
762
  return false;
@@ -744,9 +802,7 @@
744
802
  if (end.charAt(0) === '+' || end.charAt(0) === '-') {
745
803
  return start + parseFloat(end);
746
804
  }
747
- else {
748
- return parseFloat(end);
749
- }
805
+ return parseFloat(end);
750
806
  };
751
807
  Tween.prototype._swapEndStartRepeatValues = function (property) {
752
808
  var tmp = this._valuesStartRepeat[property];
@@ -762,7 +818,7 @@
762
818
  return Tween;
763
819
  }());
764
820
 
765
- var VERSION = '18.6.4';
821
+ var VERSION = '20.0.0';
766
822
 
767
823
  /**
768
824
  * Tween.js - Licensed under the MIT license
@@ -793,7 +849,7 @@
793
849
  Easing: Easing,
794
850
  Group: Group,
795
851
  Interpolation: Interpolation,
796
- now: now$1,
852
+ now: now,
797
853
  Sequence: Sequence,
798
854
  nextId: nextId,
799
855
  Tween: Tween,
@@ -815,11 +871,11 @@
815
871
  exports.default = exports$1;
816
872
  exports.getAll = getAll;
817
873
  exports.nextId = nextId;
818
- exports.now = now$1;
874
+ exports.now = now;
819
875
  exports.remove = remove;
820
876
  exports.removeAll = removeAll;
821
877
  exports.update = update;
822
878
 
823
879
  Object.defineProperty(exports, '__esModule', { value: true });
824
880
 
825
- })));
881
+ }));
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@tweenjs/tween.js",
3
3
  "description": "Super simple, fast and easy to use tweening engine which incorporates optimised Robert Penner's equations.",
4
- "version": "18.6.4",
4
+ "version": "20.0.0",
5
+ "type": "module",
5
6
  "main": "dist/tween.cjs.js",
6
7
  "types": "dist/tween.d.ts",
7
8
  "module": "dist/tween.esm.js",
@@ -31,8 +32,8 @@
31
32
  "tsc": "tsc",
32
33
  "tsc-watch": "tsc --watch",
33
34
  "examples": "npx serve .",
34
- "test": "npm run build && npm run test-unit && npm run test-lint",
35
- "test-unit": "nodeunit test/unit/nodeunitheadless.js",
35
+ "test": "npm run build && npm run test-lint && npm run test-unit",
36
+ "test-unit": "nodeunit test/unit/nodeunitheadless.cjs",
36
37
  "test-lint": "npm run prettier -- --check && eslint 'src/**/*.ts'",
37
38
  "lint": "npm run prettier -- --write && eslint 'src/**/*.ts' --fix",
38
39
  "prettier": "prettier './**/*.{js,ts,md,json,html,css}'",
@@ -51,11 +52,11 @@
51
52
  "eslint-config-prettier": "^6.7.0",
52
53
  "eslint-plugin-prettier": "^3.1.1",
53
54
  "nodeunit": "^0.11.3",
54
- "prettier": "^2.0.0",
55
+ "prettier": "2.8.7",
55
56
  "rimraf": "^3.0.0",
56
- "rollup": "^2.0.0",
57
- "rollup-plugin-dts": "1.4.10",
57
+ "rollup": "3.20.7",
58
+ "rollup-plugin-dts": "5.3.0",
58
59
  "tslib": "^1.10.0",
59
- "typescript": "^4.0.0"
60
+ "typescript": "5.0.4"
60
61
  }
61
62
  }