Skip to content

Commit 4ed8bfc

Browse files
Use enums with CapStyle
1 parent 20f131b commit 4ed8bfc

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

matplotlib_pyodide/html5_canvas_backend.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88
from matplotlib import __version__, figure, interactive
9+
from matplotlib._enums import CapStyle
910
from matplotlib.backend_bases import (
1011
FigureManagerBase,
1112
GraphicsContextBase,
@@ -146,12 +147,31 @@ def restore(self):
146147
self.renderer.ctx.restore()
147148

148149
def set_capstyle(self, cs):
150+
"""
151+
Set the cap style for lines in the graphics context.
152+
153+
Parameters
154+
----------
155+
cs : CapStyle or str
156+
The cap style to use. Can be a CapStyle enum value or a string
157+
that can be converted to a CapStyle.
158+
"""
159+
if isinstance(cs, str):
160+
cs = CapStyle(cs)
161+
162+
# Convert the JoinStyle enum to its name if needed
163+
if hasattr(cs, "name"):
164+
cs = cs.name.lower()
165+
149166
if cs in ["butt", "round", "projecting"]:
150167
self._capstyle = cs
151168
self.renderer.ctx.lineCap = _capstyle_d[cs]
152169
else:
153170
raise ValueError(f"Unrecognized cap style. Found {cs}")
154171

172+
def get_capstyle(self):
173+
return self._capstyle
174+
155175
def set_clip_rectangle(self, rectangle):
156176
self.renderer.ctx.save()
157177
if not rectangle:
@@ -297,8 +317,12 @@ def _set_style(self, gc, rgbFace=None):
297317
rgbFace, gc.get_alpha(), gc.get_forced_alpha()
298318
)
299319

300-
if gc.get_capstyle():
301-
self.ctx.lineCap = _capstyle_d[gc.get_capstyle()]
320+
capstyle = gc.get_capstyle()
321+
if capstyle:
322+
# Get the string name if it's an enum
323+
if hasattr(capstyle, "name"):
324+
capstyle = capstyle.name.lower()
325+
self.ctx.lineCap = _capstyle_d[capstyle]
302326

303327
self.ctx.strokeStyle = self._matplotlib_color_to_CSS(
304328
gc.get_rgb(), gc.get_alpha(), gc.get_forced_alpha()

0 commit comments

Comments
 (0)