<jupyter_start><jupyter_text>Fast Batch Drawing:To speed up the drawing of many objects ipycanvas offers a batch api to draw many items to the canvas quickly<jupyter_code>import numpy as np
from ipycanvas import Canvas, hold_canvas
import math<jupyter_output><empty_output><jupyter_text>Batch Draw Rects<jupyter_code>canvas = Canvas(width=800, height=300)
n_rects = 1000
x = np.random.randint(0, canvas.width, size=(n_rects))
y = np.random.randint(0, canvas.width, size=(n_rects))
width = np.random.randint(10, 40, size=(n_rects))
height = np.random.randint(10, 40, size=(n_rects))
with hold_canvas(canvas):
    canvas.fill_style = "cyan"
    canvas.fill_rects(x, y, width, height)
    canvas.line_width = 2
    canvas.stroke_style = "black"
    canvas.stroke_rects(x, y, width, height)
canvas<jupyter_output><empty_output><jupyter_text>Batch Draw Styled Rects<jupyter_code>canvas = Canvas(width=800, height=300)
n_rects = 1000
x = np.random.randint(0, canvas.width, size=(n_rects))
y = np.random.randint(0, canvas.width, size=(n_rects))
width = np.random.randint(10, 40, size=(n_rects))
height = np.random.randint(10, 40, size=(n_rects))
colors_fill = np.random.randint(0, 255, size=(n_rects, 3))
colors_outline = np.random.randint(0, 255, size=(n_rects, 3))
alphas = np.random.random(n_rects)
with hold_canvas(canvas):
    canvas.fill_styled_rects(x, y, width, height, color=colors_fill, alpha=alphas)
    canvas.line_width = 2
    canvas.stroke_styled_rects(x, y, width, height, color=colors_outline, alpha=alphas)
canvas<jupyter_output><empty_output><jupyter_text>Batch Draw Circlesdraw many circles each cirlcle has the same color<jupyter_code>canvas = Canvas(width=800, height=300)
# draw n circles with and random radii, random colors, and random alpha values
n_circles = 100
x = np.random.randint(0, canvas.width, size=(n_circles))
y = np.random.randint(0, canvas.width, size=(n_circles))
r = np.random.randint(10, 20, size=(n_circles))
canvas.fill_style = "red"
alphas = np.random.random(n_circles)
with hold_canvas(canvas):
    # the filled circles
    canvas.fill_circles(x, y, r)
    # the outlines
    canvas.stroke_style = "blue"
    canvas.stroke_circles(x, y, r)
canvas<jupyter_output><empty_output><jupyter_text>Batch Draw Styled Circlesdraw many circles where each circle can have a diferent color and alpha channel<jupyter_code>canvas = Canvas(width=800, height=300)
n_circles = 1000
x = np.random.randint(0, canvas.width, size=(n_circles))
y = np.random.randint(0, canvas.width, size=(n_circles))
r = np.random.randint(10, 20, size=(n_circles))
colors_fill = np.random.randint(0, 255, size=(n_circles, 3))
colors_outline = np.random.randint(0, 255, size=(n_circles, 3))
alphas = np.random.random(n_circles)
with hold_canvas(canvas):
    canvas.fill_styled_circles(x, y, r, color=colors_fill, alpha=alphas)
    canvas.line_width = 2
    canvas.stroke_styled_circles(x, y, r, color=colors_outline)
canvas<jupyter_output><empty_output><jupyter_text>Batch Draw Arcs<jupyter_code>canvas = Canvas(width=800, height=300)
n_circles = 1000
x = np.random.randint(0, canvas.width, size=(n_circles))
y = np.random.randint(0, canvas.width, size=(n_circles))
r = np.random.randint(10, 20, size=(n_circles))
start_angle = np.random.randint(0, 360, size=(n_circles))
end_angle = np.random.randint(0, 360, size=(n_circles))
start_angle = 0
end_angle = math.pi
start_angle = np.random.random(n_circles) * math.pi
end_angle = np.random.random(n_circles) * math.pi
alphas = np.random.random(n_circles)
with hold_canvas(canvas):
    canvas.fill_style = "cyan"
    canvas.fill_arcs(x, y, r, start_angle, end_angle)
    canvas.line_width = 1
    canvas.stroke_style = "black"
    canvas.stroke_arcs(x, y, r, start_angle, end_angle)
