from manim import *
# 这是使用 Manim 的推荐方式,因为单个脚本通常使用 Manim 命名空间中的多个名称。在这串代码中使用了 Scene、Circle、PINK和Create
# 建立圆的动画
class CreateCircle(Scene):
def construct(self):
# 大多数时候,编写动画脚本的代码完全包含在类construct()的方法中Scene。在里面construct(),您可以创建对象,将它们显示在屏幕上,并为它们设置动画。
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set the color and transparency
# 这两行创建一个圆圈并设置其颜色和不透明度
self.play(Create(circle)) # show the circle on screen
# 使用动画Create在屏幕上显示圆圈
# 所有动画必须驻留在construct()的类的方法Scene中。其他代码(例如辅助函数或数学函数)可能驻留在类之外。
# manim -pql scene.py CreateCircle 这是运行的代码 其中 CreateCircle表示你要预览的类,所以这个代码只运行这个demo文件里面的对应的这个类的代码
# 标志-p 告诉 manim 在渲染场景后播放场景,而-ql 标志告诉 manim 以低质量渲染场景
# 视频以 480 分辨率、每秒 15 帧的速度生成的。因此,输出可以在里面找到 media/videos/scene/480p15
# 若变成:manim -pqh scene.py SquareToCircle
# 标志 -ql(低质量)已被-qh 标志取代,高品质。Manim 将花费相当长的时间来渲染此文件,并且一旦完成就会播放它。这时候Manim 新建了一个文件夹media/videos/1080p60,对应高分辨率和每秒 60 帧。
# 正方形转圆动画:Transform
class SquareToCircle(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set color and transparency
square = Square() # create a square
square.rotate(PI / 4) # rotate a certain amount # 旋转90度
self.play(Create(square)) # animate the creation of the square # 创建一个正方形动画
self.play(Transform(square, circle)) # interpolate the square into the circle # 利用Tranform命令将正方形变圆
self.play(FadeOut(square)) # fade out animation # 淡出
# 设置多个物体动画:next_to
class SquareAndCircle(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set the color and transparency
square = Square() # create a square
square.set_fill(BLUE, opacity=0.5) # set the color and transparency
square.next_to(circle, RIGHT, buff=0.5) # set the position
# 我们首先通过传递该方法的第一个参数来指定粉红色圆圈作为正方形的参考点circle。第二个参数用于指定Mobject相对于参考点的放置方向。
# 在本例中,我们将方向设置为RIGHT,告诉 Manim 将正方形放置在圆的右侧。最后,buff=0.5在两个对象之间应用一个小距离缓冲区。
# 尝试改为RIGHT、LEFT、UP或DOWN,看看它如何改变正方形的位置。
# 使用定位方法,您可以渲染具有多个场景Mobject,使用坐标设置它们在场景中的位置或相对于彼此定位它们
self.play(Create(circle), Create(square)) # show the shapes on screen
self.wait()
# 使用.animate方法
def construct(self):
circle = Circle() # create a circle
square = Square() # create a square
self.play(Create(square)) # show the square on screen
self.play(square.animate.rotate(PI / 4)) # rotate the square
# 当使用了.animate方法,则可以在动画过程中来对物体属性进行修改,而不需要在外面修改
self.play(ReplacementTransform(square, circle)) # transform the square into a circle
# ReplacementTransform和Transform是一样的
self.play(circle.animate.set_fill(PINK, opacity=0.5)) # color the circle on screen
# 尽管最终结果与SquareToCircle结果相同,但是.animate显示rotate并动态set_fill应用于Mobject,而不是使用已应用的更改来创建它们
# 使用shift方法
class DifferentRotations(Scene):
def construct(self):
left_square = Square(color=BLUE, fill_opacity=0.7).shift(2 * LEFT)
# fill_opacity表示的是内部正方形的填充
right_square = Square(color=GREEN, fill_opacity=0.7).shift(2 * RIGHT)
# .shift是可以用于定义其位置的
self.play(left_square.animate.rotate(PI), Rotate(right_square, angle=PI), run_time=5)
# run_time表示的是视频的时间
self.wait()