RoughCanvas

The RoughCanvas give a nice hand-drawn style to your canvas!

In [ ]:
from ipycanvas import RoughCanvas

Draw shapes

The rough canvas gives a nice hand-drawn style to your canvas, note that it only works with direct draw calls, it does not work with paths created with begin_path, those paths will be drawned normally.

In [ ]:
canvas = RoughCanvas()

canvas.stroke_rect(100, 100, 100, 100)
canvas.fill_rect(50, 50, 100, 100)

canvas.stroke_circle(300, 300, 100)
canvas.fill_circle(350, 350, 100)

canvas.stroke_line(200, 200, 300, 300)

canvas

Fill styles

With the RoughCanvas, not only you can modify the fill color but you can also modify the fill styling, using one of the following values: 'hachure' (default), 'solid', 'zigzag', 'cross-hatch', 'dots', 'sunburst', 'dashed' or 'zigzag-line'

In [ ]:
canvas = RoughCanvas(width=850, height=100)

canvas.fill_style = 'blue'
canvas.line_width = 2.

rough_fill_style_values = ['hachure', 'solid', 'zigzag', 'cross-hatch', 'dots', 'sunburst', 'dashed', 'zigzag-line']

for i in range(len(rough_fill_style_values)):
    canvas.rough_fill_style = rough_fill_style_values[i]

    canvas.fill_rect(10 + i * 100, 10, 90, 80)


canvas

Stroke styles

You can still change the stroke width using the line_width attribute, just like with the normal Canvas class

In [ ]:
canvas = RoughCanvas(width=600, height=300)

canvas.line_width = 3.
canvas.stroke_rect(100, 100, 100, 100)

canvas.line_width = 10.
canvas.stroke_rect(300, 100, 100, 100)

canvas

Sketch options

There are some options you can play with to change the style of your drawing

Roughness: value indicating how rough the drawing is

In [ ]:
canvas = RoughCanvas(width=850, height=100)

canvas.fill_style = 'green'

for i in range(8):
    canvas.roughness = i

    canvas.fill_rect(10 + i * 100, 10, 90, 80)


canvas

Bowing: value indicating how curvy the lines are when drawing a sketch

In [ ]:
canvas = RoughCanvas(width=850, height=150)

canvas.fill_style = 'green'

for i in range(8):
    canvas.bowing = i * 3.

    canvas.stroke_rect(20 + i * 100, 20, 90, 110)


canvas

Make a rough Pie chart

In [ ]:
from math import pi

c = RoughCanvas(width=600, height=600)

c.fill_style = 'green'
c.fill_arc(300, 300, 200, 0, 0.9 * pi)

c.fill_style = 'red'
c.fill_arc(300, 300, 200, 0.9 * pi, pi + 0.2 * pi)

c.fill_style = 'blue'
c.fill_arc(300, 300, 200, pi + 0.2 * pi, 2 * pi)

c.stroke_style = 'black'
c.stroke_arc(300, 300, 200, 0, 2 * pi)

c

Draw thousands of shapes at once

In [ ]:
n_particles = 3_000

import numpy as np

x = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)
y = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)
size = np.random.randint(4, 8, n_particles)

canvas = RoughCanvas(width=800, height=500)

canvas
In [ ]:
canvas.fill_rects(x, y, size)