{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MultiCanvas\n", "\n", "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)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipycanvas import MultiCanvas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = MultiCanvas(n_canvases=3, width=200, height=200)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def draw_square(canvas, pos, width, color):\n", " canvas.fill_style = color\n", " canvas.fill_rect(pos, pos, width, width)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Drawing on each canvas layer successively" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw_square(m[0], 0, 200, 'orange')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw_square(m[1], 50, 100, 'blue')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw_square(m[2], 75, 50, 'green')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Drawing on the entire background canvas won't clear the upper layers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw_square(m[0], 0, 200, 'red')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Clearing the middle canvas won't clear the upper layer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[1].clear()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "draw_square(m[0], 0, 200, 'orange')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.1" } }, "nbformat": 4, "nbformat_minor": 4 }