Canvas¶
from ui import Canvas
...
c = Canvas(o)
c.text("Hello world", (10, 20))
c.display()
-
class
ui.Canvas(o, base_image=None, name='', interactive=False)[source]¶ Bases:
objectThis 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
PILlibrary 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.Imageobject theCanvasis 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.pyfont_cachedictionary, 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/fontsby 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&y1are coordinates of the start, andx2&y2are 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&yare coordinates of the top left corner.You can pass a
fontkeyword argument to it - it accepts either aPIL.ImageFontobject 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.ImageDrawmethod.Keyword arguments:
fill: text color (default: white, as default canvas color)
-
vertical_text(text, coords, **kwargs)[source]¶ Draw vertical text on the canvas. Coordinates are expected in (x, y) format, where
x&yare coordinates of the top left corner.You can pass a
fontkeyword argument to it - it accepts either aPIL.ImageFontobject 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.ImageDrawmethod.Keyword arguments:
fill: text color (default: white, as default canvas color)
-
custom_shape_text(text, coords_cb, **kwargs)[source]¶ Draw text on the canvas, getting the position for each character from a supplied function. Coordinates are expected in (x, y) format, where
x&yare coordinates of the top left corner of the character.You can pass a
fontkeyword argument to it - it accepts either aPIL.ImageFontobject 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.ImageDrawmethod.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&y1are coordinates of the top left corner, andx2&y2are 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, wherexXandyXare 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&ycare coordinates of the circle center andris 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&y1are coordinates of the top left corner, andx2&y2are 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.
-
centered_text(text, cw=None, ch=None, font=None)[source]¶ Draws centered text on the canvas. This is mostly a convenience function, used in some UI elements. You can also pass alternate screen center values so that text is centered related to those, as opposed to the real screen center.
-
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.
-
get_centered_text_bounds(text, cw=None, ch=None, font=None)[source]¶ Returns the coordinates for the text to be centered on the screen. The coordinates come wrapped in a
Rectobject. If you use a non-default font, pass it asfont. You can also pass alternate screen center values so that text is centered related to those, as opposed to the real screen center.
-
class
ui.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:
widthheighttype: ZPUI output device type list (["b&w"]by default)device_mode: PIL device.mode attribute (by default,'1')