Skip to content

graphics

graphics

graphics

Graphics library extending MicroPython's framebuf module.

Classes

Area

Area(x, y=None, w=None, h=None)

Represents a rectangular area defined by its position and dimensions.

Attributes:

Name Type Description
x int | float

The x-coordinate of the top-left corner of the area.

y int | float

The y-coordinate of the top-left corner of the area.

w int | float

The width of the area.

h int | float

The height of the area.

Methods:

Name Description
contains

Checks if the specified point is contained within the area.

contains_area

Checks if the specified area is contained within the area.

intersects

Checks if the current Area object intersects with another Area object.

touches_or_intersects

Checks if the current Area object touches or intersects with another Area object.

shift

Returns a new Area shifted by the specified amount in the x and y directions.

clip

Clips the current Area object to the specified Area object.

Special Methods

eq(other): Checks if the current Area object is equal to another Area object. ne(other): Checks if the current Area object is not equal to another Area object. add(other): Computes the union of the current Area object and another Area object. iter(): Returns an iterator over the elements of the Area object. repr(): Returns a string representation of the Area object. str(): Returns a string representation of the Area object.

Initializes a new instance of the Area class.

Parameters:

Name Type Description Default
x int | float | tuple

The x-coordinate of the top-left corner of the area or a tuple containing the x, y, w, and h coordinates of the area.

required
y int | float

The y-coordinate of the top-left corner of the area.

None
w int | float

The width of the area.

None
h int | float

The height of the area.

None
Methods:
contains
contains(x, y=None)

Checks if the specified point is contained within the area.

Parameters:

Name Type Description Default
x int | tuple

The x-coordinate of the point to check or a tuple containing the x and y coordinates of the point.

required
y int

The y-coordinate of the point to check.

None

Returns:

Type Description
bool

True if the point is contained within the area, False otherwise.

contains_area
contains_area(other)

Checks if the specified area is contained within the area.

Parameters:

Name Type Description Default
other Area

The other area to check.

required

Returns:

Type Description
bool

True if the other area is contained within the area, False otherwise.

intersects
intersects(other)

Checks if the current Area object intersects with another Area object.

Parameters:

Name Type Description Default
other Area

The other Area object to check for overlap.

required

Returns:

Type Description
bool

True if the two Area objects intersect, False otherwise.

touches_or_intersects
touches_or_intersects(other)

Checks if the current Area object touches or intersects with another Area object.

Parameters:

Name Type Description Default
other Area

The other Area object to check for overlap or touch.

required

Returns:

Type Description
bool

True if the two Area objects touch or intersect, False otherwise.

shift
shift(dx=0, dy=0)

Returns a new Area shifted by the specified amount in the x and y directions.

Parameters:

Name Type Description Default
dx int | float

The amount to shift the area in the x direction.

0
dy int | float

The amount to shift the area in the y direction.

0

Returns:

Type Description
Area

A new Area object shift by the specified amount in the x and y directions.

clip
clip(other)

Clips the current Area object to the specified Area object.

Parameters:

Name Type Description Default
other Area

The other Area object to clip to.

required

Returns:

Type Description
Area

A new Area object representing the clipped area.

offset
offset(d1, d2=None, d3=None, d4=None)

Returns a new Area offset by the specified amount(s).

If only one argument is provided, it is used as the offset in all 4 directions. If two arguments are provided, the first is used as the offset in the x direction and the second as the offset in the y direction. If three arguments are provided, they are used as the offsets in the left, top/bottom, and right directions, respectively. If four arguments are provided, they are used as the offsets in the left, top, right, and bottom directions, respectively.

Parameters:

Name Type Description Default
d1 int | float

The offset in the x direction or the offset in all 4 directions.

required
d2 int | float

The offset in the y direction or the offset in the top/bottom direction.

None
d3 int | float

The offset in the right direction.

None
d4 int | float

The offset in the bottom direction.

None

Returns:

Type Description
Area

A new Area object offset by the specified amount(s).

inset
inset(d1, d2=None, d3=None, d4=None)

Returns a new Area inset by the specified amount(s).

If only one argument is provided, it is used as the inset in all 4 directions. If two arguments are provided, the first is used as the inset in the x direction and the second as the inset in the y direction. If three arguments are provided, they are used as the insets in the left, top/bottom, and right directions, respectively. If four arguments are provided, they are used as the insets in the left, top, right, and bottom directions, respectively.

Parameters:

