dustland

dustball in dustland

manim 文本

Manim 文本

MarkupText

Text

image-20220907163441733

参数 说明
text str对象,需要写入的字符串
color str对象,文字的颜色,默认为WHITE
font 文字的字体
font_size 文字的大小,默认为48
size 文字的缩放比例,默认为1,表示原来的尺寸
tab_width 制表符tab的宽度,默认为4,表示4个空格的宽度
slant 设置斜体,默认为NORMAL。设为ITATIC表示文字设为斜体
weight 设置加粗,默认为NORMAL。设为BOLD表示文字加粗
gradient 可迭代对象,且元素为颜色,表示设置的渐变色的各个节点
t2c 字典Dict,子串到颜色的映射表
t2g 字典Dict,子串到渐变色节点迭代器的映射表

Text类只接受一个字符串,不支持数学公式,不支持’\t’,换行可以用’\n’

属性

在构造函数上通过键值对的形式传递参数,修改属性

颜色相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
text = Text('Hello', color=BLUE)

text = Text('Hello, world!', t2c={'world':BLUE})

text = Text('Hello', gradient=(BLUE, GREEN,RED))#颜色渐变,从左到右依次


text = Text( #精确模式
'Google',
t2c={
'[:1]':'#3174f0', '[1:2]':'#e53125', #下标0到1之间使用蓝色,1到2之间使用红色...
'[2:3]':'#fbb003', '[3:4]':'#3174f0',
'[4:5]':'#269a43', '[5:]':'#e53125',
}
)

text = Text( #继承自Mobject的属性#就和调css差不多
'empire march!',
fill_color=RED,#填充颜色
fill_opacity=0.5,#填充色的透明度
stroke_color=BLACK,#边线颜色
stroke_width=2,#边线宽度
)

字体相关

1
2
text = Text('Hello', font='Source Han Sans')#修改整体字体
text = Text('Hello, world!', t2f={'world':'Forte'})#修改局部字体

倾斜相关

1
2
text = Text('Hello', slant=ITALIC)#整体倾斜,slant要么是ITALIC要么是NORMAL
text = Text('Hello, world!', t2s={'world':ITALIC})#局部倾斜

粗细相关

1
2
text = Text('Hello', weight=BOLD)	#整体加粗,weight=NORMAL/BOLD
text = Text('Hello, world!', t2w={'world':BOLD}) #局部加粗

大小相关

1
text = Text('Hello', size=5)#整体5号

行间距

line_spacing_height,lsh

1
text = Text('Hello\nWorld', lsh=1.5)#1.5倍行间距,使用\n换行时有效

函数

Text类对象实例化之后,作用于对象的成员函数

1
2
3
4
5
6
7
8
9
10
self.play(FadeIn(text))
self.wait()
text.set_color(RED)#更改颜色
self.wait()
text[1:6].set_color(BLUE)#更改局部颜色
self.wait()
text.set_color_by_gradient(RED,BLACK)#更改渐变色
self.wait()
text[4:8].set_color_by_gradient(WHITE,BLUE)#更改局部渐变色
self.wait()

SingleStringTex

manim中Tex的基类,主要用于渲染单行公式或者数学符号.

也可以使用换行符//渲染多行公式

属性 说明
tex_string str对象,表示输入的tex代码
fill_opacity float,填充的透明度
font_size 字体大小,默认为48
alignment str对象,对齐方式,默认为\center
1
2
3
4
5
class Main(Scene):
def construct(self):
tex=SingleStringTex(r"f(x)=a_0+a_1x+a_2x^2+...+a_nx^n=\sum_{i=0}^n a_i x^i")
tex.set_color(BLUE)
self.play(Write(tex))

效果

image-20220909100945214

文字转换为公式的效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from turtle import left
from manimlib import *

class Main(Scene):
def construct(self):
text=Text("Definition of the ellipse")
tex2 = SingleStringTex(
tex_string=r"\frac{x^2}{a^2}+\frac{y^2}{b^2}=1",
font_size=80
)
self.add(text)
self.wait()
self.play(ReplacementTransform(text, tex2), run_time=1)
self.wait()

Main4

公式上色

使用字典映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from turtle import left
from manimlib import *

class Main(Scene):
def construct(self):
color_dict={#颜色字典
"x":RED,#x字母都上色红色
"f":PINK,
"a":GREEN
}
tex=Tex(
r"f(x)=a_0+a_1x+a_2x^2+...+a_nx^n=\sum_{i=0}^n a_i x^i",
font_size=60,
tex_to_color_map=color_dict#使用颜色字典
)
self.add(tex)
self.play(Write(tex))
# self.wait()

效果:
Main5

Tex

Tex是SingleStringTex的派生类,可以接收任意多个tex代码作为参数.

如果想要使用多行公式,不如使用换行符//,在typora中编辑好了之后放到SingleStringTex中

TexText

Tex的派生类

1
2
3
4
5
class TexText(Tex):
CONFIG = {
"math_mode": False,
"arg_separator": "",
}

math_mode默认是false,此时的用法和typora中很像

BulletedList

项目列表,TexText的子类

用于产生条目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from turtle import left, position
from manimlib import *

class demo(Scene):
def construct(self):
text=Text("To-Do List")
self.add(text)
self.play(Write(text))
self.wait()
text2=text.copy()
text2.move_to(np.array([0,3,0]))
self.play(ReplacementTransform(text,text2))#产生移动效果
tex=BulletedList(#项目列表
"win32 WinMine",
"win32 notepad",
"x86 assembly",
"linux kernel 0.12"
)
self.add(tex)
self.play(Write(tex))

效果

Main6

Title

参数 说明
scale_factor float或者int,缩放比例,用来控制标题大小。默认值为1,表示不做缩放
include_underline bool,表示标题是否需要下划线,默认为True
underline_width float或者int,表示标题下下划线的宽度,默认为屏幕宽度减去2
match_underline_width_to_text bool,表示是否需要让下划线宽度适应于标题的宽度,默认为False
underline_buff float,下划线的厚度,默认为MED_SMALL_BUFF(也就是0.25)

数学公式渲染的标题,好处就是自动定在比较靠上的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from turtle import left, position
from manimlib import *

class Main(Scene):
def construct(self):
title=Title(
r"TODO List"
)
self.play(Write(title))
tex=BulletedList(#项目列表
"win32 WinMine",
"win32 notepad",
"x86 assembly",
"linux kernel 0.12"
)
self.add(tex)
self.play(Write(tex))

image-20220909105811995

如果设置tex支持中文,则渲染效果是很好的,但是所有数学公式的渲染都需要花费时间,这是tex相对于text的最大缺点

BraceLabel

SingleStringTex的派生类,增强的Brace,可以在花括号上写字了

用于标注形体长度等等

1
2
3
4
5
6
7
8
9
10
11
12
from manimlib import *

class Main(Scene):
def construct(self):
circle=Circle()
self.play(Write(circle))
bl=BraceLabel(
circle,
text="d=10",
brace_direction=UP
)
self.play(Write(bl))

image-20220909110914772

BraceText

类似BraceLabel,但是允许$行内公式$

1
2
3
4
5
6
7
8
9
10
11
12
from manimlib import *

class Main(Scene):
def construct(self):
circle=Circle()
self.play(Write(circle))
bt=BraceText(
obj=circle,
text="直径$=10$",
brace_direction=UP
)
self.play(Write(bt))

image-20220909111120584