MultiCanvas

A MultiCanvas allows you to have multiple canvases one on top of the other. This is very useful when you need multiple layers that don't have the same update frequency (e.g. if you make a Tetris game you might only want to render the background image once, and never draw it again).

In [ ]:
from ipycanvas import MultiCanvas
In [ ]:
m = MultiCanvas(n_canvases=3, width=200, height=200)
In [ ]:
m
In [ ]:
def draw_square(canvas, pos, width, color):
    canvas.fill_style = color
    canvas.fill_rect(pos, pos, width, width)

Drawing on each canvas layer successively

In [ ]:
draw_square(m[0], 0, 200, 'orange')
In [ ]:
draw_square(m[1], 50, 100, 'blue')
In [ ]:
draw_square(m[2], 75, 50, 'green')

Drawing on the entire background canvas won't clear the upper layers

In [ ]:
draw_square(m[0], 0, 200, 'red')

Clearing the middle canvas won't clear the upper layer

In [ ]:
m[1].clear()
In [ ]:
draw_square(m[0], 0, 200, 'orange')
In [ ]: