@tweenjs/tween.js 18.3.2 → 18.6.0

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