Manim视频尺寸和坐标系统
1. 视频宽度与高度
ManimConfig用字典存储了所有配置选项,跟大小有关的选项有:
pixel_width = config["pixel_width"] # 1920 is default
pixel_height = config["pixel_height"] # 1080 is default
aspect_ratio = config['aspect_ratio'] # 1920/1080 = 1.7777777777777777
frame_width = config["frame_width"] # 8.0 * 1.777 = 14.222
frame_height = config["frame_height"] # 8.0
media_width = config['media_width'] # 60%
window_size # default
zero_pad # 4
默认情况下,整个视频的宽度为14.2
,高度为8.0
,一个单位指1
。
2. 位置
2.1 坐标系统
整个画布的中央作为坐标系统的原点,np.array([0., 0., 0.])
。
ORIGIN: np.ndarray = np.array((0.0, 0.0, 0.0))
"""The center of the coordinate system."""
写一小段代码,画出坐标轴:
from manim import *
frame_width = config.frame_width
frame_height = config.frame_height
class Test(Scene):
def construct(self):
# self.wait(1)
legend = Text('frame_width = {}\nframe_height = {}'.format(round(frame_width, 2), frame_height), font_size=20)
self.add(legend.to_corner(UR))
# coordinate system
line1 = Line(frame_width * LEFT, frame_width * RIGHT)
line2 = Line(frame_height * UP, frame_height * DOWN)
self.add(line1, line2)
for i in range(-10, 10):
if i == 0:
continue
# x axis
dot = Dot(np.array([i, 0, 0]), color=YELLOW)
text = Text('{}'.format(i), font_size=20, color=YELLOW).next_to(dot, DOWN)
self.add(dot, text)
# y axis
dot = Dot(np.array([0, i, 0]), color=RED)
text = Text('{}'.format(i), font_size=20, color=RED).next_to(dot)
self.add(dot, text)
可以得到下图:
2.2 一些距离常量
manim/constants.py
定义了一些常量。LEFT
, RIGHT
; UP
, DOWN
; IN
, OUT
分别表示在XYZ轴上移动一个单位距离,如:
# One unit step in the positive Y direction.
UP: np.ndarray = np.array((0.0, 1.0, 0.0))
UP == np.array([0, 1, 0])
DOWN == np.array([0, -1, 0])
LEFT == np.array([-1, 0, 0])
RIGHT == np.array([1, 0, 0])
除此之外,还定义了常量UL
,UR
,DL
,DR
,分别表示上左、上右、下左、下右。
UL == np.array([-1, 1, 0])
DL == np.array([-1, -1, 0])
UR == np.array([1, 1, 0])
DR == np.array([1, -1, 0])
2.3 内边距(padding)
比如to_edge
,to_corner
默认的padding为0.5
。next_to
默认的padding为0.25
。
to_corner(self, corner=LEFT + DOWN, buff=DEFAULT_MOBJECT_TO_EDGE_BUFFER)
to_edge(self, edge=LEFT, buff=DEFAULT_MOBJECT_TO_EDGE_BUFFER)
DEFAULT_MOBJECT_TO_EDGE_BUFFER: float = MED_LARGE_BUFF
MED_LARGE_BUFF: float = 0.5
更多常量见contants.py
。
# Default buffers (padding)
SMALL_BUFF: float = 0.1
MED_SMALL_BUFF: float = 0.25
MED_LARGE_BUFF: float = 0.5
LARGE_BUFF: float = 1
DEFAULT_MOBJECT_TO_EDGE_BUFFER: float = MED_LARGE_BUFF
DEFAULT_MOBJECT_TO_MOBJECT_BUFFER: float = MED_SMALL_BUFF
3. 指定对象的位置
指定对象的位置,可以用绝对位置,也可以用相对位置。
3.1 绝对位置
通过三维坐标np.array([x, y, z])
指定对象的位置,如:点Dot,参数point
指定了点的位置,默认是坐标原点,即np.array([0., 0., 0.])
。
class Dot(point=array([0., 0., 0.]), radius=0.08, stroke_width=0, fill_opacity=1.0, color='#FFFFFF', **kwargs)[source]
3.2 相对位置
以另一个对象为参照物,指定一个对象的位置,通过next_to指定,direction
可以是LEFT
, RIGHT
; UP
, DOWN
。
# Move this Mobject next to another’s Mobject or coordinate.
next_to(mobject_or_point, direction=array([1., 0., 0.]), buff=0.25, aligned_edge=array([0., 0., 0.]), submobject_to_align=None, index_of_submobject_to_align=None, coor_mask=array([1, 1, 1]))
align_to可以向另一个对象对齐:
# Aligns mobject to another Mobject in a certain direction
align_to(mobject_or_point, direction=array([0., 0., 0.]), alignment_vect=array([0., 1., 0.]))
# Examples
# 左侧对齐
mob1.align_to(mob2, LEFT)
# moves mob1 horizontally so that it’s center is directly above/below the center of mob2
mob1.align_to(mob2, alignment_vect = RIGHT)