-
Notifications
You must be signed in to change notification settings - Fork 3
/
web-animations.js
5623 lines (5248 loc) · 169 KB
/
web-animations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2012 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
'use strict';
var ASSERT_ENABLED = false;
var SVG_NS = 'http://www.w3.org/2000/svg';
function assert(check, message) {
console.assert(ASSERT_ENABLED,
'assert should not be called when ASSERT_ENABLED is false');
console.assert(check, message);
// Some implementations of console.assert don't actually throw
if (!check) { throw message; }
}
function detectFeatures() {
var el = createDummyElement();
el.style.cssText = 'width: calc(0px);' +
'width: -webkit-calc(0px);';
var calcFunction = el.style.width.split('(')[0];
function detectProperty(candidateProperties) {
return [].filter.call(candidateProperties, function(property) {
return property in el.style;
})[0];
}
var transformProperty = detectProperty([
'transform',
'webkitTransform',
'msTransform']);
var perspectiveProperty = detectProperty([
'perspective',
'webkitPerspective',
'msPerspective']);
return {
calcFunction: calcFunction,
transformProperty: transformProperty,
transformOriginProperty: transformProperty + 'Origin',
perspectiveProperty: perspectiveProperty,
perspectiveOriginProperty: perspectiveProperty + 'Origin'
};
}
var features = detectFeatures();
function prefixProperty(property) {
switch (property) {
case 'transform':
return features.transformProperty;
case 'transformOrigin':
return features.transformOriginProperty;
case 'perspective':
return features.perspectiveProperty;
case 'perspectiveOrigin':
return features.perspectiveOriginProperty;
default:
return property;
}
}
function createDummyElement() {
return document.documentElement.namespaceURI == SVG_NS ?
document.createElementNS(SVG_NS, 'g') :
document.createElement('div');
}
var constructorToken = {};
var deprecationsSilenced = {};
var createObject = function(proto, obj) {
var newObject = Object.create(proto);
Object.getOwnPropertyNames(obj).forEach(function(name) {
Object.defineProperty(
newObject, name, Object.getOwnPropertyDescriptor(obj, name));
});
return newObject;
};
var abstractMethod = function() {
throw 'Abstract method not implemented.';
};
var deprecated = function(name, deprecationDate, advice, plural) {
if (deprecationsSilenced[name]) {
return;
}
var auxVerb = plural ? 'are' : 'is';
var today = new Date();
var cutoffDate = new Date(deprecationDate);
cutoffDate.setMonth(cutoffDate.getMonth() + 3); // 3 months grace period
if (today < cutoffDate) {
console.warn('Web Animations: ' + name +
' ' + auxVerb + ' deprecated and will stop working on ' +
cutoffDate.toDateString() + '. ' + advice);
deprecationsSilenced[name] = true;
} else {
throw new Error(name + ' ' + auxVerb + ' no longer supported. ' + advice);
}
};
var defineDeprecatedProperty = function(object, property, getFunc, setFunc) {
var descriptor = {
get: getFunc,
configurable: true
};
if (setFunc) {
descriptor.set = setFunc;
}
Object.defineProperty(object, property, descriptor);
};
var IndexSizeError = function(message) {
Error.call(this);
this.name = 'IndexSizeError';
this.message = message;
};
IndexSizeError.prototype = Object.create(Error.prototype);
/** @constructor */
var TimingDict = function(timingInput) {
if (typeof timingInput === 'object') {
for (var k in timingInput) {
if (k in TimingDict.prototype) {
this[k] = timingInput[k];
}
}
} else if (isDefinedAndNotNull(timingInput)) {
this.duration = Number(timingInput);
}
};
TimingDict.prototype = {
delay: 0,
endDelay: 0,
fill: 'auto',
iterationStart: 0,
iterations: 1,
duration: 'auto',
playbackRate: 1,
direction: 'normal',
easing: 'linear'
};
/** @constructor */
var Timing = function(token, timingInput, changeHandler) {
if (token !== constructorToken) {
throw new TypeError('Illegal constructor');
}
this._dict = new TimingDict(timingInput);
this._changeHandler = changeHandler;
};
Timing.prototype = {
_timingFunction: function(timedItem) {
var timingFunction = TimingFunction.createFromString(
this.easing, timedItem);
this._timingFunction = function() {
return timingFunction;
};
return timingFunction;
},
_invalidateTimingFunction: function() {
delete this._timingFunction;
},
_iterations: function() {
var value = this._dict.iterations;
return value < 0 ? 1 : value;
},
_duration: function() {
var value = this._dict.duration;
return typeof value === 'number' ? value : 'auto';
},
_clone: function() {
return new Timing(
constructorToken, this._dict, this._updateInternalState.bind(this));
}
};
// Configures an accessor descriptor for use with Object.defineProperty() to
// allow the property to be changed and enumerated, to match __defineGetter__()
// and __defineSetter__().
var configureDescriptor = function(descriptor) {
descriptor.configurable = true;
descriptor.enumerable = true;
return descriptor;
};
Timing._defineProperty = function(prop) {
Object.defineProperty(Timing.prototype, prop, configureDescriptor({
get: function() {
return this._dict[prop];
},
set: function(value) {
if (isDefinedAndNotNull(value)) {
if (prop == 'duration' && value == 'auto') {
// duration is not always a number
} else if (['delay', 'endDelay', 'iterationStart', 'iterations',
'duration', 'playbackRate'].indexOf(prop) >= 0) {
value = Number(value);
}
this._dict[prop] = value;
} else {
delete this._dict[prop];
}
// FIXME: probably need to implement specialized handling parsing
// for each property
if (prop === 'easing') {
// Cached timing function may be invalid now.
this._invalidateTimingFunction();
}
this._changeHandler();
}
}));
};
for (var prop in TimingDict.prototype) {
Timing._defineProperty(prop);
}
var isDefined = function(val) {
return typeof val !== 'undefined';
};
var isDefinedAndNotNull = function(val) {
return isDefined(val) && (val !== null);
};
/** @constructor */
var AnimationTimeline = function(token) {
if (token !== constructorToken) {
throw new TypeError('Illegal constructor');
}
// TODO: This will probably need to change.
this._startTime = documentTimeZeroAsClockTime;
};
AnimationTimeline.prototype = {
get currentTime() {
if (this._startTime === undefined) {
this._startTime = documentTimeZeroAsClockTime;
if (this._startTime === undefined) {
return null;
}
}
return relativeTime(cachedClockTime(), this._startTime);
},
get effectiveCurrentTime() {
return this.currentTime || 0;
},
play: function(source) {
return new AnimationPlayer(constructorToken, source, this);
},
getCurrentPlayers: function() {
return PLAYERS.filter(function(player) {
return !player._isPastEndOfActiveInterval();
});
},
toTimelineTime: function(otherTime, other) {
if ((this.currentTime === null) || (other.currentTime === null)) {
return null;
} else {
return otherTime + other._startTime - this._startTime;
}
},
_pauseAnimationsForTesting: function(pauseAt) {
PLAYERS.forEach(function(player) {
player.pause();
player.currentTime = pauseAt;
});
}
};
// TODO: Remove dead players from here?
var PLAYERS = [];
var playersAreSorted = false;
var playerSequenceNumber = 0;
// Methods for event target objects.
var initializeEventTarget = function(eventTarget) {
eventTarget._handlers = {};
eventTarget._onHandlers = {};
};
var setOnEventHandler = function(eventTarget, type, handler) {
if (typeof handler === 'function') {
eventTarget._onHandlers[type] = {
callback: handler,
index: (eventTarget._handlers[type] || []).length
};
} else {
eventTarget._onHandlers[type] = null;
}
};
var getOnEventHandler = function(eventTarget, type) {
if (isDefinedAndNotNull(eventTarget._onHandlers[type])) {
return eventTarget._onHandlers[type].callback;
}
return null;
};
var addEventHandler = function(eventTarget, type, handler) {
if (typeof handler !== 'function') {
return;
}
if (!isDefinedAndNotNull(eventTarget._handlers[type])) {
eventTarget._handlers[type] = [];
} else if (eventTarget._handlers[type].indexOf(handler) !== -1) {
return;
}
eventTarget._handlers[type].push(handler);
};
var removeEventHandler = function(eventTarget, type, handler) {
if (!eventTarget._handlers[type]) {
return;
}
var index = eventTarget._handlers[type].indexOf(handler);
if (index === -1) {
return;
}
eventTarget._handlers[type].splice(index, 1);
if (isDefinedAndNotNull(eventTarget._onHandlers[type]) &&
(index < eventTarget._onHandlers[type].index)) {
eventTarget._onHandlers[type].index -= 1;
}
};
var hasEventHandlersForEvent = function(eventTarget, type) {
return (isDefinedAndNotNull(eventTarget._handlers[type]) &&
eventTarget._handlers[type].length > 0) ||
isDefinedAndNotNull(eventTarget._onHandlers[type]);
};
var callEventHandlers = function(eventTarget, type, event) {
var callbackList;
if (isDefinedAndNotNull(eventTarget._handlers[type])) {
callbackList = eventTarget._handlers[type].slice();
} else {
callbackList = [];
}
if (isDefinedAndNotNull(eventTarget._onHandlers[type])) {
callbackList.splice(eventTarget._onHandlers[type].index, 0,
eventTarget._onHandlers[type].callback);
}
setTimeout(function() {
for (var i = 0; i < callbackList.length; i++) {
callbackList[i].call(eventTarget, event);
}
}, 0);
};
var createEventPrototype = function() {
var prototype = Object.create(window.Event.prototype, {
type: { get: function() { return this._type; } },
target: { get: function() { return this._target; } },
currentTarget: { get: function() { return this._target; } },
eventPhase: { get: function() { return this._eventPhase; } },
bubbles: { get: function() { return false; } },
cancelable: { get: function() { return false; } },
timeStamp: { get: function() { return this._timeStamp; } },
defaultPrevented: { get: function() { return false; } }
});
prototype._type = '';
prototype._target = null;
prototype._eventPhase = Event.NONE;
prototype._timeStamp = 0;
prototype._initialize = function(target) {
this._target = target;
this._eventPhase = Event.AT_TARGET;
this._timeStamp = cachedClockTime();
};
return prototype;
};
/** @constructor */
var AnimationPlayer = function(token, source, timeline) {
if (token !== constructorToken) {
throw new TypeError('Illegal constructor');
}
enterModifyCurrentAnimationState();
try {
this._registeredOnTimeline = false;
this._sequenceNumber = playerSequenceNumber++;
this._timeline = timeline;
this._startTime =
this.timeline.currentTime === null ? 0 : this.timeline.currentTime;
this._storedTimeLag = 0.0;
this._pausedState = false;
this._holdTime = null;
this._previousCurrentTime = null;
this._playbackRate = 1.0;
this._hasTicked = false;
this.source = source;
this._checkForLegacyHandlers();
this._lastCurrentTime = undefined;
this._finishedFlag = false;
initializeEventTarget(this);
playersAreSorted = false;
maybeRestartAnimation();
} finally {
exitModifyCurrentAnimationState(ensureRetickBeforeGetComputedStyle);
}
};
AnimationPlayer.prototype = {
set source(source) {
enterModifyCurrentAnimationState();
try {
if (isDefinedAndNotNull(this.source)) {
// To prevent infinite recursion.
var oldTimedItem = this.source;
this._source = null;
oldTimedItem._attach(null);
}
this._source = source;
if (isDefinedAndNotNull(this.source)) {
this.source._attach(this);
this._update();
maybeRestartAnimation();
}
this._checkForLegacyHandlers();
} finally {
exitModifyCurrentAnimationState(repeatLastTick);
}
},
get source() {
return this._source;
},
// This is the effective current time.
set currentTime(currentTime) {
enterModifyCurrentAnimationState();
try {
this._currentTime = currentTime;
} finally {
exitModifyCurrentAnimationState(repeatLastTick);
}
},
get currentTime() {
return this._currentTime;
},
set _currentTime(seekTime) {
// If we are paused or seeking to a time where limiting applies (i.e. beyond
// the end in the current direction), update the hold time.
var sourceContentEnd = this.source ? this.source.endTime : 0;
if (this.paused ||
(this.playbackRate > 0 && seekTime >= sourceContentEnd) ||
(this.playbackRate < 0 && seekTime <= 0)) {
this._holdTime = seekTime;
// Otherwise, clear the hold time (it may been set by previously seeking to
// a limited time) and update the time lag.
} else {
this._holdTime = null;
this._storedTimeLag = (this.timeline.effectiveCurrentTime -
this.startTime) * this.playbackRate - seekTime;
}
this._update();
maybeRestartAnimation();
},
get _currentTime() {
this._previousCurrentTime = (this.timeline.effectiveCurrentTime -
this.startTime) * this.playbackRate - this.timeLag;
return this._previousCurrentTime;
},
get _unlimitedCurrentTime() {
return (this.timeline.effectiveCurrentTime - this.startTime) *
this.playbackRate - this._storedTimeLag;
},
get timeLag() {
if (this.paused) {
return this._pauseTimeLag;
}
// Apply limiting at start of interval when playing in reverse
if (this.playbackRate < 0 && this._unlimitedCurrentTime <= 0) {
if (this._holdTime === null) {
this._holdTime = Math.min(this._previousCurrentTime, 0);
}
return this._pauseTimeLag;
}
// Apply limiting at end of interval when playing forwards
var sourceContentEnd = this.source ? this.source.endTime : 0;
if (this.playbackRate > 0 &&
this._unlimitedCurrentTime >= sourceContentEnd) {
if (this._holdTime === null) {
this._holdTime = Math.max(this._previousCurrentTime, sourceContentEnd);
}
return this._pauseTimeLag;
}
// Finished limiting so store pause time lag
if (this._holdTime !== null) {
this._storedTimeLag = this._pauseTimeLag;
this._holdTime = null;
}
return this._storedTimeLag;
},
get _pauseTimeLag() {
return ((this.timeline.currentTime || 0) - this.startTime) *
this.playbackRate - this._holdTime;
},
set startTime(startTime) {
enterModifyCurrentAnimationState();
try {
// This seeks by updating _startTime and hence the currentTime. It does
// not affect _storedTimeLag.
this._startTime = startTime;
this._holdTime = null;
playersAreSorted = false;
this._update();
maybeRestartAnimation();
} finally {
exitModifyCurrentAnimationState(repeatLastTick);
}
},
get startTime() {
return this._startTime;
},
set _paused(isPaused) {
if (isPaused === this._pausedState) {
return;
}
if (this._pausedState) {
this._storedTimeLag = this.timeLag;
this._holdTime = null;
maybeRestartAnimation();
} else {
this._holdTime = this.currentTime;
}
this._pausedState = isPaused;
},
get paused() {
return this._pausedState;
},
get timeline() {
return this._timeline;
},
set playbackRate(playbackRate) {
enterModifyCurrentAnimationState();
try {
var cachedCurrentTime = this.currentTime;
// This will impact currentTime, so perform a compensatory seek.
this._playbackRate = playbackRate;
this.currentTime = cachedCurrentTime;
} finally {
exitModifyCurrentAnimationState(repeatLastTick);
}
},
get playbackRate() {
return this._playbackRate;
},
get finished() {
return this._isLimited;
},
get _isLimited() {
var sourceEnd = this.source ? this.source.endTime : 0;
return ((this.playbackRate > 0 && this.currentTime >= sourceEnd) ||
(this.playbackRate < 0 && this.currentTime <= 0));
},
cancel: function() {
this.source = null;
},
finish: function() {
if (this.playbackRate < 0) {
this.currentTime = 0;
} else if (this.playbackRate > 0) {
var sourceEndTime = this.source ? this.source.endTime : 0;
if (sourceEndTime === Infinity) {
throw new Error('InvalidStateError');
}
this.currentTime = sourceEndTime;
}
},
play: function() {
this._paused = false;
if (!this.source) {
return;
}
if (this.playbackRate > 0 &&
(this.currentTime < 0 ||
this.currentTime >= this.source.endTime)) {
this.currentTime = 0;
} else if (this.playbackRate < 0 &&
(this.currentTime <= 0 ||
this.currentTime > this.source.endTime)) {
this.currentTime = this.source.endTime;
}
},
pause: function() {
this._paused = true;
},
reverse: function() {
if (this.playbackRate === 0) {
return;
}
if (this.source) {
if (this.playbackRate > 0 && this.currentTime >= this.source.endTime) {
this.currentTime = this.source.endTime;
} else if (this.playbackRate < 0 && this.currentTime < 0) {
this.currentTime = 0;
}
}
this.playbackRate = -this.playbackRate;
this._paused = false;
},
_update: function() {
if (this.source !== null) {
this.source._updateInheritedTime(
this.timeline.currentTime === null ? null : this._currentTime);
this._registerOnTimeline();
}
},
_hasFutureAnimation: function() {
return this.source === null || this.playbackRate === 0 ||
this.source._hasFutureAnimation(this.playbackRate > 0);
},
_isPastEndOfActiveInterval: function() {
return this.source === null ||
this.source._isPastEndOfActiveInterval();
},
_isCurrent: function() {
return this.source && this.source._isCurrent();
},
_hasFutureEffect: function() {
return this.source && this.source._hasFutureEffect();
},
_getLeafItemsInEffect: function(items) {
if (this.source) {
this.source._getLeafItemsInEffect(items);
}
},
_isTargetingElement: function(element) {
return this.source && this.source._isTargetingElement(element);
},
_getAnimationsTargetingElement: function(element, animations) {
if (this.source) {
this.source._getAnimationsTargetingElement(element, animations);
}
},
set onfinish(handler) {
return setOnEventHandler(this, 'finish', handler);
},
get onfinish() {
return getOnEventHandler(this, 'finish');
},
addEventListener: function(type, handler) {
if (type === 'finish') {
addEventHandler(this, type, handler);
}
},
removeEventListener: function(type, handler) {
if (type === 'finish') {
removeEventHandler(this, type, handler);
}
},
_generateEvents: function() {
if (!this._finishedFlag && this.finished &&
hasEventHandlersForEvent(this, 'finish')) {
var event = new AnimationPlayerEvent('finish', {
currentTime: this.currentTime,
timelineTime: this.timeline.currentTime
});
event._initialize(this);
callEventHandlers(this, 'finish', event);
}
this._finishedFlag = this.finished;
// The following code is for deprecated TimedItem event handling and should
// be removed once we stop supporting it.
if (!isDefinedAndNotNull(this._lastCurrentTime)) {
this._lastCurrentTime = 0;
}
if (this._needsLegacyHandlerPass) {
var timeDelta = this._unlimitedCurrentTime - this._lastCurrentTime;
if (timeDelta > 0) {
this.source._generateLegacyEvents(
this._lastCurrentTime, this._unlimitedCurrentTime,
this.timeline.currentTime, 1);
}
}
this._lastCurrentTime = this._unlimitedCurrentTime;
},
// These two legacy methods are for deprecated TimedItem event handling and
// should be removed once we stop supporting it.
_legacyHandlerAdded: function() {
this._needsLegacyHandlerPass = true;
},
_checkForLegacyHandlers: function() {
this._needsLegacyHandlerPass = this.source !== null &&
this.source._hasLegacyEventHandlers();
},
_registerOnTimeline: function() {
if (!this._registeredOnTimeline) {
PLAYERS.push(this);
this._registeredOnTimeline = true;
}
},
_deregisterFromTimeline: function() {
PLAYERS.splice(PLAYERS.indexOf(this), 1);
this._registeredOnTimeline = false;
}
};
/** @constructor */
var AnimationPlayerEvent = function(type, eventInit) {
this._type = type;
this.currentTime = eventInit.currentTime;
this.timelineTime = eventInit.timelineTime;
};
AnimationPlayerEvent.prototype = createEventPrototype();
/** @constructor */
var TimedItem = function(token, timingInput) {
if (token !== constructorToken) {
throw new TypeError('Illegal constructor');
}
this.timing = new Timing(
constructorToken, timingInput,
this._specifiedTimingModified.bind(this));
this._inheritedTime = null;
this.currentIteration = null;
this._iterationTime = null;
this._animationTime = null;
this._startTime = 0.0;
this._player = null;
this._parent = null;
this._updateInternalState();
this._fill = this._resolveFillMode(this.timing.fill);
initializeEventTarget(this);
};
TimedItem.prototype = {
// TODO: It would be good to avoid the need for this. We would need to modify
// call sites to instead rely on a call from the parent.
get _effectiveParentTime() {
return this.parent !== null && this.parent._iterationTime !== null ?
this.parent._iterationTime : 0;
},
get localTime() {
return this._inheritedTime === null ?
null : this._inheritedTime - this._startTime;
},
get startTime() {
return this._startTime;
},
get duration() {
var result = this.timing._duration();
if (result === 'auto') {
result = this._intrinsicDuration();
}
return result;
},
get activeDuration() {
var repeatedDuration = this.duration * this.timing._iterations();
return repeatedDuration / Math.abs(this.timing.playbackRate);
},
get endTime() {
return this._startTime + this.activeDuration + this.timing.delay +
this.timing.endDelay;
},
get parent() {
return this._parent;
},
get previousSibling() {
if (!this.parent) {
return null;
}
var siblingIndex = this.parent.indexOf(this) - 1;
if (siblingIndex < 0) {
return null;
}
return this.parent.children[siblingIndex];
},
get nextSibling() {
if (!this.parent) {
return null;
}
var siblingIndex = this.parent.indexOf(this) + 1;
if (siblingIndex >= this.parent.children.length) {
return null;
}
return this.parent.children[siblingIndex];
},
_attach: function(player) {
// Remove ourselves from our parent, if we have one. This also removes any
// exsisting player.
this._reparent(null);
this._player = player;
},
// Takes care of updating the outgoing parent. This is called with a non-null
// parent only from TimingGroup.splice(), which takes care of calling
// TimingGroup._childrenStateModified() for the new parent.
_reparent: function(parent) {
if (parent === this) {
throw new Error('parent can not be set to self!');
}
enterModifyCurrentAnimationState();
try {
if (this._player !== null) {
this._player.source = null;
this._player = null;
}
if (this.parent !== null) {
this.remove();
}
this._parent = parent;
// In the case of a AnimationSequence parent, _startTime will be updated
// by TimingGroup.splice().
if (this.parent === null || this.parent.type !== 'seq') {
this._startTime =
this._stashedStartTime === undefined ? 0.0 : this._stashedStartTime;
this._stashedStartTime = undefined;
}
// In the case of the parent being non-null, _childrenStateModified() will
// call this via _updateChildInheritedTimes().
// TODO: Consider optimising this case by skipping this call.
this._updateTimeMarkers();
} finally {
exitModifyCurrentAnimationState(
Boolean(this.player) ? repeatLastTick : null);
}
},
_intrinsicDuration: function() {
return 0.0;
},
_resolveFillMode: abstractMethod,
_updateInternalState: function() {
this._fill = this._resolveFillMode(this.timing.fill);
if (this.parent) {
this.parent._childrenStateModified();
} else if (this._player) {
this._player._registerOnTimeline();
}
this._updateTimeMarkers();
},
_specifiedTimingModified: function() {
enterModifyCurrentAnimationState();
try {
this._updateInternalState();
} finally {
exitModifyCurrentAnimationState(
Boolean(this.player) ? repeatLastTick : null);
}
},
// We push time down to children. We could instead have children pull from
// above, but this is tricky because a TimedItem may use either a parent
// TimedItem or an AnimationPlayer. This requires either logic in
// TimedItem, or for TimedItem and AnimationPlayer to implement Timeline
// (or an equivalent), both of which are ugly.
_updateInheritedTime: function(inheritedTime) {
this._inheritedTime = inheritedTime;
this._updateTimeMarkers();
},
_updateAnimationTime: function() {
if (this.localTime < this.timing.delay) {
if (this._fill === 'backwards' ||
this._fill === 'both') {
this._animationTime = 0;
} else {
this._animationTime = null;
}
} else if (this.localTime <
this.timing.delay + this.activeDuration) {
this._animationTime = this.localTime - this.timing.delay;
} else {
if (this._fill === 'forwards' ||
this._fill === 'both') {
this._animationTime = this.activeDuration;
} else {
this._animationTime = null;
}
}
},
_updateIterationParamsZeroDuration: function() {
this._iterationTime = 0;
var isAtEndOfIterations = this.timing._iterations() !== 0 &&
this.localTime >= this.timing.delay;
this.currentIteration = (
isAtEndOfIterations ?
this._floorWithOpenClosedRange(
this.timing.iterationStart + this.timing._iterations(),
1.0) :
this._floorWithClosedOpenRange(this.timing.iterationStart, 1.0));
// Equivalent to unscaledIterationTime below.
var unscaledFraction = (
isAtEndOfIterations ?
this._modulusWithOpenClosedRange(
this.timing.iterationStart + this.timing._iterations(),
1.0) :
this._modulusWithClosedOpenRange(this.timing.iterationStart, 1.0));
var timingFunction = this.timing._timingFunction(this);
this._timeFraction = (
this._isCurrentDirectionForwards() ?
unscaledFraction :
1.0 - unscaledFraction);
ASSERT_ENABLED && assert(
this._timeFraction >= 0.0 && this._timeFraction <= 1.0,
'Time fraction should be in the range [0, 1]');
if (timingFunction) {
this._timeFraction = timingFunction.scaleTime(this._timeFraction);
}
},
_getAdjustedAnimationTime: function(animationTime) {
var startOffset =
multiplyZeroGivesZero(this.timing.iterationStart, this.duration);
return (this.timing.playbackRate < 0 ?
(animationTime - this.activeDuration) : animationTime) *
this.timing.playbackRate + startOffset;
},
_scaleIterationTime: function(unscaledIterationTime) {
return this._isCurrentDirectionForwards() ?
unscaledIterationTime :
this.duration - unscaledIterationTime;
},
_updateIterationParams: function() {
var adjustedAnimationTime =
this._getAdjustedAnimationTime(this._animationTime);
var repeatedDuration = this.duration * this.timing._iterations();
var startOffset = this.timing.iterationStart * this.duration;
var isAtEndOfIterations = (this.timing._iterations() !== 0) &&
(adjustedAnimationTime - startOffset === repeatedDuration);
this.currentIteration = isAtEndOfIterations ?
this._floorWithOpenClosedRange(
adjustedAnimationTime, this.duration) :
this._floorWithClosedOpenRange(
adjustedAnimationTime, this.duration);
var unscaledIterationTime = isAtEndOfIterations ?
this._modulusWithOpenClosedRange(
adjustedAnimationTime, this.duration) :
this._modulusWithClosedOpenRange(
adjustedAnimationTime, this.duration);
this._iterationTime = this._scaleIterationTime(unscaledIterationTime);
if (this.duration == Infinity) {
this._timeFraction = 0;
return;
}
this._timeFraction = this._iterationTime / this.duration;
ASSERT_ENABLED && assert(
this._timeFraction >= 0.0 && this._timeFraction <= 1.0,
'Time fraction should be in the range [0, 1], got ' +
this._timeFraction + ' ' + this._iterationTime + ' ' +
this.duration + ' ' + isAtEndOfIterations + ' ' +
unscaledIterationTime);
var timingFunction = this.timing._timingFunction(this);
if (timingFunction) {
this._timeFraction = timingFunction.scaleTime(this._timeFraction);
}
this._iterationTime = this._timeFraction * this.duration;
},
_updateTimeMarkers: function() {
if (this.localTime === null) {
this._animationTime = null;
this._iterationTime = null;
this.currentIteration = null;
this._timeFraction = null;
return false;
}
this._updateAnimationTime();
if (this._animationTime === null) {
this._iterationTime = null;
this.currentIteration = null;
this._timeFraction = null;
} else if (this.duration === 0) {
this._updateIterationParamsZeroDuration();
} else {
this._updateIterationParams();
}
maybeRestartAnimation();
},
_floorWithClosedOpenRange: function(x, range) {
return Math.floor(x / range);
},
_floorWithOpenClosedRange: function(x, range) {