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