Python easing functions
For precise programmatic animation
- Translated from the JavaScript in Sean Yen’s Easing equations
- Illustrations adapted from Andrey Sitnik and Ivan Solovev’s Easings.net
Example usage:
duration = 30
for frame in range(duration):
return easeInOutQuad(frame/duration)
linear
def linear(t):
return t
easeInSine
def easeInSine(t):
import math
return -math.cos(t * math.pi / 2) + 1
easeOutSine
def easeOutSine(t):
import math
return math.sin(t * math.pi / 2)
easeInOutSine
def easeInOutSine(t):
import math
return -(math.cos(math.pi * t) - 1) / 2
easeInQuad
def easeInQuad(t):
return t * t
easeOutQuad
def easeOutQuad(t):
return -t * (t - 2)
easeInOutQuad
def easeInOutQuad(t):
t *= 2
if t < 1:
return t * t / 2
else:
t -= 1
return -(t * (t - 2) - 1) / 2
easeInCubic
def easeInCubic(t):
return t * t * t
easeOutCubic
def easeOutCubic(t):
t -= 1
return t * t * t + 1
easeInOutCubic
def easeInOutCubic(t):
t *= 2
if t < 1:
return t * t * t / 2
else:
t -= 2
return (t * t * t + 2) / 2
easeInQuart
def easeInQuart(t):
return t * t * t * t
easeOutQuart
def easeOutQuart(t):
t -= 1
return -(t * t * t * t - 1)
easeInOutQuart
def easeInOutQuart(t):
t *= 2
if t < 1:
return t * t * t * t / 2
else:
t -= 2
return -(t * t * t * t - 2) / 2
easeInQuint
def easeInQuint(t):
return t * t * t * t * t
easeOutQuint
def easeOutQuint(t):
t -= 1
return t * t * t * t * t + 1
easeInOutQuint
def easeInOutQuint(t):
t *= 2
if t < 1:
return t * t * t * t * t / 2
else:
t -= 2
return (t * t * t * t * t + 2) / 2
easeInExpo
def easeInExpo(t):
import math
return math.pow(2, 10 * (t - 1))
easeOutExpo
def easeOutExpo(t):
import math
return -math.pow(2, -10 * t) + 1
easeInOutExpo
def easeInOutExpo(t):
import math
t *= 2
if t < 1:
return math.pow(2, 10 * (t - 1)) / 2
else:
t -= 1
return -math.pow(2, -10 * t) - 1
easeInCirc
def easeInCirc(t):
import math
return 1 - math.sqrt(1 - t * t)
easeOutCirc
def easeOutCirc(t):
import math
t -= 1
return math.sqrt(1 - t * t)
easeInOutCirc
def easeInOutCirc(t):
import math
t *= 2
if t < 1:
return -(math.sqrt(1 - t * t) - 1) / 2
else:
t -= 2
return (math.sqrt(1 - t * t) + 1) / 2