Hey guys,
I’m trying to build a simple pattern generator to create posters for some design idea. I’m using mainly p5.js and also the p5.gui library to have an interface to control some functions.
During this process I came across one problem I couldn’t solve so far. I have a text input field, and everytime I type something it also automatically updates the whole draw function, which changes the background pattern. But I somehow want to find a way, to only update my whole sketch with the created reload button.
I also tried to make another simpflified version of the generator to see if I did something wrong along the way, but seems I still can’t figure it out. Here is how the code looks in simplified version with only the base functions:
//GUI options
var forms = ['rectangle', 'ellipse', 'triangle'];
var sentence = 'Here comes your text';
//reload button
var reload;
//grid
var col = 10;
var row = 10;
function setup() {
  //Creating Canvas
  createCanvas(600,800);
  //Creating the GUI
  gui = createGui('Random Pattern', width/2+50, 50);
  gui.addGlobals('forms', 'sentence');
  //Reload button
  reload = createButton('Reload');
  reload.position(50,20);
  reload.mousePressed(redraw);
  //not drawing the whole time
  noLoop();
}
function draw() {
  //General Style
  background(220,220,220);
  textSize(50);
  fill(180);
  noStroke();
  //Drawing in a Grid
  for (var i = 0; i < col; i++) {
    for (var j = 0; j < row; j++) {
        var xval = int(random(13));
        var yval = int(random(17));
        var x = xval * 50;
        var y = yval * 50;
        switch (forms) {
          case 'rectangle':
          push();
          fill(0);
          noStroke();
          rect(x,y,50,50);
          pop();
          break;
          case 'ellipse':
          push();
          translate(25,25);
          noStroke();
          fill(255);
          ellipse(x,y,50,50);
          pop();
          break;
          case 'triangle':
          push();
          noStroke();
          fill(155,155,155);
          triangle(x,y+50,x+25,y,x+50,y+50);
          pop();
          break;
        }
    }
  }
  text(sentence.toUpperCase(), 50, 200, 500, 400);
}
Maybe someone had a similar problem already or has an idea how to solve this?


