forked from flackr/scroll-timeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-animation.js
1965 lines (1685 loc) · 71.6 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 {
ANIMATION_RANGE_NAMES,
ScrollTimeline,
addAnimation,
removeAnimation,
fractionalOffset,
} from "./scroll-timeline-base";
import {splitIntoComponentValues} from './utils';
const nativeDocumentGetAnimations = document.getAnimations;
const nativeElementGetAnimations = window.Element.prototype.getAnimations;
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 ?? null;
if (timelineTime === null) {
return
}
// Run auto align start time procedure, in case measurements are ready
autoAlignStartTime(details);
if (details.pendingTask === 'play' && (details.startTime !== null || details.holdTime !== null)) {
commitPendingPlay(details);
} else if (details.pendingTask === 'pause') {
commitPendingPause(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 rangeDuration = details.rangeDuration ?? 100;
const limit = effectEnd(details);
const percent = limit ? rangeDuration * 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 (convertedTime)
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 rangeDuration = details.rangeDuration ?? 100;
const duration = effectEnd(details);
return value.value * duration / rangeDuration;
}
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;
}
}
/**
* Procedure to silently set the current time of an animation to seek time
* https://drafts.csswg.org/web-animations-2/#silently-set-the-current-time
* @param details
* @param {CSSUnitValue} seekTime
*/
function silentlySetTheCurrentTime(details, seekTime) {
// The procedure to silently set the current time of an animation, animation, to seek time is as follows:
// 1. If seek time is an unresolved time value, then perform the following steps.
// 1. If the current time is resolved, then throw a TypeError.
// 2. Abort these steps.
if (seekTime == null) {
if (details.currentTime !== null) {
throw new TypeError();
}
}
// 2. Let valid seek time be the result of running the validate a CSSNumberish time procedure with seek time as the input.
// 3. If valid seek time is false, abort this procedure.
seekTime = fromCssNumberish(details, seekTime);
// 4. Set auto align start time to false.
details.autoAlignStartTime = false;
// 5. Update either animation’s hold time or start time as follows:
//
// 5a If any of the following conditions are true:
// - animation’s hold time is resolved, or
// - animation’s start time is unresolved, or
// - animation has no associated timeline or the associated timeline is inactive, or
// - animation’s playback rate is 0,
// 1. Set animation’s hold time to seek time.
//
// 5b Otherwise,
// Set animation’s start time to the result of evaluating timeline time - (seek time / playback rate) where
// timeline time is the current time value of timeline associated with animation.
if (details.holdTime !== null || details.startTime === null ||
details.timeline.phase === 'inactive' || details.animation.playbackRate === 0) {
details.holdTime = seekTime;
} else {
details.startTime =
fromCssNumberish(details, details.timeline.currentTime) - seekTime / details.animation.playbackRate;
}
// 6. If animation has no associated timeline or the associated timeline is inactive, make animation’s start time
// unresolved.
// This preserves the invariant that when we don’t have an active timeline it is only possible to set either the
// start time or the animation’s current time.
if (details.timeline.phase === 'inactive') {
details.startTime = null;
}
// 7. Make animation’s previous current time unresolved.
details.previousCurrentTime = 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;
if (playbackRate > 0 && unconstrainedCurrentTime >= upperBound &&
details.previousCurrentTime != null) {
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 = details.timeline.currentTime;
if (timelineTime == null)
return;
const timelineTimeMs = fromCssNumberish(details, timelineTime);
setNativeCurrentTime(details,
(timelineTimeMs - details.startTime) *
details.animation.playbackRate);
} else if (details.holdTime !== null) {
setNativeCurrentTime(details, details.holdTime);
}
}
// Sets the time of the underlying animation, nudging the time slightly if at
// a scroll-timeline boundary to remain in the active phase.
function setNativeCurrentTime(details, time) {
const timeline = details.timeline;
const playbackRate = details.animation.playbackRate;
const atScrollTimelineBoundary =
timeline.currentTime &&
timeline.currentTime.value == (playbackRate < 0 ? 0 : 100);
const delta =
atScrollTimelineBoundary ? (playbackRate < 0 ? 0.001 : -0.001) : 0;
details.animation.currentTime = time + delta;
}
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 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.
//
// 4. Let previous current time be the animation’s current time
//
// 5. Let enable seek be true if the auto-rewind flag is true and has finite timeline is false.
// Otherwise, initialize to false.
//
// 6. Perform the steps corresponding to the first matching condition from
// the following, if any:
//
// 6a If animation’s effective playback rate > 0, enable seek is
// true and either animation’s:
// previous current time is unresolved, or
// previous current time < zero, or
// previous current time >= associated effect end,
// 6a1. Set the animation’s hold time to zero.
//
// 6b If animation’s effective playback rate < 0, enable seek is
// true and either animation’s:
// previous current time is unresolved, or
// previous current time is ≤ zero, or
// previous current time is > associated effect end,
// 6b1. If associated effect end is positive infinity,
// throw an "InvalidStateError" DOMException and abort these steps.
// 6b2. Otherwise,
// 5b2a Set the animation’s hold time to the animation’s associated effect end.
//
// 6c If animation’s effective playback rate = 0 and animation’s current time
// is unresolved,
// 6c1. Set the animation’s hold time to zero.
let previousCurrentTime = fromCssNumberish(details,
details.proxy.currentTime);
const playbackRate = effectivePlaybackRate(details);
if (playbackRate == 0 && previousCurrentTime == null) {
details.holdTime = 0;
}
// 7. If has finite timeline and previous current time is unresolved:
// Set the flag auto align start time to true.
// NOTE: If play is called for a CSS animation during style update, the animation’s start time cannot be reliably
// calculated until post layout since the start time is to align with the start or end of the animation range
// (depending on the playback rate). In this case, the animation is said to have an auto-aligned start time,
// whereby the start time is automatically adjusted as needed to align the animation’s progress to the
// animation range.
if (previousCurrentTime == null) {
details.autoAlignStartTime = true;
}
// Not by spec, but required by tests in play-animation.html:
// - Playing a finished animation restarts the animation aligned at the start
// - Playing a pause-pending but previously finished animation realigns with the scroll position
// - Playing a finished animation clears the start time
if (details.proxy.playState === 'finished' || abortedPause) {
details.holdTime = null
details.startTime = null
details.autoAlignStartTime = true;
}
// 8. If animation's hold time is resolved, let its start time be
// unresolved.
if (details.holdTime) {
details.startTime = null;
}
// 9. If animation has a pending play task or a pending pause task,
// 9.1 Cancel that task.
// 9.2 Set has pending ready promise to true.
if (details.pendingTask) {
details.pendingTask = null;
hasPendingReadyPromise = true;
}
// 10. If the following three conditions are all satisfied:
// animation’s hold time is unresolved, and
// aborted pause is false, and
// animation does not have a pending playback rate,
// abort this procedure.
// Additonal check for polyfill: Does not have the auto align start time flag set.
// If we return when this flag is set, a play task will not be scheduled, leaving the animation in the
// idle state. If the animation is in the idle state, the auto align procedure will bail.
// TODO: update with results of https://github.com/w3c/csswg-drafts/issues/9871
if (details.holdTime === null && !details.autoAlignStartTime &&
!abortedPause && details.pendingPlaybackRate === null)
return;
// 11. 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);
// 12. Schedule a task to run as soon as animation is ready.
if (!details.readyPromise)
createReadyPromise(details);
details.pendingTask = 'play';
// Additional step for the polyfill.
// This must run after setting up the ready promise, otherwise we will run
// the procedure for calculating auto aligned start time before play state is running
addAnimation(details.timeline, details.animation,
tickAnimation.bind(details.proxy));
// 13. 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 (!details) return;
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.proxy.playState !== 'paused' && details.animation.playState != 'idle')
details.animation.cancel();
return;
}
// When updating timeline current time, the start time of any attached animation is conditionally updated. For each
// attached animation, run the procedure for calculating an auto-aligned start time.
autoAlignStartTime(details);
if (details.pendingTask) {
// Commit pending tasks asynchronously if they are ready after aligning start time
requestAnimationFrame(() => {
if (details.pendingTask === 'play' && (details.startTime !== null || details.holdTime !== null)) {
commitPendingPlay(details);
} else if (details.pendingTask === 'pause') {
commitPendingPause(details);
}
});
}
const playState = this.playState;
if (playState == 'running' || playState == 'finished') {
const timelineTimeMs = fromCssNumberish(details, timelineTime);
setNativeCurrentTime(
details,
(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 renormalizeTiming(details) {
// Force renormalization.
details.specifiedTiming = null;
}
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 rangeDuration = details.duration ?? 100;
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(rangeDuration * iteration_duration / limit) :
CSS.percent(0);
// 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 range 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' || details.autoDurationEffect) {
if (details.timeline) {
details.autoDurationEffect = true
// 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.delay - timing.endDelay) /
timing.iterations
: 0;
// When the rangeStart comes after the rangeEnd, we end up in a situation
// that cannot work. We can tell this by having ended up with a negative
// duration. In that case, we need to adjust the computed timings. We do
// this by setting the duration to 0 and then assigning the remainder of
// the totalDuration to the endDelay
if (timing.duration < 0) {
timing.duration = 0;
timing.endDelay = totalDuration - timing.delay;
}
// 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) {
if (!argumentsList || !argumentsList.length)
return;
// Additional validation that is specific to scroll timelines.
if (details.timeline && argumentsList[0]) {
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");
}
if (typeof duration !== 'undefined' && duration !== 'auto') {
details.autoDurationEffect = null
}
}
// Apply updates on top of the original specified timing.
if (details.specifiedTiming) {
target.apply(effect, [details.specifiedTiming]);
}
target.apply(effect, argumentsList);
renormalizeTiming(details);
}
};
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;
}
// Computes the start delay as a fraction of the active cover range.
function fractionalStartDelay(details) {
if (!details.animationRange) return 0;
if (details.animationRange.start === 'normal') {
details.animationRange.start = getNormalStartRange(details.timeline);
}
return fractionalOffset(details.timeline, details.animationRange.start);
}
// Computes the ends delay as a fraction of the active cover range.
function fractionalEndDelay(details) {
if (!details.animationRange) return 0;
if (details.animationRange.end === 'normal') {
details.animationRange.end = getNormalEndRange(details.timeline);
}
return 1 - fractionalOffset(details.timeline, details.animationRange.end);
}
// Map from an instance of ProxyAnimation to internal details about that animation.
// See ProxyAnimation constructor for details.
let proxyAnimations = new WeakMap();
// Clear cache containing the ProxyAnimation instances when leaving the page.
// See https://github.com/flackr/scroll-timeline/issues/146#issuecomment-1698159183
// for details.
window.addEventListener('pagehide', (e) => {
proxyAnimations = new WeakMap();
}, false);
// Map from the real underlying native animation to the ProxyAnimation proxy of it.
let proxiedAnimations = new WeakMap();
/**
* Procedure for calculating an auto-aligned start time.
* https://drafts.csswg.org/web-animations-2/#animation-calculating-an-auto-aligned-start-time
* @param details
*/
function autoAlignStartTime(details) {
// When attached to a non-monotonic timeline, the start time of the animation may be layout dependent. In this case,
// we defer calculation of the start time until the timeline has been updated post layout. When updating timeline
// current time, the start time of any attached animation is conditionally updated. The procedure for calculating an
// auto-aligned start time is as follows:
// 1. If the auto-align start time flag is false, abort this procedure.
if (!details.autoAlignStartTime) {
return;
}
// 2. If the timeline is inactive, abort this procedure.
if (!details.timeline || !details.timeline.currentTime) {
return;
}
// 3. If play state is idle, abort this procedure.
// 4. If play state is paused, and hold time is resolved, abort this procedure.
if (details.proxy.playState === 'idle' ||
(details.proxy.playState === 'paused' && details.holdTime !== null)) {
return;
}
const previousRangeDuration = details.rangeDuration;
let startOffset, endOffset;
// 5. Let start offset be the resolved timeline time corresponding to the start of the animation attachment range.
// In the case of view timelines, it requires a calculation based on the proportion of the cover range.
try {
startOffset = CSS.percent(fractionalStartDelay(details) * 100);
} catch {
// TODO: Validate supported values for range start, to avoid exceptions when resolving the values.
// Range start is invalid, falling back to default value
startOffset = CSS.percent(0);
details.animationRange.start = 'normal';
console.warn("Exception when calculating start offset", e);
}
// 6. Let end offset be the resolved timeline time corresponding to the end of the animation attachment range.
// In the case of view timelines, it requires a calculation based on the proportion of the cover range.
try {
endOffset = CSS.percent((1 - fractionalEndDelay(details)) * 100);
} catch {
// TODO: Validate supported values for range end, to avoid exceptions when resolving the values.
// Range start is invalid, falling back to default value
endOffset = CSS.percent(100);
details.animationRange.end = 'normal';
console.warn("Exception when calculating end offset", e);
}
// Store the range duration, until we can find a spec aligned method to calculate iteration duration
// TODO: Clarify how range duration should be resolved
details.rangeDuration = endOffset.value - startOffset.value;
// 7. Set start time to start offset if effective playback rate ≥ 0, and end offset otherwise.
const playbackRate = effectivePlaybackRate(details);
details.startTime = fromCssNumberish(details,playbackRate >= 0 ? startOffset : endOffset);
// 8. Clear hold time.
details.holdTime = null;
// Additional polyfill step needed to renormalize timing when range has changed
if (details.rangeDuration !== previousRangeDuration) {
renormalizeTiming(details);
}
}
function unsupportedTimeline(timeline) {
throw new Error('Unsupported timeline class');
}
function getNormalStartRange(timeline) {
if (timeline instanceof ViewTimeline) {
return { rangeName: 'cover', offset: CSS.percent(0) };
}
if (timeline instanceof ScrollTimeline) {
return CSS.percent(0);
}
unsupportedTimeline(timeline);
}
function getNormalEndRange(timeline) {
if (timeline instanceof ViewTimeline) {
return { rangeName: 'cover', offset: CSS.percent(100) };
}
if (timeline instanceof ScrollTimeline) {
return CSS.percent(100);
}
unsupportedTimeline(timeline);
}
function parseAnimationRange(timeline, value) {
const animationRange = {
start: getNormalStartRange(timeline),
end: getNormalEndRange(timeline),
};
if (!value)
return animationRange;
if (timeline instanceof ViewTimeline) {
// Format:
// <start-name> <start-offset> <end-name> <end-offset>
// <name> --> <name> 0% <name> 100%
// <name> <start-offset> <end-offset> --> <name> <start-offset>
// <name> <end-offset>
// <start-offset> <end-offset> --> cover <start-offset> cover <end-offset>
// TODO: Support all formatting options once ratified in the spec.
const parts = splitIntoComponentValues(value);
const rangeNames = [];
const offsets = [];
parts.forEach(part => {
if (ANIMATION_RANGE_NAMES.includes(part)) {
rangeNames.push(part);
} else {
try {
offsets.push(CSSNumericValue.parse(part));
} catch (e) {
throw TypeError(`Could not parse range "${value}"`);
}
}
});
if (rangeNames.length > 2 || offsets.length > 2 || offsets.length == 1) {
throw TypeError("Invalid time range or unsupported time range format.");
}
if (rangeNames.length) {
animationRange.start.rangeName = rangeNames[0];
animationRange.end.rangeName = rangeNames.length > 1 ? rangeNames[1] : rangeNames[0];
}
if (offsets.length > 1) {