Skip to content

Commit 59ff327

Browse files
JasonGrace2282pre-commit-ci[bot]jsonvillanuevabehackl
authored
Added Docstring/Example for :meth:always_redraw (#3312)
* Added docstring & example for always_redraw * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * made more descriptive names for vars in example * Change runtime 10s->3s Co-authored-by: Jason Villanueva <[email protected]> * change Create -> add Co-authored-by: Benjamin Hackl <[email protected]> * Added typehint for always_redraw * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Changed description of :meth:`always_redraw` Co-authored-by: Benjamin Hackl <[email protected]> * Added return type to :meth:`always_redraw` Co-authored-by: Benjamin Hackl <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jason Villanueva <[email protected]> Co-authored-by: Benjamin Hackl <[email protected]>
1 parent 9561896 commit 59ff327

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

manim/animation/updaters/mobject_update_utils.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616

1717
import inspect
18+
from collections.abc import Callable
1819

1920
import numpy as np
2021

@@ -55,9 +56,46 @@ def updater(mob):
5556
return mobject
5657

5758

58-
def always_redraw(func):
59+
def always_redraw(func: Callable[[], Mobject]) -> Mobject:
60+
"""Redraw the mobject constructed by a function every frame.
61+
62+
This function returns a mobject with an attached updater that
63+
continuously regenerates the mobject according to the
64+
specified function.
65+
66+
Parameters
67+
----------
68+
func
69+
A function without (required) input arguments that returns
70+
a mobject.
71+
72+
Examples
73+
--------
74+
75+
.. manim:: TangentAnimation
76+
77+
class TangentAnimation(Scene):
78+
def construct(self):
79+
ax = Axes()
80+
sine = ax.plot(np.sin, color=RED)
81+
alpha = ValueTracker(0)
82+
point = always_redraw(
83+
lambda: Dot(
84+
sine.point_from_proportion(alpha.get_value()),
85+
color=BLUE)
86+
)
87+
tangent = always_redraw(
88+
lambda: TangentLine(
89+
sine,
90+
alpha=alpha.get_value(),
91+
color=YELLOW,
92+
length=4)
93+
)
94+
self.add(ax, sine, point, tangent)
95+
self.play(alpha.animate.set_value(1), rate_func=linear, run_time=2)
96+
"""
5997
mob = func()
60-
mob.add_updater(lambda m: mob.become(func()))
98+
mob.add_updater(lambda _: mob.become(func()))
6199
return mob
62100

63101

0 commit comments

Comments
 (0)