Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Last active October 17, 2018 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davidgilbertson/ab95e8e14c403dc0419602bb7fa3f6e7 to your computer and use it in GitHub Desktop.
Save davidgilbertson/ab95e8e14c403dc0419602bb7fa3f6e7 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