Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

feat: Add three animations that together simulate a typing animation (#3612)

VPC4a0c7057db6d
diff --git a/manim/animation/creation.py b/manim/animation/creation.py
index 2e15182bc..4e3fe5b98 100644
--- a/manim/animation/creation.py
+++ b/manim/animation/creation.py
@@ -70,6 +70,8 @@ __all__ = [
"RemoveTextLetterByLetter",
"ShowSubmobjectsOneByOne",
"AddTextWordByWord",
+ "TypeWithCursor",
+ "UntypeWithCursor",
]
@@ -80,7 +82,9 @@ import numpy as np
if TYPE_CHECKING:
from manim.mobject.text.text_mobject import Text
+ from manim.scene.scene import Scene
+from manim.constants import RIGHT, TAU
from manim.mobject.opengl.opengl_surface import OpenGLSurface
from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject
from manim.utils.color import ManimColor
@@ -88,7 +92,6 @@ from manim.utils.color import ManimColor
from .. import config
from ..animation.animation import Animation
from ..animation.composition import Succession
-from ..constants import TAU
from ..mobject.mobject import Group, Mobject
from ..mobject.types.vectorized_mobject import VMobject
from ..utils.bezier import integer_interpolate
@@ -674,3 +677,176 @@ class AddTextWordByWord(Succession):
)
)
super().__init__(*anims, **kwargs)
+
+
+class TypeWithCursor(AddTextLetterByLetter):
+ """Similar to :class:`~.AddTextLetterByLetter` , but with an additional cursor mobject at the end.
+
+ Parameters
+ ----------
+ time_per_char
+ Frequency of appearance of the letters.
+ cursor
+ :class:`~.Mobject` shown after the last added letter.
+ buff
+ Controls how far away the cursor is to the right of the last added letter.
+ keep_cursor_y
+ If ``True``, the cursor's y-coordinate is set to the center of the ``Text`` and remains the same throughout the animation. Otherwise, it is set to the center of the last added letter.
+ leave_cursor_on
+ Whether to show the cursor after the animation.
+
+ .. tip::
+ This is currently only possible for class:`~.Text` and not for class:`~.MathTex`.
+
+
+ Examples
+ --------
+
+ .. manim:: InsertingTextExample
+ :ref_classes: Blink
+
+ class InsertingTextExample(Scene):
+ def construct(self):
+ text = Text("Inserting", color=PURPLE).scale(1.5).to_edge(LEFT)
+ cursor = Rectangle(
+ color = GREY_A,
+ fill_color = GREY_A,
+ fill_opacity = 1.0,
+ height = 1.1,
+ width = 0.5,
+ ).move_to(text[0]) # Position the cursor
+
+ self.play(TypeWithCursor(text, cursor))
+ self.play(Blink(cursor, blinks=2))
+
+ """
+
+ def __init__(
+ self,
+ text: Text,
+ cursor: Mobject,
+ buff: float = 0.1,
+ keep_cursor_y: bool = True,
+ leave_cursor_on: bool = True,
+ time_per_char: float = 0.1,
+ reverse_rate_function=False,
+ introducer=True,
+ **kwargs,
+ ) -> None:
+ self.cursor = cursor
+ self.buff = buff
+ self.keep_cursor_y = keep_cursor_y
+ self.leave_cursor_on = leave_cursor_on
+ super().__init__(
+ text,
+ time_per_char=time_per_char,
+ reverse_rate_function=reverse_rate_function,
+ introducer=introducer,
+ **kwargs,
+ )
+
+ def begin(self) -> None:
+ self.y_cursor = self.cursor.get_y()
+ self.cursor.initial_position = self.mobject.get_center()
+ if self.keep_cursor_y:
+ self.cursor.set_y(self.y_cursor)
+
+ self.cursor.set_opacity(0)
+ self.mobject.add(self.cursor)
+ super().begin()
+
+ def finish(self) -> None:
+ if self.leave_cursor_on:
+ self.cursor.set_opacity(1)
+ else:
+ self.cursor.set_opacity(0)
+ self.mobject.remove(self.cursor)
+ super().finish()
+
+ def clean_up_from_scene(self, scene: Scene) -> None:
+ if not self.leave_cursor_on:
+ scene.remove(self.cursor)
+ super().clean_up_from_scene(scene)
+
+ 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)
+
+ if index != 0:
+ self.cursor.next_to(
+ self.all_submobs[index - 1], RIGHT, buff=self.buff
+ ).set_y(self.cursor.initial_position[1])
+ else:
+ self.cursor.move_to(self.all_submobs[0]).set_y(
+ self.cursor.initial_position[1]
+ )
+
+ if self.keep_cursor_y:
+ self.cursor.set_y(self.y_cursor)
+ self.cursor.set_opacity(1)
+
+
+class UntypeWithCursor(TypeWithCursor):
+ """Similar to :class:`~.RemoveTextLetterByLetter` , but with an additional cursor mobject at the end.
+
+ Parameters
+ ----------
+ time_per_char
+ Frequency of appearance of the letters.
+ cursor
+ :class:`~.Mobject` shown after the last added letter.
+ buff
+ Controls how far away the cursor is to the right of the last added letter.
+ keep_cursor_y
+ If ``True``, the cursor's y-coordinate is set to the center of the ``Text`` and remains the same throughout the animation. Otherwise, it is set to the center of the last added letter.
+ leave_cursor_on
+ Whether to show the cursor after the animation.
+
+ .. tip::
+ This is currently only possible for class:`~.Text` and not for class:`~.MathTex`.
+
+
+ Examples
+ --------
+
+ .. manim:: DeletingTextExample
+ :ref_classes: Blink
+
+ class DeletingTextExample(Scene):
+ def construct(self):
+ text = Text("Deleting", color=PURPLE).scale(1.5).to_edge(LEFT)
+ cursor = Rectangle(
+ color = GREY_A,
+ fill_color = GREY_A,
+ fill_opacity = 1.0,
+ height = 1.1,
+ width = 0.5,
+ ).move_to(text[0]) # Position the cursor
+
+ self.play(UntypeWithCursor(text, cursor))
+ self.play(Blink(cursor, blinks=2))
+
+ """
+
+ def __init__(
+ self,
+ text: Text,
+ cursor: VMobject | None = None,
+ time_per_char: float = 0.1,
+ reverse_rate_function=True,
+ introducer=False,
+ remover=True,
+ **kwargs,
+ ) -> None:
+ super().__init__(
+ text,
+ cursor=cursor,
+ time_per_char=time_per_char,
+ reverse_rate_function=reverse_rate_function,
+ introducer=introducer,
+ remover=remover,
+ **kwargs,
+ )
diff --git a/manim/animation/indication.py b/manim/animation/indication.py
index bb3f71219..ffc310718 100644
--- a/manim/animation/indication.py
+++ b/manim/animation/indication.py
@@ -34,6 +34,7 @@ __all__ = [
"ApplyWave",
"Circumscribe",
"Wiggle",
+ "Blink",
]
from typing import Callable, Iterable, Optional, Tuple, Type, Union
@@ -53,6 +54,7 @@ from ..animation.creation import Create, ShowPartial, Uncreate
from ..animation.fading import FadeIn, FadeOut
from ..animation.movement import Homotopy
from ..animation.transform import Transform
+from ..animation.updaters.update import UpdateFromFunc
from ..constants import *
from ..mobject.mobject import Mobject
from ..mobject.types.vectorized_mobject import VGroup, VMobject
@@ -76,8 +78,6 @@ class FocusOn(Transform):
The color of the spotlight.
run_time
The duration of the animation.
- kwargs
- Additional arguments to be passed to the :class:`~.Succession` constructor
Examples
--------
@@ -643,3 +643,68 @@ class Circumscribe(Succession):
super().__init__(
ShowPassingFlash(frame, time_width, run_time=run_time), **kwargs
)
+
+
+class Blink(Succession):
+ """Blink the mobject.
+
+ Parameters
+ ----------
+ mobject
+ The mobject to be blinked.
+ time_on
+ The duration that the mobject is shown for one blink.
+ time_off
+ The duration that the mobject is hidden for one blink.
+ blinks
+ The number of blinks
+ hide_at_end
+ Whether to hide the mobject at the end of the animation.
+ kwargs
+ Additional arguments to be passed to the :class:`~.Succession` constructor.
+
+ Examples
+ --------
+
+ .. manim:: BlinkingExample
+
+ class BlinkingExample(Scene):
+ def construct(self):
+ text = Text("Blinking").scale(1.5)
+ self.add(text)
+ self.play(Blink(text, blinks=3))
+
+ """
+
+ def __init__(
+ self,
+ mobject: Mobject,
+ time_on: float = 0.5,
+ time_off: float = 0.5,
+ blinks: int = 1,
+ hide_at_end: bool = False,
+ **kwargs
+ ):
+ animations = [
+ UpdateFromFunc(
+ mobject,
+ update_function=lambda mob: mob.set_opacity(1.0),
+ run_time=time_on,
+ ),
+ UpdateFromFunc(
+ mobject,
+ update_function=lambda mob: mob.set_opacity(0.0),
+ run_time=time_off,
+ ),
+ ] * blinks
+
+ if not hide_at_end:
+ animations.append(
+ UpdateFromFunc(
+ mobject,
+ update_function=lambda mob: mob.set_opacity(1.0),
+ run_time=time_on,
+ ),
+ )
+
+ super().__init__(*animations, **kwargs)