静态效果主要用于在场景中创建不动的元素或文本,主要功能包括:
- 基本图形:如
Circle、Square、Triangle、Polygon等,可以用来创建静态的形状。 - 文本:可以使用
Text或者MathTex来添加静态文本和数学公式。 - 组合图形:使用
VGroup、Group等组合多个图形或文本元素。 - 设置颜色和样式:可以通过设置
fill_color、stroke_color、stroke_width等属性来调整静态元素的视觉效果。
上面的是基本的静态效果目标,这是目标怎么实现呢?接下来慢慢介绍一下这些把。
3.1 静态添加方法
self.add()在Manim中,self.add()是一个非常重要的方法,它用于将对象添加到当前的场景中。调用self.add()后,所添加的对象将成为该场景的一部分,并会在渲染时显示出来。
添加对象:self.add()可以用于将图形、文本或其他场景元素添加到当前的场景中。
- 支持多种对象: 可以直接添加单个对象,也可以一次性添加多个对象(例如,使用
*运算符或将多个对象打包成列表)。
3.2基本图形的静态添加
这个代码定义了一个 Manim 场景 `BasicShapes`,在其中创建了四种基本形状,并将它们放置在不同的位置。运行此代码后,您将在屏幕上看到这些形状。
from manim import * class BasicShapes(Scene): def construct(self): # 创建基本图形 circle = Circle() # 圆形 square = Square() # 正方形 triangle = Triangle() # 三角形 polygon = RegularPolygon(5) # 五边形(正多边形) # 设置位置 circle.move_to(LEFT *3) square.move_to(ORIGIN) triangle.move_to(RIGHT *3) polygon.move_to(RIGHT *5) # 将图形添加到场景中 self.add(circle, square, triangle, polygon)3.3文本的静态添加
在Manim中,文本的处理是一个非常重要的部分,特别是对于动态演示和数学表达。下面是关于普通文本和数学文本显示的简要介绍。在处理文本中要解释Text的属性以及如何在使用它。
在这里面有一个非常重要的内容:“字体”。在这个过程中我们可以使用font设置不同的字体。
通过下面的代码我们得到想要的字体格式:
import manimpango manimpango.list_fonts()字体格式的运行结果:
['Alef', 'Amiri', 'Amiri Quran', 'Arial', 'Bahnschrift', 'Caladea', 'Calibri', 'Cambria', 'Cambria Math', 'Candara'] ['Carlito', 'Cascadia Code', 'Cascadia Mono', 'Century Gothic', 'Comic Sans MS', 'Consolas', 'Constantia', 'Corbel', 'Courier New', 'Cursive'] ['David CLM', 'David CLM MediumItalic', 'David Libre', 'DejaVu Math TeX Gyre', 'DejaVu Sans', 'DejaVu Sans Mono', 'DejaVu Serif', 'DengXian', 'Ebrima', 'FangSong'] ['Fantasy', 'Frank Ruehl CLM', 'Frank Ruehl CLM MediumOblique', 'Frank Ruhl Hofshi', 'Franklin Gothic', 'Gabriola', 'Gadugi', 'Gentium Basic', 'Gentium Book Basic', 'Georgia'] ['HYSWLongFangSong', 'Impact', 'Ink Free', 'Javanese Text', 'KaiTi', 'Leelawadee UI', 'Liberation Mono', 'Liberation Sans', 'Liberation Serif', 'Linux Biolinum G'] ['Linux Libertine Display G', 'Linux Libertine G', 'Lucida Console', 'Lucida Sans Unicode', 'MS Gothic', 'MS PGothic', 'MS UI Gothic', 'MV Boli', 'Malgun Gothic', 'Microsoft Himalaya'] ['Microsoft JhengHei', 'Microsoft JhengHei UI', 'Microsoft New Tai Lue', 'Microsoft PhagsPa', 'Microsoft Sans Serif', 'Microsoft Tai Le', 'Microsoft YaHei', 'Microsoft YaHei UI', 'Microsoft Yi Baiti', 'MingLiU-ExtB'] ['MingLiU_HKSCS-ExtB', 'Miriam CLM', 'Miriam Libre', 'Miriam Mono CLM', 'Miriam Mono CLM BoldOblique', 'Mongolian Baiti', 'Monospace', 'Myanmar Text', 'NSimSun', 'Nachlieli CLM'] ['Nirmala UI', 'Noto Kufi Arabic', 'Noto Mono', 'Noto Naskh Arabic', 'Noto Naskh Arabic UI', 'Noto Sans', 'Noto Sans Arabic', 'Noto Sans Arabic UI', 'Noto Sans Armenian', 'Noto Sans Georgian'] ['Noto Sans Hebrew', 'Noto Sans Lao', 'Noto Sans Lisu', 'Noto Serif', 'Noto Serif Armenian', 'Noto Serif Georgian', 'Noto Serif Hebrew', 'Noto Serif Lao', 'PMingLiU-ExtB', 'Palatino Linotype'] ['Reem Kufi', 'Rubik', 'Sans', 'Sans-Serif', 'Scheherazade', 'Segoe Print', 'Segoe Script', 'Segoe UI', 'Segoe UI Emoji', 'Segoe UI Historic'] ['Segoe UI Symbol', 'Segoe UI Variable Display', 'Segoe UI Variable Small', 'Segoe UI Variable Text', 'Serif', 'SimHei', 'SimSun', 'SimSun-ExtB', 'Sitka Banner', 'Sitka Display'] ['Sitka Heading', 'Sitka Small', 'Sitka Subheading', 'Sitka Text', 'Sylfaen', 'System-ui', 'Tahoma', 'Times New Roman', 'Trebuchet MS', 'Verdana'] ['Yu Gothic', 'Yu Gothic UI']3.3.1普通文本文字的显示
普通文本文字的显示在Manim中,可以使用Text类来显示普通文本。这个类允许你将字符串渲染为屏幕上的文本。
1.设置倾斜和加粗
倾斜是文本的样式,它可以是NORMAL(默认),倾斜。通常,对于许多字体来说,ITALIC和OBLIQUE看起来很相似,但是ITALIC使用罗马字体格式,而OBLIQUE使用ITALIC Style。
class SlantsExample(Scene): def construct(self): a = Text("Italic", slant=ITALIC) b = Text("OBLIQUE", slant=OBLIQUE).shift(DOWN) self.add(a,b)也可以调成不同粗细的字体
from manim import * class DifferentWeight(Scene): def construct(self): import manimpango # 创建一个空的VGroup来存储不同字重的文本 g = VGroup() # 获取所有字重并按照字重值进行排序 weight_list = dict( sorted( { weight: manimpango.Weight(weight).value for weight in manimpango.Weight }.items(), key=lambda x: x[1 ) ) # 遍历不同的字重并创建对应的文本对象 for weight in weight_list: g += Text(weight.name, weight=weight.name, font="Open Sans") # 将排列好的文本对象添加到场景中并进行缩放 self.add(g.arrange(DOWN).scale(0.5))2.颜色的使用
你们可以使用color设置文本的颜色,选择直接染色。
from manim import * class SimpleColor(Scene): def construct(self): col = Text("RED COLOR", color=RED) self.add(col)也可以局部染色,您可以使用诸如t2c (t2c是 "text to color" 的缩写)之类的实用程序为特定字符着色。如果你的文本包含迭代文本中解释的结扎,这可能会有问题。t2c接受两种类型的字典,键可以包含像[2:-1]或[4:8]这样的索引,这类似于Python中的切片工作方式。这些值应该是color中的Text的颜色。关键字包含的单词或字符应该分别上色,值应该是color中的颜色:比如:
from manim import * class Textt2cExample(Scene): def construct(self): t2cindices = Text('Hello', t2c={'[1:-1]': BLUE}).move_to(LEFT) t2cwords = Text('World',t2c={'rl':RED}).next_to(t2cindices, RIGHT) self.add(t2cindices, t2cwords)t2c是 "text to color" 的缩写,它允许你通过给定的索引范围为文本部分设置颜色。{'[1:-1]': BLUE}表示将文本中的某些字符范围应用指定的颜色。在这个例子中:[1:-1]是一个切片字符串,表示从索引1到倒数第一个索引(-1表示最后一个字符,不包括该字符)。因此,这里的范围是字符 "e" 和 "l"(即 "Hello" 的中间部分)。BLUE是要应用的颜色,表示这些字符会被染成蓝色。
3.3.2数学文字文本的显示
数学文字文本的显示对于数学表达式,Manim提供了MathTex和Tex类。MathTex适用于LaTeX格式的数学公式,而Tex则支持一般的LaTeX文本。
在默认情况下,传递给MathTex的所有内容都处于数学模式。更准确地说,MathTex是在数学环境中处理的。您可以通过将公式包含在Tex中来实现类似的效果。
class MathTeXDemo(Scene): def construct(self): self.camera.background_color = "#0303130" rtarrow0 = MathTex(r"\xrightarrow{x^6y^8}", font_size=96) rtarrow1 = Tex(r"$\xrightarrow{x^6y^8}$", font_size=96) self.add(VGroup(rtarrow0, rtarrow1).arrange(DOWN))1.数学字符上上颜色
TeX对象可以接受多个字符串作为参数。之后,您可以通过索引(如tex[1])或通过选择tex代码的部分来引用各个部分。在这个例子中,我们使用set_color_by_tex()设置了字符的颜色:
class LaTeXSubstrings(Scene): def construct(self): self.camera.background_color = "#0303130" tex = Tex('Hello', r'$\bigstar$', r'\LaTeX', font_size=144) tex.set_color_by_tex('ig', RED) tex.set_color_by_tex('lo', YELLOW_A) self.add(tex)注意,set_color_by_tex()对包含Tex的整个子字符串上色,而不仅仅是特定的符号或Tex表达式。
class IncorrectLaTeXSubstringColoring(Scene): def construct(self): self.camera.background_color = "#0303130" equation = MathTex( r"e^x = x^0 + x^1 + \frac{1}{2} x^2 + \frac{1}{6} x^3 + \cdots + \frac{1}{n!} x^n + \cdots" ) equation.set_color_by_tex("x", YELLOW) self.add(equation)2.特定的字符串上上颜色
正如你所看到的,把上方整个方程涂成黄色,与预期相反。要把x涂成黄色,我们必须这样做:
class CorrectLaTeXSubstringColoring(Scene): def construct(self): self.camera.background_color = "#0303130" equation = MathTex( r"e^x = x^0 + x^1 + \frac{1}{2} x^2 + \frac{1}{6} x^3 + \cdots + \frac{1}{n!} x^n + \cdots", substrings_to_isolate="x" ) equation.set_color_by_tex("x", YELLOW) self.add(equation)通过将substrings_to_isolate设置为x,我们自动将MathTex拆分为子字符串,并将x组件隔离为单独的子字符串。只有这样才能使用set_color_by_tex()来实现所需的结果。
注意,Manim还支持一种自定义语法,该语法允许轻松地将TeX字符串拆分为子字符串:只需用双括号将公式中想要隔离的部分括起来。在字符串MathTex(r"{{a^2}} + {{b^2}} = {{c^2}}")中,呈现的对象将由子字符串a^2, +, b^2, =和c^2组成。这使得类似文本之间的转换为:
class CorrectLaTeXSubstringColoring(Scene): def construct(self): self.camera.background_color = "#0303130" equation = MathTex( r"e^x = x^0 + x^1 + \frac{1}{2} x^2 + \frac{1}{6} x^3 + \cdots + \frac{1}{n!} x^n + \cdots", substrings_to_isolate=["x", "^3", "+"] ) equation.set_color_by_tex("x", YELLOW) equation.set_color_by_tex("^3", RED) equation.set_color_by_tex("+", BLUE) self.add(equation)3.多元素函数的颜色修改
如果实现下面一样的效果,就会按照我写的代码来实现就好啦。
class CorrectLaTeXSubstringColoring(Scene): def construct(self): self.camera.background_color = "#0303130" equation = MathTex( r"e^x = x^0 + x^1 + \frac{1}{2} x^2 + \frac{1}{6} x^3 + \cdots + \frac{1}{n!} x^n + \cdots", substrings_to_isolate=["x", "^", "+"] ) # 设置系数的颜色 for i in range(1, 10): equation.set_color_by_tex(f"{i}", GREEN) # 设置变量的颜色 equation.set_color_by_tex("x", YELLOW) # 设置幂次方的颜色 for i in range(10): equation.set_color_by_tex(f"x^{i}", BLUE) self.add(equation)