OLD | NEW |
| (Empty) |
1 <!-- | |
2 // Copyright 2015 The Chromium Authors. All rights reserved. | |
3 // Use of this source code is governed by a BSD-style license that can be | |
4 // found in the LICENSE file. | |
5 --> | |
6 <script> | |
7 const kDefaultAlpha = -5707.62; | |
8 const kDefaultBeta = 172; | |
9 const kDefaultGamma = 3.7; | |
10 | |
11 function positionAtTime(t) { | |
12 return kDefaultAlpha * Math.exp(-kDefaultGamma * t) - kDefaultBeta * t - kDefa
ultAlpha; | |
13 } | |
14 | |
15 function velocityAtTime(t) { | |
16 return -kDefaultAlpha * kDefaultGamma * Math.exp(-kDefaultGamma * t) - kDefaul
tBeta; | |
17 } | |
18 | |
19 function timeAtVelocity(v) { | |
20 return -Math.log((v + kDefaultBeta) / (-kDefaultAlpha * kDefaultGamma)) / kDef
aultGamma; | |
21 } | |
22 | |
23 var kMaxVelocity = velocityAtTime(0); | |
24 var kCurveDuration = timeAtVelocity(0); | |
25 | |
26 module.exports = class FlingCurve { | |
27 constructor(velocity, startTime) { | |
28 var startingVelocity = Math.min(kMaxVelocity, Math.abs(velocity)); | |
29 this.timeOffset_ = timeAtVelocity(startingVelocity); | |
30 this.positionOffset_ = positionAtTime(this.timeOffset_); | |
31 this.startTime_ = startTime / 1000; | |
32 this.previousPosition_ = 0; | |
33 this.direction_ = Math.sign(velocity); | |
34 Object.preventExtensions(this); | |
35 } | |
36 | |
37 update(timeStamp) { | |
38 var t = timeStamp / 1000 - this.startTime_ + this.timeOffset_; | |
39 if (t >= kCurveDuration) | |
40 return 0; | |
41 var position = positionAtTime(t) - this.positionOffset_; | |
42 var positionDelta = position - this.previousPosition_; | |
43 this.previousPosition_ = position; | |
44 return this.direction_ * Math.max(0, positionDelta); | |
45 } | |
46 }; | |
47 </script> | |
OLD | NEW |