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"
10 #include "base/logging.h"
11 #include "cc/animation/timing_function.h"
12 #include "ui/gfx/animation/tween.h"
14 const double kDurationDivisor
= 60.0;
20 static float MaximumDimension(gfx::Vector2dF delta
) {
21 return std::max(std::abs(delta
.x()), std::abs(delta
.y()));
24 static base::TimeDelta
DurationFromDelta(gfx::Vector2dF delta
) {
25 // The duration of a scroll animation depends on the size of the scroll.
26 // The exact relationship between the size and the duration isn't specified
27 // by the CSSOM View smooth scroll spec and is instead left up to user agents
28 // to decide. The calculation performed here will very likely be further
29 // tweaked before the smooth scroll API ships.
30 return base::TimeDelta::FromMicroseconds(
31 (std::sqrt(MaximumDimension(delta
)) / kDurationDivisor
) *
32 base::Time::kMicrosecondsPerSecond
);
35 static scoped_ptr
<TimingFunction
> EaseOutWithInitialVelocity(double velocity
) {
36 // Based on EaseInOutTimingFunction::Create with first control point rotated.
37 const double r2
= 0.42 * 0.42;
38 const double v2
= velocity
* velocity
;
39 const double x1
= std::sqrt(r2
/ (v2
+ 1));
40 const double y1
= std::sqrt(r2
* v2
/ (v2
+ 1));
41 return CubicBezierTimingFunction::Create(x1
, y1
, 0.58, 1);
46 scoped_ptr
<ScrollOffsetAnimationCurve
> ScrollOffsetAnimationCurve::Create(
47 const gfx::ScrollOffset
& target_value
,
48 scoped_ptr
<TimingFunction
> timing_function
) {
49 return make_scoped_ptr(
50 new ScrollOffsetAnimationCurve(target_value
, timing_function
.Pass()));
53 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
54 const gfx::ScrollOffset
& target_value
,
55 scoped_ptr
<TimingFunction
> timing_function
)
56 : target_value_(target_value
), timing_function_(timing_function
.Pass()) {
59 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}
61 void ScrollOffsetAnimationCurve::SetInitialValue(
62 const gfx::ScrollOffset
& initial_value
) {
63 initial_value_
= initial_value
;
64 total_animation_duration_
= DurationFromDelta(
65 target_value_
.DeltaFrom(initial_value_
));
68 gfx::ScrollOffset
ScrollOffsetAnimationCurve::GetValue(double t
) const {
69 double duration
= (total_animation_duration_
- last_retarget_
).InSecondsF();
70 t
-= last_retarget_
.InSecondsF();
73 return initial_value_
;
78 double progress
= (timing_function_
->GetValue(t
/ duration
));
79 return gfx::ScrollOffset(
80 gfx::Tween::FloatValueBetween(
81 progress
, initial_value_
.x(), target_value_
.x()),
82 gfx::Tween::FloatValueBetween(
83 progress
, initial_value_
.y(), target_value_
.y()));
86 base::TimeDelta
ScrollOffsetAnimationCurve::Duration() const {
87 return total_animation_duration_
;
90 AnimationCurve::CurveType
ScrollOffsetAnimationCurve::Type() const {
94 scoped_ptr
<AnimationCurve
> ScrollOffsetAnimationCurve::Clone() const {
95 scoped_ptr
<TimingFunction
> timing_function(
96 static_cast<TimingFunction
*>(timing_function_
->Clone().release()));
97 scoped_ptr
<ScrollOffsetAnimationCurve
> curve_clone
=
98 Create(target_value_
, timing_function
.Pass());
99 curve_clone
->initial_value_
= initial_value_
;
100 curve_clone
->total_animation_duration_
= total_animation_duration_
;
101 curve_clone
->last_retarget_
= last_retarget_
;
102 return curve_clone
.Pass();
105 void ScrollOffsetAnimationCurve::UpdateTarget(
107 const gfx::ScrollOffset
& new_target
) {
108 gfx::ScrollOffset current_position
= GetValue(t
);
109 gfx::Vector2dF old_delta
= target_value_
.DeltaFrom(initial_value_
);
110 gfx::Vector2dF new_delta
= new_target
.DeltaFrom(current_position
);
112 double old_duration
=
113 (total_animation_duration_
- last_retarget_
).InSecondsF();
114 double new_duration
= DurationFromDelta(new_delta
).InSecondsF();
116 double old_velocity
= timing_function_
->Velocity(
117 (t
- last_retarget_
.InSecondsF()) / old_duration
);
119 // TimingFunction::Velocity gives the slope of the curve from 0 to 1.
120 // To match the "true" velocity in px/sec we must adjust this slope for
121 // differences in duration and scroll delta between old and new curves.
122 double new_velocity
=
123 old_velocity
* (new_duration
/ old_duration
) *
124 (MaximumDimension(old_delta
) / MaximumDimension(new_delta
));
126 initial_value_
= current_position
;
127 target_value_
= new_target
;
128 total_animation_duration_
= base::TimeDelta::FromSecondsD(t
+ new_duration
);
129 last_retarget_
= base::TimeDelta::FromSecondsD(t
);
130 timing_function_
= EaseOutWithInitialVelocity(new_velocity
);