# 导入Manim核心库 from manim import * # 导入Manim配置模块 import manim class Updaters(Scene): def construct(self): # 创建圆角矩型,线宽8,线色白,填充色蓝,宽4.5高2,位置在上3左4 rectangle = RoundedRectangle(stroke_width=8, stroke_color=WHITE, fill_color=BLUE_B, width=4.5, height=2).shift( UP * 3 + LEFT * 4) # 创建数学公式,设置渐变色,设置高度1.5 mathtext = MathTex("\\frac{3}{4} = 0.75" ).set_color_by_gradient(GREEN, PINK).set_height(1.5) # 数学公式位置放在圆角矩型中间 mathtext.move_to(rectangle.get_center()) # 给公式添加一个更新器,公式的位置保持在圆角矩型中间 mathtext.add_updater(lambda x: x.move_to(rectangle.get_center())) # 开始动画播放 # 动画,矩形出现方式 self.play(FadeIn(rectangle)) # 动画 公式出现 self.play(Write(mathtext)) # 动画,矩形移动向右1.5向下5,矩形移动时,公式跟随移动 self.play(rectangle.animate.shift(RIGHT * 1.5 + DOWN * 5), run_time=6) # 等待上面动画完成 self.wait() # 删除公式的更新器 mathtext.clear_updaters() # 动画,矩形向左2向上1,公式不跟随矩形移动 self.play(rectangle.animate.shift(LEFT * 2 + UP * 1), run_time=6) # 当直接运行本脚本时执行以下代码 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 = Updaters() # 执行渲染流程(包含文件生成和预览) scene.render()