from manim import *
class RotationUpdater(Scene):
    """官方示例 RotationUpdater：给对象挂按 dt 累加的 updater，实现每帧持续旋转。
    白线静止作参照，黄线先正转 2 秒再反转 2 秒，最后摘除 updater 停住。
    rotate_about_origin 绕的是世界原点，所以线绕原点扫过一圈圆周轨迹。
    """
    def construct(self):
        title = Text("用 updater 让对象每帧持续旋转", font="STLiti", font_size=34).to_edge(UP)
        subtitle = Text("rotate_about_origin(dt) 绕世界原点，不是绕自身中心",
                        font="STLiti", font_size=22, color=BLUE).next_to(title, DOWN, buff=0.18)
        self.play(FadeIn(title), FadeIn(subtitle), run_time=0.7)
        guide = Circle(radius=2.0, color=GREY_D, stroke_width=2)
        origin = Dot(ORIGIN, radius=0.08, color=BLUE)
        line_reference = Line(ORIGIN, LEFT * 2, stroke_width=6).set_color(WHITE)
        line_moving = Line(ORIGIN, LEFT * 2, stroke_width=9).set_color(YELLOW)
        legend = VGroup(
            Text("白线：静止参照", font="STLiti", font_size=22, color=WHITE),
            Text("黄线：挂 updater 持续旋转", font="STLiti", font_size=22, color=YELLOW),
            Text("灰圆：线端点扫过的轨迹", font="STLiti", font_size=22, color=GREY_B),
        ).arrange(DOWN, aligned_edge=LEFT, buff=0.18).to_corner(UL).shift(DOWN * 1.9)
        self.play(FadeIn(guide), FadeIn(origin), FadeIn(line_reference),
                  FadeIn(line_moving), FadeIn(legend), run_time=0.9)
        def updater_forth(mobj, dt):
            mobj.rotate_about_origin(dt)
        def updater_back(mobj, dt):
            mobj.rotate_about_origin(-dt)
        stage1 = Text("正转：dt > 0，每帧累加", font="STLiti", font_size=24, color=YELLOW).to_edge(DOWN)
        stage2 = Text("反转：换一个 updater，dt < 0", font="STLiti", font_size=24, color=GREEN).to_edge(DOWN)
        stage3 = Text("摘除 updater：停在最后一帧", font="STLiti", font_size=24, color=BLUE).to_edge(DOWN)
        self.add(stage1)
        line_moving.add_updater(updater_forth)
        self.wait(2.0)
        self.remove(stage1)
        self.add(stage2)
        line_moving.remove_updater(updater_forth)
        line_moving.add_updater(updater_back)
        self.wait(2.0)
        self.remove(stage2)
        self.add(stage3)
        line_moving.remove_updater(updater_back)
        self.wait(1.8)
