from manim import *
import numpy as np
def head(cn, en):
    t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
    s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
    return VGroup(t, s)
def foot(cn):
    return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
class UpdaterLinkDemo(Scene):
    def construct(self):
        self.add(head("高级特效：用 updater 联动多个对象", "add_updater / ValueTracker"))
        t = ValueTracker(-3.0)
        axes = Axes(x_range=[-4, 4, 1], y_range=[-2, 4, 1], x_length=8, y_length=4.6,
                    axis_config={"color": GREY_B}).shift(DOWN * 0.3)
        graph = axes.plot(lambda x: 0.25 * x**2, color=BLUE).set_stroke(width=5)
        dot = always_redraw(lambda: Dot(axes.c2p(t.get_value(), 0.25 * t.get_value()**2),
                                        color=YELLOW, radius=0.14))
        vline = always_redraw(lambda: DashedLine(axes.c2p(t.get_value(), 0),
                                                 dot.get_center(), color=GREY_A))
        label = always_redraw(lambda: Text("x = %.2f" % t.get_value(),
                                           font="Consolas", font_size=22,
                                           color=YELLOW).next_to(dot, UP, buff=0.18))
        self.add(axes, graph, dot, vline, label)
        self.play(t.animate.set_value(3.0), run_time=3.6, rate_func=there_and_back)
        self.play(FadeIn(foot("只驱动一个 ValueTracker，其余对象自动跟随")), run_time=0.4)
        self.wait(2.2)
