Skip to content

Commit e8f6211

Browse files
committed
more styled batch drawing
1 parent d845664 commit e8f6211

File tree

3 files changed

+392
-36
lines changed

3 files changed

+392
-36
lines changed

examples/batch_drawing.ipynb

Lines changed: 215 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,123 @@
1111
},
1212
{
1313
"cell_type": "code",
14-
"execution_count": 1,
14+
"execution_count": 46,
1515
"id": "3ea1b15d-478b-41a2-83a1-3e66e5ff5d24",
1616
"metadata": {},
1717
"outputs": [],
1818
"source": [
1919
"import numpy as np\n",
20-
"from ipycanvas import Canvas, hold_canvas"
20+
"from ipycanvas import Canvas, hold_canvas\n",
21+
"import math"
2122
]
2223
},
2324
{
2425
"cell_type": "markdown",
25-
"id": "2117b808-65d2-44dd-a780-dd9100809a68",
26+
"id": "b3b9c73f-d4fe-4c80-978e-60f4932c69d7",
27+
"metadata": {
28+
"tags": []
29+
},
30+
"source": [
31+
"# Batch Draw Rects"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 45,
37+
"id": "7de46419-aefa-4039-a27c-ad92f903a5ff",
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"data": {
42+
"application/vnd.jupyter.widget-view+json": {
43+
"model_id": "4f16d21e2fa442d9b215ed9f5c7b7ef3",
44+
"version_major": 2,
45+
"version_minor": 0
46+
},
47+
"text/plain": [
48+
"Canvas(height=300, width=800)"
49+
]
50+
},
51+
"metadata": {},
52+
"output_type": "display_data"
53+
}
54+
],
55+
"source": [
56+
"canvas = Canvas(width=800, height=300)\n",
57+
"n_rects = 1000\n",
58+
"x = np.random.randint(0, canvas.width, size=(n_rects))\n",
59+
"y = np.random.randint(0, canvas.width, size=(n_rects))\n",
60+
"width = np.random.randint(10, 40, size=(n_rects))\n",
61+
"height = np.random.randint(10, 40, size=(n_rects))\n",
62+
"with hold_canvas(canvas):\n",
63+
" canvas.fill_style = 'cyan'\n",
64+
" canvas.fill_rects(x, y, width, height)\n",
65+
" canvas.line_width = 2\n",
66+
" canvas.stroke_style = 'black'\n",
67+
" canvas.stroke_rects(x, y, width, height)\n",
68+
"canvas"
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"id": "bd1cb3db-2ceb-49d3-87aa-9929b262a066",
74+
"metadata": {},
75+
"source": [
76+
"# Batch Draw Styled Rects"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 47,
82+
"id": "f051cd10-5cf3-4a6f-bcb3-d10d5528c185",
2683
"metadata": {},
84+
"outputs": [
85+
{
86+
"data": {
87+
"application/vnd.jupyter.widget-view+json": {
88+
"model_id": "b9af46082601430fabb28f7dc57484a7",
89+
"version_major": 2,
90+
"version_minor": 0
91+
},
92+
"text/plain": [
93+
"Canvas(height=300, width=800)"
94+
]
95+
},
96+
"metadata": {},
97+
"output_type": "display_data"
98+
}
99+
],
100+
"source": [
101+
"canvas = Canvas(width=800, height=300)\n",
102+
"n_rects = 1000\n",
103+
"x = np.random.randint(0, canvas.width, size=(n_rects))\n",
104+
"y = np.random.randint(0, canvas.width, size=(n_rects))\n",
105+
"width = np.random.randint(10, 40, size=(n_rects))\n",
106+
"height = np.random.randint(10, 40, size=(n_rects))\n",
107+
"colors_fill = np.random.randint(0, 255, size=(n_rects,3))\n",
108+
"colors_outline = np.random.randint(0, 255, size=(n_rects,3))\n",
109+
"alphas = np.random.random(n_rects)\n",
110+
"with hold_canvas(canvas):\n",
111+
" canvas.fill_styled_rects(x, y, width, height, color=colors_fill,alpha=alphas)\n",
112+
" canvas.line_width = 2\n",
113+
" canvas.stroke_styled_rects(x, y, width, height, color=colors_outline,alpha=alphas)\n",
114+
"canvas"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"id": "2117b808-65d2-44dd-a780-dd9100809a68",
120+
"metadata": {
121+
"tags": []
122+
},
27123
"source": [
28124
"## Batch Draw Circles\n",
29125
"draw many circles each cirlcle has the same color"
30126
]
31127
},
32128
{
33129
"cell_type": "code",
34-
"execution_count": 2,
130+
"execution_count": 48,
35131
"id": "82b532b6-06b0-4255-b4fa-e03a6363ddf0",
36132
"metadata": {
37133
"tags": []
@@ -40,7 +136,7 @@
40136
{
41137
"data": {
42138
"application/vnd.jupyter.widget-view+json": {
43-
"model_id": "92b6da42c15842298d3faf678a6381fa",
139+
"model_id": "2d3f918019954eb49bf2e2985a2f152c",
44140
"version_major": 2,
45141
"version_minor": 0
46142
},
@@ -81,14 +177,14 @@
81177
},
82178
{
83179
"cell_type": "code",
84-
"execution_count": 3,
180+
"execution_count": 49,
85181
"id": "ce545eee-d9c0-47ab-afb7-274fa22389cd",
86182
"metadata": {},
87183
"outputs": [
88184
{
89185
"data": {
90186
"application/vnd.jupyter.widget-view+json": {
91-
"model_id": "e209d607b8de4226a5dbff72536b0ec7",
187+
"model_id": "11134765d61041fd9395b155ef309910",
92188
"version_major": 2,
93189
"version_minor": 0
94190
},
@@ -118,23 +214,125 @@
118214
},
119215
{
120216
"cell_type": "markdown",
121-
"id": "afea33c6-c6a8-4528-a1d9-7535a6810e9a",
217+
"id": "279d10b5-ed1b-4fa6-8bbe-f25313ad0475",
122218
"metadata": {},
219+
"source": [
220+
"# Batch Draw Arcs"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": 50,
226+
"id": "e36c1407-9a46-42fa-b06a-1a8283feee86",
227+
"metadata": {},
228+
"outputs": [
229+
{
230+
"data": {
231+
"application/vnd.jupyter.widget-view+json": {
232+
"model_id": "66ae6bd2dbfe4d4eb530b33054e1d2ba",
233+
"version_major": 2,
234+
"version_minor": 0
235+
},
236+
"text/plain": [
237+
"Canvas(height=300, width=800)"
238+
]
239+
},
240+
"metadata": {},
241+
"output_type": "display_data"
242+
}
243+
],
244+
"source": [
245+
"canvas = Canvas(width=800, height=300)\n",
246+
"n_circles = 1000\n",
247+
"x = np.random.randint(0, canvas.width, size=(n_circles))\n",
248+
"y = np.random.randint(0, canvas.width, size=(n_circles))\n",
249+
"r = np.random.randint(10, 20, size=(n_circles))\n",
250+
"start_angle = np.random.randint(0, 360, size=(n_circles))\n",
251+
"end_angle = np.random.randint(0, 360, size=(n_circles))\n",
252+
"start_angle = 0\n",
253+
"end_angle = math.pi\n",
254+
"start_angle = np.random.random(n_circles) * math.pi \n",
255+
"end_angle = np.random.random(n_circles) * math.pi \n",
256+
"alphas = np.random.random(n_circles)\n",
257+
"with hold_canvas(canvas):\n",
258+
" canvas.fill_style = 'cyan'\n",
259+
" canvas.fill_arcs(x,y,r,start_angle, end_angle)\n",
260+
" canvas.line_width = 1\n",
261+
" canvas.stroke_style = 'black'\n",
262+
" canvas.stroke_arcs(x,y,r,start_angle, end_angle)\n",
263+
"canvas"
264+
]
265+
},
266+
{
267+
"cell_type": "markdown",
268+
"id": "549983e9-f9bb-4ca7-a024-8f1307f15ba8",
269+
"metadata": {
270+
"tags": []
271+
},
272+
"source": [
273+
"# Batch Draw Styled Arcs"
274+
]
275+
},
276+
{
277+
"cell_type": "code",
278+
"execution_count": 51,
279+
"id": "1e4431ab-e064-448d-ab72-decba895bb7c",
280+
"metadata": {},
281+
"outputs": [
282+
{
283+
"data": {
284+
"application/vnd.jupyter.widget-view+json": {
285+
"model_id": "61b1d04c125d4420bebae9650ed99c58",
286+
"version_major": 2,
287+
"version_minor": 0
288+
},
289+
"text/plain": [
290+
"Canvas(height=300, width=800)"
291+
]
292+
},
293+
"metadata": {},
294+
"output_type": "display_data"
295+
}
296+
],
297+
"source": [
298+
"canvas = Canvas(width=800, height=300)\n",
299+
"n_circles = 20\n",
300+
"x = np.random.randint(0, canvas.width, size=(n_circles))\n",
301+
"y = np.random.randint(0, canvas.height, size=(n_circles))\n",
302+
"r = np.random.randint(30, 50, size=(n_circles))\n",
303+
"start_angle = np.random.random(n_circles) * math.pi \n",
304+
"end_angle = np.random.random(n_circles) * math.pi \n",
305+
"colors_fill = np.random.randint(0, 255, size=(n_circles,3))\n",
306+
"colors_outline = np.random.randint(0, 255, size=(n_circles,3))\n",
307+
"alphas = np.random.random(n_circles)\n",
308+
"with hold_canvas(canvas):\n",
309+
" canvas.fill_styled_arcs(x,y,r,start_angle, end_angle,color=colors_fill,alpha=1)\n",
310+
" canvas.line_width = 3\n",
311+
" canvas.stroke_styled_arcs(x,y,r,start_angle, end_angle,color=colors_outline,alpha=1)\n",
312+
"canvas"
313+
]
314+
},
315+
{
316+
"cell_type": "markdown",
317+
"id": "afea33c6-c6a8-4528-a1d9-7535a6810e9a",
318+
"metadata": {
319+
"tags": []
320+
},
123321
"source": [
124322
"# Batch Draw Polygons / LineSegments\n",
125323
"## Case 1: All Polygons / LineSegments have the same number of points"
126324
]
127325
},
128326
{
129327
"cell_type": "code",
130-
"execution_count": 4,
328+
"execution_count": 52,
131329
"id": "885a5ec6-06a8-4430-815e-63c06f6fa2cb",
132330
"metadata": {},
133331
"outputs": [
134332
{
135333
"data": {
136334
"application/vnd.jupyter.widget-view+json": {
137-
"model_id": "f0bfa8a9abe541059c8f0898e913b652",
335+
"model_id": "8efa7e28168e4da1a69045f4865a68e3",
138336
"version_major": 2,
139337
"version_minor": 0
140338
},
@@ -184,14 +382,14 @@
184382
},
185383
{
186384
"cell_type": "code",
187-
"execution_count": 5,
385+
"execution_count": 33,
188386
"id": "6bf31d36-01af-484c-9360-b97f504532a2",
189387
"metadata": {},
190388
"outputs": [
191389
{
192390
"data": {
193391
"application/vnd.jupyter.widget-view+json": {
194-
"model_id": "08ef9e57eb5f493697fc6aa2c63c32ac",
392+
"model_id": "8915993f0f1a4f128ba7871a858cf99f",
195393
"version_major": 2,
196394
"version_minor": 0
197395
},
@@ -239,7 +437,7 @@
239437
},
240438
{
241439
"cell_type": "code",
242-
"execution_count": 6,
440+
"execution_count": 34,
243441
"id": "34643bf9-d9d9-4296-aa02-67531e6f500f",
244442
"metadata": {
245443
"tags": []
@@ -248,7 +446,7 @@
248446
{
249447
"data": {
250448
"application/vnd.jupyter.widget-view+json": {
251-
"model_id": "219e9b29162d4d5897d53a461c97c232",
449+
"model_id": "751412241a3c4294a8323f61cd77e8f3",
252450
"version_major": 2,
253451
"version_minor": 0
254452
},
@@ -295,14 +493,14 @@
295493
},
296494
{
297495
"cell_type": "code",
298-
"execution_count": 8,
496+
"execution_count": 35,
299497
"id": "272deb5a-fc18-44b0-af01-04f3afb28247",
300498
"metadata": {},
301499
"outputs": [
302500
{
303501
"data": {
304502
"application/vnd.jupyter.widget-view+json": {
305-
"model_id": "aa24d7e01ad04540be42d2702cb3ff74",
503+
"model_id": "3af7556653c44b7ca378466c438d2016",
306504
"version_major": 2,
307505
"version_minor": 0
308506
},
@@ -357,4 +555,4 @@
357555
},
358556
"nbformat": 4,
359557
"nbformat_minor": 5
360-
}
558+
}

0 commit comments

Comments
 (0)