Skip to content

Commit 940f5e9

Browse files
committed
Use alpha=1 as default for batch commands
1 parent ea23937 commit 940f5e9

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

docs/source/drawing_shapes.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ Circles
357357
with hold_canvas(canvas):
358358
canvas.fill_styled_circles(x, y, r, color=colors_fill, alpha=alphas)
359359
canvas.line_width = 2
360-
canvas.stroke_styled_circles(x, y, r, color=colors_outline, alpha=1)
360+
canvas.stroke_styled_circles(x, y, r, color=colors_outline)
361361
canvas
362362
363363
@@ -441,7 +441,7 @@ Case 1: All polygons / line-segments have the same number of points
441441
442442
points_per_polygon = np.ones([n_polygons]) * n_points_per_polygon
443443
with hold_canvas(canvas):
444-
canvas.stroke_styled_polygons(polygons, color=colors_fill, alpha=1)
444+
canvas.stroke_styled_polygons(polygons, color=colors_fill)
445445
canvas
446446
447447
@@ -474,7 +474,7 @@ Case 1: All polygons / line-segments have the same number of points
474474
num=n_line_segments)[:, None]
475475
476476
with hold_canvas(canvas):
477-
canvas.stroke_styled_line_segments(line_segments, color=colors_fill, alpha=1)
477+
canvas.stroke_styled_line_segments(line_segments, color=colors_fill)
478478
canvas
479479
480480
@@ -501,7 +501,7 @@ Polygons can be given as a list of ndarrays:
501501
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
502502
503503
with hold_canvas(canvas):
504-
canvas.fill_styled_polygons(polygons, color=colors, alpha=1)
504+
canvas.fill_styled_polygons(polygons, color=colors)
505505
canvas
506506
507507
.. image:: images/draw_styled_polygons_from_list.png

examples/3d_monkey.ipynb

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -317,24 +317,28 @@
317317
" self.draw()\n",
318318
"\n",
319319
" def draw(self):\n",
320-
" with hold_canvas(self):\n",
321-
" self.clear()\n",
320+
" triangles = []\n",
321+
" colors = []\n",
322+
"\n",
323+
" # Now let's draw triangles\n",
324+
" for i in self.order:\n",
325+
" triangle = np.array(self.proj_triangles[i])\n",
326+
" # print(triangle)\n",
327+
" normal = self.triangles_normals[i]\n",
322328
"\n",
323-
" # Now let's draw triangles\n",
324-
" for i in self.order:\n",
325-
" triangle = self.proj_triangles[i]\n",
326-
" normal = self.triangles_normals[i]\n",
329+
" # Shading depending on light direction and face normal\n",
330+
" light = np.dot(light_direction, normal)\n",
331+
" if light < 0.4:\n",
332+
" light = 0.4\n",
333+
" elif light > 1:\n",
334+
" light = 1\n",
327335
"\n",
328-
" # Shading depending on light direction and face normal\n",
329-
" light = np.dot(light_direction, normal)\n",
330-
" if light < 0.4:\n",
331-
" light = 0.4\n",
332-
" elif light > 1:\n",
333-
" light = 1\n",
334-
" r, g, b = int(214 * light), int(224 * light), int(125 * light)\n",
335-
" self.fill_style = \"rgb({}, {}, {})\".format(r, g, b)\n",
336+
" triangles.append(triangle)\n",
337+
" colors.append([int(214 * light), int(224 * light), int(125 * light)])\n",
336338
"\n",
337-
" self.fill_polygon(triangle)\n",
339+
" with hold_canvas(self):\n",
340+
" self.clear()\n",
341+
" self.fill_styled_polygons(triangles, colors)\n",
338342
"\n",
339343
" def mouse_down_handler(self, pixel_x, pixel_y):\n",
340344
" self.dragging = True\n",
@@ -370,11 +374,18 @@
370374
"monkey = Monkey()\n",
371375
"monkey"
372376
]
377+
},
378+
{
379+
"cell_type": "code",
380+
"execution_count": null,
381+
"metadata": {},
382+
"outputs": [],
383+
"source": []
373384
}
374385
],
375386
"metadata": {
376387
"kernelspec": {
377-
"display_name": "Python 3",
388+
"display_name": "Python 3 (ipykernel)",
378389
"language": "python",
379390
"name": "python3"
380391
},
@@ -388,7 +399,7 @@
388399
"name": "python",
389400
"nbconvert_exporter": "python",
390401
"pygments_lexer": "ipython3",
391-
"version": "3.9.0"
402+
"version": "3.10.0"
392403
}
393404
},
394405
"nbformat": 4,

