Change line() color in a nested while-loop

@anon98404100 a few things:

  • Processing, and I strongly believe P5.js as well, only update their screens at the end of the draw loop
  • You’re changing the stroke AFTER you draw the line, then draw restarts, set stroke to 0 and draws again
  • while (or if for that matter) x == 40 AND x == 60 never happens. x cannot be both 40 And 60 at the same time, use or: ||

Here’s your code with the proper changes, but note it does not draw all the lines at once, for that you’d need to store the x values in an array of some sorts and use a for loop to draw them

var x = 20;
var stap = 20;
var stop = 600;

function setup () {
  createcanvas(700, 700);
}

function draw () {

  stroke(0);
  if (x < stop) {
    if (x == 40 || x == 60) {
      stroke(255);
    }
    line(x, 60, x, 80);
    x += stap;
  }
}

Hope this solves your question :slight_smile:

1 Like