Canvas Actor classes

The Canvas Actor classes provide classes to create drawings/images and to display them.

PainterActor class

class PainterActor(*args, **kwargs)

Bases: sphof.canvas_actors.Painter, sphof.actors.Actor

The PainterActor class is an Actor with all the Painter class’s methods and providing methods to handover the image to a LeadActor.

example:

from sphof import PainterActor
from random import randint
 
class MyPainter(PainterActor):

    def setup(self):
        self.count = 0                   # initialize counter

    def update(self):
        self.count += 1                  # increment counter
        if self.count == self.get_width():
            self.count = 0               # reset counter
            self.send_img()              # emit the imgID so a 
                                         # LeadActor could 
                                         # display it
    def draw(self):
        start = (self.count, 0)          # start position
        end = ( self.count, 
                self.get_height())       # end position
        color = (
                randint(7,210),          # red
                randint(16,210),         # green
                randint(70,210)          # blue
                )
        self.line((start, end), color, 2)# draw line

To display the PainterActor’s drawing you need to send the image to CanvasActor which can display the image on screen. In order to send an image use the send_img() method.

The send_img() method emits a ‘imgID’ signal containing a pointer to the image of this Actor. It calls reset() so the actor can paint on a new canvas.

This class has many methods inherited from the sphof.Painter class, ie:

Each class’s extra methods are documented below.

send_img()

Sends the image as a signal to any subscribers using the ‘imgID’ emitter. The canvas is reset after the image is sent!

CanvasActor class

class CanvasActor(*args, **kwargs)

Bases: sphof.canvas_actors.Painter, sphof.actors.LeadActor

The CanvasActor class implements methods for drawing on a canvas (screen) similar to the PainterActor

To display drawings of PainterActors you need to receive the image of a PainterActor by subscribing to the ‘imgID’ signal emitter of the PainterActor.

example:

 from sphof import CanvasActor
 
 class MyCanvas(CanvasActor):

     def setup(self):
         self.painter_img = None
         self.register_int("PaintingID", 0, "rs") # create a sensor for image ids
         # ... setup the painter here

     def on_peer_enter(self, peer, name, headers):
         if name == "PainterName":       # PainterName is the name of your PainterActor
             self.signal_subscribe(self.uuid(), "PaintingID", peer, "imgID")

     def on_peer_signaled(self, peer, name, data):
         if name == "PainterName":
             self.painter_img = self.get_img_from_id(self.get_value("PaintingID"))

     def draw(self):
         if self.painter_img:
             self.draw_img(self.painter_img)
la = MyCanvas()
la.run()
get_img_from_id(imgID)

Get the image from the given imgID

draw_img(img, x=0, y=0)

Draw the image at position x,y

LonePainterActor class

class LonePainterActor(*args, **kwargs)

Bases: sphof.canvas_actors.Painter, sphof.actors.LoneActor

Painter class

class Painter(*args, **kwargs)

The Painter class provides simple methods for drawing, ie:

The default width and height are 200 by 600 pixels.

Each class’s method is documented below

reset()

Clears the image to the background color

get_width()

Returns the width of the canvas

set_width(width)

Set the width of the canvas, it will reset your image! :param width: Width of the canvas in pixels

get_height()

Returns the height of the canvas

set_height(height)

Set the height of the canvas, it will reset your image! :param width: Width of the canvas in pixels

arc(*args, **kwargs)

Draws an arc (a portion of a circle outline) between the start and end angles, inside the given bounding box.

Parameters:
  • xy – Four points to define the bounding box. Sequence of [(x0, y0), (x1, y1)] or [x0, y0, x1, y1].
  • start – Starting angle, in degrees. Angles are measured from 3 o’clock, increasing clockwise.
  • end – Ending angle, in degrees.
  • fill – Color to use for the arc.
bitmap(*args, **kwargs)

Draws a bitmap (mask) at the given position, using the current fill color for the non-zero portions. The bitmap should be a valid transparency mask (mode “1”) or matte (mode “L” or “RGBA”).

This is equivalent to doing image.paste(xy, color, bitmap).

To paste pixel data into an image, use the paste() method on the image itself.

chord(*args, **kwargs)

Same as arc(), but connects the end points with a straight line.

Parameters:
  • xy – Four points to define the bounding box. Sequence of [(x0, y0), (x1, y1)] or [x0, y0, x1, y1].
  • outline – Color to use for the outline.
  • fill – Color to use for the fill.
ellipse(*args, **kwargs)

Draws an ellipse inside the given bounding box.

Parameters:
  • xy – Four points to define the bounding box. Sequence of either [(x0, y0), (x1, y1)] or [x0, y0, x1, y1].
  • outline – Color to use for the outline.
  • fill – Color to use for the fill.
line(*args, **kwargs)

Draws a line between the coordinates in the xy list.

Parameters:
  • xy – Sequence of either 2-tuples like [(x, y), (x, y), ...] or numeric values like [x, y, x, y, ...].
  • fill – Color to use for the line.
  • width – The line width, in pixels. Note that line joins are not handled well, so wide polylines will not look good.
pieslice(*args, **kwargs)

Same as arc, but also draws straight lines between the end points and the center of the bounding box.

Parameters:
  • xy – Four points to define the bounding box. Sequence of [(x0, y0), (x1, y1)] or [x0, y0, x1, y1].
  • start – Starting angle, in degrees. Angles are measured from 3 o’clock, increasing clockwise.
  • end – Ending angle, in degrees.
  • fill – Color to use for the fill.
  • outline – Color to use for the outline.
point(*args, **kwargs)

Draws points (individual pixels) at the given coordinates.

Parameters:
  • xy – Sequence of either 2-tuples like [(x, y), (x, y), ...] or numeric values like [x, y, x, y, ...].
  • fill – Color to use for the point.
polygon(*args, **kwargs)

Draws a polygon.

The polygon outline consists of straight lines between the given coordinates, plus a straight line between the last and the first coordinate.

Parameters:
  • xy – Sequence of either 2-tuples like [(x, y), (x, y), ...] or numeric values like [x, y, x, y, ...].
  • outline – Color to use for the outline.
  • fill – Color to use for the fill.
rectangle(*args, **kwargs)
Draws a rectangle.
Parameters:
  • xy – Four points to define the bounding box. Sequence of either [(x0, y0), (x1, y1)] or [x0, y0, x1, y1]. The second point is just outside the drawn rectangle.
  • outline – Color to use for the outline.
  • fill – Color to use for the fill.
text(xy, text, fill)

Draws the string at the given position.

Parameters:
  • xy – Top left corner of the text.
  • text – Text to be drawn.
  • fill – Color to use for the text.
textsize(text)

Return the size of the given string, in pixels.

Parameters:text – Text to be measured.