文本测试--效果:

代码:
| |
| from manim import * |
| class TransformExample(Scene): |
| def construct(self): |
| banner = ManimBanner() |
| banner.shift(UP * 0.5) |
| self.play(banner.create(), run_time=1) |
| self.play(banner.animate.scale(0.3), run_time=0.5) |
| self.play(banner.expand(), run_time=1) |
| |
| t = Text("测试中文能否显示").next_to(banner, DOWN ) |
| tex = VGroup( |
| Text("测试数学公式:", font_size=30), |
| Tex(r"$\sum_{n=1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6}$"), |
| ) |
| tex.arrange(RIGHT, buff=SMALL_BUFF) |
| tex.next_to(t, DOWN) |
| self.play(Write(t), run_time=1) |
| self.play(Write(tex), run_time=1) |
| |
| self.wait() |
| self.clear() |
| |
| t1=Text("manim", font="Consolas", slant=ITALIC).next_to(banner, UP) |
| t2=Text("数学", font="STHupo", font_size=60).next_to(banner, UP) |
| t3=Text("之旅", font="STXingkai", weight=BOLD).next_to(banner, UP) |
| vg = VGroup(t1, t2, t3) |
| vg.next_to(banner, UP) |
| vg.arrange(RIGHT, buff=SMALL_BUFF) |
| self.play(Write(vg),run_time=1) |
| self.wait() |
| |
| t4=Text("Powred by ZJZ", t2c={"Powred": BLUE, "by": RED, "ZJZ": GREEN}, weight=BOLD) |
| t4.next_to(tex, DOWN * 3) |
| self.play(Write(t4),run_time=1) |
| self.wait() |
| self.clear() |
| d1=Dot() |
| label = Text("A wild circle appears!") |
| label.next_to(d1, DOWN, buff=0.5) |
| label2 = Text("Powered by ZJZ!") |
| label2.next_to(d1, DOWN, buff=0.5) |
| |
| t5=Text('大家好') |
| t6=Ellipse(color=BLUE,fill_color=WHITE,fill_opacity=0.5) |
| t6.surround(t5) |
| self.play(Write(t6),Write(t5)) |
| self.wait() |
| self.clear() |
| announcement = Text("Let us draw a circle.") |
| |
| self.play(Write(announcement)) |
| self.wait() |
| |
| self.play(announcement.animate.next_to(d1, UP, buff=0.5)) |
| |
| |
| self.play(Write(label)) |
| self.wait() |
| |
| self.play(Transform(label, label2)) |
| self.wait() |
| |
| self.play(Write(announcement),Write(label)) |
| self.wait() |
| self.clear() |
| |
| formula = MathTex(r"[z^n]f(z) = \frac{1}{2\pi i}\oint_{\gamma} \frac{f(z)}{z^{n+1}}~dz") |
| self.play(Write(formula), run_time=3) |
| self.wait() |
| self.clear() |
| |
| eq1 = MathTex("42 {{ a^2 }} + {{ b^2 }} = {{ c^2 }}") |
| eq2 = MathTex("42 {{ a^2 }} = {{ c^2 }} - {{ b^2 }}") |
| eq3 = MathTex(r"a^2 = \frac{c^2 - b^2}{42}") |
| self.add(eq1) |
| self.wait() |
| self.play(TransformMatchingTex(eq1, eq2)) |
| self.wait() |
| self.play(TransformMatchingShapes(eq2, eq3)) |
| self.wait() |
| self.clear() |
| |
| text = Text("") |
| print(text.font_list()) |
| |
| self.wait(1) |
| |
| |
| self.example01() |
| self.wait() |
| self.clear() |
| |
| self.example02() |
| self.wait() |
| self.clear() |
| |
| self.example03() |
| self.wait() |
| self.clear() |
| |
| self.example04() |
| |
| self.wait() |
| |
| def example01(self): |
| """ |
| 字体相关设置 |
| """ |
| left = LEFT * 2.5 |
| title = Text("设置不同字体:", font="KaiTi", font_size=25).shift( |
| LEFT * 2 + UP * 2 |
| ) |
| self.play(Write(title)) |
| |
| |
| label = Text("Hack字体:", t2f={"字体": "STLiti"}, font_size=25).shift( |
| left + UP |
| ) |
| text = Text("Hello Manim", font="Hack") |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("STHupo字体:", t2f={"字体": "STHupo"}, font_size=25).shift(left) |
| text = Text("Hello Manim", font="STHupo") |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("STZhongsong字体:", t2f={"字体": "STZhongsong"}, font_size=25).shift( |
| LEFT * 2 + DOWN |
| ) |
| text = Text("Hello Manim", font="STZhongsong") |
| text.next_to(label, direction=RIGHT, buff=0.2) |
| self.add(label) |
| self.play(Write(text)) |
| |
| def example02(self): |
| """ |
| 显示样式相关设置 |
| """ |
| left = LEFT * 2.5 |
| title = Text("设置文本的样式:", font="STLiti", font_size=25).shift( |
| LEFT * 2 + UP * 2 |
| ) |
| self.play(Write(title)) |
| |
| label = Text("粗体:", font="STLiti", font_size=25).shift(left + UP) |
| text = Text("Hello Manim", weight=BOLD) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("斜体:", font="STLiti", font_size=25).shift(left) |
| text = Text("Hello Manim", slant=ITALIC) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("行间距:", font="STLiti", font_size=25).shift(left + DOWN * 1.5) |
| text = Text("row1-Manim\nrow2-Manim", line_spacing=2) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| def example03(self): |
| """ |
| 颜色相关设置 |
| """ |
| left = LEFT * 2.5 |
| title = Text("设置文本颜色:", font="STLiti", font_size=25).shift( |
| LEFT * 2 + UP * 2 |
| ) |
| self.play(Write(title)) |
| |
| label = Text("红色:", font="STLiti", font_size=25).shift(left + UP) |
| text = Text("Hello Manim", color=RED) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("黄色:", font="STLiti", font_size=25).shift(left) |
| text = Text("Hello Manim", color=YELLOW) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("红变蓝:", font="STLiti", font_size=25).shift(left + DOWN) |
| text = Text("Hello Manim1", gradient=(RED, BLUE)) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("蓝变绿:", font="STLiti", font_size=25).shift(left + DOWN * 2) |
| text = Text("Hello Manim2", gradient=(BLUE, GREEN)) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| def example04(self): |
| """ |
| t2* 局部设置系列 |
| """ |
| left = LEFT * 2.5 |
| title = Text("设置文本局部:", font="STLiti", font_size=25).shift( |
| LEFT * 2 + UP * 2 |
| ) |
| self.play(Write(title)) |
| |
| label = Text("两种字体:", font="STLiti", font_size=25).shift(left + UP) |
| text = Text("Hello Manim", t2f={"Hello": "STLiti", "Manim": "Harrington"}) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("粗体斜体:", font="STLiti", font_size=25).shift(left) |
| text = Text("Hello Manim", t2s={"Hello": ITALIC}, t2w={"Manim": BOLD}) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("两种颜色:", font="STLiti", font_size=25).shift(left + DOWN) |
| text = Text("Hello Manim", t2c={"Hello": RED, "Manim": GREEN}) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
| |
| label = Text("两种渐变:", font="STLiti", font_size=25).shift(left + DOWN * 2) |
| text = Text("Hello1 Manim2", t2g={"ello1": (RED, BLUE), "anim2": (BLUE, GREEN)}) |
| text.next_to(label, direction=RIGHT, buff=0.5) |
| self.add(label) |
| self.play(Write(text)) |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~