update.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from __future__ import annotations
  2. from manimlib.animation.animation import Animation
  3. from typing import TYPE_CHECKING
  4. if TYPE_CHECKING:
  5. from typing import Callable
  6. from manimlib.mobject.mobject import Mobject
  7. class UpdateFromFunc(Animation):
  8. """
  9. update_function of the form func(mobject), presumably
  10. to be used when the state of one mobject is dependent
  11. on another simultaneously animated mobject
  12. """
  13. def __init__(
  14. self,
  15. mobject: Mobject,
  16. update_function: Callable[[Mobject], Mobject | None],
  17. suspend_mobject_updating: bool = False,
  18. **kwargs
  19. ):
  20. self.update_function = update_function
  21. super().__init__(
  22. mobject,
  23. suspend_mobject_updating=suspend_mobject_updating,
  24. **kwargs
  25. )
  26. def interpolate_mobject(self, alpha: float) -> None:
  27. self.update_function(self.mobject)
  28. class UpdateFromAlphaFunc(Animation):
  29. def __init__(
  30. self,
  31. mobject: Mobject,
  32. update_function: Callable[[Mobject, float], Mobject | None],
  33. suspend_mobject_updating: bool = False,
  34. **kwargs
  35. ):
  36. self.update_function = update_function
  37. super().__init__(mobject, suspend_mobject_updating=suspend_mobject_updating, **kwargs)
  38. def interpolate_mobject(self, alpha: float) -> None:
  39. self.update_function(self.mobject, alpha)
  40. class MaintainPositionRelativeTo(Animation):
  41. def __init__(
  42. self,
  43. mobject: Mobject,
  44. tracked_mobject: Mobject,
  45. **kwargs
  46. ):
  47. self.tracked_mobject = tracked_mobject
  48. self.diff = mobject.get_center() - tracked_mobject.get_center()
  49. super().__init__(mobject, **kwargs)
  50. def interpolate_mobject(self, alpha: float) -> None:
  51. target = self.tracked_mobject.get_center()
  52. location = self.mobject.get_center()
  53. self.mobject.shift(target - location + self.diff)