// set up vars var curPos = 0; var distance = 2000; var friction = 0.8; var velocity = distance / ( friction * ( 1 / ( 1 - friction ) ) ); // in timer: setInterval(function(){ velocity *= friction; curPos += velocity; },30);The curPos value will ease into the target distance, no matter what you set as the friction and distance. Friction must be a decimal between zero and one.
A collection of handy code snippets in the languages that I use on a daily basis.
September 25, 2011
Given distance and friction, calculate the initial velocity to stop at the distance target
While simulating iOS scroll views in javascript, I needed to find the initial velocity to send the scroll content back into position if you drag it out of bounds. Since the in-bounds scroll view inertia was already working with a given friction, I wanted to use this to run the calculation. After some brute-force experimenting, I came up with the equation to calculate this velocity:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment