-
-
Save dvp0/360c75c8fe34828ea31efb813a07a0a0 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