Skip to content

Instantly share code, notes, and snippets.

@alexnoz
Last active February 17, 2020 17:27
Show Gist options
  • Save alexnoz/3ec1fc4a076b3e8764a8f39e691f6e85 to your computer and use it in GitHub Desktop.
Save alexnoz/3ec1fc4a076b3e8764a8f39e691f6e85 to your computer and use it in GitHub Desktop.
A bunch of easing functions
function linear (m) {
return m
}
function power1In (m) {
return -Math.cos(m * (Math.PI / 2)) + 1
}
function power1Out (m) {
return Math.sin(m * (Math.PI / 2))
}
function power1InOut (m) {
return -0.5 * (Math.cos(Math.PI * m) - 1)
}
function power2In (m) {
return m * m
}
function power2Out (m) {
return m * (2 - m)
}
function power2InOut (m) {
return m < 0.5 ? 2 * m * m : -1 + (4 - 2 * m) * m
}
function power3In (m) {
return m * m * m
}
function power3Out (m) {
return (--m) * m * m + 1
}
function power3InOut (m) {
return m < 0.5 ? 4 * m * m * m : (m - 1) * (2 * m - 2) * (2 * m - 2) + 1
}
function power4In (m) {
return m * m * m * m
}
function power4Out (m) {
return 1 - (--m) * m * m * m
}
function power4InOut (m) {
return m < 0.5 ? 8 * m * m * m * m : 1 - 8 * (--m) * m * m * m
}
function power5In (m) {
return m * m * m * m * m
}
function power5Out (m) {
return 1 + (--m) * m * m * m * m
}
function power5InOut (m) {
return m < 0.5 ? 16 * m * m * m * m * m : 1 + 16 * (--m) * m * m * m * m
}
function expoIn (m) {
return (m === 0) ? 0 : Math.pow(2, 10 * (m - 1))
}
function expoOut (m) {
return (m === 1) ? 1 : -Math.pow(2, -10 * m) + 1
}
function expoInOut (m) {
return (m === 0)
? 0
: (m === 1)
? 1
: ((m /= 0.5) < 1)
? (0.5 * Math.pow(2, 10 * (m - 1)))
: (0.5 * (-Math.pow(2, -10 * --m) + 2))
}
function circIn (m) {
return -(Math.sqrt(1 - (m * m)) - 1)
}
function circOut (m) {
return Math.sqrt(1 - Math.pow((m - 1), 2))
}
function circInOut (m) {
return ((m /= 0.5) < 1)
? (-0.5 * (Math.sqrt(1 - m * m) - 1))
: (0.5 * (Math.sqrt(1 - (m -= 2) * m) + 1))
}
function backIn (m) {
const s = 1.70158
return m * m * ((s + 1) * m - s)
}
function backOut (m) {
const s = 1.70158
return (m = m - 1) * m * ((s + 1) * m + s) + 1
}
function backInOut (m) {
const r = 1.525
let s = 1.70158
return (m /= 0.5) < 1
? (0.5 * (m * m * (((s *= r) + 1) * m - s)))
: (0.5 * ((m -= 2) * m * (((s *= r) + 1) * m + s) + 2))
}
function elastic (m) {
return -1 * Math.pow(4, -8 * m) * Math.sin((m * 6 - 1) * (2 * Math.PI) / 2) + 1
}
function swingFromTo (m) {
const r = 1.525
let s = 1.70158
return (m /= 0.5) < 1
? (0.5 * (m * m * (((s *= r) + 1) * m - s)))
: (0.5 * ((m -= 2) * m * (((s *= r) + 1) * m + s) + 2))
}
function swingFrom (m) {
const s = 1.70158
return m * m * ((s + 1) * m - s)
}
function swingTo (m) {
const s = 1.70158
return (m -= 1) * m * ((s + 1) * m + s) + 1
}
function bounce (m) {
const q = 2.25, u = 0.984375, v = 7.5625, w = 0.9375, x = 2.75, y = 2.625, z = 0.75
return (m < 1 / x)
? (v * m * m)
: (m < 2 / x)
? (v * (m -= 1.5 / x) * m + z)
: (m < 2.5 / x)
? (v * (m -= q / x) * m + w)
: (v * (m -= y / x) * m + u)
}
function bouncePast (m) {
const q = 2.25, u = 0.984375, v = 7.5625, w = 0.9375, x = 2.75, y = 2.625, z = 0.75
return (m < 1 / x)
? (v * m * m)
: (m < 2 / x)
? (2 - (v * (m -= 1.5 / x) * m + z))
: (m < 2.5 / x)
? (2 - (v * (m -= q / x) * m + w))
: (2 - (v * (m -= y / x) * m + u))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment