diff --git a/manim/mobject/geometry/shape_matchers.py b/manim/mobject/geometry/shape_matchers.py index 892f912599..dd696881fa 100644 --- a/manim/mobject/geometry/shape_matchers.py +++ b/manim/mobject/geometry/shape_matchers.py @@ -53,7 +53,7 @@ def __init__( self, *mobjects: Mobject, color: ParsableManimColor = YELLOW, - buff: float = SMALL_BUFF, + buff: float | tuple[float, float] = SMALL_BUFF, corner_radius: float = 0.0, **kwargs: Any, ) -> None: @@ -64,11 +64,17 @@ def __init__( "Expected all inputs for parameter mobjects to be a Mobjects" ) + if isinstance(buff, tuple): + buff_x = buff[0] + buff_y = buff[1] + else: + buff_x = buff_y = buff + group = Group(*mobjects) super().__init__( color=color, - width=group.width + 2 * buff, - height=group.height + 2 * buff, + width=group.width + 2 * buff_x, + height=group.height + 2 * buff_y, corner_radius=corner_radius, **kwargs, ) @@ -108,7 +114,7 @@ def __init__( stroke_width: float = 0, stroke_opacity: float = 0, fill_opacity: float = 0.75, - buff: float = 0, + buff: float | tuple[float, float] = 0, **kwargs: Any, ) -> None: if color is None: diff --git a/tests/module/mobject/geometry/test_unit_geometry.py b/tests/module/mobject/geometry/test_unit_geometry.py index 210af0fdf5..a7ff5e2c48 100644 --- a/tests/module/mobject/geometry/test_unit_geometry.py +++ b/tests/module/mobject/geometry/test_unit_geometry.py @@ -116,6 +116,17 @@ def test_SurroundingRectangle(): assert sr.get_fill_opacity() == 0.42 +def test_SurroundingRectangle_buff(): + sq = Square() + rect1 = SurroundingRectangle(sq, buff=1) + assert rect1.width == sq.width + 2 + assert rect1.height == sq.height + 2 + + rect2 = SurroundingRectangle(sq, buff=(1, 2)) + assert rect2.width == sq.width + 2 + assert rect2.height == sq.height + 4 + + def test_BackgroundRectangle(manim_caplog): circle = Circle() square = Square()