examples/animation.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
],
222222
"metadata": {
223223
"kernelspec": {
224-
"display_name": "Python 3",
224+
"display_name": "Python 3 (ipykernel)",
225225
"language": "python",
226226
"name": "python3"
227227
},
@@ -235,7 +235,7 @@
235235
"name": "python",
236236
"nbconvert_exporter": "python",
237237
"pygments_lexer": "ipython3",
238-
"version": "3.9.0"
238+
"version": "3.10.0"
239239
}
240240
},
241241
"nbformat": 4,

examples/batch_drawing.ipynb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
"with hold_canvas(canvas):\n",
149149
" canvas.fill_styled_circles(x, y, r, color=colors_fill, alpha=alphas)\n",
150150
" canvas.line_width = 2\n",
151-
" canvas.stroke_styled_circles(x, y, r, color=colors_outline, alpha=1)\n",
151+
" canvas.stroke_styled_circles(x, y, r, color=colors_outline)\n",
152152
"canvas"
153153
]
154154
},
@@ -216,11 +216,9 @@
216216
"colors_outline = np.random.randint(0, 255, size=(n_circles, 3))\n",
217217
"alphas = np.random.random(n_circles)\n",
218218
"with hold_canvas(canvas):\n",
219-
" canvas.fill_styled_arcs(x, y, r, start_angle, end_angle, color=colors_fill, alpha=1)\n",
219+
" canvas.fill_styled_arcs(x, y, r, start_angle, end_angle, color=colors_fill)\n",
220220
" canvas.line_width = 3\n",
221-
" canvas.stroke_styled_arcs(\n",
222-
" x, y, r, start_angle, end_angle, color=colors_outline, alpha=1\n",
223-
" )\n",
221+
" canvas.stroke_styled_arcs(x, y, r, start_angle, end_angle, color=colors_outline)\n",
224222
"canvas"
225223
]
226224
},
@@ -273,7 +271,7 @@
273271
"\n",
274272
"points_per_polygon = np.ones([n_polygons]) * n_points_per_polygon\n",
275273
"with hold_canvas(canvas):\n",
276-
" canvas.stroke_styled_polygons(polygons, color=colors_fill, alpha=1)\n",
274+
" canvas.stroke_styled_polygons(polygons, color=colors_fill)\n",
277275
"canvas"
278276
]
279277
},
@@ -304,7 +302,7 @@
304302
"line_segments[:, :, 1] += np.linspace(1.0, canvas.height, num=n_line_segments)[:, None]\n",
305303
"\n",
306304
"with hold_canvas(canvas):\n",
307-
" canvas.stroke_styled_line_segments(line_segments, color=colors_fill, alpha=1)\n",
305+
" canvas.stroke_styled_line_segments(line_segments, color=colors_fill)\n",
308306
"canvas"
309307
]
310308
},
@@ -337,7 +335,7 @@
337335
"colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]\n",
338336
"\n",
339337
"with hold_canvas(canvas):\n",
340-
" canvas.fill_styled_polygons(polygons, color=colors, alpha=1)\n",
338+
" canvas.fill_styled_polygons(polygons, color=colors)\n",
341339
"canvas"
342340
]
343341
},
@@ -398,7 +396,7 @@
398396
"name": "python",
399397
"nbconvert_exporter": "python",
400398
"pygments_lexer": "ipython3",
401-
"version": "3.9.7"
399+
"version": "3.10.0"
402400
}
403401
},
404402
"nbformat": 4,

ipycanvas/canvas.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ def stroke_rects(self, x, y, width, height=None):
519519

520520
self._send_canvas_command(COMMANDS['strokeRects'], args, buffers)
521521

522-
def fill_styled_rects(self, x, y, width, height, color, alpha):
522+
def fill_styled_rects(self, x, y, width, height, color, alpha=1):
523523
"""Draw filled and styled rectangles of sizes ``(width, height)`` at the ``(x, y)`` positions
524524
525525
Where ``x``, ``y``, ``width`` and ``height`` arguments are NumPy arrays, lists or scalar values.
@@ -543,7 +543,7 @@ def fill_styled_rects(self, x, y, width, height, color, alpha):
543543
populate_args(alpha, args, buffers)
544544
self._send_canvas_command(COMMANDS['fillStyledRects'], args, buffers)
545545

546-
def stroke_styled_rects(self, x, y, width, height, color, alpha):
546+
def stroke_styled_rects(self, x, y, width, height, color, alpha=1):
547547
"""Draw rectangular styled outlines of sizes ``(width, height)`` at the ``(x, y)`` positions.of sizes ``(width, height)``
548548
549549
Where ``x``, ``y``, ``width`` and ``height`` arguments are NumPy arrays, lists or scalar values.
@@ -653,7 +653,7 @@ def stroke_circles(self, x, y, radius):
653653

