-
Notifications
You must be signed in to change notification settings - Fork 0
/
easing.py
42 lines (30 loc) · 1.05 KB
/
easing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Easing functions from https://github.com/semitable/easing-functions"""
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
def func(self, t: float) -> float:
raise NotImplementedError
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)
class LinearInOut(EasingBase):
def func(self, t: float) -> float:
return t
class QuadEaseInOut(EasingBase):
def func(self, t: float) -> float:
if t < 0.5:
return 2 * t * t
return (-2 * t * t) + (4 * t) - 1
class CubicEaseInOut(EasingBase):
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