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

Unified Diff: LayoutTests/fast/workers/resources/snap.js

Issue 474683003: Not for review - Rebase of crrev.com/62833003 Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: . Created 6 years, 2 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
« no previous file with comments | « LayoutTests/fast/workers/resources/scrub.js ('k') | LayoutTests/fast/workers/resources/snap-points.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: LayoutTests/fast/workers/resources/snap.js
diff --git a/LayoutTests/fast/workers/resources/snap.js b/LayoutTests/fast/workers/resources/snap.js
new file mode 100644
index 0000000000000000000000000000000000000000..94931c60829bb5090c24e2847c2338085f176196
--- /dev/null
+++ b/LayoutTests/fast/workers/resources/snap.js
@@ -0,0 +1,72 @@
+(function(scope) {
+ 'use strict';
+
+ var tokens = null;
+ var scroll = null;
+ var previous = 0;
+ var consecutiveSnappableFrames = 0;
+
+ function getClosestSnapPoint(oldscroll, delta) {
+ var diff = oldscroll % delta;
+ if (diff >= delta / 2) {
+ return oldscroll + (delta - diff);
+ }
+
+ return oldscroll - diff;
+ }
+
+ function getVelocity(distance) {
+ if (distance === 0)
+ return 0;
+ var sign = distance > 0 ? 1 : -1;
+
+ return distance / 5;
+ //return Math.log(distance + 1);
+ //return sign * (Math.log(Math.abs(distance)+0.0025)+2);
+ }
+
+ function shouldSnap(previous, current) {
+ var pixelThreshold = 0;
+ //var pixelThreshold = 10;
+ var frameThreshold = 10;
+
+ if (Math.abs(current - previous) <= pixelThreshold) {
+ consecutiveSnappableFrames++;
+ } else {
+ consecutiveSnappableFrames = 0;
+ }
+
+ return consecutiveSnappableFrames >= frameThreshold;
+ }
+
+ // An extremely cheesy animation.
+ function tick(context) {
+ var seconds = context.timestamp / 1000.0;
+ var oldscroll = context.getScalar(scroll);
+ var newscrollposition = oldscroll;
+
+ if (shouldSnap(previous, oldscroll)) {
+ console.log('good');
+ var snap = getClosestSnapPoint(oldscroll, 400);
+ var distance = snap - oldscroll;
+ var velocity = getVelocity(distance);
+ newscrollposition += velocity;
+ console.log("snap " + snap + ", distance " + distance + ", velocity " + velocity);
+
+ context.setScalar(scroll, newscrollposition);
+ } else {
+ console.log('bad ' + previous + ' ' + oldscroll);
+ }
+
+ previous = newscrollposition;
+ scope.teleportMessage(context, tick);
+ }
+
+ scope.onmessage = function(e) {
+ tokens = e.data;
+ scroll = tokens[0];
+ var context = new TeleportContext(tokens);
+ scope.teleportMessage(context, tick);
+ };
+
+})(self);
« no previous file with comments | « LayoutTests/fast/workers/resources/scrub.js ('k') | LayoutTests/fast/workers/resources/snap-points.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698