manim中有几个特殊的用于形状匹配的对象,它们的作用是标记和注释已有的对象,本身一般不单独使用。

形状匹配对象一共有4种:

  1. BackgroundRectangle:为已有的对象提供一个矩形的背景
  2. Cross:用交叉线标记已有对象
  3. SurroundingRectangle:用矩形框围住某个对象
  4. Underline:为某个对象添加一条下划线

形状匹配的作用就是在动画中突出标记一些对象,便于更好的表达动画中的内容。

1. 主要参数

BackgroundRectangle的主要参数有:

参数名称 类型 说明
mobject Mobject 被添加背景的对象
color Color 背景的颜色
stroke_width float 背景边框的粗细程度
stroke_opacity float 背景边框的透明度
fill_opacity float 背景的透明度
buff float 背景的大小,buff越大,背景的范围越大

背景默认是没有边框的,通过stroke_width参数添加边框。

Cross的主要参数有:

参数名称 类型 说明
mobject Mobject 被添加交叉标记的对象
stroke_color Color 交叉线的颜色
stroke_width float 交叉线的粗细程度
scale_factor float 交叉线的长度

SurroundingRectangle的主要参数有:

参数名称 类型 说明
mobject Mobject 矩形框所维的对象
color Color 矩形框的颜色
buff float 矩形框与其中内容之间的间隔
corner_radius float 矩形框尖角的曲率

Underline的主要参数有:

参数名称 类型 说明
mobject Mobject 添加下划线的对象
buff float 下划线与对象之间的间隔

2. 使用示例

形状匹配的对象总体来看都比较简单,它们的多用在一些辅助显示其他主要对象的场景中。

下面通过一些示例看看它们的作用。

2.1. 矩形背景示例

manim中的动画默认是黑色背景的,可以通过BackgroundRectangle给某个对象添加一个其他颜色的矩形背景。

用来突出显示或着重表现某个对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class N064_01(Scene):
def construct(self):
sc = self
c = Circle()
self.play(*[FadeIn(_o) for _o in [BackgroundRectangle(c, color=RED)]], run_time=1.0)
c = Circle()
self.play(*[FadeIn(_o) for _o in [BackgroundRectangle(c, color=RED, fill_opacity=0.2)]], run_time=1.0)
c = Circle()
self.play(*[FadeIn(_o) for _o in [BackgroundRectangle(c, color=RED, fill_opacity=0.2, stroke_width=1, stroke_opacity=1)]], run_time=1.0)
c = Circle()
b = BackgroundRectangle(c, color=RED, fill_opacity=0.2).rotate(PI / 4)
self.wait(0.5)
self.wait(2.10)

下载 Python 脚本

2.2. 交叉标记示例

交叉标记的交点就是被标记对象的中心,标记的颜色,粗细和长短都可以调整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class N064_02(Scene):
def construct(self):
sc = self
c1 = Circle(color=GREEN)
self.play(*[FadeIn(_o) for _o in [Cross(c1, stroke_color=RED)]], run_time=1.0)
c2 = Circle(color=YELLOW)
self.play(*[FadeIn(_o) for _o in [Cross(c2, stroke_color=BLUE)]], run_time=1.0)
c3 = Circle(color=BLUE)
self.play(*[FadeIn(_o) for _o in [Cross(c3, stroke_color=GREEN)]], run_time=1.0)
c1 = Circle(color=GREEN)
self.play(*[FadeIn(_o) for _o in [Cross(c1, stroke_width=2)]], run_time=1.0)
c2 = Circle(color=YELLOW)
self.play(*[FadeIn(_o) for _o in [Cross(c2, stroke_width=5)]], run_time=1.0)
c3 = Circle(color=BLUE)
self.play(*[FadeIn(_o) for _o in [Cross(c3, stroke_width=10)]], run_time=1.0)
c1 = Circle(color=GREEN)
self.play(*[FadeIn(_o) for _o in [Cross(c1, scale_factor=0.5)]], run_time=1.0)
c2 = Circle(color=YELLOW)
self.play(*[FadeIn(_o) for _o in [Cross(c2, scale_factor=1)]], run_time=1.0)
c3 = Circle(color=BLUE)
self.play(*[FadeIn(_o) for _o in [Cross(c3, scale_factor=2)]], run_time=1.0)
self.wait(0.5)

下载 Python 脚本

2.3. 矩形边框示例

矩形边框一般用来突出显示某些文字,边框的尖角和与文字的间距都可以调整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class N064_04(Scene):
def construct(self):
sc = self
t1 = Text('English')
self.play(*[FadeIn(_o) for _o in [Underline(t1, color=GREEN)]], run_time=1.0)
t2 = Text('中文')
self.play(*[FadeIn(_o) for _o in [Underline(t2, color=BLUE)]], run_time=1.0)
t3 = MathTex('a^2+b^2=c^2')
self.play(*[FadeIn(_o) for _o in [Underline(t3, color=YELLOW)]], run_time=1.0)
t1 = Square(side_length=1)
self.play(*[FadeIn(_o) for _o in [Underline(t1, color=GREEN)]], run_time=1.0)
t2 = Circle(radius=0.5)
self.play(*[FadeIn(_o) for _o in [Underline(t2, color=BLUE)]], run_time=1.0)
t3 = Star(outer_radius=0.6)
self.play(*[FadeIn(_o) for _o in [Underline(t3, color=YELLOW)]], run_time=1.0)
self.wait(0.5)

2.4. 下划线示例

下划线是最简单的一种标记,不仅仅是中英文字,它也可以加在公式和图形的下面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from manim import *
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
class Demo(Scene):
def construct(self):
t1 = Text("English")
Underline(t1, color=GREEN)
t2 = Text("中文")
Underline(t2, color=BLUE)
t3 = Tex("")
Underline(t3, color=YELLOW)
t1 = Square(side_length=1)
Underline(t1, color=GREEN)
t2 = Circle(radius=0.5)
_obj = t2
self.play(FadeIn(_obj), run_time=1.0)
self.wait(3.00)
self.wait(1.60)

下载 Python 脚本

3. 附件