Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added `cap_style` feature to `VMobject` (#3516)

Benjamín Ubilla03f9d4b90bfe
diff --git a/manim/camera/camera.py b/manim/camera/camera.py
index ea42a5c08..1844b4086 100644
--- a/manim/camera/camera.py
+++ b/manim/camera/camera.py
@@ -37,6 +37,14 @@ LINE_JOIN_MAP = {
}
+CAP_STYLE_MAP = {
+ CapStyleType.AUTO: None, # TODO: this could be improved
+ CapStyleType.ROUND: cairo.LineCap.ROUND,
+ CapStyleType.BUTT: cairo.LineCap.BUTT,
+ CapStyleType.SQUARE: cairo.LineCap.SQUARE,
+}
+
+
class Camera:
"""Base camera class.
@@ -778,11 +786,13 @@ class Camera:
ctx.set_line_width(
width
* self.cairo_line_width_multiple
- # This ensures lines have constant width as you zoom in on them.
* (self.frame_width / self.frame_width),
+ # This ensures lines have constant width as you zoom in on them.
)
if vmobject.joint_type != LineJointType.AUTO:
ctx.set_line_join(LINE_JOIN_MAP[vmobject.joint_type])
+ if vmobject.cap_style != CapStyleType.AUTO:
+ ctx.set_line_cap(CAP_STYLE_MAP[vmobject.cap_style])
ctx.stroke_preserve()
return self
diff --git a/manim/constants.py b/manim/constants.py
index 065a10fcf..30a20e050 100644
--- a/manim/constants.py
+++ b/manim/constants.py
@@ -76,6 +76,7 @@ __all__ = [
"CTRL_VALUE",
"RendererType",
"LineJointType",
+ "CapStyleType",
]
# Messages
@@ -305,3 +306,41 @@ class LineJointType(Enum):
ROUND = 1
BEVEL = 2
MITER = 3
+
+
+class CapStyleType(Enum):
+ """Collection of available cap styles.
+
+ See the example below for a visual illustration of the different
+ cap styles.
+
+ Examples
+ --------
+
+ .. manim:: CapStyleVariants
+ :save_last_frame:
+
+ class CapStyleVariants(Scene):
+ def construct(self):
+ arcs = VGroup(*[
+ Arc(
+ radius=1,
+ start_angle=0,
+ angle=TAU / 4,
+ stroke_width=20,
+ color=GREEN,
+ cap_style=cap_style,
+ )
+ for cap_style in CapStyleType
+ ])
+ arcs.arrange(RIGHT, buff=1)
+ self.add(arcs)
+ for arc in arcs:
+ label = Text(arc.cap_style.name, font_size=24).next_to(arc, DOWN)
+ self.add(label)
+ """
+
+ AUTO = 0
+ ROUND = 1
+ BUTT = 2
+ SQUARE = 3
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index 6048fe4c6..9e8c97358 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -123,6 +123,7 @@ class VMobject(Mobject):
# TODO, do we care about accounting for varying zoom levels?
tolerance_for_point_equality: float = 1e-6,
n_points_per_cubic_curve: int = 4,
+ cap_style: CapStyleType = CapStyleType.AUTO,
**kwargs,
):
self.fill_opacity = fill_opacity
@@ -150,6 +151,7 @@ class VMobject(Mobject):
self.shade_in_3d: bool = shade_in_3d
self.tolerance_for_point_equality: float = tolerance_for_point_equality
self.n_points_per_cubic_curve: int = n_points_per_cubic_curve
+ self.cap_style: CapStyleType = cap_style
super().__init__(**kwargs)
self.submobjects: list[VMobject]
@@ -340,6 +342,34 @@ class VMobject(Mobject):
self.background_stroke_color = ManimColor(color)
return self
+ def set_cap_style(self, cap_style: CapStyleType) -> Self:
+ """
+ Sets the cap style of the :class:`VMobject`.
+
+ Parameters
+ ----------
+ cap_style
+ The cap style to be set. See :class:`.CapStyleType` for options.
+
+ Returns
+ -------
+ :class:`VMobject`
+ ``self``
+
+ Examples
+ --------
+ .. manim:: CapStyleExample
+ :save_last_frame:
+
+ class CapStyleExample(Scene):
+ def construct(self):
+ line = Line(LEFT, RIGHT, color=YELLOW, stroke_width=20)
+ line.set_cap_style(CapStyleType.ROUND)
+ self.add(line)
+ """
+ self.cap_style = cap_style
+ return self
+
def set_background_stroke(self, **kwargs) -> Self:
kwargs["background"] = True
self.set_stroke(**kwargs)
@@ -2458,7 +2488,7 @@ class CurvesAsSubmobjects(VGroup):
if len(self.submobjects) == 0:
caller_name = sys._getframe(1).f_code.co_name
raise Exception(
- f"Cannot call CurvesAsSubmobjects.{caller_name} for a CurvesAsSubmobject with no submobjects"
+ f"Cannot call CurvesAsSubmobjects. {caller_name} for a CurvesAsSubmobject with no submobjects"
)
def _get_submobjects_with_points(self):
@@ -2468,7 +2498,7 @@ class CurvesAsSubmobjects(VGroup):
if len(submobjs_with_pts) == 0:
caller_name = sys._getframe(1).f_code.co_name
raise Exception(
- f"Cannot call CurvesAsSubmobjects.{caller_name} for a CurvesAsSubmobject whose submobjects have no points"
+ f"Cannot call CurvesAsSubmobjects. {caller_name} for a CurvesAsSubmobject whose submobjects have no points"
)
return submobjs_with_pts
diff --git a/tests/test_graphical_units/control_data/mobjects/vmobject_cap_styles.npz b/tests/test_graphical_units/control_data/mobjects/vmobject_cap_styles.npz
new file mode 100644
index 000000000..c7ca1b2dc
Binary files /dev/null and b/tests/test_graphical_units/control_data/mobjects/vmobject_cap_styles.npz differ
diff --git a/tests/test_graphical_units/test_mobjects.py b/tests/test_graphical_units/test_mobjects.py
index 21ea20437..88e30cacf 100644
--- a/tests/test_graphical_units/test_mobjects.py
+++ b/tests/test_graphical_units/test_mobjects.py
@@ -53,3 +53,22 @@ def test_vmobject_joint_types(scene):
lines.arrange(RIGHT, buff=1)
scene.add(lines)
+
+
+@frames_comparison
+def test_vmobject_cap_styles(scene):
+ arcs = VGroup(
+ *[
+ Arc(
+ radius=1,
+ start_angle=0,
+ angle=TAU / 4,
+ stroke_width=20,
+ color=GREEN,
+ cap_style=cap_style,
+ )
+ for cap_style in CapStyleType
+ ]
+ )
+ arcs.arrange(RIGHT, buff=1)
+ scene.add(arcs)