from manim import *
import numpy as np
import sympy as sp
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)
def bg_image(h=60, w=100, mode=0):
    a = np.zeros((h, w, 4), dtype=np.uint8)
    a[:, :, 3] = 255
    if mode == 0:
        a[:, :, 0] = np.linspace(10, 90, w, dtype=np.uint8)[None, :]
        a[:, :, 2] = np.linspace(60, 160, w, dtype=np.uint8)[None, :]
    elif mode == 1:
        a[:, :, 1] = np.linspace(20, 110, h, dtype=np.uint8)[:, None]
        a[:, :, 2] = np.linspace(40, 130, h, dtype=np.uint8)[:, None]
    else:
        a[:, :, 0] = 120
        a[:, :, 1] = 70
        a[:, :, 2] = np.linspace(30, 140, w, dtype=np.uint8)[None, :]
    return ImageMobject(a)
class WaveText(Animation):
    def __init__(self, mobject, amplitude=0.45, frequency=2.0, phase_step=0.55, **kwargs):
        super().__init__(mobject, **kwargs)
        self.amplitude = amplitude
        self.frequency = frequency
        self.phase_step = phase_step
        self.base = [c.get_center().copy() for c in mobject]
    def interpolate_mobject(self, alpha):
        for i, c in enumerate(self.mobject):
            off = self.amplitude * np.sin(TAU * self.frequency * alpha - i * self.phase_step)
            c.move_to(self.base[i] + UP * off)
class WaveTune(Scene):
    def construct(self):
        self.add(head("文字波浪：振幅与频率对比", "amplitude / frequency"))
        t1 = Text("LOW AMP", font="Consolas", font_size=44, color=GREEN).shift(UP * 1.1)
        t2 = Text("HIGH AMP", font="Consolas", font_size=44, color=YELLOW).shift(DOWN * 1.1)
        self.add(t1, t2)
        self.play(WaveText(t1, amplitude=0.16, frequency=1.5, phase_step=0.30, run_time=4.4),
                  WaveText(t2, amplitude=0.38, frequency=2.4, phase_step=0.32, run_time=4.4))
        self.play(FadeIn(foot("振幅决定起伏高度，频率决定单位时间的波动次数")), run_time=0.4)
        self.wait(1.0)
