摘要:class BubbleEffect(Animation): """ 彩色气泡特效动画类 继承自Animation类,用于创建彩色气泡上升、变大、透明度变化的效果 """ #技术分享def __init__( self, bubble_count=25, bu
本文将介绍如何使用 Manim 框架实现一个简单而实用的气泡特效,该特效可用于多种场景,如背景装饰、数据可视化过渡等。
气泡特效的核心在于 BubbleEffect 类,它继承自 Manim 的 Animation 类,通过重写关键方法来实现气泡的上升、变大和透明度变化效果。
BubbleEffect 类的基本结构如下:
class BubbleEffect(Animation): """ 彩色气泡特效动画类 继承自Animation类,用于创建彩色气泡上升、变大、透明度变化的效果 """ #技术分享def __init__( self, bubble_count=25, bubble_size_range=(0.1, 0.5), rise_speed_range=(0.6, 2.2), growth_rate_range=(0.005, 0.015), fade_rate_range=(0.02, 0.06), colors=None, **kwargs ):def create_bubble(self):def interpolate_mobject(self, alpha):在 __init__ 方法中,我们首先定义了一系列参数,如气泡数量、大小范围、上升速度等,然后创建一个 VGroup 来存放所有气泡:
self.bubbles = VGroupself.bubble_count = bubble_countself.bubble_size_range = bubble_size_rangeself.rise_speed_range = rise_speed_rangeself.growth_rate_range = growth_rate_rangeself.fade_rate_range = fade_rate_rangeself.colors = colors or [RED, BLUE, GREEN, YELLOW, PURPLE, ORANGE]self.run_time = kwargs.get("run_time", 5.0)for _ in range(bubble_count): bubble = self.create_bubble self.bubbles.add(bubble)super.__init__(self.bubbles, **kwargs)每个气泡通过 create_bubble 方法创建,该方法随机设置气泡的大小、颜色和初始位置,并为每个气泡分配独立的上升速度、生长速度和消失速度:
def create_bubble(self): """创建单个彩色气泡""" size = random.uniform(*self.bubble_size_range) color = random.choice(self.colors) bubble = Circle(radius=size, color=color, fill_opacity=0.4, stroke_width=2)x_pos = random.uniform(-config.frame_width / 2 + 1, config.frame_width / 2 - 1) y_pos = random.uniform(-config.frame_height / 2, -config.frame_height / 2 + 2) bubble.move_to([x_pos, y_pos, 0])bubble.rise_speed = random.uniform(*self.rise_speed_range) bubble.growth_rate = random.uniform(*self.growth_rate_range) bubble.fade_rate = random.uniform(*self.fade_rate_range) bubble.initial_radius = sizereturn bubble动画的核心在于 interpolate_mobject 方法,它控制着每个气泡在每一帧的状态变化:
def interpolate_mobject(self, alpha): """插值函数,控制彩色气泡的动画效果""" dt = 1 / config.frame_rate for bubble in self.bubbles: bubble.shift(UP * bubble.rise_speed * dt)bubble.scale(1 + bubble.growth_rate * dt)current_opacity = bubble.get_fill_opacity new_opacity = current_opacity - bubble.fade_rate * dtif new_opacity config.frame_height / 2: x_pos = random.uniform( -config.frame_width / 2 + 1, config.frame_width / 2 - 1 ) y_pos = random.uniform( -config.frame_height / 2, -config.frame_height / 2 + 2 ) bubble.move_to([x_pos, y_pos, 0]) bubble.set_fill(opacity=0.4) bubble.set_stroke(opacity=0.4) else: bubble.set_fill(opacity=new_opacity) bubble.set_stroke(opacity=new_opacity)这个方法实现了三个关键效果:
上升 :每个气泡以自己的速度向上移动变大 :每个气泡以自己的速度缓慢变大透明度变化 :每个气泡逐渐变得透明特别值得注意的是,当气泡超出屏幕顶部或透明度降到0以下时,代码会将气泡重置到底部,从而实现循环不断的气泡效果。
代码提供了两个使用示例,分别展示了普通气泡效果和彩色气泡效果。
SimpleBubbleEffectExample 类展示了如何创建灰度的气泡效果:
class SimpleBubbleEffectExample(Scene): """普通气泡特效示例场景"""def construct(self): title = Text("普通气泡特效演示", font_size=48) title.to_edge(UP) self.play(Write(title)) self.wait(0.5)bubble_effect = BubbleEffect( bubble_count=25, colors=[GRAY], bubble_size_range=(0.1, 0.5), rise_speed_range=(0.6, 2.2), growth_rate_range=(0.1, 0.5), fade_rate_range=(0.02, 0.06), run_time=2, )self.play(bubble_effect) self.waitColorfulBubbleEffectExample 类展示了如何创建彩色的气泡效果:
class ColorfulBubbleEffectExample(Scene): """彩色气泡特效示例场景"""def construct(self): title = Text("彩色气泡特效演示", font_size=48) title.to_edge(UP) self.play(Write(title)) self.wait(0.5)bubble_effect = BubbleEffect( bubble_count=25, bubble_size_range=(0.1, 0.5), rise_speed_range=(0.6, 2.2), growth_rate_range=(0.1, 0.5), fade_rate_range=(0.02, 0.06), run_time=2, )self.play(bubble_effect) self.wait两者的主要区别在于 SimpleBubbleEffectExample 显式指定了 colors=[GRAY] ,而 ColorfulBubbleEffectExample 则使用了默认的彩色列表。
BubbleEffect 类提供了多个参数,可以根据需要进行调整:
| 参数名 | 类型 | 默认值 | 说明 | | ---
| bubble_count | int | 25 | 气泡数量 | | bubble_size_range | tuple | (0.1, 0.5) | 气泡大小范围 | | rise_speed_range | tuple | (0.6, 2.2) | 气泡上升速度范围 | | growth_rate_range | tuple | (0.005, 0.015) | 气泡生长速度范围 | | fade_rate_range | tuple | (0.02, 0.06) | 气泡透明度变化速度范围 | | colors | list | [RED, BLUE, GREEN, YELLOW, PURPLE, ORANGE] | 气泡颜色列表 | | run_time | float | 5.0 | 动画运行时间 |
通过调整这些参数,可以创建各种不同风格的气泡效果。
这个气泡特效实现简单但效果出色,可以为各种 Manim 动画项目增添生动的视觉元素。
来源:墨码行者