OLD | NEW |
| (Empty) |
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 "base/logging.h" | |
6 #include "base/memory/scoped_ptr.h" | |
7 #include "cc/animation/timing_function.h" | |
8 #include "cc/base/math_util.h" | |
9 | |
10 namespace cc { | |
11 | |
12 TimingFunction::TimingFunction() {} | |
13 | |
14 TimingFunction::~TimingFunction() {} | |
15 | |
16 scoped_ptr<CubicBezierTimingFunction> CubicBezierTimingFunction::Create( | |
17 double x1, double y1, double x2, double y2) { | |
18 return make_scoped_ptr(new CubicBezierTimingFunction(x1, y1, x2, y2)); | |
19 } | |
20 | |
21 CubicBezierTimingFunction::CubicBezierTimingFunction(double x1, | |
22 double y1, | |
23 double x2, | |
24 double y2) | |
25 : bezier_(x1, y1, x2, y2) {} | |
26 | |
27 CubicBezierTimingFunction::~CubicBezierTimingFunction() {} | |
28 | |
29 float CubicBezierTimingFunction::GetValue(double x) const { | |
30 return static_cast<float>(bezier_.Solve(x)); | |
31 } | |
32 | |
33 float CubicBezierTimingFunction::Velocity(double x) const { | |
34 return static_cast<float>(bezier_.Slope(x)); | |
35 } | |
36 | |
37 void CubicBezierTimingFunction::Range(float* min, float* max) const { | |
38 double min_d = 0; | |
39 double max_d = 1; | |
40 bezier_.Range(&min_d, &max_d); | |
41 *min = static_cast<float>(min_d); | |
42 *max = static_cast<float>(max_d); | |
43 } | |
44 | |
45 scoped_ptr<TimingFunction> CubicBezierTimingFunction::Clone() const { | |
46 return make_scoped_ptr(new CubicBezierTimingFunction(*this)); | |
47 } | |
48 | |
49 // These numbers come from | |
50 // http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag. | |
51 scoped_ptr<TimingFunction> EaseTimingFunction::Create() { | |
52 return CubicBezierTimingFunction::Create(0.25, 0.1, 0.25, 1.0); | |
53 } | |
54 | |
55 scoped_ptr<TimingFunction> EaseInTimingFunction::Create() { | |
56 return CubicBezierTimingFunction::Create(0.42, 0.0, 1.0, 1.0); | |
57 } | |
58 | |
59 scoped_ptr<TimingFunction> EaseOutTimingFunction::Create() { | |
60 return CubicBezierTimingFunction::Create(0.0, 0.0, 0.58, 1.0); | |
61 } | |
62 | |
63 scoped_ptr<TimingFunction> EaseInOutTimingFunction::Create() { | |
64 return CubicBezierTimingFunction::Create(0.42, 0.0, 0.58, 1); | |
65 } | |
66 | |
67 scoped_ptr<StepsTimingFunction> StepsTimingFunction::Create( | |
68 int steps, | |
69 float steps_start_offset) { | |
70 return make_scoped_ptr(new StepsTimingFunction(steps, steps_start_offset)); | |
71 } | |
72 | |
73 StepsTimingFunction::StepsTimingFunction(int steps, float steps_start_offset) | |
74 : steps_(steps), steps_start_offset_(steps_start_offset) { | |
75 // Restrict it to CSS presets: step_start, step_end and step_middle. | |
76 // See the Web Animations specification, 3.12.4. Timing in discrete steps. | |
77 DCHECK(steps_start_offset_ == 0 || steps_start_offset_ == 1 || | |
78 steps_start_offset_ == 0.5); | |
79 } | |
80 | |
81 StepsTimingFunction::~StepsTimingFunction() { | |
82 } | |
83 | |
84 float StepsTimingFunction::GetValue(double t) const { | |
85 const double steps = static_cast<double>(steps_); | |
86 const double value = MathUtil::ClampToRange( | |
87 std::floor((steps * t) + steps_start_offset_) / steps, 0.0, 1.0); | |
88 return static_cast<float>(value); | |
89 } | |
90 | |
91 scoped_ptr<TimingFunction> StepsTimingFunction::Clone() const { | |
92 return make_scoped_ptr(new StepsTimingFunction(*this)); | |
93 } | |
94 | |
95 void StepsTimingFunction::Range(float* min, float* max) const { | |
96 *min = 0.0f; | |
97 *max = 1.0f; | |
98 } | |
99 | |
100 float StepsTimingFunction::Velocity(double x) const { | |
101 return 0.0f; | |
102 } | |
103 | |
104 } // namespace cc | |
OLD | NEW |