r/processing • • 13d ago

Help request drawing program

i’m creating a drawing program within processing and i’m trying to add a way to change the background/canvas color. within the draw function, i have a color wheel that changes the brush color, as well as a color wheel that changes the canvas color. all is going smoothly, except when i try to put it all together. if i draw the canvas within the void setup, the user is unable to change its color. however, if i draw the canvas within the void draw, it redraws itself between every iteration of the brushstroke, so the brush only ever shows up at its current state and never leaves a mark behind.

i think i worded this oddly but hopefully it makes sense. can anybody help me figure out how to allow the canvas color to change while not “erasing” the brush strokes?

4 Upvotes

7 comments sorted by

View all comments

6

u/forgotmyusernamedamm 13d ago

There's a couple ways to solve this. The easiest is to use PGraphics. With a pgraphic you can save your painting into an image with no background. Then show that image over a background with a color you can change. Here's sample code.

PGraphics pg;
color bgColor; 
void setup() {
  size(500, 500);
  pg = createGraphics(500, 500);
  bgColor = color(200, 100, 20); 
}

void draw() {
  background(bgColor); 
  pg.beginDraw();
  pg.stroke(0, 200, 100); 
  pg.strokeWeight(6); 
  if(mousePressed){
  pg.line(mouseX, mouseY, pmouseX, pmouseY); 
  }
  pg.endDraw();
  image(pg, 0, 0); 
}
void keyPressed(){ 
  if(key == ' '){
    bgColor = color(random(255), random(255),random(255)); 
  }
}

2

u/EthanHermsey 13d ago

Second it, this is the way to do it.