diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py index c77174c89c..698dc03d56 100644 --- a/manim/animation/updaters/mobject_update_utils.py +++ b/manim/animation/updaters/mobject_update_utils.py @@ -15,6 +15,7 @@ import inspect +from collections.abc import Callable import numpy as np @@ -55,9 +56,46 @@ def updater(mob): return mobject -def always_redraw(func): +def always_redraw(func: Callable[[], Mobject]) -> Mobject: + """Redraw the mobject constructed by a function every frame. + + This function returns a mobject with an attached updater that + continuously regenerates the mobject according to the + specified function. + + Parameters + ---------- + func + A function without (required) input arguments that returns + a mobject. + + Examples + -------- + + .. manim:: TangentAnimation + + class TangentAnimation(Scene): + def construct(self): + ax = Axes() + sine = ax.plot(np.sin, color=RED) + alpha = ValueTracker(0) + point = always_redraw( + lambda: Dot( + sine.point_from_proportion(alpha.get_value()), + color=BLUE) + ) + tangent = always_redraw( + lambda: TangentLine( + sine, + alpha=alpha.get_value(), + color=YELLOW, + length=4) + ) + self.add(ax, sine, point, tangent) + self.play(alpha.animate.set_value(1), rate_func=linear, run_time=2) + """ mob = func() - mob.add_updater(lambda m: mob.become(func())) + mob.add_updater(lambda _: mob.become(func())) return mob