如何在 Manim Community 中将文本放入矩形内

185*_* sa 6 python animation python-3.x algorithm-animation manim

这就是我想做的事情

我对 manim 很陌生,我正在尝试将文本放入图像中给出的矩形内,我该怎么做?:(

小智 9

您可以使用VGroup将框和文本分组在一起。

示例代码:

from manimlib import *

def create_textbox(color, string):
    result = VGroup() # create a VGroup
    box = Rectangle(  # create a box
        height=2, width=3, fill_color=color, 
        fill_opacity=0.5, stroke_color=color
    )
    text = Text(string).move_to(box.get_center()) # create text
    result.add(box, text) # add both objects to the VGroup
    return result


class TextBox(Scene):  
    def construct(self):

        # create text box
        textbox = create_textbox(color=BLUE, string="Hello world")
        self.add(textbox)

        # move text box around
        self.play(textbox.animate.shift(2*RIGHT), run_time=3)
        self.play(textbox.animate.shift(2*UP), run_time=3)
        self.wait()
Run Code Online (Sandbox Code Playgroud)