keyReleased()+ saveFrame() functions not doing anything

Hi all, I’m an extreme novice coder taking a generative art course.

I am trying to save the image I created with a series of for loops and random functions (I included one in the script below) but it is not saving the image in the Processing>>document folder at all, or anywhere from what I can tell.

int alpha = 10;``
void setup() {
  size (1280, 548);
  background(205, 198, 188);
  noFill();
  
   for (int i = 0; i < 40; i++) {
    if (i < 30 && i > 15) {
      stroke(150, 144, 132, alpha);
    } else {
      stroke (150, 144, 132);
    }

    quad(994 + random(-20, 20),
      415 + random(-20, 20),
      1108 + random(-20, 20),
      415 + random(-20, 20),
      1108 + random(-20, 20),
      520 + random(-20, 20),
      994 + random(-20, 20),
      520 + random(-20, 20));
  }
}

void keyReleased() {
  if (key == 's') {
    saveFrame();
  }
}

If you run it and it does create an image or if you see any egregious errors in the keyReleased() or saveFrame() syntax please let me know!

1 Like

Update:
I was able to fix it by not using the keyReleased() function and just doing a saveFrame() instead within setup().

int alpha = 10;


void setup() {
  size (1280, 548);
  background(205, 198, 188);
  noFill();

for (int i = 0; i < 40; i++) {
    if (i < 30 && i > 15) {
      stroke(150, 144, 132, alpha);
    } else {
      stroke (150, 144, 132);
    }

    quad(994 + random(-20, 20),
      415 + random(-20, 20),
      1108 + random(-20, 20),
      415 + random(-20, 20),
      1108 + random(-20, 20),
      520 + random(-20, 20),
      994 + random(-20, 20),
      520 + random(-20, 20));
  }
  saveFrame("sketch-######.png");
}

I hope this helps anyone else having the same issue.

1 Like

Hi and welcome

Good place to start

Hi

void keyPressed()
{
  if(key=='s') saveFrame("any.png");
}

I think that you need a draw() function to make keyPressed and keyReleased etc. work

2 Likes