canvas<jupyter_output><empty_output><jupyter_text>Batch Draw Styled Arcs<jupyter_code>canvas = Canvas(width=800, height=300)
n_circles = 20
x = np.random.randint(0, canvas.width, size=(n_circles))
y = np.random.randint(0, canvas.height, size=(n_circles))
r = np.random.randint(30, 50, size=(n_circles))
start_angle = np.random.random(n_circles) * math.pi
end_angle = np.random.random(n_circles) * math.pi
colors_fill = np.random.randint(0, 255, size=(n_circles, 3))
colors_outline = np.random.randint(0, 255, size=(n_circles, 3))
alphas = np.random.random(n_circles)
with hold_canvas(canvas):
    canvas.fill_styled_arcs(x, y, r, start_angle, end_angle, color=colors_fill)
    canvas.line_width = 3
    canvas.stroke_styled_arcs(x, y, r, start_angle, end_angle, color=colors_outline)
canvas<jupyter_output><empty_output><jupyter_text>Batch Draw Polygons / LineSegments Case 1: All Polygons / LineSegments have the same number of points<jupyter_code>canvas = Canvas(width=800, height=300)
n_polygons = 50

# each polygon has 4 points
n_points_per_polygon = 4

polygons = np.zeros([n_polygons, n_points_per_polygon, 2])

polygons[:, 0, 0] = 0.0
polygons[:, 0, 1] = 0.0

polygons[:, 1, 0] = 1.0
polygons[:, 1, 1] = 0.0

polygons[:, 2, 0] = 1.0
polygons[:, 2, 1] = 1.0

polygons[:, 3, 0] = 0.0
polygons[:, 3, 1] = 1.0

colors_fill = np.random.randint(0, 255, size=(n_polygons, 3))
colors_outline = np.random.randint(0, 255, size=(n_polygons, 3))

# scale each polygon
polygons *= np.linspace(1.0, 200.0, num=n_polygons)[:, None, None]

# translate each polygon
polygons += np.linspace(1.0, 100.0, num=n_polygons)[:, None, None]

points_per_polygon = np.ones([n_polygons]) * n_points_per_polygon
with hold_canvas(canvas):
    canvas.stroke_styled_polygons(polygons, color=colors_fill)
canvas
canvas = Canvas(width=800, height=300)

n_line_segments = 20

n_points_per_line_segment = 500

line_segments = np.zeros([n_line_segments, n_points_per_line_segment, 2])

x = np.linspace(0, canvas.width, num=n_points_per_line_segment)[None, :]
line_segments[:, :, 0] = np.linspace(0, canvas.width, num=n_points_per_line_segment)[
    None, :
]
line_segments[:, :, 1] = (30.0 * np.sin(x * 0.1))[None, :]

colors_outline = np.random.randint(0, 255, size=(n_polygons, 3))

# translate line segments in y direction
line_segments[:, :, 1] += np.linspace(1.0, canvas.height, num=n_line_segments)[:, None]

with hold_canvas(canvas):
    canvas.stroke_styled_line_segments(line_segments, color=colors_fill)
canvas<jupyter_output><empty_output><jupyter_text>Batch Draw Polygons / LineSegments Case 2: Polygons / LineSegments can have different number of Points. Points are specified as list of arrays<jupyter_code>canvas = Canvas(width=400, height=400)

triangle = [(0, 0), (0, 40), (30, 40)]  # triangle
rectangle = [(100, 100), (300, 100), (300, 200), (100, 200)]  # rectangle
irregular = np.random.randint(0, 400, size=(5, 2))  # irregular with 5 sides
polygons = [triangle, rectangle, irregular]
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]

with hold_canvas(canvas):
    canvas.fill_styled_polygons(polygons, color=colors)
canvas<jupyter_output><empty_output><jupyter_text>Batch Draw Polygons / LineSegments Case 2: Polygons / LineSegments can have different number of Points. Points given as a flat array<jupyter_code>canvas = Canvas(width=400, height=400)
n_polygons = 20
points_per_polygon = np.random.randint(3, 6, size=n_polygons)
total_points = np.sum(points_per_polygon)
polygons = np.random.randint(0, 400, size=[total_points, 2])
alpha = np.random.random(n_polygons)
colors_fill = np.random.randint(0, 255, size=(n_polygons, 3))
colors_outline = np.random.randint(0, 255, size=(n_polygons, 3))

with hold_canvas(canvas):
    # the filling
    canvas.fill_styled_polygons(
        polygons, points_per_polygon=points_per_polygon, color=colors_fill, alpha=alpha
    )

    # draw outlines ontop where each line has the same style
    canvas.stroke_style = "black"
    canvas.line_width = 2
    canvas.stroke_polygons(polygons, points_per_polygon=points_per_polygon)
canvas<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13420245398773006, 'fraction_numerical': 0.042050102249488755, 'mean_word_length': 5.980374665477252, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '281646', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3237, 'n_tokens_neox': 2997, 'n_words': 637, 'path': 'examples/batch_drawing.ipynb', 'repo_name': '0xflotus/ipycanvas', 'size': '7824'}