》)
Manim 实例大全整合完整版适配版本manim v0.19运行环境Python3.8安装依赖pip install manim manim-dsa numpy渲染命令# 单场景480p快速预览 manim -pql main.py 场景类名 # 批量渲染所有场景 manim -pql main.py # 清晰度参数l(480p)/m(720p)/h(1080p)/k(4K) 全局配置默认开启中文支持、适配深色主题 from manim import * import numpy as np # 全局中文配置 config.font SimHei config.unicode True一、入门基础场景1.1 欢迎文本演示class HelloWorld(Scene): def construct(self): text Text(Hello Manim! 动画创作, font_size72, colorBLUE) self.play(Write(text), run_time2) self.wait(2) self.play(FadeOut(text))1.2 基础圆形创建class CreateCircle(Scene): def construct(self): circle Circle(radius2, colorGREEN) circle.set_fill(GREEN, opacity0.5) self.play(Create(circle)) self.wait()二、基础几何图形场景2.1 多基础图形展示class BasicShapes(Scene): def construct(self): # 基础图形定义 c Circle(radius1, colorBLUE).shift(LEFT*3) s Square(side_length2, colorRED) t Triangle(colorYELLOW).shift(RIGHT*3) line Line(UP, DOWN, colorWHITE).shift(DOWN*2) # 批量创建动画 self.play(Create(c), Create(s), Create(t), Create(line)) self.wait()2.2 图形分组与自动排列class GroupArrange(Scene): def construct(self): # 分组横向排列间距1 g VGroup( Circle(colorBLUE), Square(colorRED), Triangle(colorYELLOW) ).arrange(RIGHT, buff1) self.play(Create(g)) self.wait()三、文本与LaTeX公式场景3.1 中文文本演示class TextDemo(Scene): def construct(self): t1 Text(Manim 文本演示, font_size48, colorORANGE) t2 Text(完美支持中文显示, colorGREEN).next_to(t1, DOWN) self.play(Write(t1), Write(t2)) self.wait()3.2 基础数学公式展示class FormulaDemo(Scene): def construct(self): # 经典物理公式 eq1 MathTex(rEmc^2, font_size60, colorYELLOW) # 高斯积分公式 eq2 MathTex( r\int_{-\infty}^{\infty} e^{-x^2}dx\sqrt{\pi}, font_size40, colorBLUE ).shift(DOWN*2) self.play(Write(eq1), Write(eq2)) self.wait()3.3 公式分段高亮动画class FormulaStep(Scene): def construct(self): eq MathTex( ra^2b^2c^2, substrings_to_isolate[a^2, b^2, c^2] ) self.play(Write(eq)) self.play(eq.animate.set_color_by_tex(a^2, RED)) self.play(eq.animate.set_color_by_tex(b^2, GREEN)) self.play(eq.animate.set_color_by_tex(c^2, BLUE)) self.wait()四、坐标系与函数图像场景4.1 坐标轴与坐标点标记class AxesDemo(Scene): def construct(self): # 创建坐标轴并显示刻度 axes Axes( x_range[-5,5], y_range[-3,3], axis_config{include_numbers: True} ) # 创建(2,1)坐标点 dot Dot(axes.coords_to_point(2,1), colorRED) label MathTex((2,1)).next_to(dot, RIGHT) self.play(Create(axes)) self.play(Create(dot), Write(label)) self.wait()4.2 三角函数图像绘制class FunctionGraph(Scene): def construct(self): axes Axes(x_range[-PI, PI], y_range[-1.5,1.5]) sin_graph axes.plot(lambda x: np.sin(x), colorBLUE, stroke_width3) cos_graph axes.plot(lambda x: np.cos(x), colorRED, stroke_width3) # 函数标签 sin_label MathTex(ry\sin x, colorBLUE).shift(UP*2RIGHT*2) cos_label MathTex(ry\cos x, colorRED).shift(UP*1RIGHT*2) self.play(Create(axes)) self.play(Create(sin_graph), Create(cos_graph)) self.play(Write(sin_label), Write(cos_label)) self.wait()4.3 动态滑块控制函数振幅class SliderSin(Scene): def construct(self): # 创建振幅滑块 0-5 slider Slider(0, 5, 0.1, value1, width8).shift(DOWN*2) val DecimalNumber(slider.get_value(), num_decimal_places2).next_to(slider, UP) axes Axes(x_range[-PI, PI], y_range[-5,5]) # 实时更新函数图像 graph always_redraw(lambda: axes.plot(lambda x: np.sin(x)*slider.get_value(), colorYELLOW, stroke_width3) ) # 数值实时更新 def update_val(mob): mob.set_value(slider.get_value()) val.add_updater(update_val) self.add(axes, slider, val, graph) self.wait(5)五、核心动画变换场景5.1 移动、旋转、缩放变换class MoveRotateScale(Scene): def construct(self): s Square(colorBLUE).scale(2) self.play(Create(s)) self.play(s.animate.shift(RIGHT*3)) # 平移 self.play(s.animate.rotate(PI/2)) # 旋转90度 self.play(s.animate.scale(0.5)) # 缩小一半 self.wait()5.2 图形形态转换方转圆class SquareToCircle(Scene): def construct(self): circle Circle(colorPINK, fill_opacity0.5) square Square().rotate(PI/4) self.play(Create(square)) self.play(Transform(square, circle)) self.play(FadeOut(square))5.3 物体沿路径运动class MoveAlongPathDemo(Scene): def construct(self): path Circle(radius2, colorGRAY) dot Dot(colorRED, radius0.15) self.play(Create(path)) self.play(MoveAlongPath(dot, path), run_time3) self.wait()5.4 渐入渐出与高亮闪烁class FadeBlink(Scene): def construct(self): t Text(动画效果演示, font_size60) self.play(FadeIn(t)) self.play(t.animate.flash(colorYELLOW, line_length0.3)) self.play(FadeOut(t))六、3D立体动画场景6.1 3D坐标轴与旋转立方体class ThreeDCube(ThreeDScene): def construct(self): axes ThreeDAxes() cube Cube(side_length2, colorBLUE, fill_opacity0.5) # 设置初始相机角度 self.set_camera_orientation(phi75*DEGREES, theta30*DEGREES) self.play(Create(axes), Create(cube)) # 开启自动旋转 self.begin_ambient_camera_rotation(rate0.2) self.wait(5)6.2 3D曲面函数图像class ThreeDSurface(ThreeDScene): def construct(self): axes ThreeDAxes() # 自定义曲面 z sin(x)cos(y) surface Surface( lambda u,v: np.array([u, v, np.sin(u)*np.cos(v)]), u_range[-PI, PI], v_range[-PI, PI], colorBLUE, fill_opacity0.7 ) self.set_camera_orientation(phi60*DEGREES, theta45*DEGREES) self.play(Create(axes), Create(surface)) self.wait(5)七、算法可视化场景7.1 冒泡排序可视化from manim_dsa import * class BubbleSort(Scene): def construct(self): arr [5,3,8,4,2] marr MArray(arr, styleMArrayStyle.BLUE).add_indexes() self.play(Create(marr)) n len(arr) # 冒泡排序核心逻辑可视化 for i in range(n): for j in range(n-i-1): self.play(marr[j].animate.highlight(), marr[j1].animate.highlight()) if arr[j] arr[j1]: arr[j], arr[j1] arr[j1], arr[j] self.play(marr.animate.swap(j,j1)) self.play(marr[j].animate.unhighlight(), marr[j1].animate.unhighlight()) self.wait(2)八、综合经典案例场景8.1 勾股定理图形演示class Pythagoras(Scene): def construct(self): # 直角三角形 3-4-5 tri Polygon([0,0,0], [4,0,0], [4,3,0], colorBLUE, fill_opacity0.2) # 三边正方形 a_square Square(side_length3, colorRED, fill_opacity0.3).next_to(tri, DOWN, buff0) b_square Square(side_length4, colorGREEN, fill_opacity0.3).next_to(tri, LEFT, buff0) c_square Square(side_length5, colorYELLOW, fill_opacity0.3).rotate(53*DEGREES).move_to(tri.get_center()) # 公式文本 formula MathTex(ra^2b^2c^2, font_size50).shift(UP*3) self.play(Create(tri)) self.play(Create(a_square), Create(b_square)) self.play(Create(c_square), Write(formula)) self.wait(3)8.2 定积分面积可视化class IntegralArea(Scene): def construct(self): axes Axes(x_range[0,5], y_range[0,10]) f lambda x: x**2 graph axes.plot(f, colorBLUE, stroke_width3) # 绘制1-4区间积分面积 area axes.get_area(graph, x_range[1,4], colorYELLOW, opacity0.5) # 积分公式 formula MathTex(r\int_{1}^{4} x^2 dx, font_size45).shift(UP*2RIGHT*3) self.play(Create(axes), Create(graph)) self.play(Write(formula)) self.play(FadeIn(area)) self.wait(3)