Manim 默认的画布底色是纯黑,深色元素压在黑底上很容易糊成一片。改底色有两条路:渲染前用 config.background_color 定全局,或在 construct() 里改 self.camera.background_color 做动态切换。这段代码把四种底色排成卡片,再逐一切换画布底色。

完整代码

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
from manim import *
class BackgroundColorDemo(Scene):
"""background_color:画布底色怎么改,以及它对整帧的影响"""
def construct(self):
title = Text("background_color 控制画布底色", font_size=34).to_edge(UP, buff=0.5)
self.play(Write(title))
cards_spec = [("#0b1e3a", "深蓝", False), ("#1b1b2f", "暗紫", False),
("#123a2b", "墨绿", False), ("#f2f2f2", "浅灰", True)]
cards = VGroup()
for hexs, name, light in cards_spec:
box = RoundedRectangle(width=2.2, height=1.3, corner_radius=0.15,
fill_color=hexs, fill_opacity=1, stroke_color=GREY)
label = Text(name, font_size=24, color=BLACK if light else WHITE)
label.move_to(box.get_center())
cards.add(VGroup(box, label))
cards.arrange(RIGHT, buff=0.45).shift(DOWN * 0.3)
self.play(LaggedStart(*[FadeIn(c, shift=UP * 0.3) for c in cards], lag_ratio=0.2))
self.wait(0.3)
for hexs, _, light in cards_spec[:3]:
self.camera.background_color = hexs
self.wait(0.75)
self.camera.background_color = BLACK
self.wait(0.3)
note = Text("底色一换,白色描边元素可能直接看不见", font_size=22, color=YELLOW)
note.next_to(cards, DOWN, buff=0.5)
self.play(FadeIn(note, shift=UP * 0.15))
self.wait(1.2)
self.wait(0.40)

演示视频

下载 Python 脚本

要点

  • config.background_color = "#0b1e3a" 影响整段渲染;写进 manim.cfg 的 [CLI] 段就成项目默认
  • self.camera.background_color 可以在场景里随时改,适合做过场或分节换色
  • 换浅色底后白色描边和默认白色文字会直接消失,要同步把元素配色改成深色

代码来源:Manim Community 官方文档 Guides / Configuration(MIT),示例代码按 Manim CE 0.20.1 编写并实跑验证。