Name Type Description Default
d1 int | float

The inset in the x direction or the inset in all 4 directions.

required
d2 int | float

The inset in the y direction or the inset in the top/bottom direction.

None
d3 int | float

The inset in the right direction.

None
d4 int | float

The inset in the bottom direction.

None

Returns:

Type Description
Area

A new Area object inset by the specified amount(s).

Draw

Draw(canvas)

Draw shapes on a canvas (display, FrameBuffer, or compatible object).

Each method delegates to graphics._shapes and returns an Area bounding box.

Parameters:

Name Type Description Default
canvas

Object with framebuf-compatible drawing methods.

required
Example
draw = Draw(display)
draw.fill(0x0000)
draw.rect(10, 10, 100, 100, 0xFFFF)
Methods:
arc
arc(x, y, r, a0, a1, c)

Draw an arc. Returns Area bounds.

blit
blit(source, x, y, key=-1, palette=None)

Blit a source buffer. Returns Area bounds.

blit_rect
blit_rect(buf, x, y, w, h)

Blit a raw rectangle buffer. Returns Area bounds.

blit_tranparent
blit_tranparent(buf, x, y, w, h, key=None)

Blit with transparency (typo preserved for compatibility). Returns Area bounds.

circle
circle(x, y, r, c, f=False)

Draw a circle. Returns Area bounds.

ellipse
ellipse(x, y, r1, r2, c, f=False, m=15, w=None, h=None)

Draw an ellipse. Returns Area bounds.

fill
fill(c)

Fill the canvas. Returns Area bounds.

fill_rect
fill_rect(x, y, w, h, c)

Fill a rectangle. Returns Area bounds.

gradient_rect
gradient_rect(x, y, w, h, c1, c2=None, vertical=True)

Fill a rectangle with a gradient. Returns Area bounds.

hline
hline(x, y, w, c)

Draw a horizontal line. Returns Area bounds.

line
line(x1, y1, x2, y2, c)

Draw a line. Returns Area bounds.

pixel
pixel(x, y, c)

Draw a pixel. Returns Area bounds.

poly
poly(x, y, coords, c, f=False)

Draw a polygon from flat coordinates. Returns Area bounds.

polygon
polygon(points, x, y, c, angle=0, center_x=0, center_y=0)

Draw a polygon from point list. Returns Area bounds.

rect
rect(x, y, w, h, c, f=False)

Draw a rectangle. Returns Area bounds.

round_rect
round_rect(x, y, w, h, r, c, f=False)

Draw a rounded rectangle. Returns Area bounds.

triangle
triangle(x1, y1, x2, y2, x3, y3, c, f=False)

Draw a triangle. Returns Area bounds.

vline
vline(x, y, h, c)

Draw a vertical line. Returns Area bounds.

text
text(*args, **kwargs)

Draw text using height from kwargs. Returns Area bounds.

text8
text8(*args, **kwargs)

Draw 8px-high text. Returns Area bounds.

text14
text14(*args, **kwargs)

Draw 14px-high text. Returns Area bounds.

text16
text16(*args, **kwargs)

Draw 16px-high text. Returns Area bounds.

Font

Font(font_data=None, height=None, cached=True)

A class to read binary fonts like those found at https://github.com/spacerace/romfont and draw text to a canvas.

Parameters:

Name Type Description Default
font_data str | byterray

The path to the font .bin file or memoryview. Default is None.

None
height int

The height of the font. Default is None.

None
cached bool

If True, the font file will be read into memory on init. If False, the font file will be read from disk each time it is needed.

True
Attributes
width property
width

Return the width of the font in pixels.

height property
height

Return the height of the font in pixels.

Methods:
deinit
deinit()

Close the font file as cleanup.

draw_char
draw_char(char, x, y, canvas, color, scale=1, inverted=False)

Draw one character at position (x,y).

Parameters:

Name Type Description Default
char str

The character to draw.

required
x int

The x position to draw the character.

required
y int

The y position to draw the character.

required
canvas Canvas

The DisplayDriver, FrameBuffer, or other canvas-like object to draw on.

required
color int

The color to draw the character in.

required
scale int

The scale factor to draw the character at. Default is 1.

1
inverted bool

If True, draw the character inverted. Default is False.

False

Returns:

Type Description
Area

The area that was drawn to.

text
text(canvas, string, x, y, color, scale=1, inverted=False)

Draw text to the canvas.

Parameters:

Name Type Description Default
canvas Canvas

