实现多动画的三种方法

📅 2026/6/25 14:48:09
实现多动画的三种方法
方法一使用add()方法最简单直接这是最基础的方法适合初学者理解。它的原理是把多个动画添加到场景中让它们同时开始播放。from manim import * class SimpleMultiAnimation(Scene): def construct(self): # 创建两个图形 circle Circle(colorBLUE).shift(LEFT * 2) square Square(colorRED).shift(RIGHT * 2) # 添加图形到场景 self.add(circle, square) self.wait(0.5) # 同时执行多个动画 self.play(circle.animate.shift(UP * 2), square.animate.shift(DOWN * 2)) self.wait()运行效果蓝色圆向上移动的同时红色正方形向下移动。方法二使用AnimationGroup适合组织相关动画当你有多个动画需要作为一个整体来控制时AnimationGroup是个很好的选择。class AnimationGroupExample(Scene): def construct(self): # 创建三个点 dots VGroup(*[Dot(radius0.1) for _ in range(3)]) dots.arrange(RIGHT, buff1) # 为每个点创建不同的动画 animations [ dots[0].animate.shift(UP*2).set_color(YELLOW), dots[1].animate.shift(DOWN*2).set_color(GREEN), dots[2].animate.shift(LEFT*2).set_color(RED) ] # 使用AnimationGroup同时播放 self.play(AnimationGroup(*animations)) self.wait()方法三使用LaggedStart和Succession进阶控制如果想要更精细的控制比如让动画依次开始但部分重叠可以使用这些高级类class LaggedStartExample(Scene): def construct(self): squares VGroup(*[Square(side_length1) for _ in range(4)]) squares.arrange(RIGHT, buff0.5) # 动画依次开始但会重叠播放 self.play( LaggedStart( *[ s.animate.rotate(PI).set_color(random_bright_color()) for s in squares ], lag_ratio0.3 # 每个动画之间的延迟时间比例 ), run_time3, ) self.wait()同时执行不同速率的动画在实际制作动画时我们经常需要让不同的动画以不同的速度进行。比如一个图形快速移动另一个缓慢旋转。这时我们可以通过ApplyMethod方法精确控制每个动画的运动曲线class RateFunctionsExample(Scene): def construct(self): # 创建三个物体 dot1 Dot(colorRED, radius0.2).shift(LEFT * 3 UP * 2) dot2 Dot(colorGREEN, radius0.2).shift(LEFT * 3) dot3 Dot(colorBLUE, radius0.2).shift(LEFT * 3 DOWN * 2) self.add(dot1, dot2, dot3) anim1 ApplyMethod(dot1.shift, RIGHT * 6, rate_funclinear) # 匀速 anim2 ApplyMethod( dot2.shift, RIGHT * 6, rate_funcrate_functions.ease_out_quad ) # 先快后慢 anim3 ApplyMethod( dot3.shift, RIGHT * 6, rate_funcrate_functions.ease_in_quad ) # 先慢后快 # 使用不同的速率函数 self.play( anim1, anim2, anim3, run_time3, ) self.wait()