3b1b-manim/manimlib/animation/update.py

67 lines
1.9 KiB
Python
Raw Normal View History

from __future__ import annotations
from manimlib.animation.animation import Animation
from typing import TYPE_CHECKING
if TYPE_CHECKING:
2022-04-12 19:19:59 +08:00
from typing import Callable
from manimlib.mobject.mobject import Mobject
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,
2022-12-14 16:02:15 -08:00
update_function: Callable[[Mobject], Mobject | None],
suspend_mobject_updating: bool = False,
**kwargs
):
2019-02-10 10:43:45 -08:00
self.update_function = update_function
2022-12-14 16:02:15 -08:00
super().__init__(
mobject,
suspend_mobject_updating=suspend_mobject_updating,
**kwargs
)
def interpolate_mobject(self, alpha: float) -> None:
self.update_function(self.mobject)
2022-12-14 16:02:15 -08:00
class UpdateFromAlphaFunc(Animation):
def __init__(
self,
mobject: Mobject,
update_function: Callable[[Mobject, float], Mobject | None],
suspend_mobject_updating: bool = False,
**kwargs
):
self.update_function = update_function
super().__init__(mobject, suspend_mobject_updating=suspend_mobject_updating, **kwargs)
def interpolate_mobject(self, alpha: float) -> None:
self.update_function(self.mobject, alpha)
class MaintainPositionRelativeTo(Animation):
def __init__(
self,
mobject: Mobject,
tracked_mobject: Mobject,
**kwargs
):
2019-02-10 10:43:45 -08:00
self.tracked_mobject = tracked_mobject
2022-04-12 19:19:59 +08:00
self.diff = mobject.get_center() - tracked_mobject.get_center()
2019-02-10 10:43:45 -08:00
super().__init__(mobject, **kwargs)
def interpolate_mobject(self, alpha: float) -> None:
2019-02-10 10:43:45 -08:00
target = self.tracked_mobject.get_center()
location = self.mobject.get_center()
self.mobject.shift(target - location + self.diff)