【manim】之滚动字幕

概要

manim 是一个做数学视频的 python 库,这个库功能非常强大。
具体可以参考官方介绍:https://github.com/ManimCommunity/manim/

它本身只是封装数学相关的几何体和一些基础动画,所以,制作视频时,需要进一步封装更复杂的动画来满足视频的要求。
最近做的一个视频有很多公式推导,所以封装了一个滚动字幕的组件。

代码封装

核心代码如下:

 # -*- coding: utf-8 -*-
from manim import *

class text_displayer:
"""
字幕替换封装
"""

def __init__(
self, sc: Scene, arr, start_position=UP * 3, display_length=1, buff=0.5
) -> None:
"""
初始化

Parameters
---------
sc
绘制字幕的场景
arr
字幕列表,是 list 类型
start_position
字幕开始位置,默认位置偏上 UP*3
display_length
最多显示字幕行数,超出时则隐藏最早的那一行,其他行相应移动位置
buff
每行字幕间隔的位置
"""
self.sc = sc # 当前场景
self.text_arr = arr # 所有文本
self.start_position: int = start_position # 开始显示的位置
self.display_length: int = display_length # 最多显示的行数
self.buff = buff # 每行文本之间的间隔
self.cur_index: int = 0 # 当前的index

def next(self) -> bool:
if self.cur_index >= len(self.text_arr):
return False

# 是否需要上移
if self.cur_index >= self.display_length: # 已达到显示的最大值
# 清除第一层的文字
self.sc.play(FadeOut(self.text_arr[self.cur_index - self.display_length]))

# 上移已有的文字
for i in range(self.display_length - 1, 0, -1):
self.sc.play(
self.text_arr[self.cur_index - i].animate.move_to(
self.start_position
+ DOWN * (self.display_length - 1 - i) * self.buff
)
)

# 显示当前行
d = self.cur_index // self.display_length
if d == 0:
self.sc.play(
Write(
self.text_arr[self.cur_index].shift(
self.start_position - UP * self.buff * self.cur_index
)
)
)
else:
self.sc.play(
Write(
self.text_arr[self.cur_index].shift(
self.start_position - UP * self.buff * (self.display_length - 1)
)
)
)

self.cur_index += 1
return True

整体比较简单,通过初始化函数

__init__
设置相关的参数,然后不断调用
next()
方法显示字幕,直至返回
False
为止。

测试效果

测试代码如下:

 # -*- coding: utf-8 -*-
from manim import *


class Example(Scene):
def construct(self):
arr = [
Text("第一行", color=RED),
Text("第二行", color=YELLOW),
Text("第三行", color=BLUE),
Text("第四行", color=RED),
Text("第五行", color=YELLOW),
Text("第六行", color=BLUE),
Text("第七行", color=RED),
Text("第八行", color=YELLOW),
]

# 最多显示一行
td = text_displayer(self, arr, start_position=UP, display_length=1)

# 最多显示三行
# td = text_displayer(self, arr, start_position=UP, display_length=3)

while td.next():
pass

最多显示一行的效果如下:
out.gif

最多显示三行的效果:
out.gif

标签: python

添加新评论