Skip to content

Instantly share code, notes, and snippets.

@robweychert
Last active November 1, 2024 14:02
Show Gist options
  • Save robweychert/7efa6a5f762207245646b16f29dd6671 to your computer and use it in GitHub Desktop.
Save robweychert/7efa6a5f762207245646b16f29dd6671 to your computer and use it in GitHub Desktop.
Python easing functions

Python easing functions

For precision 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

image

def linear(t):
    return t

easeInSine

image

def easeInSine(t):
    import math
    return -math.cos(t * math.pi / 2) + 1

easeOutSine

image

def easeOutSine(t):
    import math
    return math.sin(t * math.pi / 2)

easeInOutSine

image

def easeInOutSine(t):
    import math
    return -(math.cos(math.pi * t) - 1) / 2

easeInQuad

image

def easeInQuad(t):
    return t * t

easeOutQuad

image

def easeOutQuad(t):
    return -t * (t - 2)

easeInOutQuad

image

def easeInOutQuad(t):
    t *= 2
    if t < 1:
        return t * t / 2
    else:
        t -= 1
        return -(t * (t - 2) - 1) / 2

easeInCubic

image

def easeInCubic(t):
    return t * t * t

easeOutCubic

image

def easeOutCubic(t):
    t -= 1
    return t * t * t + 1

easeInOutCubic

image

def easeInOutCubic(t):
    t *= 2
    if t < 1:
        return t * t * t / 2
    else:
        t -= 2
        return (t * t * t + 2) / 2

easeInQuart

image

def easeInQuart(t):
    return t * t * t * t

easeOutQuart

image

def easeOutQuart(t):
    t -= 1
    return -(t * t * t * t - 1)

easeInOutQuart

image

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

image

def easeInQuint(t):
    return t * t * t * t * t

easeOutQuint

image

def easeOutQuint(t):
    t -= 1
    return t * t * t * t * t + 1

easeInOutQuint

image

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

image

def easeInExpo(t):
    import math
    return math.pow(2, 10 * (t - 1))

easeOutExpo

image

def easeOutExpo(t):
    import math
    return -math.pow(2, -10 * t) + 1

easeInOutExpo

image

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

image

def easeInCirc(t):
    import math
    return 1 - math.sqrt(1 - t * t)

easeOutCirc

image

def easeOutCirc(t):
    import math
    t -= 1
    return math.sqrt(1 - t * t)

easeInOutCirc

image

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
def linear(t):
return t
def easeInSine(t):
return -math.cos(t * math.pi / 2) + 1
def easeOutSine(t):
return math.sin(t * math.pi / 2)
def easeInOutSine(t):
return -(math.cos(math.pi * t) - 1) / 2
def easeInQuad(t):
return t * t
def easeOutQuad(t):
return -t * (t - 2)
def easeInOutQuad(t):
t *= 2
if t < 1:
return t * t / 2
else:
t -= 1
return -(t * (t - 2) - 1) / 2
def easeInCubic(t):
return t * t * t
def easeOutCubic(t):
t -= 1
return t * t * t + 1
def easeInOutCubic(t):
t *= 2
if t < 1:
return t * t * t / 2
else:
t -= 2
return (t * t * t + 2) / 2
def easeInQuart(t):
return t * t * t * t
def easeOutQuart(t):
t -= 1
return -(t * t * t * t - 1)
def easeInOutQuart(t):
t *= 2
if t < 1:
return t * t * t * t / 2
else:
t -= 2
return -(t * t * t * t - 2) / 2
def easeInQuint(t):
return t * t * t * t * t
def easeOutQuint(t):
t -= 1
return t * t * t * t * t + 1
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
def easeInExpo(t):
return math.pow(2, 10 * (t - 1))
def easeOutExpo(t):
return -math.pow(2, -10 * t) + 1
def easeInOutExpo(t):
t *= 2
if t < 1:
return math.pow(2, 10 * (t - 1)) / 2
else:
t -= 1
return -math.pow(2, -10 * t) - 1
def easeInCirc(t):
return 1 - math.sqrt(1 - t * t)
def easeOutCirc(t):
t -= 1
return math.sqrt(1 - t * t)
def easeInOutCirc(t):
t *= 2
if t < 1:
return -(math.sqrt(1 - t * t) - 1) / 2
else:
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