본문 바로가기

Programming/Manim Lectures

[05-2-A] Text: for general text string

반응형

Text class can only handle normal characters, not formulas. Instead, you can specify and use all fonts on your PC, and rendering is fast. (Rendering: Collecting multiple scenes into a video)


Therefore, it is more convenient to use the Text class than TextMobject when it contains plain strings without formulas.


Text class

object > Container > Mobject > VMobject > SVGMobject > Text
manimlib.mobject.svg.text_mobject.Text(self, text, **config)

Make the text string given as an argument into an object that Manim can render. LaTex formula is not supported, and fonts installed on the PC can be specified and used.


Whitespace characters before and after text are ignored.

 

Parameters: text
    Text string to be converted to Text object 

Parameters: **config
    - color: text color ex)color=YELLOW
    - font: text font name to be used. ex)font='Arial'
    - size: text size ex)size=0.3
    - stroke_width: Text outline thickness, recommended to set stroke_width=0 
    - t2c: text to color, specifying a dictionary for color assignment by text

To get the word "Hello World" on the screen, you can do the following:

 

t = Text("Hello World")
self.add()
self.wait()

 

Text Animation

 

To make a Text object called text appear on the screen, simply call self.add(text).

To make an animation that writes text on the screen, you can do self.play(Write(text)) instead of self.add(text), and the fade-in effect is self.play(FadeIn(text)).

 

Position

If you don't specify the position, it is displayed in the center of the screen. You can specify the position through the position movement methods such as to_edge, move_to, next_to, shift. (See [04]Coordinate)

 

Alignment

If multiple Text objects are collected and grouped into VGroup, you can arrange each Text object in this group by arrange method.

group = VGroup(t1, t2, t3)
group.arrange(DOWN)  # align sub-objecs in the group from top to bottom

 

Color

To set the text color to yellow, use t.set_color(YELLOW), and to give a gradient effect that changes from red to yellow, use t.set_color_by_gradient(RED, YELLOW).

 

Size/Rotate

You can use the scale method like t.scale(0.5), and if the parameter scale_facto is greater than 1, it is enlarged, and if it is less than 1, it is reduced.

 

You can also resize the height to be same as the target object, such as t.set_height(tgt_label.get_height()). At this time, the width is also resized to the original proportion by default.

If you don't want to change the width, set t.set_height(0.5, stretch=True).

 

The way to rotate the text is t.rotate(45 * DEGREES). It rotates the text 45 degrees counterclockwise.

 

Copy/Save_State/Restore

When copying the created Text object, you can do t2 = t1.copy().

 

When you save th current state and restore it later, you can do the following.

t1.save_state()
...
t1.restore()

 

The difference with TexMobject/TextMobject

Unlike TextMobject or TexMobject, Text does not support the following expressions.

tex = TexMobject("a", "+", "b", "=", "c")

In the above case, tex becomes a list with 5 elements, so each element can be processed like tex[0].set_color(RED). However, Text does not support this expression.

 

In other words, the following code generates an error.

text = Tex("a", "+", "b", "=", "c")  # It cause error

Therefore, if you want to divide and control each string, you can create a separate Text object as shown in the example below, and then group and control it with VGroup.

 

str = ["Apple","Pear", "Pineapple", "Watermelon"]
text = VGroup(*[Text(s) for s in str])
text.arrange(DOWN)
self.add(text)
self.wait()

 

Different color designation for each letter

When creating Text object, the color of each character can be specified using the t2c parameter.

 

        t1 = Text("Hello, this is one sentence",
                  font='굴림', size=0.5, stroke_width=1,
                  t2c={'Hello': YELLOW, 'one': RED}
                  )

        t2 = Text("Google",
                  font='Arial', size=0.7, stroke_width=0,
                  t2c={
                      '[:1]': '#4285F4',  # G
                      '[1:2]': '#EA4335',  # o
                      '[2:3]': '#FBBC05',  # o
                      '[3:4]': '#4285F4',  # g
                      '[4:5]': '#34A853',  # l
                      '[5:]': '#EA4335',  # e
                  }
                  )

 

Text Size

The character size of the Text can be determined by the 'size' value and the default value is 1. The unit is MUnit, and 1 MUnit is one cell in the Manim Coordinate System.

 

In Manim, the entire screen is composed of 14.2 units on the x-axis and 8 units on the y-axis, so 1 MUnit is never small. Therefore, if it is about 0.5 to 1, characters of the appropriate size will be displayed in the video.

 

 


Next: [05-2-B]TexMobject: for equation

 

[05-2-B]TexMobject: for equation

TextMobject class object > Container > Mobject > VMobject > SVGMobject > SingleStringTexMobject > TexMobject manimlib.mobject.svg.text_mobject.TexMobject(self, *tex_strings, **kwargs) Makes the tex_..

infograph.tistory.com

Go To: [99] Table of Contents

반응형