1 // Copyright 2012 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 <algorithm> 6 #include <cmath> 7 8 #include "base/logging.h" 9 #include "cc/animation/timing_function.h" 10 11 namespace cc { 12 13 namespace { 14 15 static const double BEZIER_EPSILON = 1e-7; 16 static const int MAX_STEPS = 30; 17 18 static double eval_bezier(double x1, double x2, double t) { 19 const double x1_times_3 = 3.0 * x1; 20 const double x2_times_3 = 3.0 * x2; 21 const double h3 = x1_times_3; 22 const double h1 = x1_times_3 - x2_times_3 + 1.0; 23 const double h2 = x2_times_3 - 6.0 * x1; 24 return t * (t * (t * h1 + h2) + h3); 25 } 26 27 static double bezier_interp(double x1, 28 double y1, 29 double x2, 30 double y2, 31 double x) { 32 DCHECK_GE(1.0, x1); 33 DCHECK_LE(0.0, x1); 34 DCHECK_GE(1.0, x2); 35 DCHECK_LE(0.0, x2); 36 37 x1 = std::min(std::max(x1, 0.0), 1.0); 38 x2 = std::min(std::max(x2, 0.0), 1.0); 39 x = std::min(std::max(x, 0.0), 1.0); 40 41 // Step 1. Find the t corresponding to the given x. I.e., we want t such that 42 // eval_bezier(x1, x2, t) = x. There is a unique solution if x1 and x2 lie 43 // within (0, 1). 44 // 45 // We're just going to do bisection for now (for simplicity), but we could 46 // easily do some newton steps if this turns out to be a bottleneck. 47 double t = 0.0; 48 double step = 1.0; 49 for (int i = 0; i < MAX_STEPS; ++i, step *= 0.5) { 50 const double error = eval_bezier(x1, x2, t) - x; 51 if (std::abs(error) < BEZIER_EPSILON) 52 break; 53 t += error > 0.0 ? -step : step; 54 } 55 56 // We should have terminated the above loop because we got close to x, not 57 // because we exceeded MAX_STEPS. Do a DCHECK here to confirm. 58 DCHECK_GT(BEZIER_EPSILON, std::abs(eval_bezier(x1, x2, t) - x)); 59 60 // Step 2. Return the interpolated y values at the t we computed above. 61 return eval_bezier(y1, y2, t); 62 } 63 64 } // namespace 65 66 TimingFunction::TimingFunction() {} 67 68 TimingFunction::~TimingFunction() {} 69 70 double TimingFunction::Duration() const { 71 return 1.0; 72 } 73 74 scoped_ptr<CubicBezierTimingFunction> CubicBezierTimingFunction::Create( 75 double x1, double y1, double x2, double y2) { 76 return make_scoped_ptr(new CubicBezierTimingFunction(x1, y1, x2, y2)); 77 } 78 79 CubicBezierTimingFunction::CubicBezierTimingFunction(double x1, 80 double y1, 81 double x2, 82 double y2) 83 : x1_(x1), y1_(y1), x2_(x2), y2_(y2) {} 84 85 CubicBezierTimingFunction::~CubicBezierTimingFunction() {} 86 87 float CubicBezierTimingFunction::GetValue(double x) const { 88 return static_cast<float>(bezier_interp(x1_, y1_, x2_, y2_, x)); 89 } 90 91 scoped_ptr<AnimationCurve> CubicBezierTimingFunction::Clone() const { 92 return make_scoped_ptr( 93 new CubicBezierTimingFunction(*this)).PassAs<AnimationCurve>(); 94 } 95 96 // These numbers come from 97 // http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag. 98 scoped_ptr<TimingFunction> EaseTimingFunction::Create() { 99 return CubicBezierTimingFunction::Create( 100 0.25, 0.1, 0.25, 1.0).PassAs<TimingFunction>(); 101 } 102 103 scoped_ptr<TimingFunction> EaseInTimingFunction::Create() { 104 return CubicBezierTimingFunction::Create( 105 0.42, 0.0, 1.0, 1.0).PassAs<TimingFunction>(); 106 } 107 108 scoped_ptr<TimingFunction> EaseOutTimingFunction::Create() { 109 return CubicBezierTimingFunction::Create( 110 0.0, 0.0, 0.58, 1.0).PassAs<TimingFunction>(); 111 } 112 113 scoped_ptr<TimingFunction> EaseInOutTimingFunction::Create() { 114 return CubicBezierTimingFunction::Create( 115 0.42, 0.0, 0.58, 1).PassAs<TimingFunction>(); 116 } 117 118 } // namespace cc 119