1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' as math;
double _evaluateCubic(double a, double b, double m) {
// TODO(abarth): Would Math.pow be faster?
return 3 * a * (1 - m) * (1 - m) * m + 3 * b * (1 - m) * m * m + m * m * m;
}
const double _kCubicErrorBound = 0.001;
abstract class Curve {
double transform(double t);
}
class Linear implements Curve {
const Linear();
double transform(double t) {
return t;
}
}
class Interval implements Curve {
final double start;
final double end;
Interval(this.start, this.end) {
assert(start >= 0.0);
assert(start <= 1.0);
assert(end >= 0.0);
assert(end <= 1.0);
}
double transform(double t) {
return ((t - start) / (end - start)).clamp(0.0, 1.0);
}
}
class Cubic implements Curve {
final double a;
final double b;
final double c;
final double d;
const Cubic(this.a, this.b, this.c, this.d);
double transform(double t) {
double start = 0.0;
double end = 1.0;
while (true) {
double midpoint = (start + end) / 2;
double estimate = _evaluateCubic(a, c, midpoint);
if ((t - estimate).abs() < _kCubicErrorBound)
return _evaluateCubic(b, d, midpoint);
if (estimate < t)
start = midpoint;
else
end = midpoint;
}
}
}
double _bounce(double t) {
if (t < 1.0 / 2.75) {
return 7.5625 * t * t;
} else if (t < 2 / 2.75) {
t -= 1.5 / 2.75;
return 7.5625 * t * t + 0.75;
} else if (t < 2.5 / 2.75) {
t -= 2.25 / 2.75;
return 7.5625 * t * t + 0.9375;
}
t -= 2.625 / 2.75;
return 7.5625 * t * t + 0.984375;
}
class BounceInCurve implements Curve {
const BounceInCurve();
double transform(double t) {
return 1.0 - _bounce(1.0 - t);
}
}
class BounceOutCurve implements Curve {
const BounceOutCurve();
double transform(double t) {
return _bounce(t);
}
}
class BounceInOutCurve implements Curve {
const BounceInOutCurve();
double transform(double t) {
if (t < 0.5)
return (1.0 - _bounce(1.0 - t)) * 0.5;
else
return _bounce(t * 2.0 - 1.0) * 0.5 + 0.5;
}
}
class ElasticInCurve implements Curve {
const ElasticInCurve([this.period = 0.4]);
final double period;
double transform(double t) {
double s = period / 4.0;
t = t - 1.0;
return -math.pow(2.0, 10.0 * t) * math.sin((t - s) * (math.PI * 2.0) / period);
}
}
class ElasticOutCurve implements Curve {
const ElasticOutCurve([this.period = 0.4]);
final double period;
double transform(double t) {
double s = period / 4.0;
return math.pow(2.0, -10 * t) * math.sin((t - s) * (math.PI * 2.0) / period) + 1.0;
}
}
class ElasticInOutCurve implements Curve {
const ElasticInOutCurve([this.period = 0.4]);
final double period;
double transform(double t) {
double s = period / 4.0;
t = 2.0 * t - 1.0;
if (t < 0.0)
return -0.5 * math.pow(2.0, 10.0 * t) * math.sin((t - s) * (math.PI * 2.0) / period);
else
return math.pow(2.0, -10.0 * t) * math.sin((t - s) * (math.PI * 2.0) / period) * 0.5 + 1.0;
}
}
const Linear linear = const Linear();
const Cubic ease = const Cubic(0.25, 0.1, 0.25, 1.0);
const Cubic easeIn = const Cubic(0.42, 0.0, 1.0, 1.0);
const Cubic easeOut = const Cubic(0.0, 0.0, 0.58, 1.0);
const Cubic easeInOut = const Cubic(0.42, 0.0, 0.58, 1.0);
const BounceInCurve bounceIn = const BounceInCurve();
const BounceOutCurve bounceOut = const BounceOutCurve();
const BounceInOutCurve bounceInOut = const BounceInOutCurve();
const ElasticInCurve elasticIn = const ElasticInCurve();
const ElasticOutCurve elasticOut = const ElasticOutCurve();
const ElasticInOutCurve elasticInOut = const ElasticInOutCurve();