如何让多个动画“齐步走”?
<p>在我们制作数学动画时,经常会遇到多个动作同时发生的情况。</p><p>比如:坐标轴出现的同时,标签也跟着浮现;或者一个图形变色的同时,它也在移动。</p>
<p>今天,我们就来聊聊 <code>ManimCE</code> 中同时执行多个动画的那些事儿。</p>
<h1 id="实现多动画的三种方法">实现多动画的三种方法</h1>
<h2 id="方法一使用-add-方法最简单直接">方法一:使用 <code>add()</code> 方法(最简单直接)</h2>
<p>这是最基础的方法,适合初学者理解。它的原理是把多个动画添加到场景中,让它们同时开始播放。</p>
<pre><code class="language-python">from manim import *
class SimpleMultiAnimation(Scene):
def construct(self):
# 创建两个图形
circle = Circle(color=BLUE).shift(LEFT * 2)
square = Square(color=RED).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()
</code></pre>
<p><strong>运行效果</strong>:蓝色圆向上移动的同时,红色正方形向下移动。</p>
<p><img src="https://img2024.cnblogs.com/blog/83005/202604/83005-20260426113032519-761880505.gif" alt="" loading="lazy"></p>
<h2 id="方法二使用-animationgroup适合组织相关动画">方法二:使用 <code>AnimationGroup</code>(适合组织相关动画)</h2>
<p>当你有多个动画需要作为一个整体来控制时,<code>AnimationGroup</code> 是个很好的选择。</p>
<pre><code class="language-python">class AnimationGroupExample(Scene):
def construct(self):
# 创建三个点
dots = VGroup(*)
dots.arrange(RIGHT, buff=1)
# 为每个点创建不同的动画
animations = [
dots.animate.shift(UP*2).set_color(YELLOW),
dots.animate.shift(DOWN*2).set_color(GREEN),
dots.animate.shift(LEFT*2).set_color(RED)
]
# 使用AnimationGroup同时播放
self.play(AnimationGroup(*animations))
self.wait()
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202604/83005-20260426113032422-867994906.gif" alt="" loading="lazy"></p>
<h2 id="方法三使用-laggedstart-和-succession进阶控制">方法三:使用 <code>LaggedStart</code> 和 <code>Succession</code>(进阶控制)</h2>
<p>如果想要更精细的控制,比如让动画依次开始但部分重叠,可以使用这些高级类:</p>
<pre><code class="language-python">class LaggedStartExample(Scene):
def construct(self):
squares = VGroup(*)
squares.arrange(RIGHT, buff=0.5)
# 动画依次开始,但会重叠播放
self.play(
LaggedStart(
*[
s.animate.rotate(PI).set_color(random_bright_color())
for s in squares
],
lag_ratio=0.3# 每个动画之间的延迟时间比例
),
run_time=3,
)
self.wait()
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202604/83005-20260426113032476-266391000.gif" alt="" loading="lazy"></p>
<h1 id="同时执行不同速率的动画">同时执行不同速率的动画</h1>
<p>在实际制作动画时,我们经常需要让不同的动画以不同的速度进行。</p>
<p>比如,一个图形快速移动,另一个缓慢旋转。</p>
<p>这时我们可以通过<code>ApplyMethod</code>方法,精确控制每个动画的运动曲线:</p>
<pre><code class="language-python">class RateFunctionsExample(Scene):
def construct(self):
# 创建三个物体
dot1 = Dot(color=RED, radius=0.2).shift(LEFT * 3 + UP * 2)
dot2 = Dot(color=GREEN, radius=0.2).shift(LEFT * 3)
dot3 = Dot(color=BLUE, radius=0.2).shift(LEFT * 3 + DOWN * 2)
self.add(dot1, dot2, dot3)
anim1 = ApplyMethod(dot1.shift, RIGHT * 6, rate_func=linear)# 匀速
anim2 = ApplyMethod(
dot2.shift, RIGHT * 6, rate_func=rate_functions.ease_out_quad
)# 先快后慢
anim3 = ApplyMethod(
dot3.shift, RIGHT * 6, rate_func=rate_functions.ease_in_quad
)# 先慢后快
# 使用不同的速率函数
self.play(
anim1,
anim2,
anim3,
run_time=3,
)
self.wait()
</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/83005/202604/83005-20260426113032476-384408594.gif" alt="" loading="lazy"></p>
<h1 id="常见问题">常见问题</h1>
<p><strong>Q:为什么我的动画没有同时执行?</strong><br>
A:检查是否在 <code>play()</code> 方法中正确使用了逗号分隔多个动画,而不是分号。</p>
<p><strong>Q:如何让动画精确地在同一时刻结束?</strong><br>
A:设置相同的 <code>run_time</code> 参数即可。</p>
<p><strong>Q:不同速率的动画如何同步?</strong><br>
A:使用 <code>rate_funcs</code> 参数为每个动画指定不同的速率函数。</p>
<h1 id="总结">总结</h1>
<p>掌握同时执行多个动画的技巧,能让你的数学动画更加生动和专业。</p>
<p>从简单的 <code>add()</code> 方法到复杂的速率函数组合,<code>ManimCE</code>提供了丰富的工具来满足各种需求。</p>
<p>多动手练习这些例子,相信你很快就能制作出专业级别的数学动画!</p><br><br>
来源:https://www.cnblogs.com/wang_yb/p/19932130
頁:
[1]