Skip to content

Instantly share code, notes, and snippets.

@dvp0
Forked from davidgilbertson/ease.js
Created October 17, 2018 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvp0/360c75c8fe34828ea31efb813a07a0a0 to your computer and use it in GitHub Desktop.
Save dvp0/360c75c8fe34828ea31efb813a07a0a0 to your computer and use it in GitHub Desktop.
function ease({
startValue = 0,
endValue = 1,
durationMs = 200,
onStep,
onComplete = () => {},
}) {
const raf = window.requestAnimationFrame || (func => window.setTimeout(func, 16));
const stepCount = durationMs / 16;
const valueIncrement = (endValue - startValue) / stepCount;
const sinValueIncrement = Math.PI / stepCount;
let currentValue = startValue;
let currentSinValue = 0;
function step() {
currentSinValue += sinValueIncrement;
currentValue += valueIncrement * (Math.sin(currentSinValue) ** 2) * 2;
if (currentSinValue < Math.PI) {
onStep(currentValue);
raf(step);
} else {
onStep(endValue);
onComplete();
}
}
raf(step);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment