3b1b-manim/manimlib/animation/update.py
2022-02-15 18:39:45 +08:00

58 lines
1.5 KiB
Python

from __future__ import annotations
import operator as op
from typing import Callable
from manimlib.animation.animation import Animation
from typing import TYPE_CHECKING
if TYPE_CHECKING:
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
"""
CONFIG = {
"suspend_mobject_updating": False,
}
def __init__(
self,
mobject: Mobject,
update_function: Callable[[Mobject]],
**kwargs
):
self.update_function = update_function
super().__init__(mobject, **kwargs)
def interpolate_mobject(self, alpha: float) -> None:
self.update_function(self.mobject)
class UpdateFromAlphaFunc(UpdateFromFunc):
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
):
self.tracked_mobject = tracked_mobject
self.diff = op.sub(
mobject.get_center(),
tracked_mobject.get_center(),
)
super().__init__(mobject, **kwargs)
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)