1

我不想使用 Manim 创建一个小动画。有一个矩形,将在其中绘制两条线(在矩形的左侧和底部),并且一个新的矩形应该从底部“生长”。使用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)
4

1 回答 1

3

请在分享您的代码时更加清晰,我们理解您按照自己的方式编写代码,但如果您需要帮助,您应该尽可能清晰地说明您的代码,以便帮助您完成任务。

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)

查看结果

于 2020-01-29T00:08:01.577 回答