OLD | NEW |
1 part of widgets; | 1 part of widgets; |
2 | 2 |
3 class FrameGenerator { | 3 class FrameGenerator { |
4 | 4 |
5 Function onDone; | 5 Function onDone; |
6 StreamController _controller; | 6 StreamController _controller; |
7 | 7 |
8 Stream<double> get onTick => _controller.stream; | 8 Stream<double> get onTick => _controller.stream; |
9 | 9 |
10 int _animationId = 0; | 10 int _animationId = 0; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 .map((t) => begin + (end - begin) * curve.transform(t)); | 74 .map((t) => begin + (end - begin) * curve.transform(t)); |
75 } | 75 } |
76 | 76 |
77 bool _checkForCompletion(double t) { | 77 bool _checkForCompletion(double t) { |
78 if (_done) | 78 if (_done) |
79 return false; | 79 return false; |
80 _done = t >= 1; | 80 _done = t >= 1; |
81 return true; | 81 return true; |
82 } | 82 } |
83 } | 83 } |
84 | |
85 double _evaluateCubic(double a, double b, double m) { | |
86 // TODO(abarth): Would Math.pow be faster? | |
87 return 3 * a * (1 - m) * (1 - m) * m + 3 * b * (1 - m) * m * m + m * m * m; | |
88 } | |
89 | |
90 const double _kCubicErrorBound = 0.001; | |
91 | |
92 abstract class Curve { | |
93 double transform(double t); | |
94 } | |
95 | |
96 class Linear implements Curve { | |
97 const Linear(); | |
98 | |
99 double transform(double t) { | |
100 return t; | |
101 } | |
102 } | |
103 | |
104 class Cubic implements Curve { | |
105 final double a; | |
106 final double b; | |
107 final double c; | |
108 final double d; | |
109 | |
110 const Cubic(this.a, this.b, this.c, this.d); | |
111 | |
112 double transform(double t) { | |
113 if (t == 0.0 || t == 1.0) | |
114 return t; | |
115 | |
116 double start = 0.0; | |
117 double end = 1.0; | |
118 while (true) { | |
119 double midpoint = (start + end) / 2; | |
120 double estimate = _evaluateCubic(a, c, midpoint); | |
121 | |
122 if ((t - estimate).abs() < _kCubicErrorBound) | |
123 return _evaluateCubic(b, d, midpoint); | |
124 | |
125 if (estimate < t) | |
126 start = midpoint; | |
127 else | |
128 end = midpoint; | |
129 } | |
130 } | |
131 } | |
132 | |
133 const Linear linear = const Linear(); | |
134 const Cubic ease = const Cubic(0.25, 0.1, 0.25, 1.0); | |
135 const Cubic easeIn = const Cubic(0.42, 0.0, 1.0, 1.0); | |
136 const Cubic easeOut = const Cubic(0.0, 0.0, 0.58, 1.0); | |
137 const Cubic easeInOut = const Cubic(0.42, 0.0, 0.58, 1.0); | |
OLD | NEW |