frame_width / frame_height 描述的是坐标系能装下多少个单位长度,不是像素分辨率。默认 14.2222×8 正好是 16:9,和 1920×1080 一一对应。这段代码演示只改高度会怎样把画面拉变形。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from manim import *
class FrameSizeDemo(Scene):
"""frame_width / frame_height:坐标系的画幅,单独改会拉伸"""
def construct(self):
title = Text("改画幅必须宽高一起改,否则画面被拉伸", font_size=30).to_edge(UP, buff=0.4)
self.play(Write(title))
# 左:只改 frame_height=12 -> 水平方向被压扁,圆形变横向椭圆
bad_frame = Rectangle(width=3.4, height=3.4 * 12 / 14.2222,
stroke_color=RED, stroke_width=2.5)
bad_cir = Circle(radius=0.62, color=RED, stroke_width=3).stretch(1.5, 0)
bad_cir.move_to(bad_frame.get_center())
bad_label = Text("只改 frame_height=12", font_size=20, color=RED)
bad_size = Text("画幅 14.22 x 12,比率 1.19", font_size=17, color=GREY_A)
g_bad = VGroup(bad_frame, bad_cir, bad_label, bad_size)
bad_label.next_to(bad_frame, DOWN, buff=0.55)
bad_size.next_to(bad_label, DOWN, buff=0.12)
# 右:宽高同时改 -> 比率仍是 16:9,圆形不变形
good_frame = Rectangle(width=3.4, height=3.4 * 9 / 16,
stroke_color=GREEN, stroke_width=2.5)
good_cir = Circle(radius=0.62, color=GREEN, stroke_width=3)
good_cir.move_to(good_frame.get_center())
good_label = Text("宽高同时改:21.33 x 12", font_size=20, color=GREEN)
good_size = Text("比率 1.78 = 16:9,形状不变", font_size=17, color=GREY_A)
g_good = VGroup(good_frame, good_cir, good_label, good_size)
good_label.next_to(good_frame, DOWN, buff=0.55)
good_size.next_to(good_label, DOWN, buff=0.12)
bad_frame.shift(LEFT * 3.1 + UP * 0.75)
bad_cir.move_to(bad_frame.get_center())
bad_label.next_to(bad_frame, DOWN, buff=0.5)
bad_size.next_to(bad_label, DOWN, buff=0.12)
good_frame.shift(RIGHT * 3.1 + UP * 0.75)
good_cir.move_to(good_frame.get_center())
good_label.next_to(good_frame, DOWN, buff=0.5)
good_size.next_to(good_label, DOWN, buff=0.12)
self.play(FadeIn(VGroup(g_bad), shift=UP * 0.2))
self.play(FadeIn(VGroup(g_good), shift=UP * 0.2))
self.wait(0.6)
rows = [
("config.frame_width = 14.2222", "默认 16:9", WHITE),
("config.frame_height = 12", "宽度不会跟着变", RED),
("config.frame_width = 21.33", "必须补这一行", GREEN),
]
code_col = VGroup(*[Text(c, font="Consolas", font_size=18, color=col)
for c, _, col in rows]).arrange(DOWN, buff=0.14,
aligned_edge=LEFT)
anchor = code_col.get_right() + RIGHT * 0.4
notes = VGroup()
for i, (c, note, col) in enumerate(rows):
t = Text("# " + note, font_size=17, color=col)
t.move_to(anchor + RIGHT * (t.width / 2))
t.set_y(code_col[i].get_center()[1])
notes.add(t)
VGroup(code_col, notes).to_edge(DOWN, buff=0.5)
self.play(LaggedStart(*[FadeIn(r, shift=RIGHT * 0.15)
for r in [*code_col, *notes]], lag_ratio=0.2))
self.wait(2.0)

演示视频

下载 Python 脚本

要点

  • 默认 frame_width=14.2222、frame_height=8,比例 16:9;像素尺寸另由 -r / pixel_width / pixel_height 决定
  • 两个属性互相独立,只把 frame_height 改成 12 不会带着宽度变,圆形会被压成横向椭圆
  • 要换画幅就按目标比例同时改宽高,例如高度 12 对应宽度 21.33,形状才不会被拉伸

代码来源:Manim Community 官方文档 Guides / Configuration(MIT),示例代码按 Manim CE 0.20.1 编写并实跑验证。