Skip to content

Instantly share code, notes, and snippets.

@mike-at-redspace
Created January 15, 2023 09:54
Show Gist options
  • Save mike-at-redspace/21979a0b787fb7029c6518ee5226f517 to your computer and use it in GitHub Desktop.
Save mike-at-redspace/21979a0b787fb7029c6518ee5226f517 to your computer and use it in GitHub Desktop.
Collection of easing functions
/*
## All functions return a number, which is guaranteed to start at 0 and end at 1
f(t), where t in [0..1]
f(0) -> 0
f(1) -> 1
*/
export const easing = {
// No easing, no acceleration
linear: (t) => t,
// Accelerates fast, then slows quickly towards end.
quadratic: (t) => t * (-(t * t) * t + 4 * t * t - 6 * t + 4),
// Overshoots over 1 and then returns to 1 towards end.
cubic: (t) => t * (4 * t * t - 9 * t + 6),
// Overshoots over 1 multiple times - wiggles around 1.
elastic: (t) => t * (33 * t * t * t * t - 106 * t * t * t + 126 * t * t - 67 * t + 15),
// Accelerating from zero velocity
inQuad: (t) => t * t,
// Decelerating to zero velocity
outQuad: (t) => t * (2 - t),
// Acceleration until halfway, then deceleration
inOutQuad: (t) => t <.5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
// Accelerating from zero velocity
inCubic: (t) => t * t * t,
// Decelerating to zero velocity
outCubic: (t) => (--t) * t * t + 1,
// Acceleration until halfway, then deceleration
inOutCubic: (t) => t <.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
// Accelerating from zero velocity
inQuart: (t) => t * t * t * t,
// Decelerating to zero velocity
outQuart: (t) => 1 - (--t) * t * t * t,
// Acceleration until halfway, then deceleration
inOutQuart: (t) => t <.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t,
// Accelerating from zero velocity
inQuint: (t) => t * t * t * t * t,
// Decelerating to zero velocity
outQuint: (t) => 1 + (--t) * t * t * t * t,
// Acceleration until halfway, then deceleration
inOutQuint: (t) => t < .5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t,
// Accelerating from zero velocity
inSine: (t) => -Math.cos(t * (Math.PI / 2)) + 1,
// Decelerating to zero velocity
outSine: (t) => Math.sin(t * (Math.PI / 2)),
// Accelerating until halfway, then decelerating
inOutSine: (t) => -(Math.cos(Math.PI * t) - 1) / 2,
// Exponential accelerating from zero velocity
inExpo: (t) => Math.pow(2, 10 * (t - 1)),
// Exponential decelerating to zero velocity
outExpo: (t) => -Math.pow(2, -10 * t) + 1,
// Exponential accelerating until halfway, then decelerating
inOutExpo: (t) => {
t /= .5;
if (t < 1) return Math.pow(2, 10 * (t - 1)) / 2;
t--;
return (-Math.pow( 2, -10 * t) + 2) / 2;
},
// Circular accelerating from zero velocity
inCirc: (t) => -Math.sqrt(1 - t * t) + 1,
// Circular decelerating to zero velocity Moves VERY fast at the beginning and
// then quickly slows down in the middle. This tween can actually be used
// in continuous transitions where target value changes all the time,
// because of the very quick start, it hides the jitter between target value changes.
outCirc: (t) => Math.sqrt(1 - (t = t - 1) * t),
// Circular acceleration until halfway, then deceleration
inOutCirc: (t) => {
t /= .5;
if (t < 1) return -(Math.sqrt(1 - t * t) - 1) / 2;
t -= 2;
return (Math.sqrt(1 - t * t) + 1) / 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment