Skip to content

Commit db71559

Browse files
authored
Merge pull request #263 from martinRenou/canvas_manager
Fix sync issues between canvases
2 parents 55ee2e6 + 2f339fd commit db71559

31 files changed

+607
-451
lines changed

docs/source/animation.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"from time import sleep\n",
8787
"\n",
8888
"for i in range(200):\n",
89-
" with hold_canvas(canvas):\n",
89+
" with hold_canvas():\n",
9090
" canvas.clear()\n",
9191
" canvas.fill_style = \"white\"\n",
9292
" canvas.fill_rect(0, 0, size, size)\n",

docs/source/animations.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ A simple animation loop will look like the following:
2525
steps_number = 200
2626
2727
for i in range(steps_number):
28-
with hold_canvas(canvas):
28+
with hold_canvas():
2929
# Clear the old animation step
3030
canvas.clear()
3131
@@ -49,7 +49,7 @@ You can also make an infinite animation using a ``while`` loop:
4949
display(canvas)
5050
5151
while(True):
52-
with hold_canvas(canvas):
52+
with hold_canvas():
5353
# Clear the old animation step
5454
canvas.clear()
5555
@@ -93,7 +93,7 @@ to the Jupyter client, and the animation will run entirely without any communica
9393
steps_number = 200
9494
9595
# Note how `hold_canvas` now wraps the entire for-loop
96-
with hold_canvas(canvas):
96+
with hold_canvas():
9797
for i in range(steps_number):
9898
# Clear the old animation step
9999
canvas.clear()
@@ -106,4 +106,4 @@ to the Jupyter client, and the animation will run entirely without any communica
106106
canvas.sleep(20)
107107
108108
109-
You cannot make an infinite animation using this approach.
109+
You cannot make an infinite animation using this approach.

docs/source/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ There are some API differences though:
77

88
- The Canvas widget is directly exposing the `CanvasRenderingContext2D <https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D>`_ API
99
- All the API is written in *snake_case* instead of *camelCase*, so for example ``canvas.fillStyle = 'red'`` in JavaScript becomes ``canvas.fill_style = 'red'`` in Python
10-
- The Canvas widget exposes a ``clear`` method, ``canvas.clear()`` is a shortcut for ``canvas.clear_rect(0, 0, canvas.size[0], canvas.size[1])``
10+
- The Canvas widget exposes a ``clear`` method, ``canvas.clear()`` is a shortcut for ``canvas.clear_rect(0, 0, canvas.width, canvas.height)``
1111
- We provide a `hold_canvas` context manager if you want to perform lots of commands at once
1212
- The Web canvas putImageData method does not support transparency and the current transformation state, our ``Canvas.put_image_data`` does support them!
1313

docs/source/basic_usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,5 @@ optimal performance you should try to use ``hold_canvas`` as much as possible.
109109
110110
canvas = Canvas(width=200, height=200)
111111
112-
with hold_canvas(canvas):
112+
with hold_canvas():
113113
# Perform drawings...

docs/source/clock.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"\n",
9595
"\n",
9696
"def draw_clock(hours, minutes):\n",
97-
" with hold_canvas(canvas):\n",
97+
" with hold_canvas():\n",
9898
" clear_drawing()\n",
9999
" draw_dial()\n",
100100
" draw_hands((hours % 12) * 60 + minutes)\n",

docs/source/conways_game_of_life.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
"outputs": [],
6363
"source": [
6464
"def draw(x, canvas, color=\"black\"):\n",
65-
" with hold_canvas(canvas):\n",
65+
" with hold_canvas():\n",
6666
" canvas.clear()\n",
6767
" canvas.fill_style = \"#FFF0C9\"\n",
6868
" canvas.stroke_style = \"white\"\n",
69-
" canvas.fill_rect(0, 0, canvas.size[0], canvas.size[1])\n",
69+
" canvas.fill_rect(0, 0, canvas.width, canvas.height)\n",
7070
"\n",
7171
" canvas.fill_style = color\n",
7272
" canvas.stroke_style = color\n",
@@ -446,7 +446,7 @@
446446
"canvas = RoughCanvas(width=x.shape[0] * n_pixels, height=x.shape[1] * n_pixels)\n",
447447
"canvas.fill_style = \"#FFF0C9\"\n",
448448
"canvas.stroke_style = \"white\"\n",
449-
"canvas.fill_rect(0, 0, canvas.size[0], canvas.size[1])\n",
449+
"canvas.fill_rect(0, 0, canvas.width, canvas.height)\n",
450450
"\n",
451451
"canvas"
452452
]

