Canvas¶
from ui import Canvas
...
c = Canvas(o)
c.text("Hello world", (10, 20))
c.display()
-
class
ui.canvas.
Canvas
(o, base_image=None, name='', interactive=False)[source]¶ Bases:
object
This object allows you to work with graphics on the display quicker and easier. You can draw text, graphical primitives, insert bitmaps and do other things that the
PIL
library allows, with a bunch of useful helper functions.Args:
o
: output devicebase_image
: a PIL.Image to use as a base, if neededname
: a name, for internal usageinteractive
: whether the canvas updates the display after each drawing
-
background_color
= 'black'¶ default background color to use for drawing
-
default_color
= 'white'¶ default color to use for drawing
-
width
= 0¶ width of canvas in pixels.
-
height
= 0¶ height of canvas in pixels.
-
size
= (0, 0)¶ a tuple of (width, height).
-
image
= None¶ PIL.Image
object theCanvas
is currently operating on.
-
load_font
(path, size, alias=None, type='truetype')[source]¶ Loads a font by its path for the given size, then returns it. Also, stores the font in the
canvas.py
font_cache
dictionary, so that it doesn’t have to be re-loaded later on.Supports both absolute paths, paths relative to root ZPUI directory and paths to fonts in the ZPUI font directory (
ui/fonts
by default).
-
point
(coord_pairs, **kwargs)[source]¶ Draw a point, or multiple points on the canvas. Coordinates are expected in
((x1, y1), (x2, y2), ...)
format, wherex*
&y*
are coordinates of each point you want to draw.Keyword arguments:
fill
: point color (default: white, as default canvas color)
-
line
(coords, **kwargs)[source]¶ Draw a line on the canvas. Coordinates are expected in
(x1, y1, x2, y2)
format, wherex1
&y1
are coordinates of the start, andx2
&y2
are coordinates of the end.Keyword arguments:
fill
: line color (default: white, as default canvas color)width
: line width (default: 0, which results in a single-pixel-wide line)
-
text
(text, coords, **kwargs)[source]¶ Draw text on the canvas. Coordinates are expected in (x, y) format, where
x
&y
are coordinates of the top left corner.You can pass a
font
keyword argument to it - it accepts either aPIL.ImageFont
object or a tuple of(path, size)
, which are then supplied toCanvas.load_font()
.Do notice that order of first two arguments is reversed compared to the corresponding
PIL.ImageDraw
method.Keyword arguments:
fill
: text color (default: white, as default canvas color)
-
rectangle
(coords, **kwargs)[source]¶ Draw a rectangle on the canvas. Coordinates are expected in
(x1, y1, x2, y2)
format, wherex1
&y1
are coordinates of the top left corner, andx2
&y2
are coordinates of the bottom right corner.Keyword arguments:
outline
: outline color (default: white, as default canvas color)fill
: fill color (default: None, as in, transparent)
-
polygon
(coord_pairs, **kwargs)[source]¶ Draw a polygon on the canvas. Coordinates are expected in
((x1, y1), (x2, y2), (x3, y3), [...])
format, wherexX
andyX
are points that construct a polygon.Keyword arguments:
outline
: outline color (default: white, as default canvas color)fill
: fill color (default: None, as in, transparent)
-
circle
(coords, **kwargs)[source]¶ Draw a circle on the canvas. Coordinates are expected in
(xc, yx, r)
format, wherexc
&yc
are coordinates of the circle center andr
is the radius.Keyword arguments:
outline
: outline color (default: white, as default canvas color)fill
: fill color (default: None, as in, transparent)
-
ellipse
(coords, **kwargs)[source]¶ Draw a ellipse on the canvas. Coordinates are expected in
(x1, y1, x2, y2)
format, wherex1
&y1
are coordinates of the top left corner, andx2
&y2
are coordinates of the bottom right corner.Keyword arguments:
outline
: outline color (default: white, as default canvas color)fill
: fill color (default: None, as in, transparent)
-
get_center
()[source]¶ Get center coordinates. Will not represent the physical center - especially with those displays having even numbers as width and height in pixels (that is, the absolute majority of them).
-
clear
(coords=None, fill=None)[source]¶ Fill an area of the image with default background color. If coordinates are not supplied, fills the whole canvas, effectively clearing it. Uses the background color by default.
-
check_coordinates
(coords, check_count=True)[source]¶ A helper function to check and reformat coordinates supplied to functions. Currently, accepts integer coordinates, as well as strings - denoting offsets from opposite sides of the screen.
-
check_coordinate_pairs
(coord_pairs)[source]¶ A helper function to check and reformat coordinate pairs supplied to functions. Each pair is checked by
check_coordinates
.
-
get_text_bounds
(text, font=None)[source]¶ Returns the dimensions for a given text. If you use a non-default font, pass it as
font
.
-
class
ui.canvas.
MockOutput
(width=128, height=64, type=None, device_mode='1')[source]¶ A mock output device that you can use to draw icons and other bitmaps using
Canvas
.Keyword arguments:
width
height
type
: ZPUI output device type list (["b&w-pixel"]
by default)device_mode
: PIL device.mode attribute (by default,'1'
)