Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added Docstring/Example for :meth:`always_redraw` (#3312)

Jason Grace59ff3271a5db
diff --git a/manim/animation/updaters/mobject_update_utils.py b/manim/animation/updaters/mobject_update_utils.py
index c77174c89..698dc03d5 100644
--- a/manim/animation/updaters/mobject_update_utils.py
+++ b/manim/animation/updaters/mobject_update_utils.py
@@ -15,6 +15,7 @@ __all__ = [
import inspect
+from collections.abc import Callable
import numpy as np
@@ -55,9 +56,46 @@ def f_always(method, *arg_generators, **kwargs):
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