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 namespace {
     19 
     20 static float MaximumDimension(gfx::Vector2dF delta) {
     21   return std::max(std::abs(delta.x()), std::abs(delta.y()));
     22 }
     23 
     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);
     33 }
     34 
     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>();
     43 }
     44 
     45 }  // namespace
     46 
     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()));
     52 }
     53 
     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()) {
     58 }
     59 
     60 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}
     61 
     62 void ScrollOffsetAnimationCurve::SetInitialValue(
     63     const gfx::Vector2dF& initial_value) {
     64   initial_value_ = initial_value;
     65   total_animation_duration_ = DurationFromDelta(target_value_ - initial_value_);
     66 }
     67 
     68 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const {
     69   double duration = (total_animation_duration_ - last_retarget_).InSecondsF();
     70   t -= last_retarget_.InSecondsF();
     71 
     72   if (t <= 0)
     73     return initial_value_;
     74 
     75   if (t >= duration)
     76     return target_value_;
     77 
     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()));
     83 }
     84 
     85 double ScrollOffsetAnimationCurve::Duration() const {
     86   return total_animation_duration_.InSecondsF();
     87 }
     88 
     89 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const {
     90   return ScrollOffset;
     91 }
     92 
     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>();
    102 }
    103 
    104 void ScrollOffsetAnimationCurve::UpdateTarget(
    105     double t,
    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;
    110 
    111   double old_duration =
    112       (total_animation_duration_ - last_retarget_).InSecondsF();
    113   double new_duration = DurationFromDelta(new_delta).InSecondsF();
    114 
    115   double old_velocity = timing_function_->Velocity(
    116       (t - last_retarget_.InSecondsF()) / old_duration);
    117 
    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));
    124 
    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);
    130 }
    131 
    132 }  // namespace cc
    133