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)