A community-maintained Python framework for creating mathematical animations.
Fixed rate functions and lag ratios (#1678)
Bill Shillito0c2472936796
diff --git a/manim/animation/animation.py b/manim/animation/animation.pyindex 9fee0d32a..6c6555532 100644--- a/manim/animation/animation.py+++ b/manim/animation/animation.py@@ -275,8 +275,7 @@ class Animation:The relative time to set the aniamtion to, 0 meaning the start, 1 meaningthe end."""- alpha = min(max(alpha, 0), 1)- self.interpolate_mobject(self.rate_func(alpha))+ self.interpolate_mobject(alpha)def interpolate_mobject(self, alpha: float) -> None:families = list(self.get_all_families_zipped())@@ -318,7 +317,7 @@ class Animation:full_length = (num_submobjects - 1) * lag_ratio + 1value = alpha * full_lengthlower = index * lag_ratio- return min(max((value - lower), 0), 1)+ return self.rate_func(value - lower)# Getters and settersdef set_run_time(self, run_time: float) -> "Animation":diff --git a/manim/animation/movement.py b/manim/animation/movement.pyindex 3ca1e7bf0..5c9db2fc1 100644--- a/manim/animation/movement.py+++ b/manim/animation/movement.py@@ -97,7 +97,9 @@ class PhaseFlow(Animation):def interpolate_mobject(self, alpha: float) -> None:if hasattr(self, "last_alpha"):- dt = self.virtual_time * (alpha - self.last_alpha)+ dt = self.virtual_time * (+ self.rate_func(alpha) - self.rate_func(self.last_alpha)+ )self.mobject.apply_function(lambda p: p + dt * self.function(p))self.last_alpha = alpha@@ -131,5 +133,5 @@ class MoveAlongPath(Animation):)def interpolate_mobject(self, alpha: float) -> None:- point = self.path.point_from_proportion(alpha)+ point = self.path.point_from_proportion(self.rate_func(alpha))self.mobject.move_to(point)diff --git a/manim/utils/rate_functions.py b/manim/utils/rate_functions.pyindex 8a97e1366..3b03f13e6 100644--- a/manim/utils/rate_functions.py+++ b/manim/utils/rate_functions.py@@ -102,8 +102,8 @@ __all__ = ["exponential_decay",]-import typing+from functools import wrapsfrom math import sqrtimport numpy as np@@ -112,10 +112,40 @@ from ..utils.bezier import bezierfrom ..utils.simple_functions import sigmoid+# This is a decorator that makes sure any function it's used on will+# return 0 if t<0 and 1 if t>1.+def unit_interval(function):+ @wraps(function)+ def wrapper(t, *args, **kwargs):+ if 0 <= t <= 1:+ return function(t, *args, **kwargs)+ elif t < 0:+ return 0+ else:+ return 1++ return wrapper+++# This is a decorator that makes sure any function it's used on will+# return 0 if t<0 or t>1.+def zero(function):+ @wraps(function)+ def wrapper(t, *args, **kwargs):+ if 0 <= t <= 1:+ return function(t, *args, **kwargs)+ else:+ return 0++ return wrapper+++@unit_intervaldef linear(t: float) -> float:return t+@unit_intervaldef smooth(t: float, inflection: float = 10.0) -> float:error = sigmoid(-inflection / 2)return min(@@ -124,18 +154,22 @@ def smooth(t: float, inflection: float = 10.0) -> float:)+@unit_intervaldef rush_into(t: float, inflection: float = 10.0) -> float:return 2 * smooth(t / 2.0, inflection)+@unit_intervaldef rush_from(t: float, inflection: float = 10.0) -> float:return 2 * smooth(t / 2.0 + 0.5, inflection) - 1+@unit_intervaldef slow_into(t: float) -> float:return np.sqrt(1 - (1 - t) * (1 - t))+@unit_intervaldef double_smooth(t: float) -> float:if t < 0.5:return 0.5 * smooth(2 * t)@@ -143,11 +177,13 @@ def double_smooth(t: float) -> float:return 0.5 * (1 + smooth(2 * t - 1))+@zerodef there_and_back(t: float, inflection: float = 10.0) -> float:new_t = 2 * t if t < 0.5 else 2 * (1 - t)return smooth(new_t, inflection)+@zerodef there_and_back_with_pause(t: float, pause_ratio: float = 1.0 / 3) -> float:a = 1.0 / pause_ratioif t < 0.5 - pause_ratio / 2:@@ -158,6 +194,7 @@ def there_and_back_with_pause(t: float, pause_ratio: float = 1.0 / 3) -> float:return smooth(a - a * t)+@unit_intervaldef running_start(t: float, pull_factor: float = -0.5) -> typing.Iterable: # what is func return type?@@ -174,6 +211,7 @@ def not_quite_there(return result+@zerodef wiggle(t: float, wiggles: float = 2) -> float:return there_and_back(t) * np.sin(wiggles * np.pi * t)@@ -203,84 +241,104 @@ def squish_rate_func(# "lingering", different from squish_rate_func's default params+@unit_intervaldef lingering(t: float) -> float:return squish_rate_func(lambda t: t, 0, 0.8)(t)+@unit_intervaldef exponential_decay(t: float, half_life: float = 0.1) -> float:# The half-life should be rather small to minimize# the cut-off error at the endreturn 1 - np.exp(-t / half_life)+@unit_intervaldef ease_in_sine(t: float) -> float:return 1 - np.cos((t * np.pi) / 2)+@unit_intervaldef ease_out_sine(t: float) -> float:return np.sin((t * np.pi) / 2)+@unit_intervaldef ease_in_out_sine(t: float) -> float:return -(np.cos(np.pi * t) - 1) / 2+@unit_intervaldef ease_in_quad(t: float) -> float:return t * t+@unit_intervaldef ease_out_quad(t: float) -> float:return 1 - (1 - t) * (1 - t)+@unit_intervaldef ease_in_out_quad(t: float) -> float:return 2 * t * t if t < 0.5 else 1 - pow(-2 * t + 2, 2) / 2+@unit_intervaldef ease_in_cubic(t: float) -> float:return t * t * t+@unit_intervaldef ease_out_cubic(t: float) -> float:return 1 - pow(1 - t, 3)+@unit_intervaldef ease_in_out_cubic(t: float) -> float:return 4 * t * t * t if t < 0.5 else 1 - pow(-2 * t + 2, 3) / 2+@unit_intervaldef ease_in_quart(t: float) -> float:return t * t * t * t+@unit_intervaldef ease_out_quart(t: float) -> float:return 1 - pow(1 - t, 4)+@unit_intervaldef ease_in_out_quart(t: float) -> float:return 8 * t * t * t * t if t < 0.5 else 1 - pow(-2 * t + 2, 4) / 2+@unit_intervaldef ease_in_quint(t: float) -> float:return t * t * t * t * t+@unit_intervaldef ease_out_quint(t: float) -> float:return 1 - pow(1 - t, 5)+@unit_intervaldef ease_in_out_quint(t: float) -> float:return 16 * t * t * t * t * t if t < 0.5 else 1 - pow(-2 * t + 2, 5) / 2+@unit_intervaldef ease_in_expo(t: float) -> float:return 0 if t == 0 else pow(2, 10 * t - 10)+@unit_intervaldef ease_out_expo(t: float) -> float:return 1 if t == 1 else 1 - pow(2, -10 * t)+@unit_intervaldef ease_in_out_expo(t: float) -> float:if t == 0:return 0@@ -289,17 +347,20 @@ def ease_in_out_expo(t: float) -> float:elif t < 0.5:return pow(2, 20 * t - 10) / 2else:- return 2 - pow(2, -20 * t + 10) / 2+ return (2 - pow(2, -20 * t + 10)) / 2+@unit_intervaldef ease_in_circ(t: float) -> float:return 1 - sqrt(1 - pow(t, 2))+@unit_intervaldef ease_out_circ(t: float) -> float:return sqrt(1 - pow(t - 1, 2))+@unit_intervaldef ease_in_out_circ(t: float) -> float:return ((1 - sqrt(1 - pow(2 * t, 2))) / 2@@ -308,18 +369,21 @@ def ease_in_out_circ(t: float) -> float:)+@unit_intervaldef ease_in_back(t: float) -> float:c1 = 1.70158c3 = c1 + 1return c3 * t * t * t - c1 * t * t+@unit_intervaldef ease_out_back(t: float) -> float:c1 = 1.70158c3 = c1 + 1return 1 + c3 * pow(t - 1, 3) + c1 * pow(t - 1, 2)+@unit_intervaldef ease_in_out_back(t: float) -> float:c1 = 1.70158c2 = c1 * 1.525@@ -330,6 +394,7 @@ def ease_in_out_back(t: float) -> float:)+@unit_intervaldef ease_in_elastic(t: float) -> float:c4 = (2 * np.pi) / 3if t == 0:@@ -340,6 +405,7 @@ def ease_in_elastic(t: float) -> float:return -pow(2, 10 * t - 10) * np.sin((t * 10 - 10.75) * c4)+@unit_intervaldef ease_out_elastic(t: float) -> float:c4 = (2 * np.pi) / 3if t == 0:@@ -350,6 +416,7 @@ def ease_out_elastic(t: float) -> float:return pow(2, -10 * t) * np.sin((t * 10 - 0.75) * c4) + 1+@unit_intervaldef ease_in_out_elastic(t: float) -> float:c5 = (2 * np.pi) / 4.5if t == 0:@@ -362,10 +429,12 @@ def ease_in_out_elastic(t: float) -> float:return (pow(2, -20 * t + 10) * np.sin((20 * t - 11.125) * c5)) / 2 + 1+@unit_intervaldef ease_in_bounce(t: float) -> float:return 1 - ease_out_bounce(1 - t)+@unit_intervaldef ease_out_bounce(t: float) -> float:n1 = 7.5625d1 = 2.75@@ -373,18 +442,16 @@ def ease_out_bounce(t: float) -> float:if t < 1 / d1:return n1 * t * telif t < 2 / d1:- return n1 * (t - 1.5 / d1) * t + 0.75+ return n1 * (t - 1.5 / d1) * (t - 1.5 / d1) + 0.75elif t < 2.5 / d1:- return n1 * (t - 2.25 / d1) * t + 0.9375+ return n1 * (t - 2.25 / d1) * (t - 2.25 / d1) + 0.9375else:- return n1 * (t - 2.625 / d1) * t + 0.984375+ return n1 * (t - 2.625 / d1) * (t - 2.625 / d1) + 0.984375+@unit_intervaldef ease_in_out_bounce(t: float) -> float:- c1 = 1.70158- c2 = c1 * 1.525- return (- (pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2)) / 2- if t < 0.5- else (pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2- )+ if t < 0.5:+ return (1 - ease_out_bounce(1 - 2 * t)) / 2+ else:+ return (1 + ease_out_bounce(2 * t - 1)) / 2