The DisplayDriver, FrameBuffer, or other canvas-like object to draw on.

required
string str

The text to draw.

required
x int

The x position to start drawing the text.

required
y int

The y position to start drawing the text.

required
color int

The color to draw the text in.

required
scale int

The scale factor to draw the text at. Default is 1.

1
inverted bool

If True, draw the text inverted. Default is False.

False

Returns:

Type Description
Area

The area that was drawn to.

text_width
text_width(text, scale=1)

Return the pixel width of the specified text message. Takes into account the scale factor, but not any newlines.

Parameters:

Name Type Description Default
text str

The text to measure.

required
scale int

The scale factor to measure the text at. Default is 1.

1
export
export(filename)

Export the font data in self._cache to a .py file that can be imported. The format is a single bytes object named _FONT. There are 256 lines, one for each character. The last line is FONT = memoryview(_FONT).

Parameters:

Name Type Description Default
filename str

The path to save the file to.

required

FrameBuffer

FrameBuffer(buffer, width, height, format, *args, **kwargs)

An extension of MicroPython's framebuf.FrameBuffer that adds some useful methods for drawing shapes and text. Each method returns a bounding box (x, y, w, h) of the drawn shape to indicate the area of the display that was modified. This can be used to update only the modified area of the display. Exposes attributes not exposed in the base class, such as color_depth, width, height, buffer, and format. Also adds a save method to save the framebuffer to a file, and a from_file method to load a framebuffer from a file.

Inherits from framebuf.FrameBuffer, which may be compiled into MicroPython or may be from _framebuf.py. Methods should return an Area object, but the MicroPython framebuf module returns None, so the methods inherited from framebuf.FrameBuffer are overridden to return an Area object.

Parameters:

Name Type Description Default
buffer bytearray

Framebuffer buffer

required
width int

Width in pixels

required
height int

Height in pixels

required
format int

Framebuffer format

required

Attributes:

Name Type Description
buffer bytearray

Framebuffer buffer

width int

Width in pixels

height int

Height in pixels

format int

Framebuffer format

color_depth int

Color depth

Methods:
scroll
scroll(xstep, ystep)

Shift the contents of the FrameBuffer by the given vector (xstep, ystep). This may leave a footprint of the previous colors in the FrameBuffer.

Parameters:

Name Type Description Default
xstep int

The number of pixels to shift the FrameBuffer in the x direction.

required
ystep int

The number of pixels to shift the FrameBuffer in the y direction.

required

Raises:

Type Description
ValueError

If the FrameBuffer format depth is not a multiple of 8

fill_rect
fill_rect(x, y, w, h, c)

Fill the given rectangle with the given color.

Parameters:

Name Type Description Default
x int

x coordinate

required
y int

y coordinate

required
w int

Width in pixels

required
h int

Height in pixels

required
c int

color

required

Returns:

Type Description
Area

Bounding box of the filled rectangle

pixel
pixel(x, y, c=None)

Draw a single pixel at the given location and color.

Parameters:

Name Type Description Default
x int

x coordinate

required
y int

y coordinate

required
c int

color (default: None)

None

Returns:

Type Description
Area

Bounding box of the pixel

fill
fill(c)

Fill the buffer with the given color.

Parameters:

Name Type Description Default
c int

color

required

Returns:

Type Description
Area

Bounding box of the filled buffer

ellipse
ellipse(x, y, rx, ry, c, f=False, m=15)

Draw an ellipse at the given location, radii and color.

Parameters:

Name Type Description Default
x int

Center x coordinate

required
y int

Center y coordinate

required
rx int

X radius

required
ry int

Y radius

required
c int

color

required
f bool

Fill the ellipse (default: False)

False
m int

Bitmask to determine which quadrants to draw (default: 0b1111)

15

Returns:

Type Description
Area

Bounding box of the ellipse

hline
hline(x, y, w, c)

Draw a horizontal line at the given location, width and color.

Parameters:

Name Type Description Default
x int

x coordinate

required
y int

y coordinate

required
w int

Width in pixels

required
c int

color

required

Returns:

Type Description
Area

Bounding box of the horizontal line

line
line(x1, y1, x2, y2, c)

Draw a line between the given start and end points and color.

Parameters:

Name Type Description Default
x1 int

Start x coordinate

required
y1 int

Start y coordinate

required
x2 int

End x coordinate

required
y2 int

End y coordinate

required
c int

color

required

Returns:

