From da59f0ff44f26aa67352b0798654e7d813787a57 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Sat, 27 Oct 2018 11:30:28 +0200 Subject: Make scroll animation less "drunk" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tries to use the new bezier curves to approximate our old smooth scrolling behavior. Change-Id: I3b319025fb0339ec95c0a29525f2dde0631866ed Reviewed-by: Michael BrĂ¼ning --- .../cc/animation/scroll_offset_animation_curve.cc | 23 ++++++++++++++-------- .../cc/animation/scroll_offset_animation_curve.h | 2 +- .../scroll_offset_animation_curve_factory.cc | 2 +- .../renderer/platform/animation/timing_function.cc | 6 ++++++ .../ui/gfx/animation/keyframe/timing_function.cc | 3 +++ .../ui/gfx/animation/keyframe/timing_function.h | 2 +- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/chromium/cc/animation/scroll_offset_animation_curve.cc b/chromium/cc/animation/scroll_offset_animation_curve.cc index 1b4d045c74c..a74a03f1f11 100644 --- a/chromium/cc/animation/scroll_offset_animation_curve.cc +++ b/chromium/cc/animation/scroll_offset_animation_curve.cc @@ -56,13 +56,13 @@ static float MaximumDimension(const gfx::Vector2dF& delta) { static std::unique_ptr EaseInOutWithInitialSlope(double slope) { // Clamp slope to a sane value. - slope = base::ClampToRange(slope, -1000.0, 1000.0); + slope = base::ClampToRange(slope, -100.0, 100.0); - // Based on CubicBezierTimingFunction::EaseType::EASE_IN_OUT preset + // Based on CubicBezierTimingFunction::EaseType::EASE_OUT_NATURAL preset // with first control point scaled. - const double x1 = 0.42; + const double x1 = 0.25; const double y1 = slope * x1; - return CubicBezierTimingFunction::Create(x1, y1, 0.58, 1); + return CubicBezierTimingFunction::Create(x1, y1, 0.45, 0.94); } std::unique_ptr ImpulseCurveWithInitialSlope(double slope) { @@ -141,13 +141,17 @@ ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve( duration_behavior_(duration_behavior), has_set_initial_value_(false) { DCHECK_EQ((animation_type == AnimationType::kEaseInOut || - animation_type == AnimationType::kImpulse), + animation_type == AnimationType::kImpulse || animation_type_ == AnimationType::kEaseOutNatural), duration_behavior.has_value()); switch (animation_type) { case AnimationType::kEaseInOut: timing_function_ = CubicBezierTimingFunction::CreatePreset( CubicBezierTimingFunction::EaseType::EASE_IN_OUT); break; + case AnimationType::kEaseOutNatural: + timing_function_ = CubicBezierTimingFunction::CreatePreset( + CubicBezierTimingFunction::EaseType::EASE_OUT_NATURAL); + break; case AnimationType::kLinear: timing_function_ = LinearTimingFunction::Create(); break; @@ -168,7 +172,7 @@ ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve( duration_behavior_(duration_behavior), has_set_initial_value_(false) { DCHECK_EQ((animation_type == AnimationType::kEaseInOut || - animation_type == AnimationType::kImpulse), + animation_type == AnimationType::kImpulse || animation_type_ == AnimationType::kEaseOutNatural), duration_behavior.has_value()); } @@ -229,6 +233,8 @@ base::TimeDelta ScrollOffsetAnimationCurve::SegmentDuration( base::TimeDelta delayed_by, absl::optional velocity) { switch (animation_type_) { + case AnimationType::kEaseOutNatural: + // FIXME Check! case AnimationType::kEaseInOut: DCHECK(duration_behavior_.has_value()); return EaseInOutSegmentDuration(delta, duration_behavior_.value(), @@ -389,7 +395,7 @@ void ScrollOffsetAnimationCurve::UpdateTarget( base::TimeDelta delayed_by = std::max(base::TimeDelta(), last_retarget_ - t); t = std::max(t, last_retarget_); - if (animation_type_ == AnimationType::kEaseInOut && + if ((animation_type_ == AnimationType::kEaseInOut || animation_type_ == AnimationType::kEaseOutNatural) && std::abs(MaximumDimension(target_value_.DeltaFrom(new_target))) < kEpsilon) { // Don't update the animation if the new target is the same as the old one. @@ -439,7 +445,8 @@ void ScrollOffsetAnimationCurve::UpdateTarget( velocity * (new_duration.InSecondsF() / MaximumDimension(new_delta)); DCHECK(animation_type_ == AnimationType::kImpulse || - animation_type_ == AnimationType::kEaseInOut); + animation_type_ == AnimationType::kEaseInOut || + animation_type_ == AnimationType::kEaseOutNatural); if (animation_type_ == AnimationType::kImpulse && IsNewTargetInOppositeDirection(current_position, target_value_, new_target)) { diff --git a/chromium/cc/animation/scroll_offset_animation_curve.h b/chromium/cc/animation/scroll_offset_animation_curve.h index e1ec50fa85e..2b9896e2247 100644 --- a/chromium/cc/animation/scroll_offset_animation_curve.h +++ b/chromium/cc/animation/scroll_offset_animation_curve.h @@ -118,7 +118,7 @@ class CC_ANIMATION_EXPORT ScrollOffsetAnimationCurve FRIEND_TEST_ALL_PREFIXES(ScrollOffsetAnimationCurveTest, ImpulseUpdateTargetSwitchDirections); friend class ScrollOffsetAnimationCurveFactory; - enum class AnimationType { kLinear, kEaseInOut, kImpulse }; + enum class AnimationType { kLinear, kEaseInOut, kImpulse, kEaseOutNatural }; // |duration_behavior| should be provided if (and only if) |animation_type| is // kEaseInOut. diff --git a/chromium/cc/animation/scroll_offset_animation_curve_factory.cc b/chromium/cc/animation/scroll_offset_animation_curve_factory.cc index 469792b5d83..f893b4b2efb 100644 --- a/chromium/cc/animation/scroll_offset_animation_curve_factory.cc +++ b/chromium/cc/animation/scroll_offset_animation_curve_factory.cc @@ -72,7 +72,7 @@ ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimation( const gfx::ScrollOffset& target_value, ScrollOffsetAnimationCurve::DurationBehavior duration_behavior) { return base::WrapUnique(new ScrollOffsetAnimationCurve( - target_value, ScrollOffsetAnimationCurve::AnimationType::kEaseInOut, + target_value, ScrollOffsetAnimationCurve::AnimationType::kEaseOutNatural, duration_behavior)); } diff --git a/chromium/third_party/blink/renderer/platform/animation/timing_function.cc b/chromium/third_party/blink/renderer/platform/animation/timing_function.cc index d61c9fa12c6..35520c8aef3 100644 --- a/chromium/third_party/blink/renderer/platform/animation/timing_function.cc +++ b/chromium/third_party/blink/renderer/platform/animation/timing_function.cc @@ -37,6 +37,9 @@ CubicBezierTimingFunction* CubicBezierTimingFunction::Preset( DEFINE_STATIC_REF( CubicBezierTimingFunction, ease_in_out, (base::AdoptRef(new CubicBezierTimingFunction(EaseType::EASE_IN_OUT)))); + DEFINE_STATIC_REF( + CubicBezierTimingFunction, ease_out_natural, + (base::AdoptRef(new CubicBezierTimingFunction(EaseType::EASE_OUT_NATURAL)))); switch (ease_type) { case EaseType::EASE: @@ -47,6 +50,8 @@ CubicBezierTimingFunction* CubicBezierTimingFunction::Preset( return ease_out; case EaseType::EASE_IN_OUT: return ease_in_out; + case EaseType::EASE_OUT_NATURAL: + return ease_out_natural; default: NOTREACHED(); return nullptr; @@ -63,6 +68,7 @@ String CubicBezierTimingFunction::ToString() const { return "ease-out"; case CubicBezierTimingFunction::EaseType::EASE_IN_OUT: return "ease-in-out"; + case CubicBezierTimingFunction::EaseType::EASE_OUT_NATURAL: case CubicBezierTimingFunction::EaseType::CUSTOM: return "cubic-bezier(" + String::NumberToStringECMAScript(X1()) + ", " + String::NumberToStringECMAScript(Y1()) + ", " + diff --git a/chromium/ui/gfx/animation/keyframe/timing_function.cc b/chromium/ui/gfx/animation/keyframe/timing_function.cc index 9650a501bd6..2122ddb7432 100644 --- a/chromium/ui/gfx/animation/keyframe/timing_function.cc +++ b/chromium/ui/gfx/animation/keyframe/timing_function.cc @@ -34,6 +34,9 @@ CubicBezierTimingFunction::CreatePreset(EaseType ease_type) { case EaseType::EASE_IN_OUT: return base::WrapUnique( new CubicBezierTimingFunction(ease_type, 0.42, 0.0, 0.58, 1)); + case EaseType::EASE_OUT_NATURAL: + return base::WrapUnique( + new CubicBezierTimingFunction(ease_type, 0.26, 0.46, 0.45, 0.94)); default: NOTREACHED(); return nullptr; diff --git a/chromium/ui/gfx/animation/keyframe/timing_function.h b/chromium/ui/gfx/animation/keyframe/timing_function.h index 7ce71331dfe..e08faf805e9 100644 --- a/chromium/ui/gfx/animation/keyframe/timing_function.h +++ b/chromium/ui/gfx/animation/keyframe/timing_function.h @@ -37,7 +37,7 @@ class GFX_KEYFRAME_ANIMATION_EXPORT TimingFunction { class GFX_KEYFRAME_ANIMATION_EXPORT CubicBezierTimingFunction : public TimingFunction { public: - enum class EaseType { EASE, EASE_IN, EASE_OUT, EASE_IN_OUT, CUSTOM }; + enum class EaseType { EASE, EASE_IN, EASE_OUT, EASE_IN_OUT, EASE_OUT_NATURAL, CUSTOM }; static std::unique_ptr CreatePreset( EaseType ease_type); -- cgit v1.2.1