本文将介绍如何使用Manim实现波浪形文字特效,通过自定义动画类让文字产生波浪般的动态效果。

1. 实现原理

波浪形文字特效的核心是通过自定义Animation类,对文本对象中的每个字符应用不同的位置偏移,从而形成波浪效果。

下面我们来分析具体实现:

1.1. 自定义Animation类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from manim import *
class WaveText(Animation):
"""波浪形文字特效动画类"""

def __init__(
self, text_mobject, amplitude=0.2, frequency=2, phase=0, direction=UP, **kwargs
):
# 保存参数
self.amplitude = amplitude
self.frequency = frequency
self.phase = phase
self.direction = direction

# 获取文本对象中的所有子对象(每个字符)
self.chars = text_mobject.submobjects

# 记录每个字符的原始位置
self.original_positions = [char.get_center() for char in self.chars]

super().__init__(text_mobject, **kwargs)

下载 Python 脚本

在WaveText类的初始化方法中,我们做了以下几件事:

  • 保存动画参数:振幅(amplitude)、频率(frequency)、初始相位(phase)和波动方向(direction)
  • 获取文本对象中的所有子对象(每个字符)
  • 记录每个字符的原始位置,以便后续计算偏移量
  • 调用父类的初始化方法

1.2. 实现interpolate_mobject方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from manim import *
def interpolate_mobject(self, alpha):
"""在动画过程中更新文本对象"""
# 计算当前时间相位
current_phase = self.phase + alpha * TAU * self.frequency

# 更新每个字符的位置
for i, char in enumerate(self.chars):
# 获取字符原始位置
original_pos = self.original_positions[i]

# 计算波浪偏移量
char_phase = 3 * original_pos[0]
offset = self.amplitude * np.sin(char_phase + current_phase)

# 计算新位置
new_pos = original_pos + offset * self.direction

# 更新字符位置
char.move_to(new_pos)

下载 Python 脚本

interpolate_mobject方法是实现动画效果的关键,它在动画的每一帧都会被调用:

  • alpha参数表示动画进度,范围从0到1
  • 计算当前时间的相位,结合alpha和frequency参数控制波浪的周期
  • 对每个字符应用不同的位置偏移,偏移量使用正弦函数计算
  • 字符的x坐标用于创建不同的相位,使每个字符有不同的波动
  • 最后,将计算得到的新位置应用到每个字符上

2. 使用示例

代码中提供了三个不同的场景示例,展示了波浪文字特效的各种用法:

2.1. 完整示例

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
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 WaveBasic(Scene):
def construct(self):
self.add(head("文字波浪:逐字符相位偏移", "WaveText / interpolate_mobject"))
txt = Text("MANIM WAVE", font="Consolas", font_size=58, color=BLUE)
self.add(txt)
self.play(WaveText(txt, amplitude=0.36, frequency=2.0, phase_step=0.32, run_time=4.6))
self.play(FadeIn(foot("每个字符用同一个正弦函数,只错开相位")), run_time=0.4)
self.wait(1.2)

下载 Python 脚本

这个示例展示了如何同时应用不同参数的波浪效果到多个文本对象上,包括不同的振幅、频率和方向。

2.2. 中文文本示例

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
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)

下载 Python 脚本

这个示例展示了波浪特效在中文文本上的应用,先应用垂直方向的波浪,然后应用水平方向的波浪。

2.3. 数学公式示例

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
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 WaveChinese(Scene):
def construct(self):
self.add(head("文字波浪:中文标题也能用", "Text / STLiti"))
txt = Text("数学动画真好看", font="STLiti", font_size=54, color=PINK)
self.add(txt)
self.play(WaveText(txt, amplitude=0.30, frequency=2.0, phase_step=0.32, run_time=4.6))
self.play(FadeIn(foot("中文必须用 STLiti 等 CJK 字体,否则会出现豆腐块")), run_time=0.4)
self.wait(1.2)

下载 Python 脚本

这个示例展示了波浪特效在数学公式(使用MathTex创建)上的应用,同样分别应用了垂直和水平方向的波浪效果。

下载 Python 脚本

注意:这里数学公式中的每个元素要分开写,也就是:MathTex("E", "=", "m", "c", "^2")。

如果写成一个整体的话(MathTex("E=mc^2")),整个公式会作为一个整体上下或左右摆动,不会有波浪效果。

3. 总结

3.1. 波浪文字特效的特点

  1. 高度可定制:通过调整振幅、频率、相位和方向等参数,可以创建各种不同风格的波浪效果
  2. 适用范围广:支持普通文本、中文字符和数学公式等多种文本类型
  3. 实现简洁:通过继承Animation类并重写interpolate_mobject方法,实现了核心的波浪效果
  4. 易于扩展:可以结合Manim的其他功能,如颜色变化、缩放等,创建更复杂的动画效果

3.2. 使用场景

  1. 视频片头/片尾:为标题或字幕添加波浪效果,增加视觉吸引力
  2. 教学视频:在讲解波浪、波动方程等概念时,可以用波浪文字特效直观地展示
  3. 数据可视化:为关键数据标签添加动态效果,突出重要信息
  4. 创意动画:结合其他动画效果,创建独特的视觉艺术作品

波浪文字特效是Manim中一个简单但非常实用的动画技巧,通过本文的介绍,相信你已经掌握了其实现原理和使用方法。

不妨动手尝试,为你的Manim动画增添更多动感和魅力!