Location>code7788 >text

manim Learning by Doing - Text Creation and Destruction

Popularity:805 ℃/2024-12-09 18:23:25

This article begins the introduction ofManimhit the nail on the headanimeModule.animeModules are the core attraction of the whole framework.

Manim not only provides objects that can directly realize a wide range of animation effects.

It also provides parameters to set the duration, delay time and motion rate of the animation, whereby you can play with your creativity and customize distinctive animation effects.

This article focuses on several built-in animation effects related to text.

  1. AddTextLetterByLetter: Presentation of text content by adding text letter by letter
  2. RemoveTextLetterByLetter: Eraser-like text deletion effect
  3. Write: Analog handwriting effects
  4. Unwrite:: In conjunction with theWrite The opposite of animation, used to simulate erasing handwritten content or undoing the drawing process

1. Overview of animation

1.1. AddTextLetterByLetter

AddTextLetterByLetterThe animation is characterized by a letter-by-letter addition of text to show the text content, presenting an effect of gradual text generation.

The presentation allows you to control the speed at which the letters appear, so that the pace of the animation is more in line with the needs of the content.

It is mainly suitable for teaching videos, explanation type animation and other scenes.

For example, when creating a video explaining a math theorem, the content of the theorem is shown step-by-step so that viewers can follow along verbatim to enhance their understanding.

Its main parameters are:

Parameter name typology clarification
text Text To display the text content letter by letter
time_per_char float Frequency of letter occurrences, used to control the time interval between occurrences of each letter
rate_func func The rate function used to control the appearance of letters
run_time float Running time of the animation

1.2. RemoveTextLetterByLetter

RemoveTextLetterByLetterTo realize the effect that text disappears letter by letter from back to front.

respond in singingAddTextLetterByLetter Instead, there is a sense of reverse dynamics.

It can be used with other animation effects, such as when text is deleted one by one and new text appears immediately afterward, creating a coherent content update animation.

RemoveTextLetterByLetterGenerally used for eraser-style text deletion effects, in scenarios where you need to undo input or erase content.

For example, an animation showing a letter-by-letter deletion of an error code during code editing.

Its main parameters are:

Parameter name typology clarification
text Text Text content to be deleted letter by letter
time_per_char float Controls the time interval at which each letter is deleted, i.e., how often the letters disappear one by one
rate_func func Rate function for controlling letter deletion
run_time float Running time of the animation

1.3. Write

WriteThe animation starts from the starting point of the object and shows the appearance of the object in a way similar to handwriting or drawing, giving a naturally occurring feel.

Not only text, but also for complex shapes, it is possible to have a written animated display based on the structure and path of the shape, rather than simply appearing as a whole.

Because of its analog handwriting effect, theWriteIdeal for use in mathematical derivations, drawing steps, or demonstrations of the artistic process.

For example, when showing the steps in drawing a geometric figure, you can also use theWrite animation to simulate the process of manually drawing a picture.

Its main parameters are:

Parameter name typology clarification
vmobject VMobject Objects to be hand-animated
rate_func func Rate function for writing control
reverse bool Used to control whether the writing direction is reversed or not

1.4. Unwrite

UnwriteAnimation andWrite The opposite of animation is used to simulate the process of erasing handwritten content or undoing a drawing.

It makes objects disappear in a similar way to writing in reverse, and theWrite Animation creates a complementary effect.

In instructional videos, if you need to re-explain a step, you can use theUnwrite animation to clear the previous content.

Its main parameters are:

Parameter name typology clarification
vmobject VMobject Objects to be erased
rate_func func Rate function for erasure control
reverse bool Used to control the order of erasure (front to back or back to front)

2. Examples of use

Here are some examples to demonstrate the use of text creation and destruction related animations, simplified according to the actual scene.

2.1 Video simulation of knowledge presentation

In this example of a simulated knowledge explanation video, first go through theAddTextLetterByLetterIntroduce questions to get the audience thinking.

followed byWriteAnimations show the answer derivation process to help the audience understand.

afterwardsRemoveTextLetterByLetterDelete the question to avoid an overly cluttered picture.

end up withUnwriteErase the answers and prepare for the next knowledge point.

# First use AddTextLetterByLetter to display the question letter by letter
question = Text("What is the Pythagorean Theorem?")
(UP * 2)
(AddTextLetterByLetter(question))
()

# Then use the Write animation to show the step-by-step derivation of the answer
answer = MathTex(r "a^2 + b^2 = c^2", font_size=40)
answer.next_to(question, DOWN)
(Write(answer), run_time=run_time)
()

# Then use RemoveTextLetterByLetter to remove the question letter by letter
(RemoveTextLetterByLetter(question))
()

# Finally use the Unwrite animation to erase the answer
(Unwrite(answer))

2.2 Simulated story creation animation

This example should be used in a story creation animation.AddTextLetterByLetterLet the story title appear letter by letter to add to the mystery.

WriteAnimation presents the beginning of the story and immerses the viewer in the atmosphere of the story.

followed byRemoveTextLetterByLetterrespond in singingUnwriteThe deletion of the story's beginning and title, respectively, symbolizes the end of one paragraph of the story, freeing up screen space for subsequent plot developments.

included among theseRemoveTextLetterByLetterset upreverseparameters areFalse, so that the order of deleting letters becomes from beginning to end.

# expense or outlay AddTextLetterByLetter Show story title
title = Text("The Mysterious Forest", color=YELLOW)
(UP * 2)
(AddTextLetterByLetter(title))
()

# 使expense or outlay Write Animation showing the opening description of the story
story = Text(
    "Once upon a time, \nthere was a young adventurer \nwho entered the forest.",
    font_size=30,
)
story.next_to(title, DOWN)
(Write(story))
()

# expense or outlay RemoveTextLetterByLetter Delete the beginning of the story letter by letter
(RemoveTextLetterByLetter(story))
()

# 使expense or outlay Unwrite Animated Erase Caption
(Unwrite(title, reverse=False))

2.3 Displaying text at no rate

In this example, the main demonstrationrate_funcThe use of parameters.

Three different rates are used to display the text, the first line of text is displayed at the square root of the time, so it gets progressively slower;

The display rate of the second line of text is linear, so the text is displayed one by one at an even rate;

The second line of text is displayed at the rate of time squared, so it is displayed faster and faster.

This demonstrates that there is no difference between themanimHow to utilizerate_funcparameter to realize different rates of text display animation effect.

# Prepare the text to be displayed
txt1 = Text("Slow speed for display text", font_size=30, color=BLUE)
txt2 = Text("Normal speed for display text", font_size=30, color=RED)
txt3 = Text("Fast speed for display text", font_size=30, color=GREEN)

(UP * 2)
# Set a different rate_func to control the rate at which the text is displayed
# Slower and slower rates, square root function of t
(AddTextLetterByLetter(txt1, rate_func=lambda t: t**0.5))
()

txt2.next_to(txt1, DOWN)
# Use the linear rate function to display text quickly
(AddTextLetterByLetter(txt2, rate_func=linear))
()

txt3.next_to(txt2, DOWN)
# Faster and faster rate, squared function of t
(AddTextLetterByLetter(txt3, rate_func=lambda t: t**2))
()

# Clear the text from the scene
(Unwrite(txt1), Unwrite(txt2), Unwrite(txt3))

3. Annexes

The code in the article is just an extract of the key parts, the complete code is shared on a web disk (),

Download at.Full Code (Access code: 6872)