Type Description
Area

Bounding box of the line

poly
poly(x, y, coords, c, f=False)

Draw a polygon at the given location, coordinates and color.

Parameters:

Name Type Description Default
x int

x coordinate

required
y int

y coordinate

required
coords array

Array of x, y coordinate tuples

required
c int

color

required
f bool

Fill the polygon (default: False)

False

Returns:

Type Description
Area

Bounding box of the polygon

rect
rect(x, y, w, h, c, f=False)

Draw a rectangle at the given location, size and color.

Parameters:

Name Type Description Default
x int

Top left corner x coordinate

required
y int

Top left corner y coordinate

required
w int

Width in pixels

required
h int

Height in pixels

required
c int

color

required
f bool

Fill the rectangle (default: False)

False

Returns:

Type Description
Area

Bounding box of the rectangle

vline
vline(x, y, h, c)

Draw a vertical line at the given location, height and color.

Parameters:

Name Type Description Default
x int

x coordinate

required
y int

y coordinate

required
h int

Height in pixels

required
c int

color

required

Returns:

Type Description
Area

Bounding box of the vertical line

text
text(s, x, y, c=1, scale=1, inverted=False, font_data=None, height=8)

Draw text at the given location, using the given font and color.

Parameters:

Name Type Description Default
s str

Text to draw

required
x int

x coordinate

required
y int

y coordinate

required
c int

color

1
scale int

Scale factor (default: 1)

1
inverted bool

Invert the text (default: False)

False
font_data str

Path to the font file (default: None)

None
height int

Height of the font (default: 8)

8

Returns:

Type Description
Area

Bounding box of the text

blit
blit(buf, x, y, key=-1, palette=None)

Blit the given buffer at the given location.

Parameters:

Name Type Description Default
buf FrameBuffer

FrameBuffer to blit

required
x int

x coordinate

required
y int

y coordinate

required
key int

Color key (default: -1)

-1
palette list

Palette (default: None)

None

Returns:

Type Description
Area

Bounding box of the blitted buffer

arc
arc(x, y, r, a0, a1, c)

Arc drawing function. Will draw a single pixel wide arc with a radius r centered at x, y from a0 to a1.

Parameters:

Name Type Description Default
x int

X-coordinate of the arc's center.

required
y int

Y-coordinate of the arc's center.

required
r int

Radius of the arc.

required
a0 float

Starting angle in degrees.

required
a1 float

Ending angle in degrees.

required
c int

color.

required

Returns:

Type Description
Area

The bounding box of the arc.

blit_rect
blit_rect(buf, x, y, w, h)

Blit a rectangular area from a buffer to the canvas. Uses the canvas's blit_rect method if available, otherwise writes directly to the buffer.

Parameters:

Name Type Description Default
buf memoryview

Buffer to blit. Must already be byte-swapped if necessary.

required
x int

X-coordinate to blit to.

required
y int

Y-coordinate to blit to.

required
w int

Width of the area to blit.

required
h int

Height of the area to blit.

required

Returns:

Type Description
Area

The bounding box of the blitted area.

blit_transparent
blit_transparent(buf, x, y, w, h, key)

Blit a buffer with transparency.

Parameters:

Name Type Description Default
buf memoryview

Buffer to blit.

required
x int

X-coordinate to blit to.

required
y int

Y-coordinate to blit to.

required
w int

Width of the area to blit.

required
h int

Height of the area to blit.

required
key int

Key value for transparency.

required

Returns:

Type Description
Area

The bounding box of the blitted area.

circle
circle(x0, y0, r, c, f=False)

Circle drawing function. Will draw a single pixel wide circle centered at x0, y0 and the specified r.

Parameters:

Name Type Description Default
x0 int

Center x coordinate

required
y0 int

Center y coordinate

required
r int

Radius

required
c int

Color

required
f bool

Fill the circle (default: False)

False

Returns:

Type Description
Area

The bounding box of the circle.

gradient_rect
gradient_rect(x, y, w, h, c1, c2=None, vertical=True)

Fill a rectangle with a gradient.

Parameters:

Name Type Description Default
x int

X-coordinate of the top-left corner of the rectangle.

required
y int

Y-coordinate of the top-left corner of the rectangle.

required
w int

Width of the rectangle.

required
h int

Height of the rectangle.

required
c1 int

565 encoded color for the top or left edge.

required
c2 int

565 encoded color for the bottom or right edge. If None or the same as c1, fill_rect will be called instead.

