Update V8 to version 4.7.42.
[chromium-blink-merge.git] / cc / animation / scroll_offset_animation_curve.cc
blob17dff26b9ce4975f4e17a64009f806d384ee73b2
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.
5 #include "cc/animation/scroll_offset_animation_curve.h"
7 #include <algorithm>
8 #include <cmath>
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"
15 const double kDurationDivisor = 60.0;
17 namespace cc {
19 namespace {
21 static float MaximumDimension(const gfx::Vector2dF& delta) {
22 return std::max(std::abs(delta.x()), std::abs(delta.y()));
25 static base::TimeDelta DurationFromDelta(const 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);
36 static scoped_ptr<TimingFunction> EaseOutWithInitialVelocity(double velocity) {
37 // Based on EaseInOutTimingFunction::Create with first control point rotated.
38 if (std::abs(velocity) < 1000.0) {
39 const double r2 = 0.42 * 0.42;
40 const double v2 = velocity * velocity;
41 const double x1 = std::sqrt(r2 / (v2 + 1));
42 const double y1 = std::sqrt(r2 * v2 / (v2 + 1));
43 return CubicBezierTimingFunction::Create(x1, y1, 0.58, 1);
46 // For large |velocity|, x1 approaches 0 and y1 approaches 0.42. To avoid the
47 // risk of floating point arithmetic involving infinity and NaN, use those
48 // values directly rather than computing them above.
49 return CubicBezierTimingFunction::Create(0, 0.42, 0.58, 1);
52 } // namespace
54 scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create(
55 const gfx::ScrollOffset& target_value,
56 scoped_ptr<TimingFunction> timing_function) {
57 return make_scoped_ptr(
58 new ScrollOffsetAnimationCurve(target_value, timing_function.Pass()));
61 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
62 const gfx::ScrollOffset& target_value,
63 scoped_ptr<TimingFunction> timing_function)
64 : target_value_(target_value), timing_function_(timing_function.Pass()) {
67 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}
69 void ScrollOffsetAnimationCurve::SetInitialValue(
70 const gfx::ScrollOffset& initial_value) {
71 initial_value_ = initial_value;
72 total_animation_duration_ = DurationFromDelta(
73 target_value_.DeltaFrom(initial_value_));
76 gfx::ScrollOffset ScrollOffsetAnimationCurve::GetValue(
77 base::TimeDelta t) const {
78 base::TimeDelta duration = total_animation_duration_ - last_retarget_;
79 t -= last_retarget_;
81 if (t <= base::TimeDelta())
82 return initial_value_;
84 if (t >= duration)
85 return target_value_;
87 double progress = timing_function_->GetValue(TimeUtil::Divide(t, duration));
88 return gfx::ScrollOffset(
89 gfx::Tween::FloatValueBetween(
90 progress, initial_value_.x(), target_value_.x()),
91 gfx::Tween::FloatValueBetween(
92 progress, initial_value_.y(), target_value_.y()));
95 base::TimeDelta ScrollOffsetAnimationCurve::Duration() const {
96 return total_animation_duration_;
99 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const {
100 return SCROLL_OFFSET;
103 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const {
104 scoped_ptr<TimingFunction> timing_function(
105 static_cast<TimingFunction*>(timing_function_->Clone().release()));
106 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone =
107 Create(target_value_, timing_function.Pass());
108 curve_clone->initial_value_ = initial_value_;
109 curve_clone->total_animation_duration_ = total_animation_duration_;
110 curve_clone->last_retarget_ = last_retarget_;
111 return curve_clone.Pass();
114 void ScrollOffsetAnimationCurve::UpdateTarget(
115 double t,
116 const gfx::ScrollOffset& new_target) {
117 gfx::ScrollOffset current_position =
118 GetValue(base::TimeDelta::FromSecondsD(t));
119 gfx::Vector2dF old_delta = target_value_.DeltaFrom(initial_value_);
120 gfx::Vector2dF new_delta = new_target.DeltaFrom(current_position);
122 double old_duration =
123 (total_animation_duration_ - last_retarget_).InSecondsF();
124 double new_duration = DurationFromDelta(new_delta).InSecondsF();
126 double old_velocity = timing_function_->Velocity(
127 (t - last_retarget_.InSecondsF()) / old_duration);
129 // TimingFunction::Velocity gives the slope of the curve from 0 to 1.
130 // To match the "true" velocity in px/sec we must adjust this slope for
131 // differences in duration and scroll delta between old and new curves.
132 double new_velocity =
133 old_velocity * (new_duration / old_duration) *
134 (MaximumDimension(old_delta) / MaximumDimension(new_delta));
136 initial_value_ = current_position;
137 target_value_ = new_target;
138 total_animation_duration_ = base::TimeDelta::FromSecondsD(t + new_duration);
139 last_retarget_ = base::TimeDelta::FromSecondsD(t);
140 timing_function_ = EaseOutWithInitialVelocity(new_velocity);
143 } // namespace cc