Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add animation RemoveTextLetterByLetter (#2883)

Sparsh Goenka3ad15bd58c3e
diff --git a/manim/animation/creation.py b/manim/animation/creation.py
index 85bb78ccb..2d0cab632 100644
--- a/manim/animation/creation.py
+++ b/manim/animation/creation.py
@@ -67,6 +67,7 @@ __all__ = [
"ShowIncreasingSubsets",
"SpiralIn",
"AddTextLetterByLetter",
+ "RemoveTextLetterByLetter",
"ShowSubmobjectsOneByOne",
"AddTextWordByWord",
]
@@ -84,6 +85,7 @@ if TYPE_CHECKING:
from manim.mobject.opengl.opengl_surface import OpenGLSurface
from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject
+from .. import config
from ..animation.animation import Animation
from ..animation.composition import Succession
from ..constants import TAU
@@ -495,6 +497,7 @@ class ShowIncreasingSubsets(Animation):
group: Mobject,
suspend_mobject_updating: bool = False,
int_func: Callable[[np.ndarray], np.ndarray] = np.floor,
+ reverse_rate_function=False,
**kwargs,
) -> None:
self.all_submobs = list(group.submobjects)
@@ -502,17 +505,27 @@ class ShowIncreasingSubsets(Animation):
for mobj in self.all_submobs:
mobj.set_opacity(0)
super().__init__(
- group, suspend_mobject_updating=suspend_mobject_updating, **kwargs
+ group,
+ suspend_mobject_updating=suspend_mobject_updating,
+ reverse_rate_function=reverse_rate_function,
+ **kwargs,
)
def interpolate_mobject(self, alpha: float) -> None:
n_submobs = len(self.all_submobs)
- index = int(self.int_func(self.rate_func(alpha) * n_submobs))
+ value = (
+ 1 - self.rate_func(alpha)
+ if self.reverse_rate_function
+ else self.rate_func(alpha)
+ )
+ index = int(self.int_func(value * n_submobs))
self.update_submobject_list(index)
def update_submobject_list(self, index: int) -> None:
for mobj in self.all_submobs[:index]:
mobj.set_opacity(1)
+ for mobj in self.all_submobs[index:]:
+ mobj.set_opacity(0)
class AddTextLetterByLetter(ShowIncreasingSubsets):
@@ -537,19 +550,64 @@ class AddTextLetterByLetter(ShowIncreasingSubsets):
rate_func: Callable[[float], float] = linear,
time_per_char: float = 0.1,
run_time: float | None = None,
+ reverse_rate_function=False,
+ introducer=True,
**kwargs,
) -> None:
- # time_per_char must be above 0.06, or the animation won't finish
self.time_per_char = time_per_char
if run_time is None:
- run_time = np.max((0.06, self.time_per_char)) * len(text)
+ # minimum time per character is 1/frame_rate, otherwise
+ # the animation does not finish.
+ run_time = np.max((1 / config.frame_rate, self.time_per_char)) * len(text)
+ super().__init__(
+ text,
+ suspend_mobject_updating=suspend_mobject_updating,
+ int_func=int_func,
+ rate_func=rate_func,
+ run_time=run_time,
+ reverse_rate_function=reverse_rate_function,
+ introducer=introducer,
+ **kwargs,
+ )
+
+class RemoveTextLetterByLetter(AddTextLetterByLetter):
+ """Remove a :class:`~.Text` letter by letter from the scene.
+
+ Parameters
+ ----------
+ time_per_char : :class:`float`
+ Frequency of appearance of the letters.
+
+ .. tip::
+
+ This is currently only possible for class:`~.Text` and not for class:`~.MathTex`
+
+ """
+
+ def __init__(
+ self,
+ text: Text,
+ suspend_mobject_updating: bool = False,
+ int_func: Callable[[np.ndarray], np.ndarray] = np.ceil,
+ rate_func: Callable[[float], float] = linear,
+ time_per_char: float = 0.1,
+ run_time: float | None = None,
+ reverse_rate_function=True,
+ introducer=False,
+ remover=True,
+ **kwargs,
+ ) -> None:
super().__init__(
text,
suspend_mobject_updating=suspend_mobject_updating,
int_func=int_func,
rate_func=rate_func,
+ time_per_char=time_per_char,
run_time=run_time,
+ reverse_rate_function=reverse_rate_function,
+ introducer=introducer,
+ remover=remover,
**kwargs,
)