manim_zh_doc
  • 如何安装manim
  • 快速使用
  • 最佳实践
    • Basic Concepts
      • ManimCELogo
      • BraceAnnotation
      • VectorArrow
      • GradientImageFromArray
      • BooleanOperations
    • Animations
      • PointMovingOnShapes
      • MovingAround
      • MovingAngle
      • MovingDots
      • MovingGroupToDestination
      • MovingFrameBox
      • RotationUpdater
      • PointWithTrace
    • Plotting
      • SinAndCosFunctionPlot
      • ArgMinExample
      • GraphAreaPlot
      • PolygonOnAxes
      • HeatDiagramPlot
  • 教程 And 指导
    • 快速使用
      • 概述
      • 新建Project
      • 制作动画
      • 解释
      • 正方形转换圆
      • 定位Mobject
      • 使用.animate语法进行动画处理
      • Transform与ReplacementTransform
    • 输出设置
      • Manim 输出文件夹
      • 章节
      • 命令行标志
    • Manim 的构建块
      • Mobjects
        • 创建和显示 mobjects
        • 放置 mobjects
        • 设置 mobject 的样式
        • Mobject 屏幕顺序
      • 动画
        • 制作方法动画
        • 动画运行时间
        • 创建自定义动画
        • 使用 mobject 的坐标
        • 将 mobject 转换为其他 mobject
      • 场景
  • 进阶指南
    • 配置
      • 命令行参数
        • 高级示例
        • 所有 CLI 标志的列表
      • ManimConfig 类
      • 配置文件
    • 深入了解 Manim 的内部结构
      • 介绍
      • 概述
      • Page
Powered by GitBook
On this page
  1. 教程 And 指导
  2. Manim 的构建块
  3. Mobjects

放置 mobjects

Previous创建和显示 mobjectsNext设置 mobject 的样式

Last updated 22 days ago

让我们定义一个新的 和 :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 分别移动一个单位 LEFT和 RIGHT。

与其他图形软件不同,manim 将坐标的中心放在屏幕的中心。正的垂直方向是向上的,正的水平方向是正确的。另请参见 :mod:'.constants' 模块中定义的常数 ORIGIN、UP 、DOWN 和 LEFT 。RIGHT

还有许多其他可能的方法可以在屏幕上放置对象,例如 :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。

:class:'.Scene'