None
vertical bool

If True, the gradient will be vertical. If False, the gradient will be horizontal.

True

Returns:

Type Description
Area

The bounding box of the filled area.

polygon
polygon(points, x, y, color, angle=0, center_x=0, center_y=0)

Draw a polygon on the canvas.

Parameters:

Name Type Description Default
points list

List of points to draw.

required
x int

X-coordinate of the polygon's position.

required
y int

Y-coordinate of the polygon's position.

required
color int

color.

required
angle float

Rotation angle in radians (default: 0).

0
center_x int

X-coordinate of the rotation center (default: 0).

0
center_y int

Y-coordinate of the rotation center (default: 0).

0

Raises:

Type Description
ValueError

If the polygon has less than 3 points.

Returns:

Type Description
Area

The bounding box of the polygon.

round_rect
round_rect(x0, y0, w, h, r, c, f=False)

Rounded rectangle drawing function. Will draw a single pixel wide rounded rectangle starting at x0, y0 and extending w, h pixels with the specified radius.

Parameters:

Name Type Description Default
x0 int

X-coordinate of the top-left corner of the rectangle.

required
y0 int

Y-coordinate of the top-left corner of the rectangle.

required
w int

Width of the rectangle.

required
h int

Height of the rectangle.

required
r int

Radius of the corners.

required
c int

color.

required
f bool

Fill the rectangle (default: False).

False

Returns:

Type Description
Area

The bounding box of the rectangle.

triangle
triangle(x0, y0, x1, y1, x2, y2, c, f=False)

Triangle drawing function. Draws a single pixel wide triangle with vertices at (x0, y0), (x1, y1), and (x2, y2).

Parameters:

Name Type Description Default
x0 int

X-coordinate of the first vertex.

required
y0 int

Y-coordinate of the first vertex.

required
x1 int

X-coordinate of the second vertex.

required
y1 int

Y-coordinate of the second vertex.

required
x2 int

X-coordinate of the third vertex.

required
y2 int

Y-coordinate of the third vertex.

required
c int

color.

required
f bool

Fill the triangle (default: False).

False

Returns:

Type Description
Area

The bounding box of the triangle.

text8
text8(s, x, y, c=1, scale=1, inverted=False, font_data=None)
   Place text on the canvas with an 8 pixel high font.
   Breaks on

to next line. Does not break on line going off canvas.

   Args:
       s (str): The text to draw.
       x (int): The x position to start drawing the text.
       y (int): The y position to start drawing the text.
       c (int): The color to draw the text in.  Default is 1.
       scale (int): The scale factor to draw the text at.  Default is 1.
       inverted (bool): If True, draw the text inverted.  Default is False.
       font_data (str): The path to the font file to use.  Default is None.

   Returns:
       Area: The area that was drawn to.
text14
text14(s, x, y, c=1, scale=1, inverted=False, font_data=None)
   Place text on the canvas with a 14 pixel high font.
   Breaks on

to next line. Does not break on line going off canvas.

   Args:
       s (str): The text to draw.
       x (int): The x position to start drawing the text.
       y (int): The y position to start drawing the text.
       c (int): The color to draw the text in.  Default is 1.
       scale (int): The scale factor to draw the text at.  Default is 1.
       inverted (bool): If True, draw the text inverted.  Default is False.
       font_data (str): The path to the font file to use.  Default is None.

   Returns:
       Area: The area that was drawn to.
text16
text16(s, x, y, c=1, scale=1, inverted=False, font_data=None)
   Place text on the canvas with a 16 pixel high font.
   Breaks on

to next line. Does not break on line going off canvas.

   Args:
       s (str): The text to draw.
       x (int): The x position to start drawing the text.
       y (int): The y position to start drawing the text.
       c (int): The color to draw the text in.  Default is 1.
       scale (int): The scale factor to draw the text at.  Default is 1.
       inverted (bool): If True, draw the text inverted.  Default is False.
       font_data (str): The path to the font file to use.  Default is None.

   Returns:
       Area: The area that was drawn to.
save
save(filename=None)

Save the framebuffer to a file. The file extension must match the format, otherwise the extension will be appended to the filename.

Saves 1-bit formats as PBM, 2-bit formats as PGM with max value 3, 4-bit formats as PGM with max value 15, 8-bit formats as PGM with max value 255, and 16-bit formats as BMP.

Parameters:

Name Type Description Default
filename str

Filename to save to

