Manim Code Anim
代码输入动画效果,基于 Manim 库实现的代码块动画效果。
项目简介
manim-code-anim 是一个基于 Manim 的扩展库,用于在 Manim 动画中创建带有语法高亮的代码块动画。该库基于 CasualCodersProjects/manim-code-blocks 库进行修改和扩展。
主要功能
- 支持多种编程语言的语法高亮
- 代码逐字显示动画效果
- 支持中文显示
- 可自定义字体和主题
安装
依赖项
- Python 3.12+
- Manim 0.20.0+
- tokenize-all-code 1.0.18+
安装方法
方法一:使用 pip 安装(推荐)
# 直接从 PyPI 安装
pip install manim-code-anim
方法二:从源码安装
# 克隆仓库
git clone https://github.com/yourusername/manim-code-anim.git
cd manim-code-anim
# 安装依赖
pip install -e .
项目已发布到 PyPI,可以直接使用 pip 安装。
使用示例
基本使用
from manim import *
from manim_code_anim.code_anim import CodeAnim, Python
class CodeAnimation(Scene):
def construct(self):
# 创建一个Python代码块
code = CodeAnim(
text='print("Hello World!")',
language=Python
)
# 播放创建动画
self.play(*code.create())
self.wait(2)
# 播放销毁动画
self.play(*code.uncreate())
self.wait()
if __name__ == "__main__":
scene = CodeAnimation()
scene.render()
多行代码
from manim import *
from manim_code_anim.code_anim import CodeAnim, Python
class MultiLineCode(Scene):
def construct(self):
python_code = '''
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10))
'''
code = CodeAnim(
text=python_code,
language=Python
)
self.play(*code.create())
self.wait(3)
self.play(*code.uncreate())
self.wait()
支持中文
from manim import *
from manim_code_anim.code_anim import CodeAnim, Python
class ChineseCode(Scene):
def construct(self):
code = CodeAnim(
text='''
# 这是中文注释
print("你好,世界!") # 中文注释
''',
language=Python
)
self.play(*code.create())
self.wait(2)
self.play(*code.uncreate())
self.wait()
自定义字体
from manim import *
from manim_code_anim.code_anim import CodeAnim, Python
class CustomFontCode(Scene):
def construct(self):
code = CodeAnim(
text='print("Hello, 世界!")',
language=Python,
font="Courier New", # 英文代码字体
chinese_font="SimHei" # 中文字体
)
self.play(*code.create())
self.wait(2)
self.play(*code.uncreate())
self.wait()
高亮多行代码
from manim import *
from manim_code_anim.code_anim import CodeAnim, Python
class HighlightLinesCode(Scene):
def construct(self):
python_code = '''
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10))
'''
code = CodeAnim(
text=python_code,
language=Python
)
# 播放创建动画
self.play(*code.create())
self.wait(1)
highlight1 = code.highlight_lines(BLUE, [1])
self.play(Create(highlight1))
self.wait(1)
# 高亮第2-4行(递归逻辑) - 用黄色,同时移除之前的蓝色高亮
highlight2 = code.highlight_lines(YELLOW, [2, 3, 4])
self.play(FadeOut(highlight1), Create(highlight2))
self.wait(1)
# 高亮第6行(注释)和第7-8行(测试代码)- 用绿色
highlight3 = code.highlight_lines(GREEN, [6, 7, 8])
self.play(FadeOut(highlight2), Create(highlight3))
self.wait(1)
self.play(*code.uncreate())
self.wait()
支持的编程语言
- C
- C++
- C#
- Fortran
- Go
- Haskell
- Java
- JavaScript
- Lua
- Python
- Ruby
- Rust
- SQL
- TypeScript
API 参考
CodeAnim 类
CodeAnim(
text: str, # 要显示的代码文本
language: ProgrammingLanguage | None = None, # 编程语言
theme: Theme = OneDark, # 语法高亮主题
font: str = "FiraCode Nerd Font Mono", # 英文代码字体
chinese_font: str = "Microsoft YaHei", # 中文字体
**kwargs: object # 传递给VGroup的其他参数
)
方法
create(**kwargs): 返回创建代码块的动画元组uncreate(**kwargs): 返回销毁代码块的动画元组
基于的库
本项目基于 CasualCodersProjects/manim-code-blocks 库,进行了以下修改:
- 添加了中文支持
- 优化了字体处理
- 修复了一些bug
- 完善了文档
许可证
Metadata
Release files for manim-code-anim 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| manim_code_anim-0.1.4.tar.gz | 24.4 kB | Without provenance | Details for manim_code_anim-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| manim_code_anim-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for manim_code_anim-0.1.4-py3-none-any.whl |
Total release size: 45.7 kB
Release files / manim_code_anim-0.1.4.tar.gz
| Download URL | manim_code_anim-0.1.4.tar.gz |
|---|---|
| Size | 24.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3d74172c5875c3113f80d633c8ec97fadbb733db92c697841d652edadc3a5cba
|
|
BLAKE2b-256 checksum How to use checksums |
294a2ea87d293f7fe8ae10a28ba786151d426a27bee36435ee9facc5e561ed9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.19
|
Release files / manim_code_anim-0.1.4-py3-none-any.whl
| Download URL | manim_code_anim-0.1.4-py3-none-any.whl |
|---|---|
| Size | 21.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3a04c6dbd1675569235c58a460ecb840b76e55a2b7f75116b48f22f9491e31ab
|
|
BLAKE2b-256 checksum How to use checksums |
2aca8c1ec114ef8acd87cb04ee194b1d3f280af021c97d844f8436f28dcd360c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.19
|
Security for manim-code-anim 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.