Index: cc/animation/scroll_offset_animation_curve.cc |
diff --git a/cc/animation/scroll_offset_animation_curve.cc b/cc/animation/scroll_offset_animation_curve.cc |
index a7283b8b5c601aca4a6f3dfac4e269b167c17966..1b657570eac8db3a3ba2874beb471a8e4649124f 100644 |
--- a/cc/animation/scroll_offset_animation_curve.cc |
+++ b/cc/animation/scroll_offset_animation_curve.cc |
@@ -15,6 +15,35 @@ const double kDurationDivisor = 60.0; |
namespace cc { |
+namespace { |
+ |
+static float maxDelta(gfx::Vector2dF delta) { |
ajuma
2014/07/16 20:35:23
MaximumDimension? Or InfinityNorm maybe?
skobes
2014/07/16 21:10:48
Done.
|
+ return std::max(std::abs(delta.x()), std::abs(delta.y())); |
+} |
+ |
+static base::TimeDelta durationFromDelta(gfx::Vector2dF delta) { |
ajuma
2014/07/16 20:35:23
Nit: function names should start with a capital le
skobes
2014/07/16 21:10:48
Done.
|
+ // The duration of a scroll animation depends on the size of the scroll. |
+ // The exact relationship between the size and the duration isn't specified |
+ // by the CSSOM View smooth scroll spec and is instead left up to user agents |
+ // to decide. The calculation performed here will very likely be further |
+ // tweaked before the smooth scroll API ships. |
+ return base::TimeDelta::FromMicroseconds( |
+ (std::sqrt(maxDelta(delta)) / kDurationDivisor) * |
+ base::Time::kMicrosecondsPerSecond); |
+} |
+ |
+static scoped_ptr<TimingFunction> easeOutWithInitialVelocity(double velocity) { |
ajuma
2014/07/16 20:35:23
EaseOutWithInitialVelocity
skobes
2014/07/16 21:10:48
Done.
|
+ // Based on EaseInOutTimingFunction::Create with first control point rotated. |
+ const double r2 = 0.42 * 0.42; |
+ const double v2 = velocity * velocity; |
+ const double x1 = std::sqrt(r2 / (v2 + 1)); |
+ const double y1 = std::sqrt(r2 * v2 / (v2 + 1)); |
+ return CubicBezierTimingFunction::Create(x1, y1, 0.58, 1) |
+ .PassAs<TimingFunction>(); |
+} |
+ |
+} // namespace |
+ |
scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create( |
const gfx::Vector2dF& target_value, |
scoped_ptr<TimingFunction> timing_function) { |
@@ -33,22 +62,12 @@ ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} |
void ScrollOffsetAnimationCurve::SetInitialValue( |
const gfx::Vector2dF& initial_value) { |
initial_value_ = initial_value; |
- |
- // The duration of a scroll animation depends on the size of the scroll. |
- // The exact relationship between the size and the duration isn't specified |
- // by the CSSOM View smooth scroll spec and is instead left up to user agents |
- // to decide. The calculation performed here will very likely be further |
- // tweaked before the smooth scroll API ships. |
- float delta_x = std::abs(target_value_.x() - initial_value_.x()); |
- float delta_y = std::abs(target_value_.y() - initial_value_.y()); |
- float max_delta = std::max(delta_x, delta_y); |
- duration_ = base::TimeDelta::FromMicroseconds( |
- (std::sqrt(max_delta) / kDurationDivisor) * |
- base::Time::kMicrosecondsPerSecond); |
+ total_animation_duration_ = durationFromDelta(target_value_ - initial_value_); |
} |
gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const { |
- double duration = duration_.InSecondsF(); |
+ double duration = (total_animation_duration_ - last_retarget_).InSecondsF(); |
+ t -= last_retarget_.InSecondsF(); |
if (t <= 0) |
return initial_value_; |
@@ -64,7 +83,7 @@ gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const { |
} |
double ScrollOffsetAnimationCurve::Duration() const { |
- return duration_.InSecondsF(); |
+ return total_animation_duration_.InSecondsF(); |
} |
AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { |
@@ -77,8 +96,32 @@ scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { |
scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = |
Create(target_value_, timing_function.Pass()); |
curve_clone->initial_value_ = initial_value_; |
- curve_clone->duration_ = duration_; |
+ curve_clone->total_animation_duration_ = total_animation_duration_; |
+ curve_clone->last_retarget_ = last_retarget_; |
return curve_clone.PassAs<AnimationCurve>(); |
} |
+void ScrollOffsetAnimationCurve::UpdateTarget( |
+ double t, |
+ const gfx::Vector2dF& new_target) { |
+ gfx::Vector2dF current_position = GetValue(t); |
+ gfx::Vector2dF old_delta = target_value_ - initial_value_; |
+ gfx::Vector2dF new_delta = new_target - current_position; |
+ |
+ double old_duration = |
+ (total_animation_duration_ - last_retarget_).InSecondsF(); |
+ double new_duration = durationFromDelta(new_delta).InSecondsF(); |
+ |
+ double old_velocity = timing_function_->Velocity( |
+ (t - last_retarget_.InSecondsF()) / old_duration); |
+ double new_velocity = old_velocity * (new_duration / old_duration) * |
+ (maxDelta(old_delta) / maxDelta(new_delta)); |
ajuma
2014/07/16 20:35:23
Please add a comment explaining why the new veloci
skobes
2014/07/16 21:10:48
Done.
|
+ |
+ initial_value_ = current_position; |
+ target_value_ = new_target; |
+ total_animation_duration_ = base::TimeDelta::FromSecondsD(t + new_duration); |
+ last_retarget_ = base::TimeDelta::FromSecondsD(t); |
+ timing_function_ = easeOutWithInitialVelocity(new_velocity); |
+} |
+ |
} // namespace cc |