Skip to content

Instantly share code, notes, and snippets.

@ramonfritsch
Created July 1, 2016 17:35
Show Gist options
  • Save ramonfritsch/f0b22fa7179712c2448113c9d58c7d10 to your computer and use it in GitHub Desktop.
Save ramonfritsch/f0b22fa7179712c2448113c9d58c7d10 to your computer and use it in GitHub Desktop.
var FRAME_TIME = 1000 / 60;
var DAMPING = 0.945;
function applyDamping(vel) {
return vel * DAMPING;
}
function applySpeed(x, vel) {
return x + vel;
}
function decelerate(v0, frames) {
console.log(frames, 'decelerate', v0);
var x = 0;
var v = v0;
var f = 0;
var ld = new Date();
var rd = 0;
var iid = setInterval(function () {
var d = new Date();
var dt = d - ld;
rd += dt;
while (rd >= 0) {
v = applyDamping(v);
x = applySpeed(x, v);
rd -= FRAME_TIME;
f++;
if (v <= 1)
{
clearInterval(iid);
console.log(frames, 'finish', x, v, f);
console.log(frames, '---');
break;
}
}
ld = d;
}, 1000 / frames);
}
decelerate(100, 1);
decelerate(100, 5);
decelerate(100, 40);
decelerate(100, 100);
decelerate(100, 200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment