Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: sky/examples/fn/widgets/animationgenerator.dart

Issue 979283002: Move animationgenerator.dart to sky/framework/animation/generator.dart (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sky/examples/fn/widgets/widgets.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 part of widgets;
2
3 class FrameGenerator {
4
5 Function onDone;
6 StreamController _controller;
7
8 Stream<double> get onTick => _controller.stream;
9
10 int _animationId = 0;
11 bool _cancelled = false;
12
13 FrameGenerator({this.onDone}) {
14 _controller = new StreamController(
15 sync: true,
16 onListen: _scheduleTick,
17 onCancel: cancel);
18 }
19
20 void cancel() {
21 if (_cancelled) {
22 return;
23 }
24 if (_animationId != 0) {
25 sky.window.cancelAnimationFrame(_animationId);
26 }
27 _animationId = 0;
28 _cancelled = true;
29 if (onDone != null) {
30 onDone();
31 }
32 }
33
34 void _scheduleTick() {
35 assert(_animationId == 0);
36 _animationId = sky.window.requestAnimationFrame(_tick);
37 }
38
39 void _tick(double timeStamp) {
40 _animationId = 0;
41 _controller.add(timeStamp);
42 if (!_cancelled) {
43 _scheduleTick();
44 }
45 }
46 }
47
48 class AnimationGenerator extends FrameGenerator {
49
50 Stream<double> get onTick => _stream;
51 final double duration;
52 final double begin;
53 final double end;
54 final Curve curve;
55 Stream<double> _stream;
56 bool _done = false;
57
58 AnimationGenerator(this.duration, {
59 this.begin: 0.0,
60 this.end: 1.0,
61 this.curve: linear,
62 Function onDone
63 }):super(onDone: onDone) {
64 double startTime = 0.0;
65 double targetTime = 0.0;
66 _stream = super.onTick.map((timeStamp) {
67 if (startTime == 0.0) {
68 startTime = timeStamp;
69 targetTime = startTime + duration;
70 }
71 return math.min((timeStamp - startTime) / duration, 1.0);
72 })
73 .takeWhile(_checkForCompletion)
74 .map((t) => begin + (end - begin) * curve.transform(t));
75 }
76
77 bool _checkForCompletion(double t) {
78 if (_done)
79 return false;
80 _done = t >= 1;
81 return true;
82 }
83 }
OLDNEW
« no previous file with comments | « no previous file | sky/examples/fn/widgets/widgets.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698