Files
dsProject/dsLightRag/Manim/L3_Updaters.py
2025-08-14 15:45:08 +08:00

32 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 导入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)