Last active
September 24, 2021 23:09
-
-
Save derpycoder/98529304146e607daaebe389497470d1 to your computer and use it in GitHub Desktop.
CSS Spring Animation Precompute
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
/** | |
* Taken From: | |
* Article: https://medium.com/@dtinth/spring-animation-in-css-2039de6e1a03 | |
* By: Thai Pangsakulyanont | |
*/ | |
function spring(t) { | |
return -0.5 * (2.71828 ** (-6 * t)) * ( | |
-2 * (2.71828 ** (6 * t)) + Math.sin(12 * t) + 2 * Math.cos(12 * t)); | |
} | |
function lerp(a, b, p) { | |
return a + p * (b - a); | |
} | |
function precompute() { | |
let str = "@keyframes suave {\n"; | |
for (let i = 0; i <= 100; i++) { | |
t = i / 100; | |
p = spring(t); | |
str += `\t${i}% { transform: scale(${lerp(0, 1, p)}); }\n`; | |
} | |
return str += "}"; | |
} | |
console.log(precompute()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment