|
|
import manim
|
|
|
from manim import *
|
|
|
from manim import ValueTracker # 需要显式导入ValueTracker
|
|
|
|
|
|
|
|
|
class CircleAnimation(Scene):
|
|
|
def construct(self):
|
|
|
# 初始化半径跟踪器
|
|
|
r = ValueTracker(0.5)
|
|
|
|
|
|
# 修正1:使用Circle类(首字母大写)
|
|
|
circle = always_redraw(lambda: Circle(
|
|
|
radius=r.get_value(),
|
|
|
stroke_color=YELLOW,
|
|
|
stroke_width=5
|
|
|
))
|
|
|
|
|
|
# 修正2:补全Line的闭合括号
|
|
|
line_radius = always_redraw(lambda: Line(
|
|
|
start=circle.get_center(),
|
|
|
end=circle.get_bottom(),
|
|
|
stroke_color=RED_B,
|
|
|
stroke_width=10
|
|
|
))
|
|
|
|
|
|
# 修正3:补全Line参数和闭合括号
|
|
|
line_circumference = always_redraw(lambda: Line(
|
|
|
start=ORIGIN, end=RIGHT # 需要指定起点终点
|
|
|
).set_length(2 * PI * r.get_value()) # 正确周长公式
|
|
|
.next_to(circle, DOWN, buff=0.2)
|
|
|
.set_stroke(color=YELLOW, width=5))
|
|
|
|
|
|
# 修正4:补全Polygon参数和颜色设置
|
|
|
triangle = always_redraw(lambda: Polygon(
|
|
|
circle.get_top(),
|
|
|
circle.get_left(),
|
|
|
circle.get_right(),
|
|
|
fill_color=GREEN_C,
|
|
|
fill_opacity=0.5 # 添加填充透明度
|
|
|
))
|
|
|
|
|
|
# 修正5:正确拼写LaggedStart,补全参数括号
|
|
|
self.play(LaggedStart(
|
|
|
Create(circle),
|
|
|
DrawBorderThenFill(line_radius),
|
|
|
DrawBorderThenFill(triangle),
|
|
|
run_time=4,
|
|
|
lag_ratio=0.75
|
|
|
))
|
|
|
|
|
|
# 修正6:正确转换动画
|
|
|
self.play(ReplacementTransform(
|
|
|
circle.copy().clear_updaters(), # 移除持续更新
|
|
|
line_circumference
|
|
|
), run_time=2)
|
|
|
|
|
|
# 半径变化动画
|
|
|
self.play(r.animate.set_value(2), run_time=5)
|
|
|
self.wait()
|
|
|
|
|
|
|
|
|
# 当直接运行本脚本时执行以下代码
|
|
|
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 = CircleAnimation()
|
|
|
# 执行渲染流程(包含文件生成和预览)
|
|
|
scene.render() |