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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| from manim import * import numpy as np from numpy import array run_time = 1 data = [[1, 2], [3, 4]] def dot(pos, **kwargs): return Dot(pos, **kwargs) class PulseFlashEffect(Animation): def __init__( self, mobject: Mobject, color: str = YELLOW, max_radius: float = 2.0, min_radius: float = 0.1, pulse_speed: float = 1.0, glow_width: float = 10, **kwargs ): """ 构造函数。 Parameters: mobject - 这是要添加脉冲闪烁效果的对象,必须是一个Mobject类型的对象 color - 发光的颜色,默认为黄色(YELLOW) max_radius - 发光圆环的最大半径,默认为 2.0 min_radius - 发光圆环的最小半径,默认为 0.1 pulse_speed - 脉冲的速度,默认为 1.0。这个参数控制脉冲的频率 glow_width - 发光圆环的宽度,默认为 10 **kwargs - 用于传递额外的关键字参数到父类Animation的构造函数中 """ self.glow = Circle( radius=min_radius, stroke_width=glow_width, stroke_color=color, fill_opacity=0, stroke_opacity=0.5, ).move_to(mobject.get_center()) self.max_radius = max_radius self.min_radius = min_radius self.pulse_speed = pulse_speed super().__init__(self.glow, **kwargs) def interpolate_mobject(self, alpha: float): """ 实现动画效果的核心方法。 Parameters: alpha - 是一个介于 0 和 1 之间的参数,表示动画的进度 """ t = self.pulse_speed * self.run_time * alpha phase = np.sin(2 * PI * t) radius = interpolate(self.min_radius, self.max_radius, 0.5 * (phase + 1)) opacity = interpolate( 0.8, 0.1, (radius - self.min_radius) / (self.max_radius - self.min_radius) ) self.glow.set_stroke(opacity=opacity) self.glow.set_width(2 * radius) class PulseFlashEffectExample01(Scene): def construct(self): formula = MathTex( r"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}", font_size=48, ) self.play(*[FadeIn(_o) for _o in [formula]], run_time=1.0) discriminant = formula[0][7:13] discriminant.set_color(RED) glow = PulseFlashEffect( discriminant, color=RED, max_radius=1, pulse_speed=2.0, remover=True, ) self.play( discriminant.animate.set_color(RED), glow, run_time=2, ) discriminant.set_color(WHITE) discriminant = formula[0][14:] glow = PulseFlashEffect( discriminant, color=BLUE, max_radius=1, pulse_speed=2.0, remover=True, ) self.play( discriminant.animate.set_color(BLUE), glow, run_time=2, ) self.wait() class PulseFlashEffectExample02(Scene): def construct(self): axes = Axes(x_range=[-3, 3], y_range=[-1, 10]) graph = axes.plot(lambda x: x**2, color=BLUE) self.play(*[FadeIn(_o) for _o in [axes, graph]], run_time=1.0) dot1 = Dot(color=YELLOW).move_to(axes.c2p(0, 0)) dot2 = Dot(color=BLUE).move_to(axes.c2p(1, 1)) dot3 = Dot(color=RED).move_to(axes.c2p(2, 4)) pulse1 = PulseFlashEffect( dot1, color=YELLOW, max_radius=0.8, min_radius=0.2, pulse_speed=3.0, remover=True, ) pulse2 = PulseFlashEffect( dot2, color=BLUE, max_radius=0.8, min_radius=0.2, pulse_speed=3.0, remover=True, ) pulse3 = PulseFlashEffect( dot3, color=RED, max_radius=0.8, min_radius=0.2, pulse_speed=3.0, remover=True, ) self.play(FadeIn(dot1), pulse1) self.play(FadeIn(dot2), pulse2) self.play(FadeIn(dot3), pulse3) self.wait()
|