OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 import "dart:math" as math; |
| 6 |
| 7 const double _kDefaultAlpha = -5707.62; |
| 8 const double _kDefaultBeta = 172.0; |
| 9 const double _kDefaultGamma = 3.7; |
| 10 |
| 11 double _positionAtTime(double t) { |
| 12 return kDefaultAlpha * math.exp(-kDefaultGamma * t) - kDefaultBeta * t - kDefa
ultAlpha; |
| 13 } |
| 14 |
| 15 double _velocityAtTime(double t) { |
| 16 return -kDefaultAlpha * kDefaultGamma * math.exp(-kDefaultGamma * t) - kDefaul
tBeta; |
| 17 } |
| 18 |
| 19 double _timeAtVelocity(double v) { |
| 20 return -math.log((v + kDefaultBeta) / (-kDefaultAlpha * kDefaultGamma)) / kDef
aultGamma; |
| 21 } |
| 22 |
| 23 final double _kMaxVelocity = _velocityAtTime(0.0); |
| 24 final double _kCurveDuration = _timeAtVelocity(0.0); |
| 25 |
| 26 class FlingCurve { |
| 27 double _timeOffset; |
| 28 double _positionOffset; |
| 29 double _startTime; |
| 30 double _previousPosition; |
| 31 double _direction; |
| 32 |
| 33 FlingCurve(double velocity, double startTime) { |
| 34 double startingVelocity = math.min(_kMaxVelocity, velocity.abs()); |
| 35 _timeOffset = _velocityAtTime(startingVelocity); |
| 36 _positionOffset = _positionAtTime(_timeOffset); |
| 37 _startTime = startTime / 1000.0; |
| 38 _previousPosition = 0.0; |
| 39 _direction = velocity.sign; |
| 40 } |
| 41 |
| 42 double update(double timeStamp) { |
| 43 double t = timeStamp / 1000.0 - _startTime + _timeOffset; |
| 44 if (t >= _kCurveDuration) |
| 45 return 0.0; |
| 46 double position = _positionAtTime(t) - _positionOffset; |
| 47 double positionDelta = position - _previousPosition; |
| 48 _previousPosition = position; |
| 49 return _direction * math.max(0.0, positionDelta); |
| 50 } |
| 51 } |
OLD | NEW |