Source code for manim.animation.updaters.update
"""Animations that update mobjects."""
from __future__ import annotations
__all__ = ["UpdateFromFunc", "UpdateFromAlphaFunc", "MaintainPositionRelativeTo"]
import operator as op
import typing
from manim.animation.animation import Animation
if typing.TYPE_CHECKING:
from manim.mobject.mobject import Mobject
[docs]class UpdateFromFunc(Animation):
"""
update_function of the form func(mobject), presumably
to be used when the state of one mobject is dependent
on another simultaneously animated mobject
"""
def __init__(
self,
mobject: Mobject,
update_function: typing.Callable[[Mobject], typing.Any],
suspend_mobject_updating: bool = False,
**kwargs,
) -> None:
self.update_function = update_function
super().__init__(
mobject, suspend_mobject_updating=suspend_mobject_updating, **kwargs
)
[docs]class UpdateFromAlphaFunc(UpdateFromFunc):
[docs] def interpolate_mobject(self, alpha: float) -> None:
self.update_function(self.mobject, self.rate_func(alpha))
[docs]class MaintainPositionRelativeTo(Animation):
def __init__(self, mobject: Mobject, tracked_mobject: Mobject, **kwargs) -> None:
self.tracked_mobject = tracked_mobject
self.diff = op.sub(
mobject.get_center(),
tracked_mobject.get_center(),
)
super().__init__(mobject, **kwargs)
[docs] def interpolate_mobject(self, alpha: float) -> None:
target = self.tracked_mobject.get_center()
location = self.mobject.get_center()
self.mobject.shift(target - location + self.diff)