None
from_file staticmethod
from_file(filename)

Load a framebuffer from a file.

Parameters:

Name Type Description Default
filename str

Filename to load from

required

Functions:

bmp_to_framebuffer

bmp_to_framebuffer(filename)

Convert a BMP file to a RGB565 FrameBuffer. First ensures planes is 1, bits per pixel is 16, and compression is 0.

Parameters:

Name Type Description Default
filename str

Filename of the

required

pbm_to_framebuffer

pbm_to_framebuffer(filename)

Convert a PBM file to a MONO_HLSB FrameBuffer

Parameters:

Name Type Description Default
filename str

Filename of the PBM file

required

pgm_to_framebuffer

pgm_to_framebuffer(filename)

Convert a PGM file to a GS2_HMSB, GS4_HMSB or GS8 FrameBuffer

Parameters:

Name Type Description Default
filename str

Filename of the PGM file

required

text

text(*args, height=8, **kwargs)

Selector to call the correct text function based on the height of the font. See text8, text14, and text16 for more information.

Parameters:

Name Type Description Default
height int

The height of the font to use. Default is 8.

8

text8

text8(canvas, s, x, y, c=1, scale=1, inverted=False, font_data=None)

Place text on the canvas with an 8 pixel high font. Breaks on to next line. Does not break on line going off canvas.

Args: canvas (Canvas): The DisplayDriver, FrameBuffer, or other canvas-like object to draw on. s (str): The text to draw. x (int): The x position to start drawing the text. y (int): The y position to start drawing the text. c (int): The color to draw the text in. Default is 1. scale (int): The scale factor to draw the text at. Default is 1. inverted (bool): If True, draw the text inverted. Default is False. font_data (str | byterray): The path to the font .bin file or memoryview. Default is None.

Returns: Area: The area that was drawn to.

text14

text14(canvas, s, x, y, c=1, scale=1, inverted=False, font_data=None)

Place text on the canvas with a 14 pixel high font. Breaks on to next line. Does not break on line going off canvas.

Args: canvas (Canvas): The DisplayDriver, FrameBuffer, or other canvas-like object to draw on. s (str): The text to draw. x (int): The x position to start drawing the text. y (int): The y position to start drawing the text. c (int): The color to draw the text in. Default is 1. scale (int): The scale factor to draw the text at. Default is 1. inverted (bool): If True, draw the text inverted. Default is False. font_data (str | byterray): The path to the font .bin file or memoryview. Default is None.

Returns: Area: The area that was drawn to.

text16

text16(canvas, s, x, y, c=1, scale=1, inverted=False, font_data=None)

Place text on the canvas with a 16 pixel high font. Breaks on to next line. Does not break on line going off canvas.

Args: canvas (Canvas): The DisplayDriver, FrameBuffer, or other canvas-like object to draw on. s (str): The text to draw. x (int): The x position to start drawing the text. y (int): The y position to start drawing the text. c (int): The color to draw the text in. Default is 1. scale (int): The scale factor to draw the text at. Default is 1. inverted (bool): If True, draw the text inverted. Default is False. font_data (str | byterray): The path to the font .bin file or memoryview. Default is None.

Returns: Area: The area that was drawn to.

arc

arc(canvas, x, y, r, a0, a1, c)

Arc drawing function. Will draw a single pixel wide arc with a radius r centered at x, y from a0 to a1.

Parameters:

Name Type Description Default
x int

X-coordinate of the arc's center.

required
y int

Y-coordinate of the arc's center.

required
r int

Radius of the arc.

required
a0 float

Starting angle in degrees.

required
a1 float

Ending angle in degrees.

required
c int

color.

required

Returns:

Type Description
Area

The bounding box of the arc.

blit

blit(canvas, source, x, y, key=-1, palette=None)

Blit a source to the canvas at the specified x, y location.

Parameters:

Name Type Description Default
source FrameBuffer

Source FrameBuffer object.

required
x int

X-coordinate to blit to.

required
y int

Y-coordinate to blit to.

required
key int

Key value for transparency (default: -1).

-1
palette Palette

Palette object for color translation (default: None).

None

Returns:

Type Description
Area

The bounding box of the blitted area.

blit_rect

blit_rect(canvas, buf, x, y, w, h)

Blit a rectangular area from a buffer to the canvas. Uses the canvas's blit_rect method if available, otherwise writes directly to the buffer.

Parameters:

Name Type Description Default
buf memoryview

