组件库参考
所有可用组件和动画类型的最小可运行代码片段
基础元素
Text — 文字
t = Text("量子场论", font_size=56, color="#8b5cf6")
t = Text("Hello World", font_size=36, color=WHITE, font="PingFang SC")
t = Text("粗体", font_size=40, weight=BOLD)
t = Text("斜体", font_size=40, slant=ITALIC)
MathTex — 数学公式
f = MathTex(r"E = mc^2", font_size=60, color=BLUE)
f = MathTex(r"\frac{d}{dx} f(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}", font_size=40)
f = MathTex(r"\int_0^1 x^2 \, dx = \frac{1}{3}", font_size=36, color="#06b6d4")
注意:不要在 MathTex 里写中文,用 Text()。
Dot — 点
d = Dot([x, y, 0], radius=0.1, color=RED) d = Dot(color="#8b5cf6") # 默认在原点
Line — 线
l = Line([x1, y1, 0], [x2, y2, 0], color=WHITE, stroke_width=3) l = DashedLine(start, end, color=GREY, dash_length=0.1)
Arrow — 箭头
a = Arrow([x1, y1, 0], [x2, y2, 0], buff=0, color=YELLOW, stroke_width=4) a = Arrow(start, end, buff=0) # buff=0 让箭头精确到达端点
Circle / Rectangle
c = Circle(radius=1, color="#8b5cf6", fill_opacity=0.3, stroke_width=3) r = Rectangle(width=4, height=2, color=BLUE, fill_opacity=0.2) sq = Square(side_length=1.5, color=GREEN)
坐标系
Axes — 二维坐标系
axes = Axes(
x_range=[-3, 3, 1], # [min, max, step]
y_range=[-1, 5, 1],
x_length=10, # 屏幕单位
y_length=6,
axis_config={"include_numbers": True, "font_size": 20},
).shift(DOWN * 0.3)
# 坐标标签
labels = axes.get_axis_labels(x_label="x", y_label="y")
# 坐标转屏幕坐标
point = axes.c2p(2, 3) # 数据坐标 → 屏幕坐标
NumberPlane — 网格
grid = NumberPlane(
x_range=[-4, 4, 1], y_range=[-3, 3, 1],
background_line_style={"stroke_color": BLUE_E, "stroke_width": 0.5, "stroke_opacity": 0.3},
)
NumberLine — 数轴
nl = NumberLine(x_range=[-3, 3, 1], length=10, include_numbers=True, font_size=24) dot = Dot(nl.n2p(2), radius=0.1, color=RED) # n2p: 数值 → 屏幕坐标
函数绘图
plot — 函数曲线
curve = axes.plot(lambda x: x**2, x_range=[-2.5, 2.5], color="#8b5cf6", stroke_width=3) curve = axes.plot(np.sin, x_range=[0, 2*PI], color=YELLOW)
ParametricFunction — 参数曲线
circle = ParametricFunction(
lambda t: axes.c2p(np.cos(t), np.sin(t)),
t_range=[0, TAU], color=BLUE,
)
get_area — 曲线下面积
area = axes.get_area(curve, x_range=[1, 3], color=["#8b5cf6", "#06b6d4"], opacity=0.4)
get_riemann_rectangles — Riemann 矩形
dx = 3.0 / 20 # x_range 宽度 / 矩形数
rects = axes.get_riemann_rectangles(
curve, x_range=[1, 4], dx=dx,
color=[BLUE_E, BLUE_D, BLUE_C],
fill_opacity=0.5, stroke_width=1,
)
动画类型
Write — 书写
self.play(Write(formula), run_time=1.5) # 逐符号书写
FadeIn / FadeOut
self.play(FadeIn(obj), run_time=1) self.play(FadeIn(obj, shift=UP * 0.3), run_time=1) # 带位移 self.play(FadeOut(obj), run_time=0.5)
Create — 绘制
self.play(Create(axes), run_time=1) # 绘制坐标系 self.play(Create(curve), run_time=1.5) # 绘制曲线 self.play(Create(line), run_time=1) # 绘制直线
Transform — 变形
self.play(Transform(formula1, formula2), run_time=1.2) # formula1 变成 formula2 的样子
GrowFromCenter / GrowArrow
self.play(GrowFromCenter(obj), run_time=1) # 从中心放大 self.play(GrowArrow(arrow), run_time=1) # 箭头生长
MoveToTarget
obj.generate_target() obj.target.move_to([2, 1, 0]).scale(0.5) self.play(MoveToTarget(obj), run_time=1)
动态动画
ValueTracker + always_redraw
x_val = ValueTracker(1.0)
# 移动的点
dot = always_redraw(lambda: Dot(
axes.c2p(x_val.get_value(), func(x_val.get_value())),
color=RED, radius=0.08,
))
# 切线
def get_tangent():
x0 = x_val.get_value()
slope = derivative(x0)
x1, x2 = x0 - 1, x0 + 1
y1 = func(x0) + slope * (x1 - x0)
y2 = func(x0) + slope * (x2 - x0)
return Line(axes.c2p(x1, y1), axes.c2p(x2, y2), color=GREEN, stroke_width=3)
tangent = always_redraw(get_tangent)
self.add(dot, tangent)
self.play(x_val.animate.set_value(5), run_time=4, rate_func=smooth)
TracedPath — 轨迹追踪
trail = TracedPath(lambda: dot.get_center(), stroke_color="#06b6d4", stroke_width=3) self.add(trail)
DecimalNumber — 数字动画
counter = DecimalNumber(0, num_decimal_places=0, font_size=64, color="#8b5cf6") self.play(counter.animate.set_value(100), run_time=2, rate_func=linear)
几何变换
self.play(Rotate(obj, angle=PI), run_time=1) # 旋转 self.play(obj.animate.scale(2), run_time=1) # 缩放 self.play(obj.animate.shift(RIGHT * 2), run_time=1) # 平移 self.play(obj.animate.move_to([1, 2, 0]), run_time=1) # 移到绝对位置 self.play(obj.animate.flip(LEFT), run_time=1) # 镜像翻转 obj.next_to(other, RIGHT, buff=0.2) # 相对定位 obj.to_edge(UP, buff=0.3) # 贴边
强调效果
SurroundingRectangle — 矩形框
rect = SurroundingRectangle(formula, color="#8b5cf6", buff=0.15, corner_radius=0.1) self.play(Create(rect), run_time=0.6)
Brace — 花括号
brace = BraceBetweenPoints(axes.c2p(1, 0), axes.c2p(3, 0), color=YELLOW)
text = brace.get_text("interval")
self.play(Create(brace), FadeIn(text), run_time=0.6)
Indicate — 脉冲高亮
self.play(Indicate(formula, color="#8b5cf6", scale_factor=1.3), run_time=0.8)
Flash — 闪光
self.play(Flash(dot, color=YELLOW, line_length=0.4, num_lines=12), run_time=0.8)
Circumscribe — 围绕高亮
self.play(Circumscribe(text, color="#8b5cf6", time_width=2), run_time=1)
动画编排
LaggedStart — 交错出现
self.play(LaggedStart(*[FadeIn(d, shift=UP*0.3) for d in dots], lag_ratio=0.15), run_time=1.5)
Succession — 顺序串联
self.play(anim1, run_time=0.5) self.play(anim2, run_time=0.5) # 或用 Succession(anim1, anim2, lag_ratio=0)
AnimationGroup — 同时播放
self.play(AnimationGroup(FadeIn(a), FadeIn(b), FadeIn(c), lag_ratio=0), run_time=1)
3D
ThreeDScene
class Demo3D(ThreeDScene):
def construct(self):
axes = ThreeDAxes()
self.set_camera_orientation(phi=60*DEGREES, theta=30*DEGREES)
self.play(Create(axes))
self.begin_ambient_camera_rotation(rate=0.8)
self.wait(3)
Surface — 3D 曲面
surface = Surface(
lambda u, v: axes.c2p(u, v, np.sin(u) * np.cos(v)),
u_range=[-2.5, 2.5], v_range=[-2.5, 2.5],
resolution=(24, 24), fill_opacity=0.6,
checkerboard_colors=["#8b5cf6", "#06b6d4"],
)
MovingCameraScene — 镜头缩放
class ZoomScene(MovingCameraScene):
def construct(self):
obj = Dot([2, 1, 0], color=RED)
self.play(FadeIn(obj))
self.play(self.camera.frame.animate.scale(0.5).move_to(obj), run_time=1)
self.play(self.camera.frame.animate.scale(2), run_time=1)
常用参数
| 参数 | 说明 | 示例 |
|---|---|---|
font_size | 文字/公式大小 | font_size=36 |
color | 颜色 | color="#8b5cf6" 或 color=BLUE |
stroke_width | 线宽 | stroke_width=3 |
fill_opacity | 填充透明度 | fill_opacity=0.4 |
buff | 间距 | buff=0.2 |
run_time | 动画时长(秒) | run_time=1.5 |
rate_func | 缓动函数 | rate_func=smooth / linear |
shift | 相对位移 | shift=UP * 0.5 |
to_edge | 贴边 | to_edge(UP, buff=0.3) |
品牌色
PURPLE = "#8b5cf6" # 紫色(标题、强调) CYAN = "#06b6d4" # 青色(副标题、辅助)
下一步:踩坑指南 → 常见错误和解决方案