material原创
SVGMobject ImageMobject Text Tex MarkupText MathTex
文件夹设置好,在使用路径时可以不用传入文件后缀名。文件结构如下:
manim
|--- manim.py
|--- assets
|--- raster_images
|--- picture.png
|--- raster_images
|--- svg_file.svg
|--- sounds
# SVGMobject
mob = SVGMobject(
"svg_file", # 文件名,按照本文的文件结构放置文件,无需传入后缀
color=BLUE,
stroke_width= 0
)
manim
能处理的svg
元素有:
- path
- rect
- circle
- ellipse
- polygon
- polyline
# ImageMobject
img = ImageMobject(
"picture", # 文件名,按照本文的文件结构放置文件,无需传入后缀。支持的后缀:jpg,png,gif
height=2, # 图片高度,默认为2
invert=false, # 是否反色,默认 false
color=BLUE,
stroke_width= 0
)
ImageMobject
不是 VMobject
的子类,所有有很多动画无法使用!!!如: Uncreate(img)、Transform(img, square)。
能使用的有:FadeOut(img) 淡出
# Text
可以使用 Text
利用 LaTex
编译转换出 SVG
。LaTex
的命令的\
都需要替换为 \\
转义,或在字符串前加 r
。
from manim import *
class SquareAndCircle(Scene):
def construct(self):
text = Text("Hello world", font_size=144, font="Noto Sans", color=RED) # 字体,大小,颜色
self.add(text)
self.wait()
t = Text("Hello", gradient=(RED, BLUE, GREEN), font_size=96) # 梯度颜色
self.add(t)
self.wait()
text = Text("Colors", font_size=96)
for letter in text:
letter.set_color(random_bright_color()) # 随机颜色
self.add(text)
self.wait()
# MarkupText
from manim import *
class SingleLineColor(Scene):
def construct(self):
text = MarkupText(
f'all in red <span fgcolor="{YELLOW}">except this</span>', color=RED
)
self.add(text)
self.wait()
# LaTex
from manim import *
class HelloLaTeX(Scene):
def construct(self):
tex = Tex(r"\LaTeX", font_size=144)
self.add(tex)
self.wait()
tex = Tex(r'$\mathtt{H} \looparrowright$ \LaTeX', font_size=144)
self.add(tex)
self.wait()
tex = Tex(r'Hello \LaTeX', color=BLUE, font_size=144)
self.add(tex)
self.wait()
# MathTex
from manim import *
class SquareAndCircle(Scene):
def construct(self):
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))
self.wait()
equation = MathTex(
# e^x 的泰勒公式
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)
self.wait()
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" # 只给x 上色
)
equation.set_color_by_tex("x", YELLOW)
self.add(equation)
self.wait()
text = MathTex(r"\binom{2n}{n+2}", font_size=96)
# index the first (and only) term of the MathTex mob
self.add(index_labels(text[0]))
text[0][1:3].set_color(YELLOW) # 字符数组 不同下标上色
text[0][3:6].set_color(RED) # 字符数组 不同下标上色
self.add(text)
self.wait()
上次更新: 2022/08/24, 17:50:00