Skip to content

Commit 7e95972

Browse files
committed
Add miter_limit parameter and set_line_dash method
1 parent bb1eed0 commit 7e95972

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

examples/lines.ipynb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,47 @@
140140
"\n",
141141
"canvas"
142142
]
143+
},
144+
{
145+
"cell_type": "markdown",
146+
"metadata": {},
147+
"source": [
148+
"# Line dash\n",
149+
"### Sets the current line dash pattern."
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"size = (400, 280)\n",
159+
"\n",
160+
"canvas = Canvas(size=size)\n",
161+
"canvas.scale(2)\n",
162+
"\n",
163+
"line_dashes = [\n",
164+
" [5, 10],\n",
165+
" [10, 5],\n",
166+
" [5, 10, 20],\n",
167+
" [10, 20],\n",
168+
" [20, 10],\n",
169+
" [20, 20]\n",
170+
"]\n",
171+
"\n",
172+
"canvas.line_width = 2\n",
173+
"\n",
174+
"for i in range(len(line_dashes)):\n",
175+
" x = 5 + i * 20\n",
176+
"\n",
177+
" canvas.set_line_dash(line_dashes[i])\n",
178+
" canvas.begin_path()\n",
179+
" canvas.move_to(x, 0)\n",
180+
" canvas.line_to(x, 140)\n",
181+
" canvas.stroke()\n",
182+
"canvas"
183+
]
143184
}
144185
],
145186
"metadata": {

ipycanvas/canvas.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ class Canvas(DOMWidget):
8585
#: Default to ``'miter'``
8686
line_join = Enum(['round', 'bevel', 'miter'], default_value='miter')
8787

88+
#: (float) Establishes a limit on the miter when two lines join at a sharp angle, to let you control how thick
89+
#: the junction becomes. Default to ``10.``.
90+
miter_limit = Float(10.)
91+
92+
_line_dash = List()
93+
8894
def __init__(self, *args, **kwargs):
8995
"""Create a Canvas widget."""
9096
#: Whether commands should be cached or not
@@ -188,6 +194,19 @@ def stroke_text(self, text, x, y, max_width=None):
188194
"""Stroke a given text at the given ``(x, y)`` position. Optionally with a maximum width to draw."""
189195
self._send_canvas_command('strokeText', (text, x, y, max_width))
190196

197+
# Line methods
198+
def get_line_dash(self):
199+
"""Return the current line dash pattern array containing an even number of non-negative numbers."""
200+
return self._line_dash
201+
202+
def set_line_dash(self, segments):
203+
"""Set the current line dash pattern."""
204+
if len(segments) % 2:
205+
self._line_dash = segments + segments
206+
else:
207+
self._line_dash = segments
208+
self._send_canvas_command('setLineDash', (self._line_dash, ))
209+
191210
# Image methods
192211
def put_image_data(self, image_data, dx, dy):
193212
"""Draw an image on the Canvas.
@@ -280,7 +299,7 @@ def flush(self):
280299

281300
@observe('fill_style', 'stroke_style', 'global_alpha', 'font', 'text_align',
282301
'text_baseline', 'direction', 'global_composite_operation',
283-
'line_width', 'line_cap', 'line_join')
302+
'line_width', 'line_cap', 'line_join', 'miter_limit')
284303
def _on_set_attr(self, change):
285304
command = {
286305
'name': 'set',

0 commit comments

Comments
 (0)