OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/animation/scroll_offset_animation_curve.h" | 5 #include "cc/animation/scroll_offset_animation_curve.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "cc/animation/timing_function.h" | 11 #include "cc/animation/timing_function.h" |
12 #include "ui/gfx/animation/tween.h" | 12 #include "ui/gfx/animation/tween.h" |
13 | 13 |
14 const double kDurationDivisor = 60.0; | 14 const double kDurationDivisor = 60.0; |
15 | 15 |
16 namespace cc { | 16 namespace cc { |
17 | 17 |
18 scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create( | 18 scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create( |
19 const gfx::Vector2dF& target_value, | 19 const gfx::Vector2dF& target_value, |
20 scoped_ptr<TimingFunction> timing_function) { | 20 scoped_ptr<TimingFunction> timing_function) { |
21 return make_scoped_ptr( | 21 return make_scoped_ptr( |
22 new ScrollOffsetAnimationCurve(target_value, timing_function.Pass())); | 22 new ScrollOffsetAnimationCurve(target_value, timing_function.Pass())); |
23 } | 23 } |
24 | 24 |
25 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve( | 25 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve( |
26 const gfx::Vector2dF& target_value, | 26 const gfx::Vector2dF& target_value, |
27 scoped_ptr<TimingFunction> timing_function) | 27 scoped_ptr<TimingFunction> timing_function) |
28 : target_value_(target_value), | 28 : target_value_(target_value), |
29 duration_(0.0), | 29 duration_(), |
30 timing_function_(timing_function.Pass()) {} | 30 timing_function_(timing_function.Pass()) { |
| 31 } |
31 | 32 |
32 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} | 33 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} |
33 | 34 |
34 void ScrollOffsetAnimationCurve::SetInitialValue( | 35 void ScrollOffsetAnimationCurve::SetInitialValue( |
35 const gfx::Vector2dF& initial_value) { | 36 const gfx::Vector2dF& initial_value) { |
36 initial_value_ = initial_value; | 37 initial_value_ = initial_value; |
37 | 38 |
38 // The duration of a scroll animation depends on the size of the scroll. | 39 // The duration of a scroll animation depends on the size of the scroll. |
39 // The exact relationship between the size and the duration isn't specified | 40 // The exact relationship between the size and the duration isn't specified |
40 // by the CSSOM View smooth scroll spec and is instead left up to user agents | 41 // by the CSSOM View smooth scroll spec and is instead left up to user agents |
41 // to decide. The calculation performed here will very likely be further | 42 // to decide. The calculation performed here will very likely be further |
42 // tweaked before the smooth scroll API ships. | 43 // tweaked before the smooth scroll API ships. |
43 float delta_x = std::abs(target_value_.x() - initial_value_.x()); | 44 float delta_x = std::abs(target_value_.x() - initial_value_.x()); |
44 float delta_y = std::abs(target_value_.y() - initial_value_.y()); | 45 float delta_y = std::abs(target_value_.y() - initial_value_.y()); |
45 float max_delta = std::max(delta_x, delta_y); | 46 float max_delta = std::max(delta_x, delta_y); |
46 duration_ = std::sqrt(max_delta)/kDurationDivisor; | 47 duration_ = base::TimeDelta::FromMicroseconds( |
| 48 (std::sqrt(max_delta) / kDurationDivisor) * |
| 49 base::Time::kMicrosecondsPerSecond); |
47 } | 50 } |
48 | 51 |
49 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const { | 52 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const { |
| 53 double duration = duration_.InSecondsF(); |
| 54 |
50 if (t <= 0) | 55 if (t <= 0) |
51 return initial_value_; | 56 return initial_value_; |
52 | 57 |
53 if (t >= duration_) | 58 if (t >= duration) |
54 return target_value_; | 59 return target_value_; |
55 | 60 |
56 double progress = timing_function_->GetValue(t / duration_); | 61 double progress = (timing_function_->GetValue(t / duration)); |
57 return gfx::Vector2dF(gfx::Tween::FloatValueBetween( | 62 return gfx::Vector2dF(gfx::Tween::FloatValueBetween( |
58 progress, initial_value_.x(), target_value_.x()), | 63 progress, initial_value_.x(), target_value_.x()), |
59 gfx::Tween::FloatValueBetween( | 64 gfx::Tween::FloatValueBetween( |
60 progress, initial_value_.y(), target_value_.y())); | 65 progress, initial_value_.y(), target_value_.y())); |
61 } | 66 } |
62 | 67 |
63 double ScrollOffsetAnimationCurve::Duration() const { | 68 double ScrollOffsetAnimationCurve::Duration() const { |
64 return duration_; | 69 return duration_.InSecondsF(); |
65 } | 70 } |
66 | 71 |
67 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { | 72 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { |
68 return ScrollOffset; | 73 return ScrollOffset; |
69 } | 74 } |
70 | 75 |
71 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { | 76 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { |
72 scoped_ptr<TimingFunction> timing_function( | 77 scoped_ptr<TimingFunction> timing_function( |
73 static_cast<TimingFunction*>(timing_function_->Clone().release())); | 78 static_cast<TimingFunction*>(timing_function_->Clone().release())); |
74 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = | 79 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = |
75 Create(target_value_, timing_function.Pass()); | 80 Create(target_value_, timing_function.Pass()); |
76 curve_clone->initial_value_ = initial_value_; | 81 curve_clone->initial_value_ = initial_value_; |
77 curve_clone->duration_ = duration_; | 82 curve_clone->duration_ = duration_; |
78 return curve_clone.PassAs<AnimationCurve>(); | 83 return curve_clone.PassAs<AnimationCurve>(); |
79 } | 84 } |
80 | 85 |
81 } // namespace cc | 86 } // namespace cc |
OLD | NEW |