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), timing_function_(timing_function.Pass()) { |
29 duration_(0.0), | 29 } |
30 timing_function_(timing_function.Pass()) {} | |
31 | 30 |
32 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} | 31 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} |
33 | 32 |
34 void ScrollOffsetAnimationCurve::SetInitialValue( | 33 void ScrollOffsetAnimationCurve::SetInitialValue( |
35 const gfx::Vector2dF& initial_value) { | 34 const gfx::Vector2dF& initial_value) { |
36 initial_value_ = initial_value; | 35 initial_value_ = initial_value; |
37 | 36 |
38 // The duration of a scroll animation depends on the size of the scroll. | 37 // 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 | 38 // 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 | 39 // 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 | 40 // to decide. The calculation performed here will very likely be further |
42 // tweaked before the smooth scroll API ships. | 41 // tweaked before the smooth scroll API ships. |
43 float delta_x = std::abs(target_value_.x() - initial_value_.x()); | 42 float delta_x = std::abs(target_value_.x() - initial_value_.x()); |
44 float delta_y = std::abs(target_value_.y() - initial_value_.y()); | 43 float delta_y = std::abs(target_value_.y() - initial_value_.y()); |
45 float max_delta = std::max(delta_x, delta_y); | 44 float max_delta = std::max(delta_x, delta_y); |
46 duration_ = std::sqrt(max_delta)/kDurationDivisor; | 45 duration_ = base::TimeDelta::FromMicroseconds( |
| 46 (std::sqrt(max_delta) / kDurationDivisor) * |
| 47 base::Time::kMicrosecondsPerSecond); |
47 } | 48 } |
48 | 49 |
49 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const { | 50 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const { |
| 51 double duration = duration_.InSecondsF(); |
| 52 |
50 if (t <= 0) | 53 if (t <= 0) |
51 return initial_value_; | 54 return initial_value_; |
52 | 55 |
53 if (t >= duration_) | 56 if (t >= duration) |
54 return target_value_; | 57 return target_value_; |
55 | 58 |
56 double progress = timing_function_->GetValue(t / duration_); | 59 double progress = (timing_function_->GetValue(t / duration)); |
57 return gfx::Vector2dF(gfx::Tween::FloatValueBetween( | 60 return gfx::Vector2dF(gfx::Tween::FloatValueBetween( |
58 progress, initial_value_.x(), target_value_.x()), | 61 progress, initial_value_.x(), target_value_.x()), |
59 gfx::Tween::FloatValueBetween( | 62 gfx::Tween::FloatValueBetween( |
60 progress, initial_value_.y(), target_value_.y())); | 63 progress, initial_value_.y(), target_value_.y())); |
61 } | 64 } |
62 | 65 |
63 double ScrollOffsetAnimationCurve::Duration() const { | 66 double ScrollOffsetAnimationCurve::Duration() const { |
64 return duration_; | 67 return duration_.InSecondsF(); |
65 } | 68 } |
66 | 69 |
67 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { | 70 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { |
68 return ScrollOffset; | 71 return ScrollOffset; |
69 } | 72 } |
70 | 73 |
71 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { | 74 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { |
72 scoped_ptr<TimingFunction> timing_function( | 75 scoped_ptr<TimingFunction> timing_function( |
73 static_cast<TimingFunction*>(timing_function_->Clone().release())); | 76 static_cast<TimingFunction*>(timing_function_->Clone().release())); |
74 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = | 77 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = |
75 Create(target_value_, timing_function.Pass()); | 78 Create(target_value_, timing_function.Pass()); |
76 curve_clone->initial_value_ = initial_value_; | 79 curve_clone->initial_value_ = initial_value_; |
77 curve_clone->duration_ = duration_; | 80 curve_clone->duration_ = duration_; |
78 return curve_clone.PassAs<AnimationCurve>(); | 81 return curve_clone.PassAs<AnimationCurve>(); |
79 } | 82 } |
80 | 83 |
81 } // namespace cc | 84 } // namespace cc |
OLD | NEW |