Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions manim/animation/updaters/mobject_update_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


import inspect
from collections.abc import Callable

import numpy as np

Expand Down Expand Up @@ -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


Expand Down