Using Processing PGraphics in Python Mode?

前端 未结 1 1828
无人及你
无人及你 2021-01-28 20:19

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().

1条回答
  •  清酒与你
    2021-01-28 21:23

    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()
    

    0 讨论(0)
提交回复
热议问题