diff --git a/Userland/Libraries/LibWeb/Animations/AnimationEffect.cpp b/Userland/Libraries/LibWeb/Animations/AnimationEffect.cpp index 4b5da8f79d..e2ba046138 100644 --- a/Userland/Libraries/LibWeb/Animations/AnimationEffect.cpp +++ b/Userland/Libraries/LibWeb/Animations/AnimationEffect.cpp @@ -200,6 +200,58 @@ double AnimationEffect::active_duration() const return m_iteration_duration.get() * m_iteration_count; } +Optional AnimationEffect::active_time() const +{ + return active_time_using_fill(m_fill_mode); +} + +// https://www.w3.org/TR/web-animations-1/#calculating-the-active-time +Optional AnimationEffect::active_time_using_fill(Bindings::FillMode fill_mode) const +{ + // The active time is based on the local time and start delay. However, it is only defined when the animation effect + // should produce an output and hence depends on its fill mode and phase as follows, + + // -> If the animation effect is in the before phase, + if (is_in_the_before_phase()) { + // The result depends on the first matching condition from the following, + + // -> If the fill mode is backwards or both, + if (fill_mode == Bindings::FillMode::Backwards || fill_mode == Bindings::FillMode::Both) { + // Return the result of evaluating max(local time - start delay, 0). + return max(local_time().value() - m_start_delay, 0.0); + } + + // -> Otherwise, + // Return an unresolved time value. + return {}; + } + + // -> If the animation effect is in the active phase, + if (is_in_the_active_phase()) { + // Return the result of evaluating local time - start delay. + return local_time().value() - m_start_delay; + } + + // -> If the animation effect is in the after phase, + if (is_in_the_after_phase()) { + // The result depends on the first matching condition from the following, + + // -> If the fill mode is forwards or both, + if (fill_mode == Bindings::FillMode::Forwards || fill_mode == Bindings::FillMode::Both) { + // Return the result of evaluating max(local time - start delay - active duration, 0). + return max(local_time().value() - m_start_delay - active_duration(), 0.0); + } + + // -> Otherwise, + // Return an unresolved time value. + return {}; + } + + // -> Otherwise (the local time is unresolved), + // Return an unresolved time value. + return {}; +} + // https://www.w3.org/TR/web-animations-1/#before-active-boundary-time double AnimationEffect::before_active_boundary_time() const { diff --git a/Userland/Libraries/LibWeb/Animations/AnimationEffect.h b/Userland/Libraries/LibWeb/Animations/AnimationEffect.h index d2740b63d4..514ebbde84 100644 --- a/Userland/Libraries/LibWeb/Animations/AnimationEffect.h +++ b/Userland/Libraries/LibWeb/Animations/AnimationEffect.h @@ -97,6 +97,8 @@ public: double end_time() const; Optional local_time() const; double active_duration() const; + Optional active_time() const; + Optional active_time_using_fill(Bindings::FillMode) const; double before_active_boundary_time() const; double after_active_boundary_time() const;