Skip to main content

代码输入动画效果

Project description

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+

安装方法

# 克隆仓库
git clone https://github.com/yourusername/manim-code-anim.git
cd manim-code-anim

# 安装依赖
pip install -e .

使用示例

基本使用

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

支持的编程语言

  • 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
  • 完善了文档

许可证

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

manim_code_anim-0.1.1.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

manim_code_anim-0.1.1-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file manim_code_anim-0.1.1.tar.gz.

File metadata

  • Download URL: manim_code_anim-0.1.1.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for manim_code_anim-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f9777d3f0137680decbdb240404b0c101f53537e902211ec5c3ade62fab1ed93
MD5 64da3d71277cb11847fdb269f7db8c7e
BLAKE2b-256 5b30d21bff8f231bb97f4da601f617f2ca85c7a2c2792da2b57cb2ab85e0fac8

See more details on using hashes here.

File details

Details for the file manim_code_anim-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for manim_code_anim-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c7dca6b190f0eaef1621b6b0047029895dd4fa2e0791b1e1484cabe9735711e3
MD5 71b7669584c86a2ff008abbb04b2a43d
BLAKE2b-256 c0ea56526668137cc1feb6c9f54ff05f18e4a54688f91e9686f9d90e327074e2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page