Last active
October 17, 2018 23:34
-
-
Save davidgilbertson/ab95e8e14c403dc0419602bb7fa3f6e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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