#Penner's easing functions
# from https://github.com/semitable/easing-functions
import math
[docs]
class EasingBase:
limit = (0, 1)
def __init__(self, start: float = 0, end: float = 1, duration: float = 1):
self.start = start
self.end = end
self.duration = duration
[docs]
def func(self, t: float) -> float:
raise NotImplementedError
[docs]
def ease(self, alpha: float) -> float:
t = self.limit[0] * (1 - alpha) + self.limit[1] * alpha
t /= self.duration
a = self.func(t)
return self.end * a + self.start * (1 - a)
def __call__(self, alpha: float) -> float:
return self.ease(alpha)
"""
Linear
"""
[docs]
class LinearInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return t
"""
Quadratic easing functions
"""
[docs]
class QuadEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 0.5:
return 2 * t * t
return (-2 * t * t) + (4 * t) - 1
[docs]
class QuadEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return t * t
[docs]
class QuadEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return -(t * (t - 2))
"""
Cubic easing functions
"""
[docs]
class CubicEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return t * t * t
[docs]
class CubicEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return (t - 1) * (t - 1) * (t - 1) + 1
[docs]
class CubicEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 0.5:
return 4 * t * t * t
p = 2 * t - 2
return 0.5 * p * p * p + 1
"""
Quartic easing functions
"""
[docs]
class QuarticEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return t * t * t * t
[docs]
class QuarticEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return (t - 1) * (t - 1) * (t - 1) * (1 - t) + 1
[docs]
class QuarticEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 0.5:
return 8 * t * t * t * t
p = t - 1
return -8 * p * p * p * p + 1
"""
Quintic easing functions
"""
[docs]
class QuinticEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return t * t * t * t * t
[docs]
class QuinticEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return (t - 1) * (t - 1) * (t - 1) * (t - 1) * (t - 1) + 1
[docs]
class QuinticEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 0.5:
return 16 * t * t * t * t * t
p = (2 * t) - 2
return 0.5 * p * p * p * p * p + 1
"""
Sine easing functions
"""
[docs]
class SineEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return math.sin((t - 1) * math.pi / 2) + 1
[docs]
class SineEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return math.sin(t * math.pi / 2)
[docs]
class SineEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return 0.5 * (1 - math.cos(t * math.pi))
"""
Circular easing functions
"""
[docs]
class CircularEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return 1 - math.sqrt(1 - (t * t))
[docs]
class CircularEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return math.sqrt((2 - t) * t)
[docs]
class CircularEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 0.5:
return 0.5 * (1 - math.sqrt(1 - 4 * (t * t)))
return 0.5 * (math.sqrt(-((2 * t) - 3) * ((2 * t) - 1)) + 1)
"""
Exponential easing functions
"""
[docs]
class ExponentialEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
if t == 0:
return 0
return math.pow(2, 10 * (t - 1))
[docs]
class ExponentialEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t == 1:
return 1
return 1 - math.pow(2, -10 * t)
[docs]
class ExponentialEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t == 0 or t == 1:
return t
if t < 0.5:
return 0.5 * math.pow(2, (20 * t) - 10)
return -0.5 * math.pow(2, (-20 * t) + 10) + 1
"""
Elastic Easing Functions
"""
[docs]
class ElasticEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return math.sin(13 * math.pi / 2 * t) * math.pow(2, 10 * (t - 1))
[docs]
class ElasticEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
return math.sin(-13 * math.pi / 2 * (t + 1)) * math.pow(2, -10 * t) + 1
[docs]
class ElasticEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 0.5:
return (
0.5
* math.sin(13 * math.pi / 2 * (2 * t))
* math.pow(2, 10 * ((2 * t) - 1))
)
return 0.5 * (
math.sin(-13 * math.pi / 2 * ((2 * t - 1) + 1))
* math.pow(2, -10 * (2 * t - 1))
+ 2
)
"""
Back Easing Functions
"""
[docs]
class BackEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return t * t * t - t * math.sin(t * math.pi)
[docs]
class BackEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
p = 1 - t
return 1 - (p * p * p - p * math.sin(p * math.pi))
[docs]
class BackEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 0.5:
p = 2 * t
return 0.5 * (p * p * p - p * math.sin(p * math.pi))
p = 1 - (2 * t - 1)
return 0.5 * (1 - (p * p * p - p * math.sin(p * math.pi))) + 0.5
"""
Bounce Easing Functions
"""
[docs]
class BounceEaseIn(EasingBase):
[docs]
def func(self, t: float) -> float:
return 1 - BounceEaseOut().func(1 - t)
[docs]
class BounceEaseOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 4 / 11:
return 121 * t * t / 16
elif t < 8 / 11:
return (363 / 40.0 * t * t) - (99 / 10.0 * t) + 17 / 5.0
elif t < 9 / 10:
return (4356 / 361.0 * t * t) - (35442 / 1805.0 * t) + 16061 / 1805.0
return (54 / 5.0 * t * t) - (513 / 25.0 * t) + 268 / 25.0
[docs]
class BounceEaseInOut(EasingBase):
[docs]
def func(self, t: float) -> float:
if t < 0.5:
return 0.5 * BounceEaseIn().func(t * 2)
return 0.5 * BounceEaseOut().func(t * 2 - 1) + 0.5
#####################################################################
q = QuadEaseInOut(1, 0, 1)
# for i in range(10):
# print(q(i/10))
[docs]
def cubicInterpolation(p0, p1, p2, p3, t):
t2 = t * t
t3 = t2 * t
return (
0.5
* (
(2 * p1)
+ (-p0 + p2) * t
+ (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2
+ (-p0 + 3 * p1 - 3 * p2 + p3) * t3
)
)
from numpy import array
p1 = array([0, 0])
p2 = array([1, 1])
p3 = array([2, 1])
p4 = array([3, 0])
#print(cubicInterpolation(p1, p2, p3, p4, .5))
[docs]
def ease(alpha: float, duration=10, minV=0, maxV=1) -> float:
t = minV * (1 - alpha) + maxV * alpha
t /= duration
return t
# a = self.func(t)
# return self.end * a + self.start * (1 - a)
#print(ease(1))