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.
4
5 #include "cc/animation/scroll_offset_animation_curve.h"
6
7 #include <algorithm>
8 #include <cmath>
9
10 #include "base/logging.h"
11 #include "cc/animation/timing_function.h"
12 #include "ui/gfx/animation/tween.h"
13
14 const double kDurationDivisor = 60.0;
15
16 namespace cc {
17
Create(gfx::Vector2dF target_value,scoped_ptr<TimingFunction> timing_function)18 scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create(
19 gfx::Vector2dF target_value,
20 scoped_ptr<TimingFunction> timing_function) {
21 return make_scoped_ptr(
22 new ScrollOffsetAnimationCurve(target_value, timing_function.Pass()));
23 }
24
ScrollOffsetAnimationCurve(gfx::Vector2dF target_value,scoped_ptr<TimingFunction> timing_function)25 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
26 gfx::Vector2dF target_value,
27 scoped_ptr<TimingFunction> timing_function)
28 : target_value_(target_value),
29 duration_(0.0),
30 timing_function_(timing_function.Pass()) {}
31
~ScrollOffsetAnimationCurve()32 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}
33
SetInitialValue(gfx::Vector2dF initial_value)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
GetValue(double t) const48 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const {
49 if (t <= 0)
50 return initial_value_;
51
52 if (t >= duration_)
53 return target_value_;
54
55 double progress = timing_function_->GetValue(t / duration_);
56 return gfx::Vector2dF(gfx::Tween::FloatValueBetween(
57 progress, initial_value_.x(), target_value_.x()),
58 gfx::Tween::FloatValueBetween(
59 progress, initial_value_.y(), target_value_.y()));
60 }
61
Duration() const62 double ScrollOffsetAnimationCurve::Duration() const {
63 return duration_;
64 }
65
Type() const66 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const {
67 return ScrollOffset;
68 }
69
Clone() const70 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const {
71 scoped_ptr<TimingFunction> timing_function(
72 static_cast<TimingFunction*>(timing_function_->Clone().release()));
73 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone =
74 Create(target_value_, timing_function.Pass());
75 curve_clone->initial_value_ = initial_value_;
76 curve_clone->duration_ = duration_;
77 return curve_clone.PassAs<AnimationCurve>();
78 }
79
80 } // namespace cc
81