Buffer to blit. Must already be byte-swapped if necessary.

required
x int

X-coordinate to blit to.

required
y int

Y-coordinate to blit to.

required
w int

Width of the area to blit.

required
h int

Height of the area to blit.

required

Returns:

Type Description
Area

The bounding box of the blitted area.

blit_transparent

blit_transparent(canvas, buf, x, y, w, h, key)

Blit a buffer with transparency.

Parameters:

Name Type Description Default
buf memoryview

Buffer to blit.

required
x int

X-coordinate to blit to.

required
y int

Y-coordinate to blit to.

required
w int

Width of the area to blit.

required
h int

Height of the area to blit.

required
key int

Key value for transparency.

required

Returns:

Type Description
Area

The bounding box of the blitted area.

circle

circle(canvas, x0, y0, r, c, f=False)

Circle drawing function. Will draw a single pixel wide circle centered at x0, y0 and the specified r.

Parameters:

Name Type Description Default
x0 int

Center x coordinate

required
y0 int

Center y coordinate

required
r int

Radius

required
c int

Color

required
f bool

Fill the circle (default: False)

False

Returns:

Type Description
Area

The bounding box of the circle.

ellipse

ellipse(canvas, x0, y0, r1, r2, c, f=False, m=15, w=None, h=None)

Midpoint ellipse algorithm Draw an ellipse at the given location. Radii r1 and r2 define the geometry; equal values cause a circle to be drawn. The c parameter defines the color.

The optional f parameter can be set to True to fill the ellipse. Otherwise just a one pixel outline is drawn.

The optional m parameter enables drawing to be restricted to certain quadrants of the ellipse. The LS four bits determine which quadrants are to be drawn, with bit 0 specifying Q1, b1 Q2, b2 Q3 and b3 Q4. Quadrants are numbered counterclockwise with Q1 being top right.

Parameters:

Name Type Description Default
x0 int

Center x coordinate

required
y0 int

Center y coordinate

required
r1 int

x radius

required
r2 int

y radius

required
c int

color

required
f bool

Fill the ellipse (default: False)

False
m int

Bitmask to determine which quadrants to draw (default: 0b1111)

15
w int

Width of the ellipse (default: None)

None
h int

Height of the ellipse (default: None)

None

Returns:

Type Description
Area

The bounding box of the ellipse.

fill

fill(canvas, c)

Fill the entire canvas with a color. Uses the canvas's fill method if available, otherwise calls the fill_rect function.

Parameters:

Name Type Description Default
c int

color.

required

Returns:

Type Description
Area

The bounding box of the filled area.

fill_rect

fill_rect(canvas, x, y, w, h, c)

Filled rectangle drawing function. Draws a filled rectangle starting at x, y and extending w, h pixels. Uses the canvas's fill_rect method if available, otherwise calls the pixel function for each pixel.

Parameters:

Name Type Description Default
x int

X-coordinate of the top-left corner of the rectangle.

required
y int

Y-coordinate of the top-left corner of the rectangle.

required
w int

Width of the rectangle.

required
h int

Height of the rectangle.

required
c int

color

required

Returns:

Type Description
Area

The bounding box of the filled area.

gradient_rect

gradient_rect(canvas, x, y, w, h, c1, c2=None, vertical=True)

Fill a rectangle with a gradient.

Parameters:

Name Type Description Default
x int

X-coordinate of the top-left corner of the rectangle.

required
y int

Y-coordinate of the top-left corner of the rectangle.

required
w int

Width of the rectangle.

required
h int

Height of the rectangle.

required
c1 int

565 encoded color for the top or left edge.

required
c2 int

565 encoded color for the bottom or right edge. If None or the same as c1, fill_rect will be called instead.

None
vertical bool

If True, the gradient will be vertical. If False, the gradient will be horizontal.

True

Returns:

Type Description
Area

The bounding box of the filled area.

hline

hline(canvas, x0, y0, w, c)

Horizontal line drawing function. Will draw a single pixel wide line.

Parameters:

Name Type Description Default
x0 int

X-coordinate of the start of the line.

required
y0 int

Y-coordinate of the start of the line.

required
w int

Width of the line.

required
c int

color.

required

Returns:

Type Description
Area

The bounding box of the line.

line

line(canvas, x0, y0, x1, y1, c)

Line drawing function. Will draw a single pixel wide line starting at x0, y0 and ending at x1, y1.

Parameters:

Name Type Description Default
x0 int

