Created
March 24, 2017 13:05
-
-
Save bendc/bd064f9111af22354cf33a79edc798b6 to your computer and use it in GitHub Desktop.
Tween.js ES6 easing functions
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
// k = progress between 0 and 1 | |
const easing = { | |
quadratic: { | |
in(k) { | |
return k * k; | |
}, | |
out(k) { | |
return k * (2 - k); | |
}, | |
inOut(k) { | |
if ((k *= 2) < 1) | |
return 0.5 * k * k; | |
return - 0.5 * (--k * (k - 2) - 1); | |
} | |
}, | |
cubic: { | |
in(k) { | |
return k * k * k; | |
}, | |
out(k) { | |
return --k * k * k + 1; | |
}, | |
inOut(k) { | |
if ((k *= 2) < 1) | |
return 0.5 * k * k * k; | |
return 0.5 * ((k -= 2) * k * k + 2); | |
} | |
}, | |
quartic: { | |
in(k) { | |
return k * k * k * k; | |
}, | |
out(k) { | |
return 1 - (--k * k * k * k); | |
}, | |
inOut(k) { | |
if ((k *= 2) < 1) | |
return 0.5 * k * k * k * k; | |
return - 0.5 * ((k -= 2) * k * k * k - 2); | |
} | |
}, | |
quintic: { | |
in(k) { | |
return k * k * k * k * k; | |
}, | |
out(k) { | |
return --k * k * k * k * k + 1; | |
}, | |
inOut(k) { | |
if ((k *= 2) < 1) | |
return 0.5 * k * k * k * k * k; | |
return 0.5 * ((k -= 2) * k * k * k * k + 2); | |
} | |
}, | |
exponential: { | |
in(k) { | |
return k === 0 ? 0 : Math.pow(1024, k - 1); | |
}, | |
out(k) { | |
return k === 1 ? 1 : 1 - Math.pow(2, - 10 * k); | |
}, | |
inOut(k) { | |
if (k === 0) | |
return 0; | |
if (k === 1) | |
return 1; | |
if ((k *= 2) < 1) | |
return 0.5 * Math.pow(1024, k - 1); | |
return 0.5 * (- Math.pow(2, - 10 * (k - 1)) + 2); | |
} | |
}, | |
circular: { | |
in(k) { | |
return 1 - Math.sqrt(1 - k * k); | |
}, | |
out(k) { | |
return Math.sqrt(1 - (--k * k)); | |
}, | |
inOut(k) { | |
if ((k *= 2) < 1) | |
return - 0.5 * (Math.sqrt(1 - k * k) - 1); | |
return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1); | |
} | |
}, | |
elastic: { | |
in(k) { | |
if (k === 0) | |
return 0; | |
if (k === 1) | |
return 1; | |
return -Math.pow(2, 10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI); | |
}, | |
out(k) { | |
if (k === 0) | |
return 0; | |
if (k === 1) | |
return 1; | |
return Math.pow(2, -10 * k) * Math.sin((k - 0.1) * 5 * Math.PI) + 1; | |
}, | |
inOut(k) { | |
if (k === 0) | |
return 0; | |
if (k === 1) | |
return 1; | |
k *= 2; | |
if (k < 1) | |
return -0.5 * Math.pow(2, 10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI); | |
return 0.5 * Math.pow(2, -10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI) + 1; | |
} | |
}, | |
back: { | |
in(k) { | |
const s = 1.70158; | |
return k * k * ((s + 1) * k - s); | |
}, | |
out(k) { | |
const s = 1.70158; | |
return --k * k * ((s + 1) * k + s) + 1; | |
}, | |
inOut(k) { | |
const s = 1.70158 * 1.525; | |
if ((k *= 2) < 1) | |
return 0.5 * (k * k * ((s + 1) * k - s)); | |
return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2); | |
} | |
}, | |
bounce: { | |
in(k) { | |
return 1 - easing.bounce.out(1 - k); | |
}, | |
out(k) { | |
if (k < (1 / 2.75)) { | |
return 7.5625 * k * k; | |
} else if (k < (2 / 2.75)) { | |
return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75; | |
} else if (k < (2.5 / 2.75)) { | |
return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375; | |
} else { | |
return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375; | |
} | |
}, | |
inOut(k) { | |
if (k < 0.5) | |
return easing.bounce.in(k * 2) * 0.5; | |
return easing.bounce.out(k * 2 - 1) * 0.5 + 0.5; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment