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