Processing中除setup()外,能否在draw()外部调用代码?含translate()疑问
Hey there! No need to apologize for being a newbie—we all started with these questions, so let's break this down clearly for you 😊
Can code be executed outside of draw() (other than in setup())?
Absolutely! Processing gives you several ways to run code outside the draw() loop, beyond just setup():
Custom functions: You can define your own reusable functions and call them wherever you need—whether that's in
setup(), in event handlers, or even inside other custom functions. These let you organize your code and run logic without tying it directly todraw()'s continuous loop.
Example:// Custom function defined outside setup/draw void greetUser() { println("Hello from a custom function!"); } void setup() { size(400, 400); greetUser(); // Call the function in setup } void draw() { background(200); // Call the function only once every second if (frameCount % 60 == 0) { greetUser(); } }Event handler functions: Processing has built-in functions that trigger when specific user actions happen—these run independently of
draw()'s loop. Examples includemousePressed(),keyReleased(),mouseMoved(), andwindowResized().
Example:void setup() { size(400, 400); } void draw() { background(200); ellipse(width/2, height/2, 50, 50); } // Runs only when the mouse is clicked—outside draw()'s loop void mousePressed() { fill(random(255), random(255), random(255)); println("Mouse clicked! Changed ellipse color."); }Global scope initialization: You can also declare variables or run simple initialization code directly in the global scope (outside all functions). This code runs before
setup()even starts, so it's great for setting up constants or precomputing values.
Example:// Runs before setup() int circleSize = 50; String appName = "My First Sketch"; void setup() { size(400, 400); println(appName); // Uses the global variable }
About translate() and persistent transformations
You mentioned the note that translate() resets when draw() loops again—this is because Processing resets the drawing state (like transformations, colors, and styles) at the start of each draw() iteration. If you want to keep a transformation or avoid resetting it, you have a couple options:
- Call
translate()once insetup()—sincesetup()only runs once, that transformation will persist across alldraw()frames (though this might not be what you want if you need dynamic changes). - Use
pushMatrix()andpopMatrix()insidedraw()to save and restore the transformation state. This lets you apply temporary transformations without affecting the rest of yourdraw()code:void draw() { background(200); pushMatrix(); // Save the current transformation state translate(mouseX, mouseY); // Apply temporary translation rect(-25, -25, 50, 50); popMatrix(); // Restore the original state for the next frame }
Don't overcomplicate things as a beginner—start with custom functions and event handlers to organize your code outside draw(); you'll get the hang of the execution flow quickly!
内容的提问来源于stack exchange,提问作者phlaxyr

