把对象从一个位置平滑移到另一个位置,最简洁的写法是 obj.animate.shift(...) 或 obj.animate.move_to(...)。示例演示几种移动方式的差别,顺带说明什么时候不该用 .animate。

完整代码

1
2
3
4
5
6
7
8
9
10
from manim import *
import numpy as np
class MovingAround(Scene):
def construct(self):
square = Square(color=BLUE, fill_opacity=1)
self.play(square.animate.shift(LEFT))
self.play(square.animate.set_fill(ORANGE))
self.play(square.animate.scale(0.3))
self.play(square.animate.rotate(0.4))
self.wait(1.60)

演示视频

下载 Python 脚本

要点

  • shift() 是相对位移,move_to() 是绝对定位,两者都能接 .animate
  • 想沿指定曲线走用 MoveAlongPath,不是 .animate
  • .animate 只在起点和终点之间插值,中途不会重新计算路径

代码来源:Manim Community 官方 Example Gallery 示例 MovingAround,已在 Manim CE 0.20.1 实跑验证并生成上方视频。