| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/animation/scroll_offset_animation_curve.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cmath> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "cc/animation/timing_function.h" | |
| 12 #include "cc/base/time_util.h" | |
| 13 #include "ui/gfx/animation/tween.h" | |
| 14 | |
| 15 const double kDurationDivisor = 60.0; | |
| 16 | |
| 17 namespace cc { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 static float MaximumDimension(gfx::Vector2dF delta) { | |
| 22 return std::max(std::abs(delta.x()), std::abs(delta.y())); | |
| 23 } | |
| 24 | |
| 25 static base::TimeDelta DurationFromDelta(gfx::Vector2dF delta) { | |
| 26 // The duration of a scroll animation depends on the size of the scroll. | |
| 27 // The exact relationship between the size and the duration isn't specified | |
| 28 // by the CSSOM View smooth scroll spec and is instead left up to user agents | |
| 29 // to decide. The calculation performed here will very likely be further | |
| 30 // tweaked before the smooth scroll API ships. | |
| 31 return base::TimeDelta::FromMicroseconds( | |
| 32 (std::sqrt(MaximumDimension(delta)) / kDurationDivisor) * | |
| 33 base::Time::kMicrosecondsPerSecond); | |
| 34 } | |
| 35 | |
| 36 static scoped_ptr<TimingFunction> EaseOutWithInitialVelocity(double velocity) { | |
| 37 // Based on EaseInOutTimingFunction::Create with first control point rotated. | |
| 38 const double r2 = 0.42 * 0.42; | |
| 39 const double v2 = velocity * velocity; | |
| 40 const double x1 = std::sqrt(r2 / (v2 + 1)); | |
| 41 const double y1 = std::sqrt(r2 * v2 / (v2 + 1)); | |
| 42 return CubicBezierTimingFunction::Create(x1, y1, 0.58, 1); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create( | |
| 48 const gfx::ScrollOffset& target_value, | |
| 49 scoped_ptr<TimingFunction> timing_function) { | |
| 50 return make_scoped_ptr( | |
| 51 new ScrollOffsetAnimationCurve(target_value, timing_function.Pass())); | |
| 52 } | |
| 53 | |
| 54 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve( | |
| 55 const gfx::ScrollOffset& target_value, | |
| 56 scoped_ptr<TimingFunction> timing_function) | |
| 57 : target_value_(target_value), timing_function_(timing_function.Pass()) { | |
| 58 } | |
| 59 | |
| 60 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} | |
| 61 | |
| 62 void ScrollOffsetAnimationCurve::SetInitialValue( | |
| 63 const gfx::ScrollOffset& initial_value) { | |
| 64 initial_value_ = initial_value; | |
| 65 total_animation_duration_ = DurationFromDelta( | |
| 66 target_value_.DeltaFrom(initial_value_)); | |
| 67 } | |
| 68 | |
| 69 gfx::ScrollOffset ScrollOffsetAnimationCurve::GetValue( | |
| 70 base::TimeDelta t) const { | |
| 71 base::TimeDelta duration = total_animation_duration_ - last_retarget_; | |
| 72 t -= last_retarget_; | |
| 73 | |
| 74 if (t <= base::TimeDelta()) | |
| 75 return initial_value_; | |
| 76 | |
| 77 if (t >= duration) | |
| 78 return target_value_; | |
| 79 | |
| 80 double progress = timing_function_->GetValue(TimeUtil::Divide(t, duration)); | |
| 81 return gfx::ScrollOffset( | |
| 82 gfx::Tween::FloatValueBetween( | |
| 83 progress, initial_value_.x(), target_value_.x()), | |
| 84 gfx::Tween::FloatValueBetween( | |
| 85 progress, initial_value_.y(), target_value_.y())); | |
| 86 } | |
| 87 | |
| 88 base::TimeDelta ScrollOffsetAnimationCurve::Duration() const { | |
| 89 return total_animation_duration_; | |
| 90 } | |
| 91 | |
| 92 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { | |
| 93 return SCROLL_OFFSET; | |
| 94 } | |
| 95 | |
| 96 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { | |
| 97 scoped_ptr<TimingFunction> timing_function( | |
| 98 static_cast<TimingFunction*>(timing_function_->Clone().release())); | |
| 99 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = | |
| 100 Create(target_value_, timing_function.Pass()); | |
| 101 curve_clone->initial_value_ = initial_value_; | |
| 102 curve_clone->total_animation_duration_ = total_animation_duration_; | |
| 103 curve_clone->last_retarget_ = last_retarget_; | |
| 104 return curve_clone.Pass(); | |
| 105 } | |
| 106 | |
| 107 void ScrollOffsetAnimationCurve::UpdateTarget( | |
| 108 double t, | |
| 109 const gfx::ScrollOffset& new_target) { | |
| 110 gfx::ScrollOffset current_position = | |
| 111 GetValue(base::TimeDelta::FromSecondsD(t)); | |
| 112 gfx::Vector2dF old_delta = target_value_.DeltaFrom(initial_value_); | |
| 113 gfx::Vector2dF new_delta = new_target.DeltaFrom(current_position); | |
| 114 | |
| 115 double old_duration = | |
| 116 (total_animation_duration_ - last_retarget_).InSecondsF(); | |
| 117 double new_duration = DurationFromDelta(new_delta).InSecondsF(); | |
| 118 | |
| 119 double old_velocity = timing_function_->Velocity( | |
| 120 (t - last_retarget_.InSecondsF()) / old_duration); | |
| 121 | |
| 122 // TimingFunction::Velocity gives the slope of the curve from 0 to 1. | |
| 123 // To match the "true" velocity in px/sec we must adjust this slope for | |
| 124 // differences in duration and scroll delta between old and new curves. | |
| 125 double new_velocity = | |
| 126 old_velocity * (new_duration / old_duration) * | |
| 127 (MaximumDimension(old_delta) / MaximumDimension(new_delta)); | |
| 128 | |
| 129 initial_value_ = current_position; | |
| 130 target_value_ = new_target; | |
| 131 total_animation_duration_ = base::TimeDelta::FromSecondsD(t + new_duration); | |
| 132 last_retarget_ = base::TimeDelta::FromSecondsD(t); | |
| 133 timing_function_ = EaseOutWithInitialVelocity(new_velocity); | |
| 134 } | |
| 135 | |
| 136 } // namespace cc | |
| OLD | NEW |