You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 to draw()'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 include mousePressed(), keyReleased(), mouseMoved(), and windowResized().
    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:

  1. Call translate() once in setup()—since setup() only runs once, that transformation will persist across all draw() frames (though this might not be what you want if you need dynamic changes).
  2. Use pushMatrix() and popMatrix() inside draw() to save and restore the transformation state. This lets you apply temporary transformations without affecting the rest of your draw() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:28:58