forked from flackr/scroll-timeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-animation.js
1560 lines (1339 loc) · 55.1 KB
/
proxy-animation.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
import {
ScrollTimeline,
installScrollOffsetExtension,
addAnimation,
removeAnimation
} from "./scroll-timeline-base";
const nativeElementAnimate = window.Element.prototype.animate;
const nativeAnimation = window.Animation;
class PromiseWrapper {
constructor() {
this.state = 'pending';
this.nativeResolve = this.nativeReject = null;
this.promise = new Promise((resolve, reject) => {
this.nativeResolve = resolve;
this.nativeReject = reject;
});
}
resolve(value) {
this.state = 'resolved';
this.nativeResolve(value);
}
reject(reason) {
this.state = 'rejected';
// Do not report unhandled promise rejections.
this.promise.catch(() => {});
this.nativeReject(reason);
}
}
function createReadyPromise(details) {
details.readyPromise = new PromiseWrapper();
// Trigger the pending task on the next animation frame.
requestAnimationFrame(() => {
const timelineTime = details.timeline.currentTime;
if (timelineTime !== null)
notifyReady(details);
});
}
function createAbortError() {
return new DOMException("The user aborted a request", "AbortError");
}
// Converts a time from its internal representation to a percent. For a
// monotonic timeline, time is reported as a double with implicit units of
// milliseconds. For progress-based animations, times are reported as
// percentages.
function toCssNumberish(details, value) {
if (value === null)
return value;
if (typeof value !== 'number') {
throw new DOMException(
`Unexpected value: ${value}. Cannot convert to CssNumberish`,
"InvalidStateError");
}
const limit = effectEnd(details);
const percent = limit ? 100 * value / limit : 0;
return CSS.percent(percent);
}
// Covnerts a time to its internal representation. Progress-based animations
// use times expressed as percentages. Each progress-based animation is backed
// by a native animation with a document timeline in the polyfill. Thus, we
// need to convert the timing from percent to milliseconds with implicit units.
function fromCssNumberish(details, value) {
if (!details.timeline) {
// Document timeline
if (value == null || typeof value === 'number')
return value;
const convertedTime = value.to('ms');
if (convertTime)
return convertedTime.value;
throw new DOMException(
"CSSNumericValue must be either a number or a time value for " +
"time based animations.",
"InvalidStateError");
} else {
// Scroll timeline.
if (value === null)
return value;
if (value.unit === 'percent') {
const duration = effectEnd(details);
return value.value * duration / 100;
}
throw new DOMException(
"CSSNumericValue must be a percentage for progress based animations.",
"NotSupportedError");
}
}
function normalizedTiming(details) {
// Used normalized timing in the case of a progress-based animation or
// specified timing with a document timeline. The normalizedTiming property
// is initialized and cached when fetching the timing information.
const timing = details.proxy.effect.getTiming();
return details.normalizedTiming || timing;
}
function commitPendingPlay(details) {
// https://drafts4.csswg.org/web-animations-2/#playing-an-animation-section
// Refer to steps listed under "Schedule a task to run ..."
const timelineTime = fromCssNumberish(details, details.timeline.currentTime);
if (details.holdTime != null) {
// A: If animation’s hold time is resolved,
// A.1. Apply any pending playback rate on animation.
// A.2. Let new start time be the result of evaluating:
// ready time - hold time / playback rate for animation.
// If the playback rate is zero, let new start time be simply ready
// time.
// A.3. Set the start time of animation to new start time.
// A.4. If animation’s playback rate is not 0, make animation’s hold
// time unresolved.
applyPendingPlaybackRate(details);
if (details.animation.playbackRate == 0) {
details.startTime = timelineTime;
} else {
details.startTime
= timelineTime -
details.holdTime / details.animation.playbackRate;
details.holdTime = null;
}
} else if (details.startTime !== null &&
details.pendingPlaybackRate !== null) {
// B: If animation’s start time is resolved and animation has a pending
// playback rate,
// B.1. Let current time to match be the result of evaluating:
// (ready time - start time) × playback rate for animation.
// B.2 Apply any pending playback rate on animation.
// B.3 If animation’s playback rate is zero, let animation’s hold time
// be current time to match.
// B.4 Let new start time be the result of evaluating:
// ready time - current time to match / playback rate
// for animation.
// If the playback rate is zero, let new start time be simply ready
// time.
// B.5 Set the start time of animation to new start time.
const currentTimeToMatch =
(timelineTime - details.startTime) * details.animation.playbackRate;
applyPendingPlaybackRate(details);
const playbackRate = details.animation.playbackRate;
if (playbackRate == 0) {
details.holdTime = null;
details.startTime = timelineTime;
} else {
details.startTime = timelineTime - currentTimeToMatch / playbackRate;
}
}
// 8.4 Resolve animation’s current ready promise with animation.
if (details.readyPromise && details.readyPromise.state == 'pending')
details.readyPromise.resolve(details.proxy);
// 8.5 Run the procedure to update an animation’s finished state for
// animation with the did seek flag set to false, and the
// synchronously notify flag set to false.
updateFinishedState(details, false, false);
// Additional polyfill step to update the native animation's current time.
syncCurrentTime(details);
details.pendingTask = null;
};
function commitPendingPause(details) {
// https://www.w3.org/TR/web-animations-1/#pausing-an-animation-section
// Refer to steps listed under "Schedule a task to run ..."
// 1. Let ready time be the time value of the timeline associated with
// animation at the moment when the user agent completed processing
// necessary to suspend playback of animation’s target effect.
const readyTime = fromCssNumberish(details, details.timeline.currentTime);
// 2. If animation’s start time is resolved and its hold time is not
// resolved, let animation’s hold time be the result of evaluating
// (ready time - start time) × playback rate.
if (details.startTime != null && details.holdTime == null) {
details.holdTime =
(readyTime - details.startTime) * details.animation.playbackRate;
}
// 3. Apply any pending playback rate on animation.
applyPendingPlaybackRate(details);
// 4. Make animation’s start time unresolved.
details.startTime = null;
// 5. Resolve animation’s current ready promise with animation.
details.readyPromise.resolve(details.proxy);
// 6. Run the procedure to update an animation’s finished state for
// animation with the did seek flag set to false, and the synchronously
// notify flag set to false.
updateFinishedState(details, false, false);
// Additional polyfill step to update the native animation's current time.
syncCurrentTime(details);
details.pendingTask = null;
};
function commitFinishedNotification(details) {
if (!details.finishedPromise || details.finishedPromise.state != 'pending')
return;
if (details.proxy.playState != 'finished')
return;
details.finishedPromise.resolve(details.proxy);
details.animation.pause();
// Event times are speced as doubles in web-animations-1.
// Cannot dispatch a proxy to an event since the proxy is not a fully
// transparent replacement. As a workaround, use a custom event and inject
// the necessary getters.
const finishedEvent =
new CustomEvent('finish',
{ detail: {
currentTime: details.proxy.currentTime,
timelineTime: details.proxy.timeline.currentTime
}});
Object.defineProperty(finishedEvent, 'currentTime', {
get: function() { return this.detail.currentTime; }
});
Object.defineProperty(finishedEvent, 'timelineTime', {
get: function() { return this.detail.timelineTime; }
});
requestAnimationFrame(() => {
queueMicrotask(() => {
details.animation.dispatchEvent(finishedEvent);
});
});
}
function effectivePlaybackRate(details) {
if (details.pendingPlaybackRate !== null)
return details.pendingPlaybackRate;
return details.animation.playbackRate;
}
function applyPendingPlaybackRate(details) {
if (details.pendingPlaybackRate !== null) {
details.animation.playbackRate = details.pendingPlaybackRate;
details.pendingPlaybackRate = null;
}
}
function calculateCurrentTime(details) {
if (!details.timeline)
return null;
const timelineTime = fromCssNumberish(details, details.timeline.currentTime);
if (timelineTime === null)
return null;
if (details.startTime === null)
return null;
let currentTime =
(timelineTime - details.startTime) * details.animation.playbackRate;
// Handle special case.
if (currentTime == -0)
currentTime = 0;
return currentTime;
}
function calculateStartTime(details, currentTime) {
if (!details.timeline)
return null;
const timelineTime = fromCssNumberish(details, details.timeline.currentTime);
if (timelineTime == null)
return null;
return timelineTime - currentTime / details.animation.playbackRate;
}
function updateFinishedState(details, didSeek, synchronouslyNotify) {
if (!details.timeline)
return;
// https://www.w3.org/TR/web-animations-1/#updating-the-finished-state
// 1. Calculate the unconstrained current time. The dependency on did_seek is
// required to accommodate timelines that may change direction. Without this
// distinction, a once-finished animation would remain finished even when its
// timeline progresses in the opposite direction.
let unconstrainedCurrentTime =
didSeek ? fromCssNumberish(details, details.proxy.currentTime)
: calculateCurrentTime(details);
// 2. Conditionally update the hold time.
if (unconstrainedCurrentTime && details.startTime != null &&
!details.proxy.pending) {
// Can seek outside the bounds of the active effect. Set the hold time to
// the unconstrained value of the current time in the event that this update
// is the result of explicitly setting the current time and the new time
// is out of bounds. An update due to a time tick should not snap the hold
// value back to the boundary if previously set outside the normal effect
// boundary. The value of previous current time is used to retain this
// value.
const playbackRate = effectivePlaybackRate(details);
const upperBound = effectEnd(details);
let boundary = details.previousCurrentTime;
// TODO: Support hold phase.
if (playbackRate > 0 && unconstrainedCurrentTime >= upperBound) {
if (boundary === null || boundary < upperBound)
boundary = upperBound;
details.holdTime = didSeek ? unconstrainedCurrentTime : boundary;
} else if (playbackRate < 0 && unconstrainedCurrentTime <= 0) {
if (boundary == null || boundary > 0)
boundary = 0;
details.holdTime = didSeek ? unconstrainedCurrentTime : boundary;
} else if (playbackRate != 0) {
// Update start time and reset hold time.
if (didSeek && details.holdTime !== null)
details.startTime = calculateStartTime(details, details.holdTime);
details.holdTime = null;
}
}
// Additional step to ensure that the native animation has the same value for
// current time as the proxy.
syncCurrentTime(details);
// 3. Set the previous current time.
details.previousCurrentTime = fromCssNumberish(details,
details.proxy.currentTime);
// 4. Set the current finished state.
const playState = details.proxy.playState;
if (playState == 'finished') {
if (!details.finishedPromise)
details.finishedPromise = new PromiseWrapper();
if (details.finishedPromise.state == 'pending') {
// 5. Setup finished notification.
if (synchronouslyNotify) {
commitFinishedNotification(details);
} else {
Promise.resolve().then(() => {
commitFinishedNotification(details);
});
}
}
} else {
// 6. If not finished but the current finished promise is already resolved,
// create a new promise.
if (details.finishedPromise &&
details.finishedPromise.state == 'resolved') {
details.finishedPromise = new PromiseWrapper();
}
if (details.animation.playState != 'paused')
details.animation.pause();
}
}
function effectEnd(details) {
// https://www.w3.org/TR/web-animations-1/#end-time
const timing = normalizedTiming(details);
const totalDuration =
timing.delay + timing.endDelay + timing.iterations * timing.duration;
return Math.max(0, totalDuration);
}
function hasActiveTimeline(details) {
return !details.timeline || details.timeline.phase != 'inactive';
}
function syncCurrentTime(details) {
if (!details.timeline)
return;
if (details.startTime !== null) {
const timelineTime = fromCssNumberish(details,
details.timeline.currentTime);
details.animation.currentTime =
(timelineTime - details.startTime) *
details.animation.playbackRate;
} else if (details.holdTime !== null) {
details.animation.currentTime = details.holdTime;
}
}
function resetPendingTasks(details) {
// https://www.w3.org/TR/web-animations-1/#reset-an-animations-pending-tasks
// 1. If animation does not have a pending play task or a pending pause task,
// abort this procedure.
if (!details.pendingTask)
return;
// 2. If animation has a pending play task, cancel that task.
// 3. If animation has a pending pause task, cancel that task.
details.pendingTask = null;
// 4. Apply any pending playback rate on animation.
applyPendingPlaybackRate(details);
// 5. Reject animation’s current ready promise with a DOMException named
// "AbortError".
details.readyPromise.reject(createAbortError());
// 6. Let animation’s current ready promise be the result of creating a new
// resolved Promise object.
createReadyPromise(details);
details.readyPromise.resolve(details.proxy);
}
function playInternal(details, autoRewind) {
if (!details.timeline)
return;
// https://drafts.csswg.org/web-animations/#playing-an-animation-section.
// 1. Let aborted pause be a boolean flag that is true if animation has a
// pending pause task, and false otherwise.
const abortedPause =
details.proxy.playState == 'paused' && details.proxy.pending;
// 2. Let has pending ready promise be a boolean flag that is initially
// false.
let hasPendingReadyPromise = false;
// 3. Let seek time be a time value that is initially unresolved.
let seekTime = null;
// 4. Let has finite timeline be true if animation has an associated
// timeline that is not monotonically increasing.
// Note: this value will always true at this point in the polyfill.
// Following steps are pruned based on the procedure for scroll
// timelines.
// 5. Perform the steps corresponding to the first matching condition from
// the following, if any:
//
// 5a If animation’s effective playback rate > 0, the auto-rewind flag is
// true and either animation’s:
// current time is unresolved, or
// current time < zero, or
// current time >= target effect end,
// 5a1. Set seek time to zero.
//
// 5b If animation’s effective playback rate < 0, the auto-rewind flag is
// true and either animation’s:
// current time is unresolved, or
// current time ≤ zero, or
// current time > target effect end,
// 5b1. If associated effect end is positive infinity,
// throw an "InvalidStateError" DOMException and abort these steps.
// 5b2. Otherwise,
// 5b2a Set seek time to animation's associated effect end.
//
// 5c If animation’s effective playback rate = 0 and animation’s current time
// is unresolved,
// 5c1. Set seek time to zero.
let previousCurrentTime = fromCssNumberish(details,
details.proxy.currentTime);
// Resume of a paused animation after a timeline change snaps to the scroll
// position.
if (details.resetCurrentTimeOnResume) {
previousCurrentTime = null;
details.resetCurrentTimeOnResume = false;
}
const playbackRate = effectivePlaybackRate(details);
const upperBound = effectEnd(details);
if (playbackRate > 0 && autoRewind && (previousCurrentTime == null ||
previousCurrentTime < 0 ||
previousCurrentTime >= upperBound)) {
seekTime = 0;
} else if (playbackRate < 0 && autoRewind &&
(previousCurrentTime == null || previousCurrentTime <= 0 ||
previousCurrentTime > upperBound)) {
if (upperBound == Infinity) {
// Defer to native implementation to handle throwing the exception.
details.animation.play();
return;
}
seekTime = upperBound;
} else if (playbackRate == 0 && previousCurrentTime == null) {
seekTime = 0;
}
// 6. If seek time is resolved,
// 6a1. Set animation's start time to seek time.
// 6a2. Let animation's hold time be unresolved.
// 6a3. Apply any pending playback rate on animation.
if (seekTime != null) {
details.startTime = seekTime;
details.holdTime = null;
applyPendingPlaybackRate(details);
}
// Additional step for the polyfill.
addAnimation(details.timeline, details.animation,
tickAnimation.bind(details.proxy));
// 7. If animation's hold time is resolved, let its start time be
// unresolved.
if (details.holdTime) {
details.startTime = null;
}
// 8. If animation has a pending play task or a pending pause task,
// 8.1 Cancel that task.
// 8.2 Set has pending ready promise to true.
if (details.pendingTask) {
details.pendingTask = null;
hasPendingReadyPromise = true;
}
// 9. If the following three conditions are all satisfied:
// animation’s hold time is unresolved, and
// seek time is unresolved, and
// aborted pause is false, and
// animation does not have a pending playback rate,
// abort this procedure.
if (details.holdTime === null && seekTime === null &&
!abortedPause && details.pendingPlaybackRate === null)
return;
// 10. If has pending ready promise is false, let animation’s current ready
// promise be a new promise in the relevant Realm of animation.
if (details.readyPromise && !hasPendingReadyPromise)
details.readyPromise = null;
// Additional polyfill step to ensure that the native animation has the
// correct value for current time.
syncCurrentTime(details);
// 11. Schedule a task to run as soon as animation is ready.
if (!details.readyPromise)
createReadyPromise(details);
details.pendingTask = 'play';
// 12. Run the procedure to update an animation’s finished state for animation
// with the did seek flag set to false, and the synchronously notify flag
// set to false.
updateFinishedState(details, /* seek */ false, /* synchronous */ false);
}
function tickAnimation(timelineTime) {
const details = proxyAnimations.get(this);
if (timelineTime == null) {
// While the timeline is inactive, it's effect should not be applied.
// To polyfill this behavior, we cancel the underlying animation.
if (details.animation.playState != 'idle')
details.animation.cancel();
return;
}
if (details.pendingTask) {
notifyReady(details);
}
const playState = this.playState;
if (playState == 'running' || playState == 'finished') {
const timelineTimeMs = fromCssNumberish(details, timelineTime);
details.animation.currentTime =
(timelineTimeMs - fromCssNumberish(details, this.startTime)) *
this.playbackRate;
// Conditionally reset the hold time so that the finished state can be
// properly recomputed.
if (playState == 'finished' && effectivePlaybackRate(details) != 0)
details.holdTime = null;
updateFinishedState(details, false, false);
}
}
function notifyReady(details) {
if (details.pendingTask == 'pause') {
commitPendingPause(details);
} else if (details.pendingTask == 'play') {
commitPendingPlay(details);
}
}
function createProxyEffect(details) {
const effect = details.animation.effect;
const nativeUpdateTiming = effect.updateTiming;
// Generic pass-through handler for any method or attribute that is not
// explicitly overridden.
const handler = {
get: function(obj, prop) {
const result = obj[prop];
if (typeof result === 'function')
return result.bind(effect);
return result;
},
set: function(obj, prop, value) {
obj[prop] = value;
return true;
}
};
// Override getComputedTiming to convert to percentages when using a
// progress-based timeline.
const getComputedTimingHandler = {
apply: function(target) {
// Ensure that the native animation is using normalized values.
effect.getTiming();
const timing = target.apply(effect);
if (details.timeline) {
const preConvertLocalTime = timing.localTime;
timing.localTime = toCssNumberish(details, timing.localTime);
timing.endTime = toCssNumberish(details, timing.endTime);
timing.activeDuration =
toCssNumberish(details, timing.activeDuration);
const limit = effectEnd(details);
const iteration_duration = timing.iterations ?
(limit - timing.delay - timing.endDelay) / timing.iterations : 0;
timing.duration = limit ?
CSS.percent(100 * iteration_duration / limit) :
CSS.percent(0);
// Correct for timeline phase.
const phase = details.timeline.phase;
const fill = timing.fill;
if(phase == 'before' && fill != 'backwards' && fill != 'both') {
timing.progress = null;
}
if (phase == 'after' && fill != 'forwards' && fill != 'both') {
timing.progress = null;
}
// Correct for inactive timeline.
if (details.timeline.currentTime === undefined) {
timing.localTime = null;
}
}
return timing;
}
};
// Override getTiming to normalize the timing. EffectEnd for the animation
// align with the timeline duration.
const getTimingHandler = {
apply: function(target, thisArg) {
// Arbitrary conversion of 100% to ms.
const INTERNAL_DURATION_MS = 100000;
if (details.specifiedTiming)
return details.specifiedTiming;
details.specifiedTiming = target.apply(effect);
let timing = Object.assign({}, details.specifiedTiming);
let totalDuration;
// Duration 'auto' case.
if (timing.duration === null || timing.duration === 'auto') {
if (details.timeline) {
// TODO: start and end delay are specced as doubles and currently
// ignored for a progress based animation. Support delay and endDelay
// once CSSNumberish.
timing.delay = 0;
timing.endDelay = 0;
totalDuration = timing.iterations ? INTERNAL_DURATION_MS : 0;
timing.duration =
timing.iterations ? totalDuration / timing.iterations : 0;
// Set the timing on the native animation to the normalized values
// while preserving the specified timing.
nativeUpdateTiming.apply(effect, [timing]);
}
}
details.normalizedTiming = timing;
return details.specifiedTiming;
}
};
const updateTimingHandler = {
apply: function(target, thisArg, argumentsList) {
// Additional validation that is specific to scroll timelines.
if (details.timeline) {
const options = argumentsList[0];
const duration = options.duration;
if (duration === Infinity) {
throw TypeError(
"Effect duration cannot be Infinity when used with Scroll " +
"Timelines");
}
const iterations = options.iterations;
if (iterations === Infinity) {
throw TypeError(
"Effect iterations cannot be Infinity when used with Scroll " +
"Timelines");
}
}
// Apply updates on top of the original specified timing.
if (details.specifiedTiming) {
target.apply(effect, [details.specifiedTiming]);
}
target.apply(effect, argumentsList);
// Force renormalization.
details.specifiedTiming = null;
}
};
const proxy = new Proxy(effect, handler);
proxy.getComputedTiming = new Proxy(effect.getComputedTiming,
getComputedTimingHandler);
proxy.getTiming = new Proxy(effect.getTiming, getTimingHandler);
proxy.updateTiming = new Proxy(effect.updateTiming, updateTimingHandler);
return proxy;
}
// Create an alternate Animation class which proxies API requests.
// TODO: Create a full-fledged proxy so missing methods are automatically
// fetched from Animation.
let proxyAnimations = new WeakMap();
export class ProxyAnimation {
constructor(effect, timeline) {
const animation =
(effect instanceof nativeAnimation) ?
effect : new nativeAnimation(effect, animationTimeline);
const isScrollAnimation = timeline instanceof ScrollTimeline;
const animationTimeline = isScrollAnimation ? undefined : timeline;
proxyAnimations.set(this, {
animation: animation,
timeline: isScrollAnimation ? timeline : undefined,
playState: isScrollAnimation ? "idle" : null,
readyPromise: null,
finishedPromise: null,
// Start and hold times are directly tracked in the proxy despite being
// accessible via the animation so that direct manipulation of these
// properties does not affect the play state of the underlying animation.
// Note that any changes to these values require an update of current
// time for the underlying animation to ensure that its hold time is set
// to the correct position. These values are represented as floating point
// numbers in milliseconds.
startTime: null,
holdTime: null,
previousCurrentTime: null,
// When changing the timeline on a paused animation, we defer updating the
// start time until the animation resumes playing.
resetCurrentTimeOnResume: false,
// Calls to reverse and updatePlaybackRate set a pending rate that does
// not immediately take effect. The value of this property is
// inaccessible via the web animations API and therefore explicitly
// tracked.
pendingPlaybackRate: null,
pendingTask: null,
// Record the specified timing since it may be different than the timing
// actually used for the animation. When fetching the timing, this value
// will be returned, however, the native animation will use normalized
// values.
specifiedTiming: null,
// The normalized timing has the corrected timing with the intrinsic
// iteration duration resolved.
normalizedTiming: null,
// Effect proxy that performs the necessary time conversions when using a
// progress-based timelines.
effect: null,
proxy: this
});
}
// -----------------------------------------
// Web animation API
// -----------------------------------------
get effect() {
const details = proxyAnimations.get(this);
if (!details.timeline)
return details.animation.effect;
// Proxy the effect to support timing conversions for progress based
// animations.
if (!details.effect)
details.effect = createProxyEffect(details);
return details.effect;
}
set effect(newEffect) {
proxyAnimations.get(this).animation.effect = newEffect;
// Reset proxy to force re-initialization the next time it is accessed.
details.effect = null;
}
get timeline() {
const details = proxyAnimations.get(this);
// If we explicitly set a null timeline we will return the underlying
// animation's timeline.
return details.timeline || details.animation.timeline;
}
set timeline(newTimeline) {
// https://drafts4.csswg.org/web-animations-2/#setting-the-timeline
// 1. Let old timeline be the current timeline of animation, if any.
// 2. If new timeline is the same object as old timeline, abort this
// procedure.
const oldTimeline = this.timeline;
if (oldTimeline == newTimeline)
return;
// 3. Let previous play state be animation’s play state.
const previousPlayState = this.playState;
// 4. Let previous current time be the animation’s current time.
const previousCurrentTime = this.currentTime;
const details = proxyAnimations.get(this);
const end = effectEnd(details);
const progress =
end > 0 ? fromCssNumberish(details, previousCurrentTime) / end : 0;
// 5. Let from finite timeline be true if old timeline is not null and not
// monotonically increasing.
const fromScrollTimeline = (oldTimeline instanceof ScrollTimeline);
// 6. Let to finite timeline be true if timeline is not null and not
// monotonically increasing.
const toScrollTimeline = (newTimeline instanceof ScrollTimeline);
// 7. Let the timeline of animation be new timeline.
// Cannot assume that the native implementation has mutable timeline
// support. Deferring this step until we know that we are either
// polyfilling, supporting natively, or throwing an error.
// 8. Set the flag reset current time on resume to false.
details.resetCurrentTimeOnResume = false;
// Additional step required to track whether the animation was pending in
// order to set up a new ready promise if needed.
const pending = this.pending;
if (fromScrollTimeline) {
removeAnimation(details.timeline, details.animation);
}
// 9. Perform the steps corresponding to the first matching condition from
// the following, if any:
// If to finite timeline,
if (toScrollTimeline) {
// Deferred step 7.
details.timeline = newTimeline;
// 1. Apply any pending playback rate on animation
applyPendingPlaybackRate(details);
// 2. Let seek time be zero if playback rate >= 0, and animation’s
// associated effect end otherwise.
const seekTime =
details.animation.playbackRate >= 0 ? 0 : effectEnd(details);
// 3. Update the animation based on the first matching condition if any:
switch (previousPlayState) {
// If either of the following conditions are true:
// * previous play state is running or,
// * previous play state is finished
// Set animation’s start time to seek time.
case 'running':
case 'finished':
details.startTime = seekTime;
// Additional polyfill step needed to associate the animation with
// the scroll timeline.
addAnimation(details.timeline, details.animation,
tickAnimation.bind(this));
break;
// If previous play state is paused:
// If previous current time is resolved:
// * Set the flag reset current time on resume to true.
// * Set start time to unresolved.
// * Set hold time to previous current time.
case 'paused':
details.resetCurrentTimeOnResume = true;
details.startTime = null;
details.holdTime =
fromCssNumberish(details, CSS.percent(100 * progress));
break;
// Oterwise
default:
details.holdTime = null;
details.startTime = null;
}
// Additional steps required if the animation is pending as we need to
// associate the pending promise with proxy animation.
// Note: if the native promise already has an associated "then", we will
// lose this association.
if (pending) {
if (!details.readyPromise ||
details.readyPromise.state == 'resolved') {
createReadyPromise(details);
}
if (previousPlayState == 'paused')
details.pendingTask = 'pause';
else
details.pendingTask = 'play';
}
// Note that the following steps should apply when transitioning to
// a monotonic timeline as well; however, we do not have a direct means
// of applying the steps to the native animation.
// 10. If the start time of animation is resolved, make animation’s hold
// time unresolved. This step ensures that the finished play state of
// animation is not “sticky” but is re-evaluated based on its updated
// current time.
if (details.startTime !== null)
details.holdTime = null;
// 11. Run the procedure to update an animation’s finished state for
// animation with the did seek flag set to false, and the
// synchronously notify flag set to false.
updateFinishedState(details, false, false);
return;
}
// To monotonic timeline.
if (details.animation.timeline == newTimeline) {
// Deferred step 7 from above. Clearing the proxy's timeline will
// re-associate the proxy with the native animation.
removeAnimation(details.timeline, details.animation);
details.timeline = null;
// If from finite timeline and previous current time is resolved,
// Run the procedure to set the current time to previous current time.
if (fromScrollTimeline) {
if (previousCurrentTime !== null)
details.animation.currentTime = progress * effectEnd(details);
switch (previousPlayState) {
case 'paused':
details.animation.pause();
break;
case 'running':
case 'finished':
details.animation.play();
}
}
} else {
throw TypeError("Unsupported timeline: " + newTimeline);
}
}
get startTime() {
const details = proxyAnimations.get(this);
if (details.timeline)
return toCssNumberish(details, details.startTime);
return details.animation.startTime;
}
set startTime(value) {
// https://drafts.csswg.org/web-animations/#setting-the-start-time-of-an-animation
const details = proxyAnimations.get(this);
value = fromCssNumberish(details, value);
if (!details.timeline) {
details.animation.startTime = value;
return;
}
// 1. Let timeline time be the current time value of the timeline that
// animation is associated with. If there is no timeline associated with
// animation or the associated timeline is inactive, let the timeline
// time be unresolved.
const timelineTime = fromCssNumberish(details,
details.timeline.currentTime);
// 2. If timeline time is unresolved and new start time is resolved, make
// animation’s hold time unresolved.
if (timelineTime == null && details.startTime != null) {
details.holdTime = null;
// Clearing the hold time may have altered the value of current time.
// Ensure that the underlying animations has the correct value.
syncCurrentTime(details);
}
// 3. Let previous current time be animation’s current time.
// Note: This is the current time after applying the changes from the
// previous step which may cause the current time to become unresolved.
const previousCurrentTime = fromCssNumberish(details, this.currentTime);
// 4. Apply any pending playback rate on animation.
applyPendingPlaybackRate(details);
// 5. Set animation’s start time to new start time.
details.startTime = value;
// 6. Set the reset current time on resume flag to false.