Home | History | Annotate | Download | only in animation
      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 
     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()));
     23 }
     24 
     25 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
     26     const gfx::Vector2dF& target_value,
     27     scoped_ptr<TimingFunction> timing_function)
     28     : target_value_(target_value), timing_function_(timing_function.Pass()) {
     29 }
     30 
     31 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}
     32 
     33 void ScrollOffsetAnimationCurve::SetInitialValue(
     34     const 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_ = base::TimeDelta::FromMicroseconds(
     46       (std::sqrt(max_delta) / kDurationDivisor) *
     47       base::Time::kMicrosecondsPerSecond);
     48 }
     49 
     50 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const {
     51   double duration = duration_.InSecondsF();
     52 
     53   if (t <= 0)
     54     return initial_value_;
     55 
     56   if (t >= duration)
     57     return target_value_;
     58 
     59   double progress = (timing_function_->GetValue(t / duration));
     60   return gfx::Vector2dF(gfx::Tween::FloatValueBetween(
     61                             progress, initial_value_.x(), target_value_.x()),
     62                         gfx::Tween::FloatValueBetween(
     63                             progress, initial_value_.y(), target_value_.y()));
     64 }
     65 
     66 double ScrollOffsetAnimationCurve::Duration() const {
     67   return duration_.InSecondsF();
     68 }
     69 
     70 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const {
     71   return ScrollOffset;
     72 }
     73 
     74 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const {
     75   scoped_ptr<TimingFunction> timing_function(
     76       static_cast<TimingFunction*>(timing_function_->Clone().release()));
     77   scoped_ptr<ScrollOffsetAnimationCurve> curve_clone =
     78       Create(target_value_, timing_function.Pass());
     79   curve_clone->initial_value_ = initial_value_;
     80   curve_clone->duration_ = duration_;
     81   return curve_clone.PassAs<AnimationCurve>();
     82 }
     83 
     84 }  // namespace cc
     85