I wan't to create a little animation using Manim. There is a rectangle, where two lines will be drawn (on the left and bottom side of the rectangle) and a new rectangle should "grow" from the bottom. Using GrowFromEdge(element, DOWN)
the rectangle's width get also changed, but only the height should be changed. What do I do? Using height=0
and then ApplyMethod(element.set_height, HEIGHT)
doesn't show anything. Here's my code:
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)
Please be clearer when sharing your code, we understand that you code your way, but if you need help with something you should make your code as clear as possible to facilitate the task of helping you.
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)