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

Side by Side Diff: sky/framework/animation/generator.dart

Issue 999423004: Drive overscroll animations with a physics simulation (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: More comments 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/framework/animation/kinematics.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 import 'curves.dart'; 5 import 'curves.dart';
6 import 'dart:async'; 6 import 'dart:async';
7 import 'dart:math' as math; 7 import 'dart:math' as math;
8 import 'dart:sky' as sky; 8 import 'dart:sky' as sky;
9 9
10 class FrameGenerator { 10 class FrameGenerator {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 void _tick(double timeStamp) { 45 void _tick(double timeStamp) {
46 _animationId = 0; 46 _animationId = 0;
47 _controller.add(timeStamp); 47 _controller.add(timeStamp);
48 if (!_cancelled) { 48 if (!_cancelled) {
49 _scheduleTick(); 49 _scheduleTick();
50 } 50 }
51 } 51 }
52 } 52 }
53 53
54 class AnimationGenerator extends FrameGenerator { 54 class AnimationGenerator {
55 Stream<double> get onTick => _stream; 55 Stream<double> get onTick => _stream;
56 final double initialDelay; 56 final double initialDelay;
57 final double duration; 57 final double duration;
58 final double begin; 58 final double begin;
59 final double end; 59 final double end;
60 final Curve curve; 60 final Curve curve;
61
62 FrameGenerator _generator;
61 Stream<double> _stream; 63 Stream<double> _stream;
62 bool _done = false; 64 bool _done = false;
63 65
64 AnimationGenerator({ 66 AnimationGenerator({
65 this.initialDelay: 0.0, 67 this.initialDelay: 0.0,
66 this.duration, 68 this.duration,
67 this.begin: 0.0, 69 this.begin: 0.0,
68 this.end: 1.0, 70 this.end: 1.0,
69 this.curve: linear, 71 this.curve: linear,
70 Function onDone 72 Function onDone
71 }):super(onDone: onDone) { 73 }) {
72 assert(duration != null && duration > 0.0); 74 assert(duration != null && duration > 0.0);
75 _generator = new FrameGenerator(onDone: onDone);
76
73 double startTime = 0.0; 77 double startTime = 0.0;
74 _stream = super.onTick.map((timeStamp) { 78 _stream = _generator.onTick.map((timeStamp) {
75 if (startTime == 0.0) 79 if (startTime == 0.0)
76 startTime = timeStamp; 80 startTime = timeStamp;
77 81
78 double t = (timeStamp - (startTime + initialDelay)) / duration; 82 double t = (timeStamp - (startTime + initialDelay)) / duration;
79 return math.max(0.0, math.min(t, 1.0)); 83 return math.max(0.0, math.min(t, 1.0));
80 }) 84 })
81 .takeWhile(_checkForCompletion) // 85 .takeWhile(_checkForCompletion)
82 .where((t) => t >= 0.0) 86 .where((t) => t >= 0.0)
83 .map(_transform); 87 .map(_transform);
84 } 88 }
85 89
90 void cancel() {
91 _generator.cancel();
92 }
93
86 double _transform(double t) { 94 double _transform(double t) {
87 if (_done) 95 if (_done)
88 return end; 96 return end;
89 return begin + (end - begin) * curve.transform(t); 97 return begin + (end - begin) * curve.transform(t);
90 } 98 }
91 99
92 // This is required because Dart Streams don't have takeUntil (inclusive). 100 // This is required because Dart Streams don't have takeUntil (inclusive).
93 bool _checkForCompletion(double t) { 101 bool _checkForCompletion(double t) {
94 if (_done) 102 if (_done)
95 return false; 103 return false;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 begin: _value, 146 begin: _value,
139 end: newValue, 147 end: newValue,
140 curve: curve, 148 curve: curve,
141 initialDelay: initialDelay); 149 initialDelay: initialDelay);
142 150
143 _animation.onTick.listen(_setValue, onDone: () { 151 _animation.onTick.listen(_setValue, onDone: () {
144 _animation = null; 152 _animation = null;
145 }); 153 });
146 } 154 }
147 } 155 }
OLDNEW
« no previous file with comments | « no previous file | sky/framework/animation/kinematics.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698