You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
# 导入Manim核心库
|
|
|
|
|
from manim import *
|
|
|
|
|
# 导入Manim配置模块
|
|
|
|
|
import manim
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 动画场景类必须继承自Scene
|
|
|
|
|
class BoxAnimation(Scene):
|
|
|
|
|
# construct方法是所有Manim动画的入口点
|
|
|
|
|
def construct(self):
|
|
|
|
|
"""
|
|
|
|
|
创建并动画移动一个矩形
|
|
|
|
|
步骤:
|
|
|
|
|
1. 创建带样式的矩形
|
|
|
|
|
2. 分步骤进行位移动画
|
|
|
|
|
"""
|
|
|
|
|
# 创建矩形对象
|
|
|
|
|
box = Rectangle(
|
|
|
|
|
stroke_color=GREEN, # 边框颜色设置为绿色
|
|
|
|
|
stroke_opacity=0.7, # 边框透明度70%
|
|
|
|
|
fill_color=RED_B, # 填充颜色使用红色系B版本
|
|
|
|
|
fill_opacity=0.5, # 填充透明度50%
|
|
|
|
|
height=1, # 高度1个单位(Manim默认坐标单位)
|
|
|
|
|
width=1 # 宽度1个单位
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 将矩形直接添加到场景(无动画)
|
|
|
|
|
self.add(box)
|
|
|
|
|
|
|
|
|
|
# 第一个动画:2秒内向右移动2个单位
|
|
|
|
|
self.play(box.animate.shift(RIGHT * 2), run_time=2)
|
|
|
|
|
|
|
|
|
|
# 第二个动画:2秒内向上移动3个单位
|
|
|
|
|
self.play(box.animate.shift(UP * 3), run_time=2)
|
|
|
|
|
|
|
|
|
|
# 第三个动画:2秒内向左下移动(组合位移)
|
|
|
|
|
self.play(box.animate.shift(DOWN * 5 + LEFT * 5), run_time=2)
|
|
|
|
|
|
|
|
|
|
# 第四个动画:2秒内向右上微调位置
|
|
|
|
|
self.play(box.animate.shift(UP * 1.5 + RIGHT * 1), run_time=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 当直接运行本脚本时执行以下代码
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
"""
|
|
|
|
|
配置渲染参数并执行场景渲染
|
|
|
|
|
等效命令行命令:
|
|
|
|
|
manim -pql -o custom_output 当前文件.py BoxAnimation
|
|
|
|
|
"""
|
|
|
|
|
# 配置字典说明:
|
|
|
|
|
config = {
|
|
|
|
|
"quality": "low_quality", # 渲染质量等级(low_quality对应480p)
|
|
|
|
|
"preview": True, # 渲染完成后自动打开播放器
|
|
|
|
|
"input_file": __file__, # 指定输入文件为当前文件
|
|
|
|
|
"media_dir": "./custom_output" # 自定义输出目录(默认在media/)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 使用临时配置渲染场景(配置只在with块内有效)
|
|
|
|
|
with manim.tempconfig(config):
|
|
|
|
|
# 实例化场景类
|
|
|
|
|
scene = BoxAnimation()
|
|
|
|
|
# 执行渲染流程(包含文件生成和预览)
|
|
|
|
|
scene.render()
|