654654
self._send_canvas_command(COMMANDS['strokeCircles'], args, buffers)
655655

656-
def fill_styled_circles(self, x, y, radius, color, alpha):
656+
def fill_styled_circles(self, x, y, radius, color, alpha=1):
657657
"""Draw a filled circles centered at ``(x, y)`` with a radius of ``radius``.
658658
659659
Where ``x``, ``y``, ``radius`` and ``alpha` are NumPy arrays, lists or scalar values.
@@ -669,7 +669,7 @@ def fill_styled_circles(self, x, y, radius, color, alpha):
669669
populate_args(alpha, args, buffers)
670670
self._send_canvas_command(COMMANDS['fillStyledCircles'], args, buffers)
671671

672-
def stroke_styled_circles(self, x, y, radius, color, alpha):
672+
def stroke_styled_circles(self, x, y, radius, color, alpha=1):
673673
"""Draw filled circles centered at ``(x, y)`` with a radius of ``radius``.
674674
675675
Where ``x``, ``y``, ``radius`` and ``alpha`` are NumPy arrays, lists or scalar values.
@@ -685,7 +685,7 @@ def stroke_styled_circles(self, x, y, radius, color, alpha):
685685
populate_args(alpha, args, buffers)
686686
self._send_canvas_command(COMMANDS['strokeStyledCircles'], args, buffers)
687687

688-
def fill_styled_arcs(self, x, y, radius, start_angle, end_angle, color, alpha, anticlockwise=False):
688+
def fill_styled_arcs(self, x, y, radius, start_angle, end_angle, color, alpha=1, anticlockwise=False):
689689
"""Draw filled and styled arcs centered at ``(x, y)`` with a radius of ``radius``.
690690
691691
Where ``x``, ``y``, ``radius`` and other arguments are NumPy arrays, lists or scalar values.
@@ -704,7 +704,7 @@ def fill_styled_arcs(self, x, y, radius, start_angle, end_angle, color, alpha, a
704704

705705
self._send_canvas_command(COMMANDS['fillStyledArcs'], args, buffers)
706706

707-
def stroke_styled_arcs(self, x, y, radius, start_angle, end_angle, color, alpha, anticlockwise=False):
707+
def stroke_styled_arcs(self, x, y, radius, start_angle, end_angle, color, alpha=1, anticlockwise=False):
708708
"""Draw an styled arc outlines centered at ``(x, y)`` with a radius of ``radius``.
709709
710710
Where ``x``, ``y``, ``radius`` and other arguments are NumPy arrays, lists or scalar values.
@@ -791,7 +791,7 @@ def stroke_polygons(self, points, points_per_polygon=None):
791791
"""
792792
self._draw_polygons_or_linesegments('strokePolygons', points, None, None, points_per_polygon, False, 3, "polygon")
793793

794-
def fill_styled_polygons(self, points, color, alpha, points_per_polygon=None):
794+
def fill_styled_polygons(self, points, color, alpha=1, points_per_polygon=None):
795795
"""" Draw many filled polygons at once:
796796
797797
Args:
@@ -821,7 +821,7 @@ def fill_styled_polygons(self, points, color, alpha, points_per_polygon=None):
821821
"""
822822
self._draw_polygons_or_linesegments('fillStyledPolygons', points, color, alpha, points_per_polygon, True, 3, "polygon")
823823

824-
def stroke_styled_polygons(self, points, color, alpha, points_per_polygon=None):
824+
def stroke_styled_polygons(self, points, color, alpha=1, points_per_polygon=None):
825825
"""" Draw many stroked polygons at once:
826826
827827
Args:
@@ -865,7 +865,7 @@ def stroke_lines(self, points):
865865

866866
self._send_canvas_command(COMMANDS['strokeLines'], args, buffers)
867867

868-
def stroke_styled_line_segments(self, points, color, alpha, points_per_line_segment=None):
868+
def stroke_styled_line_segments(self, points, color, alpha=1, points_per_line_segment=None):
869869
""" Draw many line segments at once:
870870
871871
Args:

0 commit comments

Comments
 (0)