[scroll-animations-1] currentTime for animations attached to a timeline attachment range · Issue #8669 · w3c/csswg-drafts (original) (raw)

While playing with ViewTimelines and trying to derive how much of an animation-range had already progressed, it surprised me that reading currentTime always returned the progress relative to the entire ViewTimeline range.

To demonstrate, I have a demo up on CodePen which you can check using the latest Chrome Canary. The demo tries to resprent the progression within the currently set animation-range for the ViewTimeline and has these controls:

On load, the animation-range is set to entry 20% entry 80%.

The demo outputs the currentTime in three possible ways:

  1. Directly read it from the animation.
    let progress = animation.currentTime;
    This doesn’t work when set to a value other than cover. This because italways returns the progress measured against the entire range of the ViewTimeline (which coincides with cover)
  2. Read it from the range
    let progress = animation.timeline.getCurrentTime(animation.rangeStart.rangeName).value;
    Using getCurrentTime[ForRange] doesn’t cut it here things here, as it only covers animations that have the same rangeName in animation-range-start and animation-range-end. Above that, it also only works for when the entire range is covered (from 0% to 100%).
  3. Read from the animation’s effect
    As a workaround, I turned to reading the animation’s effect, which did the trick:
    // This works
    let progress = animation.effect.getComputedTiming().progress * 100;
    if (animation.playState === 'finished') progress = 100;
    This, however, felt a lot like jumping through hoops to be honest.

To simplify things for authors, I would like to propose that currentTime (the method used in the first approach) returns the progress relative to the timeline attachment range.

I think this change also lines up with what @fantasai described here (and which was adopted by the WG):

The animation is attached to a timeline. But more specifically, it is attached to a timeline attachment range. By default this is the entire timeline, which in the case of a scroll-driven timeline is finite, and in the case of a time-driven timeline is infinite. The attachment range is the portion of the timeline that the animation is laid out in (somewhat analogous to a containing block).

Note that in this case getCurrentTime[ForRange] would still be relevant to keep, as it allows you to get the currentTime as if the range was spanning from 0% to 100%.

WDYT @fantasai @flackr @kevers-google @birtles?