docs/source/drag_and_drop_example.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"\n",
110110
"def handle_mouse_move(x, y):\n",
111111
" if [o for o in objects_to_draw if o.selected]:\n",
112-
" with hold_canvas(canvas):\n",
112+
" with hold_canvas():\n",
113113
" [o for o in objects_to_draw if o.selected][-1].set_x_y(x, y)\n",
114114
" canvas_restart()\n",
115115
" [o.draw() for o in objects_to_draw]\n",

docs/source/drawing_images.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ Drawing from another ``Canvas`` is by far the fastest of the three solutions pre
116116
117117
canvas = Canvas(width=800, height=600)
118118
119-
with hold_canvas(canvas):
119+
with hold_canvas():
120120
for _ in range(2_000):
121121
canvas.save()
122122
123123
# Choose a random sprite texture
124124
sprite = sprites[choice(range(3))]
125125
126126
# Choose a random sprite position
127-
pos_x = randint(0, canvas.size[0])
128-
pos_y = randint(0, canvas.size[1])
127+
pos_x = randint(0, canvas.width)
128+
pos_y = randint(0, canvas.height)
129129
130130
# Choose a random rotation angle (but first set the rotation center with `translate`)
131131
canvas.translate(pos_x, pos_y)

docs/source/drawing_shapes.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ Rects
324324
colors_fill = np.random.randint(0, 255, size=(n_rects, 3))
325325
colors_outline = np.random.randint(0, 255, size=(n_rects, 3))
326326
alphas = np.random.random(n_rects)
327-
with hold_canvas(canvas):
327+
with hold_canvas():
328328
canvas.fill_styled_rects(x, y, width, height,
329329
color=colors_fill,
330330
alpha=alphas)
@@ -354,7 +354,7 @@ Circles
354354
colors_fill = np.random.randint(0, 255, size=(n_circles, 3))
355355
colors_outline = np.random.randint(0, 255, size=(n_circles, 3))
356356
alphas = np.random.random(n_circles)
357-
with hold_canvas(canvas):
357+
with hold_canvas():
358358
canvas.fill_styled_circles(x, y, r, color=colors_fill, alpha=alphas)
359359
canvas.line_width = 2
360360
canvas.stroke_styled_circles(x, y, r, color=colors_outline)
@@ -385,7 +385,7 @@ Arcs
385385
start_angle = np.random.random(n_circles) * math.pi
386386
end_angle = np.random.random(n_circles) * math.pi
387387
alphas = np.random.random(n_circles)
388-
with hold_canvas(canvas):
388+
with hold_canvas():
389389
canvas.fill_style = "cyan"
390390
canvas.fill_arcs(x, y, r, start_angle, end_angle)
391391
canvas.line_width = 1
@@ -440,7 +440,7 @@ Case 1: All polygons / line-segments have the same number of points
440440
polygons += np.linspace(1.0, 100.0, num=n_polygons)[:, None, None]
441441
442442
points_per_polygon = np.ones([n_polygons]) * n_points_per_polygon
443-
with hold_canvas(canvas):
443+
with hold_canvas():
444444
canvas.stroke_styled_polygons(polygons, color=colors_fill)
445445
canvas
446446
@@ -473,7 +473,7 @@ Case 1: All polygons / line-segments have the same number of points
473473
line_segments[:, :, 1] += np.linspace(1.0, canvas.height,
474474
num=n_line_segments)[:, None]
475475
476-
with hold_canvas(canvas):
476+
with hold_canvas():
477477
canvas.stroke_styled_line_segments(line_segments, color=colors_fill)
478478
canvas
479479
@@ -500,7 +500,7 @@ Polygons can be given as a list of ndarrays:
500500
polygons = [triangle, rectangle, irregular]
501501
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
502502
503-
with hold_canvas(canvas):
503+
with hold_canvas():
504504
canvas.fill_styled_polygons(polygons, color=colors)
505505
canvas
506506
@@ -524,7 +524,7 @@ Polygons can be given as a flat ndarray:
524524
colors_fill = np.random.randint(0, 255, size=(n_polygons, 3))
525525
colors_outline = np.random.randint(0, 255, size=(n_polygons, 3))
526526
527-
with hold_canvas(canvas):
527+
with hold_canvas():
528528
# the filling
529529
canvas.fill_styled_polygons(
530530
polygons, points_per_polygon=points_per_polygon,

docs/source/fractals_tree.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"outputs": [],
7272
"source": [
7373
"def draw_tree(canvas):\n",
74-
" with hold_canvas(canvas):\n",
74+
" with hold_canvas():\n",
7575
" canvas.save()\n",
7676
"\n",
7777
" canvas.clear()\n",

0 commit comments

Comments
 (0)