canvas – Graphics drawing canvas

A rectangular area for graphics and other child objects.

The Canvas is a basic rectangular area on the screen where you can draw lines, text, images, and more. You can also add other child objects to a parent Canvas. The children are drawn on top of their parents.

POSITION:

Every Canvas has an X,Y position on its parent Canvas. This position is the X,Y coordinate of the upper-left corner of the child relative to its parent. The position may be changed at any time to move the child around on top of its parent.

Position values may be negative. An object may be positioned partially or fully off of the parent. Child objects are clipped to their parent Canvas.

BITMAP:

A Canvas is a Bitmap you can draw on with the Bitmap functions.

class canvas.Canvas(width, height, x=0, y=0, scale=1, transparent_color=(247, 251, 247))[source]

A rectangular area for graphics and other child objects.

Create a new Canvas.

A Canvas has a simple bitmap background. In fact, a Canvas IS a Bitmap with some added features. The Canvas manages a list of child objects (like other Canvases) that are drawn on top of their parent Canvas.

Parameters
  • width (int) – The width (in pixels) of the Canvas.

  • height (int) – The height (in pixels) of the Canvas.

  • x (int, optional) – The x coordinate relative to the parent object. Defaults to 0.

  • y (int, optional) – The y coordinate relative to the parent object. Defaults to 0.

  • scale (int, optional) –

  • transparent_color (bool, optional) – The pixel value considered to be transparent (default is colors.TRANSPARENT).

add_child(child, position=-1)[source]

Add a child object to this Canvas.

Parameters
  • child (Canvas) – The child object.

  • position (int, optional) – The index of the child within the peer list. Defaults to -1 (the end of the list).

print(obj, color=(255, 255, 255), cursor_x=None, cursor_y=None, scale=2, end='\n', background=(0, 0, 0))[source]

Print a text string onto this Canvas.

Writes/scrolls the existing content.

Parameters
  • thing (object) – either a string (for text) or an Image

  • color (tuple, optional) – text color. Default is WHITE.

  • cursor_x (int, optional) – x char coord of text. Default is current.

  • cursor_y (int, optional) – y char coord of text. Default is current.

  • scale (int, optional) – scaling factor. Default is 2.

  • end (str, optional) – the character to print on the end of the line. Default is n.

  • background (int, optional) – background color. Default is None (Transparent).

remove_all_children()[source]

Remove all children from this Canvas

remove_child(child)[source]

Remove a child from this Canvas.

Parameters

child (Canvas) – The child to remove.

show(obj)[source]

Show the given text or Image on the display.

This method renders the given text or picture.

This is a “simplifier” function for the curriculum. It clears the screen and scales images for simple display. You probably want ‘draw_text’ and ‘draw_image’ for most applications.

Parameters

obj (str or Image) – The text or image to display.

property x

Get or set this object’s X coordinate relative to its parent.

property y

Get or set this object’s Y coordinate relative to its parent.

Download Source