mojo.canvas

from mojo.canvas import Canvas

A vanilla object that sends all user input events to a given delegate.

Canvas(posSize,
       delegate=None,
       canvasSize=(1000, 1000),
       acceptsMouseMoved=False,
       hasHorizontalScroller=True,
       hasVerticalScroller=True,
       autohidesScrollers=False,
       backgroundColor=None,
       drawsBackground=True)

Methods

All events a delegate could have that can be used:

  • draw()
    Callback when the canvas get drawn.
  • becomeFirstResponder(event)
    Callback when the canvas becomes the first responder, when it starts to receive user interaction callbacks.
  • resignFirstResponder(event)
    Callback when the canvas resigns the first responder, when the canvas will not longer receive user interaction callbacks. 
  • mouseDown(event)
    Callback when the user hit the canvas with the mouse. 
  • mouseDragged(event)
    Callback when the user drag the mouse around in the canvas. 
  • mouseUp(event)
    Callback when the user lifts up the mouse from the canvas. 
  • mouseMoved(event)
    Callback when the user moves the mouse in de canvas. Be careful this is called frequently. (only when accepsMouseMoved is set True)
  • rightMouseDown(event)
    Callback when the user clicks inside the canvas with the right mouse button. 
  • rightMouseDragged(event)
    Callback when the users is dragging in the canvas with the right mouse button down.  
  • rightMouseUp(event)
    Callback when the users lift up the right mouse button from the canvas. 
  • keyDown(event)
    Callback when the users hits a key.
    The event object has a characters() method returns the pressed character key.
  • keyUp(event)
    Callback when the users lift up the key. 
  • flagChanged(event)
    Callback when the users changed a modifier flag:
    command  shift  control  alt

Example

from mojo.canvas import Canvas
from mojo.drawingTools import *
from vanilla import *

class ExampleWindow:

    def __init__(self):
        self.size = 50

        self.w = Window((400, 400), minSize=(200, 200))
        self.w.slider = Slider((10, 5, -10, 22),
                          value=self.size,
                          callback=self.sliderCallback)
        self.w.canvas = Canvas((0, 30, -0, -0), delegate=self)
        self.w.open()

    def sliderCallback(self, sender):
        self.size = sender.get()
        self.w.canvas.update()

    def draw(self):
        rect(10, 10, self.size, self.size)

ExampleWindow()