Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fading module enhancements (#1454)

Anton Ballmaier15261ea65b4f
diff --git a/docs/source/examples.rst b/docs/source/examples.rst
index 742eb5ffa..ea198961c 100644
--- a/docs/source/examples.rst
+++ b/docs/source/examples.rst
@@ -450,12 +450,12 @@ Special Camera Settings
frame_text.next_to(frame, DOWN)
- self.play(Create(frame), FadeInFrom(frame_text, direction=DOWN))
+ self.play(Create(frame), FadeIn(frame_text, shift=UP))
self.activate_zooming()
self.play(self.get_zoomed_display_pop_out_animation(), unfold_camera)
zoomed_camera_text.next_to(zoomed_display_frame, DOWN)
- self.play(FadeInFrom(zoomed_camera_text, direction=DOWN))
+ self.play(FadeIn(zoomed_camera_text, shift=UP))
# Scale in x y z
scale_factor = [0.5, 1.5, 0]
self.play(
@@ -576,7 +576,7 @@ Advanced Projects
=================
.. manim:: OpeningManim
- :ref_classes: Tex MathTex Write FadeInFrom LaggedStart NumberPlane Create
+ :ref_classes: Tex MathTex Write FadeIn LaggedStart NumberPlane Create
:ref_methods: NumberPlane.prepare_for_nonlinear_transform
class OpeningManim(Scene):
@@ -586,7 +586,7 @@ Advanced Projects
VGroup(title, basel).arrange(DOWN)
self.play(
Write(title),
- FadeInFrom(basel, UP),
+ FadeIn(basel, shift=DOWN),
)
self.wait()
@@ -594,7 +594,7 @@ Advanced Projects
transform_title.to_corner(UP + LEFT)
self.play(
Transform(title, transform_title),
- LaggedStart(*[FadeOutAndShift(obj, direction=DOWN) for obj in basel]),
+ LaggedStart(*[FadeOut(obj, shift=DOWN) for obj in basel]),
)
self.wait()
@@ -606,7 +606,7 @@ Advanced Projects
self.add(grid, grid_title) # Make sure title is on top of grid
self.play(
FadeOut(title),
- FadeInFrom(grid_title, direction=DOWN),
+ FadeIn(grid_title, shift=UP),
Create(grid, run_time=3, lag_ratio=0.1),
)
self.wait()
diff --git a/example_scenes/basic.py b/example_scenes/basic.py
index 7745402a1..becc2bc86 100644
--- a/example_scenes/basic.py
+++ b/example_scenes/basic.py
@@ -21,7 +21,7 @@ class OpeningManim(Scene):
VGroup(title, basel).arrange(DOWN)
self.play(
Write(title),
- FadeInFrom(basel, UP),
+ FadeIn(basel, shift=DOWN),
)
self.wait()
@@ -29,7 +29,7 @@ class OpeningManim(Scene):
transform_title.to_corner(UP + LEFT)
self.play(
Transform(title, transform_title),
- LaggedStart(*[FadeOutAndShift(obj, direction=DOWN) for obj in basel]),
+ LaggedStart(*[FadeOut(obj, shift=DOWN) for obj in basel]),
)
self.wait()
@@ -41,7 +41,7 @@ class OpeningManim(Scene):
self.add(grid, grid_title) # Make sure title is on top of grid
self.play(
FadeOut(title),
- FadeInFrom(grid_title, direction=DOWN),
+ FadeIn(grid_title, shift=UP),
Create(grid, run_time=3, lag_ratio=0.1),
)
self.wait()
diff --git a/manim/animation/fading.py b/manim/animation/fading.py
index f38d5cdba..37c5bc975 100644
--- a/manim/animation/fading.py
+++ b/manim/animation/fading.py
@@ -1,54 +1,14 @@
"""Fading in and out of view.
-.. manim:: Example
- :hide_source:
+.. manim:: Fading
- class Example(Scene):
+ class Fading(Scene):
def construct(self):
- s1 = Square().set_color(BLUE)
- s2 = Square().set_color(BLUE)
- s3 = Square().set_color(BLUE)
- s4 = Square().set_color(BLUE)
- s5 = Square().set_color(BLUE)
- s6 = Square().set_color(RED)
- s7 = Square().set_color(RED)
- s8 = Square().set_color(RED)
- VGroup(s1, s2, s3, s4).set_x(0).arrange(buff=1.9).shift(UP)
- VGroup(s5, s6, s7, s8).set_x(0).arrange(buff=1.9).shift(2 * DOWN)
- t1 = Text("FadeIn").scale(0.5).next_to(s1, UP)
- t2 = Text("FadeInFromPoint").scale(0.5).next_to(s2, UP)
- t3 = Text("FadeInFrom").scale(0.5).next_to(s3, UP)
- t4 = Text("VFadeIn").scale(0.5).next_to(s4, UP)
- t5 = Text("FadeInFromLarge").scale(0.4).next_to(s5, UP)
- t6 = Text("FadeOut").scale(0.45).next_to(s6, UP)
- t7 = Text("FadeOutAndShift").scale(0.45).next_to(s7, UP)
- t8 = Text("VFadeOut").scale(0.45).next_to(s8, UP)
-
- objs = [ManimBanner().scale(0.25) for _ in range(1, 9)]
- objs[0].move_to(s1.get_center())
- objs[1].move_to(s2.get_center())
- objs[2].move_to(s3.get_center())
- objs[3].move_to(s4.get_center())
- objs[4].move_to(s5.get_center())
- objs[5].move_to(s6.get_center())
- objs[6].move_to(s7.get_center())
- objs[7].move_to(s8.get_center())
- self.add(s1, s2, s3, s4, s5, s6, s7, s8, t1, t2, t3, t4, t5, t6, t7, t8)
- self.add(*objs)
-
- self.play(
- FadeIn(objs[0]),
- FadeInFromPoint(objs[1], s2.get_center()),
- FadeInFrom(objs[2], LEFT*0.2),
- VFadeIn(objs[3]),
- FadeInFromLarge(objs[4]),
- )
- self.wait(0.3)
- self.play(
- FadeOut(objs[5]),
- FadeOutAndShift(objs[6], DOWN),
- VFadeOut(objs[7])
- )
+ tex_in = Tex("Fade", "In").scale(3)
+ tex_out = Tex("Fade", "Out").scale(3)
+ self.play(FadeIn(tex_in, shift=DOWN, scale=0.66))
+ self.play(ReplacementTransform(tex_in, tex_out))
+ self.play(FadeOut(tex_out, shift=DOWN * 2, scale=1.5))
"""
@@ -66,191 +26,253 @@ __all__ = [
"VFadeInThenOut",
]
-import typing
+from typing import Callable, Optional, Union
import numpy as np
-from .. import logger
-from ..animation.animation import Animation
from ..animation.transform import Transform
from ..constants import DOWN, ORIGIN
-from ..mobject.types.opengl_vectorized_mobject import OpenGLVMobject
-from ..mobject.types.vectorized_mobject import VMobject
-from ..utils.bezier import interpolate
+from ..mobject.mobject import Mobject
+from ..scene.scene import Scene
+from ..utils.deprecation import deprecated
from ..utils.rate_functions import there_and_back
-if typing.TYPE_CHECKING:
- from ..mobject.mobject import Mobject
- from ..scene.scene import Scene
-DEFAULT_FADE_LAG_RATIO: float = 0
+class _Fade(Transform):
+ """Fade in a :class:`~.Mobject` in or out.
+ Parameters
+ ----------
+ mobject
+ The mobject to be faded in.
+ shift
+ The vector by which the mobject shifts while beeing faded.
+ target_position
+ The position to/from which the mobject moves while beeing faded in. In case
+ another mobject is given as target position, its center is used.
+ scale
+ The factor by which the mobject is scaled initially before beeing rescaling to
+ its original size while beeing faded in.
-class FadeOut(Transform):
- """A transform fading out the given mobject."""
-
- def __init__(
- self,
- vmobject: typing.Union[VMobject, OpenGLVMobject],
- remover: bool = True,
- lag_ratio: float = DEFAULT_FADE_LAG_RATIO,
- **kwargs
- ) -> None:
- super().__init__(vmobject, remover=remover, lag_ratio=lag_ratio, **kwargs)
-
- def create_target(self) -> "Mobject":
- return self.mobject.copy().fade(1)
-
- def clean_up_from_scene(self, scene: "Scene" = None) -> None:
- super().clean_up_from_scene(scene)
- self.interpolate(0)
-
+ """
-class FadeIn(Transform):
def __init__(
self,
- vmobject: typing.Union[VMobject, OpenGLVMobject],
- lag_ratio: float = DEFAULT_FADE_LAG_RATIO,
+ mobject: Mobject,
+ shift: Optional[np.ndarray] = None,
+ target_position: Optional[Union[np.ndarray, Mobject]] = None,
+ scale: float = 1,
**kwargs
) -> None:
- super().__init__(vmobject, lag_ratio=lag_ratio, **kwargs)
+ self.point_target = False
+ if shift is None:
+ if target_position is not None:
+ if isinstance(target_position, Mobject):
+ target_position = target_position.get_center()
+ shift = target_position - mobject.get_center()
+ self.point_target = True
+ else:
+ shift = ORIGIN
+ self.shift_vector = shift
+ self.scale_factor = scale
+ super().__init__(mobject, **kwargs)
- def create_target(self) -> "Mobject":
- return self.mobject
+ def _create_faded_mobject(self, fadeIn: bool) -> Mobject:
+ """Create a faded, shifted and scaled copy of the mobject.
- def create_starting_mobject(self) -> "Mobject":
- start = super().create_starting_mobject()
- start.fade(1)
- if isinstance(start, (VMobject, OpenGLVMobject)):
- start.set_stroke(opacity=0)
- start.set_fill(opacity=0)
- return start
+ Parameters
+ ----------
+ fadeIn
+ Whether the faded mobject is used to fade in.
+ Returns
+ -------
+ Mobject
+ The faded, shifted and scaled copy of the mobject.
+ """
+ faded_mobject = self.mobject.copy()
+ faded_mobject.set_opacity(0)
+ direction_modifier = -1 if fadeIn and not self.point_target else 1
+ faded_mobject.shift(self.shift_vector * direction_modifier)
+ faded_mobject.scale(self.scale_factor)
+ return faded_mobject
-class FadeInFrom(Transform):
- def __init__(
- self, mobject: "Mobject", direction: np.ndarray = DOWN, **kwargs
- ) -> None:
- self.direction = direction
- super().__init__(mobject, **kwargs)
- def create_target(self) -> "Mobject":
- return self.mobject.copy()
+class FadeIn(_Fade):
+ """Fade in a :class:`~.Mobject`.
- def begin(self) -> None:
- super().begin()
- self.starting_mobject.shift(self.direction)
- self.starting_mobject.fade(1)
+ Parameters
+ ----------
+ mobject
+ The mobject to be faded in.
+ shift
+ The vector by which the mobject shifts while beeing faded in.
+ target_position
+ The position from which the mobject starts while beeing faded in. In case
+ another mobject is given as target position, its center is used.
+ scale
+ The factor by which the mobject is scaled initially before beeing rescaling to
+ its original size while beeing faded in.
+
+ Examples
+ --------
+
+ .. manim :: FadeInExample
+
+ class FadeInExample(Scene):
+ def construct(self):
+ dot = Dot(UP * 2 + LEFT)
+ self.add(dot)
+ tex = Tex(
+ "FadeIn with ", "shift ", " or target\\_position", " and scale"
+ ).scale(1)
+ animations = [
+ FadeIn(tex[0]),
+ FadeIn(tex[1], shift=DOWN),
+ FadeIn(tex[2], target_position=dot),
+ FadeIn(tex[3], scale=1.5),
+ ]
+ self.play(AnimationGroup(*animations, lag_ratio=0.5))
+ """
-class FadeOutAndShift(FadeOut):
- def __init__(
- self, mobject: "Mobject", direction: np.ndarray = DOWN, **kwargs
- ) -> None:
- self.direction = direction
- super().__init__(mobject, **kwargs)
+ def create_target(self):
+ return self.mobject
- def create_target(self) -> "Mobject":
- target = super().create_target()
- target.shift(self.direction)
- return target
+ def create_starting_mobject(self):
+ return self._create_faded_mobject(fadeIn=True)
-class FadeOutToPoint(FadeOut):
- """Fades out a mobject while moving it to a specified point.
+class FadeOut(_Fade):
+ """Fade out a :class:`~.Mobject`.
Parameters
----------
mobject
- The :class:`~.Mobject` to be animated.
- point
- Either a point or another :class:`~.Mobject` to whose center the
- Mobject is moved in the animation. Defaults to ``ORIGIN``.
- kwargs
- Further keyword arguments are passed to the parent class.
+ The mobject to be faded out.
+ shift
+ The vector by which the mobject shifts while beeing faded out.
+ target_position
+ The position to which the mobject moves while beeing faded out. In case another
+ mobject is given as target position, its center is used.
+ scale
+ The factor by which the mobject is scaled while beeing faded out.
+
+ Examples
+ --------
+
+ .. manim :: FadeInExample
+
+ class FadeInExample(Scene):
+ def construct(self):
+ dot = Dot(UP * 2 + LEFT)
+ self.add(dot)
+ tex = Tex(
+ "FadeOut with ", "shift ", " or target\\_position", " and scale"
+ ).scale(1)
+ animations = [
+ FadeOut(tex[0]),
+ FadeOut(tex[1], shift=DOWN),
+ FadeOut(tex[2], target_position=dot),
+ FadeOut(tex[3], scale=0.5),
+ ]
+ self.play(AnimationGroup(*animations, lag_ratio=0.5))
+
"""
- def __init__(
- self,
- mobject: "Mobject",
- point: typing.Union["Mobject", np.ndarray] = ORIGIN,
- **kwargs
- ) -> None:
- self.point = point
- super().__init__(mobject, **kwargs)
+ def __init__(self, mobject: Mobject, **kwargs) -> None:
+ super().__init__(mobject, remover=True, **kwargs)
- def create_target(self) -> "Mobject":
- """Creates the target of the animation."""
- target = super().create_target()
- target.move_to(self.point)
- return target
+ def create_target(self):
+ return self._create_faded_mobject(fadeIn=False)
+ def clean_up_from_scene(self, scene: Scene = None) -> None:
+ super().clean_up_from_scene(scene)
+ self.interpolate(0)
-class FadeInFromPoint(FadeIn):
+
+@deprecated(
+ since="v0.6.0",
+ until="v0.8.0",
+ replacement="FadeIn",
+ message="You can set a shift amount there.",
+)
+class FadeInFrom(FadeIn):
def __init__(
- self, mobject: "Mobject", point: typing.Union["Mobject", np.ndarray], **kwargs
+ self, mobject: "Mobject", direction: np.ndarray = DOWN, **kwargs
) -> None:
- self.point = point
- super().__init__(mobject, **kwargs)
-
- def create_starting_mobject(self) -> "Mobject":
- start = super().create_starting_mobject()
- start.scale(0)
- start.move_to(self.point)
- return start
+ super().__init__(mobject, shift=-direction, **kwargs)
-class FadeInFromLarge(FadeIn):
- def __init__(self, mobject: "Mobject", scale_factor: float = 2, **kwargs) -> None:
- self.scale_factor = scale_factor
- super().__init__(mobject, **kwargs)
+@deprecated(
+ since="v0.6.0",
+ until="v0.8.0",
+ replacement="FadeOut",
+ message="You can set a shift amount there.",
+)
+class FadeOutAndShift(FadeIn):
+ def __init__(
+ self, mobject: "Mobject", direction: np.ndarray = DOWN, **kwargs
+ ) -> None:
+ super().__init__(mobject, shift=direction, **kwargs)
- def create_starting_mobject(self) -> "Mobject":
- start = super().create_starting_mobject()
- start.scale(self.scale_factor)
- return start
+@deprecated(
+ since="v0.6.0",
+ until="v0.8.0",
+ replacement="FadeOut",
+ message="You can set a target position there.",
+)
+class FadeOutToPoint(FadeOut):
+ def __init__(
+ self, mobject: "Mobject", point: Union["Mobject", np.ndarray] = ORIGIN, **kwargs
+ ) -> None:
+ super().__init__(mobject, target_position=point, **kwargs)
-class VFadeIn(Animation):
- """
- VFadeIn and VFadeOut only work for VMobjects,
- """
+@deprecated(
+ since="v0.6.0",
+ until="v0.8.0",
+ replacement="FadeIn",
+ message="You can set a target position and scaling factor there.",
+)
+class FadeInFromPoint(FadeIn):
def __init__(
- self, mobject: "Mobject", suspend_mobject_updating: bool = False, **kwargs
+ self, mobject: "Mobject", point: Union["Mobject", np.ndarray], **kwargs
) -> None:
- super().__init__(
- mobject, suspend_mobject_updating=suspend_mobject_updating, **kwargs
- )
+ super().__init__(mobject, target_position=point, scale=0, **kwargs)
- def interpolate_submobject(
- self, submobject: "Mobject", starting_submobject: "Mobject", alpha: float
- ) -> None:
- submobject.set_stroke(
- opacity=interpolate(0, starting_submobject.get_stroke_opacity(), alpha)
- )
- submobject.set_fill(
- opacity=interpolate(0, starting_submobject.get_fill_opacity(), alpha)
- )
+@deprecated(
+ since="v0.6.0",
+ until="v0.8.0",
+ replacement="FadeIn",
+ message="You can set a scaling factor there.",
+)
+class FadeInFromLarge(FadeIn):
+ def __init__(self, mobject: "Mobject", scale_factor: float = 2, **kwargs) -> None:
+ super().__init__(mobject, scale=scale_factor, **kwargs)
-class VFadeOut(VFadeIn):
- def __init__(self, mobject: "Mobject", remover: bool = True, **kwargs) -> None:
- super().__init__(mobject, remover=remover, **kwargs)
- def interpolate_submobject(
- self, submobject: "Mobject", starting_submobject: "Mobject", alpha: float
- ) -> None:
- super().interpolate_submobject(submobject, starting_submobject, 1 - alpha)
+@deprecated(since="v0.6.0", until="v0.8.0", replacement="FadeIn")
+class VFadeIn(FadeIn):
+ def __init__(self, mobject: "Mobject", **kwargs) -> None:
+ super().__init__(mobject, **kwargs)
+
+
+@deprecated(since="v0.6.0", until="v0.8.0", replacement="FadeOut")
+class VFadeOut(FadeOut):
+ def __init__(self, mobject: "Mobject", **kwargs) -> None:
+ super().__init__(mobject, **kwargs)
-class VFadeInThenOut(VFadeIn):
+@deprecated(since="v0.6.0", until="v0.8.0")
+class VFadeInThenOut(FadeIn):
def __init__(
self,
mobject: "Mobject",
- remover: bool = True,
- rate_func: typing.Callable[[float, float], float] = there_and_back,
+ rate_func: Callable[[float], float] = there_and_back,
**kwargs
):
- super().__init__(mobject, remover=remover, rate_func=rate_func, **kwargs)
+ super().__init__(mobject, remover=True, rate_func=rate_func, **kwargs)
diff --git a/manim/animation/indication.py b/manim/animation/indication.py
index 655c3ebf6..395824b36 100644
--- a/manim/animation/indication.py
+++ b/manim/animation/indication.py
@@ -19,8 +19,8 @@ Examples
else:
self.play(indications[i](names[i]))
self.play(AnimationGroup(
- FadeOutAndShift(names[i], UP*1.5),
- FadeInFrom(names[(i+1)%len(names)], DOWN*1.5),
+ FadeOut(names[i], shift=UP*1.5),
+ FadeIn(names[(i+1)%len(names)], shift=UP*1.5),
))
"""
diff --git a/manim/animation/transform_matching_parts.py b/manim/animation/transform_matching_parts.py
index 2de9dcb5a..96d04d1b7 100644
--- a/manim/animation/transform_matching_parts.py
+++ b/manim/animation/transform_matching_parts.py
@@ -14,7 +14,7 @@ from ..mobject.svg.tex_mobject import MathTex
from ..mobject.types.opengl_vectorized_mobject import OpenGLVGroup, OpenGLVMobject
from ..mobject.types.vectorized_mobject import VGroup, VMobject
from .composition import AnimationGroup
-from .fading import FadeInFromPoint, FadeOutToPoint
+from .fading import FadeIn, FadeOut
from .transform import FadeTransformPieces, Transform
@@ -125,11 +125,9 @@ class TransformMatchingAbstractBase(AnimationGroup):
if fade_transform_mismatches:
anims.append(FadeTransformPieces(fade_source, fade_target, **kwargs))
else:
+ anims.append(FadeOut(fade_source, target_position=fade_target, **kwargs))
anims.append(
- FadeOutToPoint(fade_source, fade_target.get_center(), **kwargs)
- )
- anims.append(
- FadeInFromPoint(fade_target.copy(), fade_source.get_center(), **kwargs)
+ FadeIn(fade_target.copy(), target_position=fade_target, **kwargs)
)
super().__init__(*anims)
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index 4b1d87ee7..9c88ba694 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -1619,7 +1619,7 @@ class VDict(VMobject):
self.play(FadeOut(my_dict["c"]))
self.wait()
- self.play(FadeOutAndShift(my_dict["r"], DOWN))
+ self.play(FadeOut(my_dict["r"], shift=DOWN))
self.wait()
# you can also make a VDict from an existing dict of mobjects
diff --git a/tests/test_composition.py b/tests/test_composition.py
index 900bc0ac2..0d804f1ec 100644
--- a/tests/test_composition.py
+++ b/tests/test_composition.py
@@ -1,15 +1,15 @@
from manim.animation.animation import Animation, Wait
from manim.animation.composition import AnimationGroup, Succession
-from manim.animation.fading import FadeIn, FadeInFrom, FadeOutAndShift
-from manim.constants import DOWN
+from manim.animation.fading import FadeIn, FadeOut
+from manim.constants import DOWN, UP
from manim.mobject.geometry import Circle, Line, Square
def test_succession_timing():
"""Test timing of animations in a succession."""
line = Line()
- animation_1s = FadeInFrom(line, direction=DOWN, run_time=1.0)
- animation_4s = FadeOutAndShift(line, direction=DOWN, run_time=4.0)
+ animation_1s = FadeIn(line, shift=UP, run_time=1.0)
+ animation_4s = FadeOut(line, shift=DOWN, run_time=4.0)
succession = Succession(animation_1s, animation_4s)
assert succession.get_run_time() == 5.0
succession.begin()
@@ -33,13 +33,13 @@ def test_succession_timing():
def test_succession_in_succession_timing():
"""Test timing of nested successions."""
line = Line()
- animation_1s = FadeInFrom(line, direction=DOWN, run_time=1.0)
- animation_4s = FadeOutAndShift(line, direction=DOWN, run_time=4.0)
+ animation_1s = FadeIn(line, shift=UP, run_time=1.0)
+ animation_4s = FadeOut(line, shift=DOWN, run_time=4.0)
nested_succession = Succession(animation_1s, animation_4s)
succession = Succession(
- FadeInFrom(line, direction=DOWN, run_time=4.0),
+ FadeIn(line, shift=UP, run_time=4.0),
nested_succession,
- FadeInFrom(line, direction=DOWN, run_time=1.0),
+ FadeIn(line, shift=UP, run_time=1.0),
)
assert nested_succession.get_run_time() == 5.0
assert succession.get_run_time() == 10.0
diff --git a/tests/test_graphical_units/test_creation.py b/tests/test_graphical_units/test_creation.py
index eacfe6bc1..75d74073a 100644
--- a/tests/test_graphical_units/test_creation.py
+++ b/tests/test_graphical_units/test_creation.py
@@ -41,24 +41,6 @@ class FadeInTest(Scene):
self.play(FadeIn(square))
-class FadeInFromTest(Scene):
- def construct(self):
- square = Square()
- self.play(FadeInFrom(square, direction=UP))
-
-
-class FadeOutAndShiftTest(Scene):
- def construct(self):
- square = Square()
- self.play(FadeOutAndShift(square, direction=UP))
-
-
-class FadeInFromLargeTest(Scene):
- def construct(self):
- square = Square()
- self.play(FadeInFromLarge(square))
-
-
class GrowFromPointTest(Scene):
def construct(self):
square = Square()