In Processing\'s Java Mode, you use PGraphics objects by declaring them globally, setting them up with createGraphics() in setup() and then referring to them in draw().
It can be fixed using global
. Be sure to use global where you initialise the canvas too, otherwise it's a local variable and your global canvas reference may still be None
Here's a basic example:
# global reference
canvas = None
def setup():
size(300, 300)
# setup global canvas
global canvas
canvas = createGraphics(300, 300)
canvas.beginDraw()
canvas.background(0);
canvas.noStroke()
canvas.blendMode(DIFFERENCE)
canvas.ellipse(150,150,150,150)
canvas.endDraw()
def draw():
# reference global canvas to draw
global canvas
image(canvas,0,0)
def mouseDragged():
diameter = dist(mouseX,mouseY,pmouseX,pmouseY)
# reference global canvas to update graphics
global canvas
canvas.beginDraw()
canvas.ellipse(mouseX,mouseY,diameter,diameter)
canvas.endDraw()