From b8b44f062c99600f3ccb767553a5e1036e156b2b Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Sun, 3 Jan 2021 16:53:49 -0800 Subject: [PATCH] Added FadeTransform and FadeTransformPieces --- manimlib/animation/fading.py | 53 +++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/manimlib/animation/fading.py b/manimlib/animation/fading.py index 6763b7f4..e8a9054d 100644 --- a/manimlib/animation/fading.py +++ b/manimlib/animation/fading.py @@ -1,5 +1,6 @@ from manimlib.animation.animation import Animation from manimlib.animation.transform import Transform +from manimlib.mobject.mobject import Group from manimlib.constants import ORIGIN from manimlib.utils.bezier import interpolate from manimlib.utils.rate_functions import there_and_back @@ -55,7 +56,57 @@ class FadeInFromPoint(FadeIn): super().__init__(mobject, shift=mobject.get_center() - point, **kwargs) -# Below will be deprecated +class FadeTransform(Transform): + CONFIG = { + "stretch": True, + "dim_to_match": 1, + } + + def __init__(self, mobject, target_mobject, **kwargs): + self.to_add_on_completion = target_mobject + super().__init__( + Group(mobject, target_mobject.copy()), + **kwargs + ) + + def begin(self): + self.ending_mobject = self.mobject.copy() + Animation.begin(self) + # Both 'start' and 'end' consists of the source and target mobjects. + # At the start, the traget should be faded replacing the source, + # and at the end it should be the other way around. + start, end = self.starting_mobject, self.ending_mobject + for m0, m1 in ((start[1], start[0]), (end[0], end[1])): + self.ghost_to(m0, m1) + + def ghost_to(self, source, target): + source.replace(target, stretch=self.stretch, dim_to_match=self.dim_to_match) + source.set_opacity(0) + + def get_all_mobjects(self): + return [ + self.mobject, + self.starting_mobject, + self.ending_mobject, + ] + + def get_all_families_zipped(self): + return Animation.get_all_families_zipped(self) + + def clean_up_from_scene(self, scene): + Animation.clean_up_from_scene(self, scene) + scene.remove(self.mobject) + scene.add(self.to_add_on_completion) + + +class FadeTransformPieces(FadeTransform): + def begin(self): + self.mobject[0].align_data(self.mobject[1]) + super().begin() + + def ghost_to(self, source, target): + for sm0, sm1 in zip(source.get_family(), target.get_family()): + super().ghost_to(sm0, sm1) class VFadeIn(Animation):