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)
42 .PassAs
<TimingFunction
>();
47 scoped_ptr
<ScrollOffsetAnimationCurve
> ScrollOffsetAnimationCurve::Create(
48 const gfx::Vector2dF
& target_value
,
49 scoped_ptr
<TimingFunction
> timing_function
) {
50 return make_scoped_ptr(
51 new ScrollOffsetAnimationCurve(target_value
, timing_function
.Pass()));
54 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
55 const gfx::Vector2dF
& target_value
,
56 scoped_ptr
<TimingFunction
> timing_function
)
57 : target_value_(target_value
), timing_function_(timing_function
.Pass()) {
60 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}
62 void ScrollOffsetAnimationCurve::SetInitialValue(
63 const gfx::Vector2dF
& initial_value
) {
64 initial_value_
= initial_value
;
65 total_animation_duration_
= DurationFromDelta(target_value_
- initial_value_
);
68 gfx::Vector2dF
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::Vector2dF(gfx::Tween::FloatValueBetween(
80 progress
, initial_value_
.x(), target_value_
.x()),
81 gfx::Tween::FloatValueBetween(
82 progress
, initial_value_
.y(), target_value_
.y()));
85 double ScrollOffsetAnimationCurve::Duration() const {
86 return total_animation_duration_
.InSecondsF();
89 AnimationCurve::CurveType
ScrollOffsetAnimationCurve::Type() const {
93 scoped_ptr
<AnimationCurve
> ScrollOffsetAnimationCurve::Clone() const {
94 scoped_ptr
<TimingFunction
> timing_function(
95 static_cast<TimingFunction
*>(timing_function_
->Clone().release()));
96 scoped_ptr
<ScrollOffsetAnimationCurve
> curve_clone
=
97 Create(target_value_
, timing_function
.Pass());
98 curve_clone
->initial_value_
= initial_value_
;
99 curve_clone
->total_animation_duration_
= total_animation_duration_
;
100 curve_clone
->last_retarget_
= last_retarget_
;
101 return curve_clone
.PassAs
<AnimationCurve
>();
104 void ScrollOffsetAnimationCurve::UpdateTarget(
106 const gfx::Vector2dF
& new_target
) {
107 gfx::Vector2dF current_position
= GetValue(t
);
108 gfx::Vector2dF old_delta
= target_value_
- initial_value_
;
109 gfx::Vector2dF new_delta
= new_target
- current_position
;
111 double old_duration
=
112 (total_animation_duration_
- last_retarget_
).InSecondsF();
113 double new_duration
= DurationFromDelta(new_delta
).InSecondsF();
115 double old_velocity
= timing_function_
->Velocity(
116 (t
- last_retarget_
.InSecondsF()) / old_duration
);
118 // TimingFunction::Velocity gives the slope of the curve from 0 to 1.
119 // To match the "true" velocity in px/sec we must adjust this slope for
120 // differences in duration and scroll delta between old and new curves.
121 double new_velocity
=
122 old_velocity
* (new_duration
/ old_duration
) *
123 (MaximumDimension(old_delta
) / MaximumDimension(new_delta
));
125 initial_value_
= current_position
;
126 target_value_
= new_target
;
127 total_animation_duration_
= base::TimeDelta::FromSecondsD(t
+ new_duration
);
128 last_retarget_
= base::TimeDelta::FromSecondsD(t
);
129 timing_function_
= EaseOutWithInitialVelocity(new_velocity
);