X-coordinate of the start of the line.

required
y0 int

Y-coordinate of the start of the line.

required
x1 int

X-coordinate of the end of the line.

required
y1 int

Y-coordinate of the end of the line.

required
c int

color.

required

Returns:

Type Description
Area

The bounding box of the line.

pixel

pixel(canvas, x, y, c)

Draw a single pixel at the specified x, y location. Uses the canvas's pixel method if available, otherwise writes directly to the buffer.

Parameters:

Name Type Description Default
x int

X-coordinate of the pixel.

required
y int

Y-coordinate of the pixel.

required
c int

color.

required

Returns:

Type Description
Area

The bounding box of the pixel.

poly

poly(canvas, x, y, coords, c, f=False)

Given a list of coordinates, draw an arbitrary (convex or concave) closed polygon at the given x, y location using the given color.

The coords must be specified as an array of integers, e.g. array('h', [x0, y0, x1, y1, ... xn, yn]) or a list or tuple of points, e.g. [(x0, y0), (x1, y1), ... (xn, yn)].

The optional f parameter can be set to True to fill the polygon. Otherwise, just a one-pixel outline is drawn.

Parameters:

Name Type Description Default
x int

X-coordinate of the polygon's position.

required
y int

Y-coordinate of the polygon's position.

required
coords list

List of coordinates.

required
c int

color.

required
f bool

Fill the polygon (default: False).

False

Returns:

Type Description
Area

The bounding box of the polygon.

polygon

polygon(canvas, points, x, y, color, angle=0, center_x=0, center_y=0)

Draw a polygon on the canvas.

Parameters:

Name Type Description Default
points list

List of points to draw.

required
x int

X-coordinate of the polygon's position.

required
y int

Y-coordinate of the polygon's position.

required
color int

color.

required
angle float

Rotation angle in radians (default: 0).

0
center_x int

X-coordinate of the rotation center (default: 0).

0
center_y int

Y-coordinate of the rotation center (default: 0).

0

Raises:

Type Description
ValueError

If the polygon has less than 3 points.

Returns:

Type Description
Area

The bounding box of the polygon.

rect

rect(canvas, x0, y0, w, h, c, f=False)

Rectangle drawing function. Will draw a single pixel wide rectangle starting at x0, y0 and extending w, h pixels.

Parameters:

Name Type Description Default
x0 int

X-coordinate of the top-left corner of the rectangle.

required
y0 int

Y-coordinate of the top-left corner of the rectangle.

required
w int

Width of the rectangle.

required
h int

Height of the rectangle.

required
c int

color.

required
f bool

Fill the rectangle (default: False).

False

Returns:

Type Description
Area

The bounding box of the rectangle.

round_rect

round_rect(canvas, x0, y0, w, h, r, c, f=False)

Rounded rectangle drawing function. Will draw a single pixel wide rounded rectangle starting at x0, y0 and extending w, h pixels with the specified radius.

Parameters:

Name Type Description Default
x0 int

X-coordinate of the top-left corner of the rectangle.

required
y0 int

Y-coordinate of the top-left corner of the rectangle.

required
w int

Width of the rectangle.

required
h int

Height of the rectangle.

required
r int

Radius of the corners.

required
c int

color.

required
f bool

Fill the rectangle (default: False).

False

Returns:

Type Description
Area

The bounding box of the rectangle.

triangle

triangle(canvas, x0, y0, x1, y1, x2, y2, c, f=False)

Triangle drawing function. Draws a single pixel wide triangle with vertices at (x0, y0), (x1, y1), and (x2, y2).

Parameters:

Name Type Description Default
x0 int

X-coordinate of the first vertex.

required
y0 int

Y-coordinate of the first vertex.

required
x1 int

X-coordinate of the second vertex.

required
y1 int

Y-coordinate of the second vertex.

required
x2 int

X-coordinate of the third vertex.

required
y2 int

Y-coordinate of the third vertex.

required
c int

color.

required
f bool

Fill the triangle (default: False).

False

Returns:

Type Description
Area

The bounding box of the triangle.

vline

vline(canvas, x0, y0, h, c)

Horizontal line drawing function. Will draw a single pixel wide line.

Parameters:

Name Type Description Default
x0 int

X-coordinate of the start of the line.

required
y0 int

Y-coordinate of the start of the line.

required
h int

Height of the line.

required
c int

color.

required

Returns:

Type Description
Area

The bounding box of the line.