Manim 从边缘增长,宽度保持不变

Manim Grow from edge with width staying the same

我不想使用 Manim 创建小动画。有一个矩形,将在其中绘制两条线(在矩形的左侧和底部),一个新矩形应该从底部开始 "grow"。使用 GrowFromEdge(element, DOWN) 矩形的宽度也会改变,但只有高度应该改变。我该怎么办?使用 height=0 然后 ApplyMethod(element.set_height, HEIGHT) 不显示任何内容。这是我的代码:

from manimlib.imports import *
from manimlib.constants import COLOR_MAP
import numpy as np


class Test(Scene):
    def construct(self):
        EXPLAIN_WIDTH = 5
        EXPLAIN_HEIGHT = 2
        explain_rect = Rectangle(
            width=EXPLAIN_WIDTH,
            height=EXPLAIN_HEIGHT,
            origin=np.array(
                    [0,
                     0,
                     0]
            )
        ).set_stroke(width=1)
        explain_line_left = Line(
            start=np.array(
                    [EXPLAIN_WIDTH / -2,
                     EXPLAIN_HEIGHT / -2,
                     0]
            ),
            end=np.array(
                    [EXPLAIN_WIDTH / -2,
                     EXPLAIN_HEIGHT / 2,
                     0]
            ),
        ).set_color(COLOR_MAP["RED_A"])
        explain_line_bottom = Line(
            start=np.array(
                    [EXPLAIN_WIDTH / -2,
                     EXPLAIN_HEIGHT / -2,
                     0]
            ),
            end=np.array(
                    [EXPLAIN_WIDTH / 2,
                     EXPLAIN_HEIGHT / -2,
                     0]
            ),
            color=COLOR_MAP["RED_A"]
        )
        explain_filled_rect = Rectangle(
            width=EXPLAIN_WIDTH,
            height=EXPLAIN_HEIGHT,
            color=None
        ).set_fill(COLOR_MAP["RED_A"], 1)

        self.play(FadeIn(explain_rect))
        self.play(
                GrowFromEdge(explain_line_left, BOTTOM),
                GrowFromEdge(explain_line_bottom, LEFT)
        )

        self.wait(1)

        self.play(GrowFromEdge(explain_filled_rect, BOTTOM))

        self.wait(2)

分享您的代码时请更清楚,我们知道您按自己的方式编写代码,但如果您需要帮助某些事情,您应该让您的代码尽可能清晰,以便于帮助您完成任务。

class Test2(Scene):
    def construct(self):
        EXPLAIN_WIDTH = 5
        EXPLAIN_HEIGHT = 2
        explain_rect = Rectangle(
                                    width=EXPLAIN_WIDTH,
                                    height=EXPLAIN_HEIGHT,
                                    origin=ORIGIN,
                                    stroke_width=1
                                )
        explain_filled_rect = explain_rect.copy()
        explain_filled_rect.set_fill(RED_A,1)
        # Use stretch=True to preserve the dimension that is not modified
        explain_filled_rect.set_height(1,stretch=True)
        # compress the definitions of your objects, this will make it easier to read them.
        explain_line_left, explain_line_bottom = [
            Line(
                    explain_rect.get_corner(start),
                    explain_rect.get_corner(end),
                    color=RED_A
                )
            for start,end in [(DL,UL),(DL,DR)] 
        ]

        self.play(FadeIn(explain_rect))
        self.play(
                GrowFromEdge(explain_line_left, BOTTOM),
                GrowFromEdge(explain_line_bottom, LEFT)
        )

        self.wait(1)
        self.add(explain_filled_rect)
        # This generates a copy of the element in an 
        # attribute called "target" to which we 
        # can indicate when we want.
        explain_filled_rect.generate_target()

        def update_test(mob,alpha):
            # 1. Reset the rectangle to its flat state
            mob.become(mob.target)
            # 2. Set the new height
            mob.set_height(alpha*explain_rect.get_height(),stretch=True)
            # 3. Move to the new place
            mob.next_to(explain_rect.get_bottom(),UP,buff=0)

        self.play(UpdateFromAlphaFunc(
                    explain_filled_rect,
                    update_test
                 ),
        )

        self.wait(2)

See the result