放置 mobjects

让我们定义一个新的 :class:'.Scene' 和 :meth:'~.Scene.add' 一些Shapes mobjects。这个脚本生成一个静态图片,显示一个圆形、一个正方形和一个三角形:

class Shapes(Scene):
    def construct(self):
        circle = Circle()
        square = Square()
        triangle = Triangle()

        circle.shift(LEFT)
        square.shift(UP)
        triangle.shift(RIGHT)

        self.add(circle, square, triangle)
        self.wait(1)

默认情况下,mobjects 被放置在坐标的中心或原点,当它们第一次被创建时。它们也被赋予一些默认颜色。此外,场景使用 :meth:'.shift' 方法放置 Shapesmobjects。 square 沿原点UP方向移动一个单位,而circle 和 triangle 分别移动一个单位 LEFTRIGHT

还有许多其他可能的方法可以在屏幕上放置对象,例如 :meth:'.move_to'、:meth:'.next_to' 和 :meth:'.align_to'。下一个场景MobjectPlacement使用这三种方法。


class MobjectPlacement(Scene):
    def construct(self):
        circle = Circle()
        square = Square()
        triangle = Triangle()

        # place the circle two units left from the origin
        circle.move_to(LEFT * 2)
        # place the square to the left of the circle
        square.next_to(circle, LEFT)
        # align the left border of the triangle to the left border of the circle
        triangle.align_to(circle, LEFT)

        self.add(circle, square, triangle)
        self.wait(1)

:meth:'.move_to' 方法使用绝对单位 (相对于ORIGIN 测量),而 :meth:'.next_to' 使用相对单位 (从作为第一个参数传递的mobject 测量)。:meth:'align_to' 不用LEFT作测量单位,而是用作确定对齐边界的一种方式。 这 mObject 边界的坐标是使用其周围的假想边界框确定的。

manim 中的许多方法可以链接在一起。例如,两个 线

square = Square()
square.shift(LEFT)

可以替换为

square = Square().shift(LEFT)

从技术上讲,这是可能的,因为大多数方法调用都会返回修改后的 mobject。

Last updated