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 27 matching lines...) Expand all Loading... |
38 | 38 |
39 void _tick(double timeStamp) { | 39 void _tick(double timeStamp) { |
40 _animationId = 0; | 40 _animationId = 0; |
41 _controller.add(timeStamp); | 41 _controller.add(timeStamp); |
42 if (!_cancelled) { | 42 if (!_cancelled) { |
43 _scheduleTick(); | 43 _scheduleTick(); |
44 } | 44 } |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 const double _kFrameTime = 1000 / 60; | |
49 | |
50 class AnimationGenerator extends FrameGenerator { | 48 class AnimationGenerator extends FrameGenerator { |
51 | 49 |
52 Stream<double> get onTick => _stream; | 50 Stream<double> get onTick => _stream; |
53 final double duration; | 51 final double duration; |
54 final double begin; | 52 final double begin; |
55 final double end; | 53 final double end; |
56 final Curve curve; | 54 final Curve curve; |
57 Stream<double> _stream; | 55 Stream<double> _stream; |
| 56 bool _done = false; |
58 | 57 |
59 AnimationGenerator(this.duration, { | 58 AnimationGenerator(this.duration, { |
60 this.begin: 0.0, | 59 this.begin: 0.0, |
61 this.end: 1.0, | 60 this.end: 1.0, |
62 this.curve: linear, | 61 this.curve: linear, |
63 Function onDone | 62 Function onDone |
64 }):super(onDone: onDone) { | 63 }):super(onDone: onDone) { |
65 double startTime = 0.0; | 64 double startTime = 0.0; |
66 double targetTime = 0.0; | 65 double targetTime = 0.0; |
67 bool done = false; | |
68 _stream = super.onTick.map((timeStamp) { | 66 _stream = super.onTick.map((timeStamp) { |
69 if (startTime == 0.0) { | 67 if (startTime == 0.0) { |
70 startTime = timeStamp; | 68 startTime = timeStamp; |
71 targetTime = startTime + duration; | 69 targetTime = startTime + duration; |
72 } | 70 } |
| 71 return math.min((timeStamp - startTime) / duration, 1.0); |
| 72 }) |
| 73 .takeWhile(_checkForCompletion) |
| 74 .map((t) => begin + (end - begin) * curve.transform(t)); |
| 75 } |
73 | 76 |
74 // Clamp the final frame to target time so we terminate the series with | 77 bool _checkForCompletion(double t) { |
75 // 1.0 exactly. | 78 if (_done) |
76 if ((timeStamp - targetTime).abs() <= _kFrameTime) { | 79 return false; |
77 return 1.0; | 80 _done = t >= 1; |
78 } | 81 return true; |
79 | |
80 return (timeStamp - startTime) / duration; | |
81 }) | |
82 .takeWhile((t) => t <= 1.0) | |
83 .map((t) => begin + (end - begin) * curve.transform(t)); | |
84 } | 82 } |
85 } | 83 } |
86 | 84 |
87 double _evaluateCubic(double a, double b, double m) { | 85 double _evaluateCubic(double a, double b, double m) { |
88 // TODO(abarth): Would Math.pow be faster? | 86 // TODO(abarth): Would Math.pow be faster? |
89 return 3 * a * (1 - m) * (1 - m) * m + 3 * b * (1 - m) * m * m + m * m * m; | 87 return 3 * a * (1 - m) * (1 - m) * m + 3 * b * (1 - m) * m * m + m * m * m; |
90 } | 88 } |
91 | 89 |
92 const double _kCubicErrorBound = 0.001; | 90 const double _kCubicErrorBound = 0.001; |
93 | 91 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 end = midpoint; | 128 end = midpoint; |
131 } | 129 } |
132 } | 130 } |
133 } | 131 } |
134 | 132 |
135 const Linear linear = const Linear(); | 133 const Linear linear = const Linear(); |
136 const Cubic ease = const Cubic(0.25, 0.1, 0.25, 1.0); | 134 const Cubic ease = const Cubic(0.25, 0.1, 0.25, 1.0); |
137 const Cubic easeIn = const Cubic(0.42, 0.0, 1.0, 1.0); | 135 const Cubic easeIn = const Cubic(0.42, 0.0, 1.0, 1.0); |
138 const Cubic easeOut = const Cubic(0.0, 0.0, 0.58, 1.0); | 136 const Cubic easeOut = const Cubic(0.0, 0.0, 0.58, 1.0); |
139 const Cubic easeInOut = const Cubic(0.42, 0.0, 0.58, 1.0); | 137 const Cubic easeInOut = const Cubic(0.42, 0.0, 0.58, 1.0); |
OLD | NEW |