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

Unified Diff: sky/framework/fling-curve.sky

Issue 880473003: sky-scrollable should use a reasonable fling curve (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Now with heights Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: sky/framework/fling-curve.sky
diff --git a/sky/framework/fling-curve.sky b/sky/framework/fling-curve.sky
new file mode 100644
index 0000000000000000000000000000000000000000..1c13908b4f974ad9427238b977794ed67dc1e1ad
--- /dev/null
+++ b/sky/framework/fling-curve.sky
@@ -0,0 +1,47 @@
+<!--
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+-->
+<script>
+const kDefaultAlpha = -5707.62;
+const kDefaultBeta = 172;
+const kDefaultGamma = 3.7;
+
+function positionAtTime(t) {
+ return kDefaultAlpha * Math.exp(-kDefaultGamma * t) - kDefaultBeta * t - kDefaultAlpha;
+}
+
+function velocityAtTime(t) {
+ return -kDefaultAlpha * kDefaultGamma * Math.exp(-kDefaultGamma * t) - kDefaultBeta;
+}
+
+function timeAtVelocity(v) {
+ return -Math.log((v + kDefaultBeta) / (-kDefaultAlpha * kDefaultGamma)) / kDefaultGamma;
+}
+
+var kMaxVelocity = velocityAtTime(0);
+var kCurveDuration = timeAtVelocity(0);
+
+module.exports = class FlingCurve {
+ constructor(velocity, startTime) {
+ var startingVelocity = Math.min(kMaxVelocity, Math.abs(velocity));
+ this.timeOffset_ = timeAtVelocity(startingVelocity);
+ this.positionOffset_ = positionAtTime(this.timeOffset_);
+ this.startTime_ = startTime / 1000;
+ this.previousPosition_ = 0;
+ this.direction_ = Math.sign(velocity);
+ Object.preventExtensions(this);
+ }
+
+ update(timeStamp) {
+ var t = timeStamp / 1000 - this.startTime_ + this.timeOffset_;
+ if (t >= kCurveDuration)
+ return 0;
+ var position = positionAtTime(t) - this.positionOffset_;
+ var positionDelta = position - this.previousPosition_;
+ this.previousPosition_ = position;
+ return this.direction_ * Math.max(0, positionDelta);
+ }
+};
+</script>

Powered by Google App Engine
This is Rietveld 408576698