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;
18 scoped_ptr
<ScrollOffsetAnimationCurve
> ScrollOffsetAnimationCurve::Create(
19 const gfx::Vector2dF
& target_value
,
20 scoped_ptr
<TimingFunction
> timing_function
) {
21 return make_scoped_ptr(
22 new ScrollOffsetAnimationCurve(target_value
, timing_function
.Pass()));
25 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
26 const gfx::Vector2dF
& target_value
,
27 scoped_ptr
<TimingFunction
> timing_function
)
28 : target_value_(target_value
),
30 timing_function_(timing_function
.Pass()) {}
32 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}
34 void ScrollOffsetAnimationCurve::SetInitialValue(
35 const gfx::Vector2dF
& initial_value
) {
36 initial_value_
= initial_value
;
38 // 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 // 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 // tweaked before the smooth scroll API ships.
43 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 max_delta
= std::max(delta_x
, delta_y
);
46 duration_
= std::sqrt(max_delta
)/kDurationDivisor
;
49 gfx::Vector2dF
ScrollOffsetAnimationCurve::GetValue(double t
) const {
51 return initial_value_
;
56 double progress
= timing_function_
->GetValue(t
/ duration_
);
57 return gfx::Vector2dF(gfx::Tween::FloatValueBetween(
58 progress
, initial_value_
.x(), target_value_
.x()),
59 gfx::Tween::FloatValueBetween(
60 progress
, initial_value_
.y(), target_value_
.y()));
63 double ScrollOffsetAnimationCurve::Duration() const {
67 AnimationCurve::CurveType
ScrollOffsetAnimationCurve::Type() const {
71 scoped_ptr
<AnimationCurve
> ScrollOffsetAnimationCurve::Clone() const {
72 scoped_ptr
<TimingFunction
> timing_function(
73 static_cast<TimingFunction
*>(timing_function_
->Clone().release()));
74 scoped_ptr
<ScrollOffsetAnimationCurve
> curve_clone
=
75 Create(target_value_
, timing_function
.Pass());
76 curve_clone
->initial_value_
= initial_value_
;
77 curve_clone
->duration_
= duration_
;
78 return curve_clone
.PassAs
<AnimationCurve
>();