LA2 – 1.1 Conditionals and If Statements


function setup() {
  createCanvas(400, 200);
}

function draw() {
  background(220);
  if(mouseY > 100) {
    background(0)
  }
  var x = 160;
  rectMode(CENTER);
  fill('blue');
  if(mouseX > 200)
    x = 60; 
  rect(width / 2, height / 2, x, x);
}

1.2 Conditionals and If, Else If, Else Statements

function setup() {
  createCanvas(400, 200);
}

function draw() {
    var redLight = 'white';
    var yellowLight = 'white';
    var greenLight = 'white';

    background('pink');
    rectMode(CENTER);
    fill(140);
    rect(width / 2, height / 2,50,100);

    if (mouseY > (height / 2 + 20)) {
        greenLight = 'green'
    }
    else if (mouseY > height / 2 - 20){ 
        yellowLight = 'yellow'; 
    }
    else {
        redLight = 'red';
    }
    fill(redLight);
    ellipse(width / 2, height / 2 - 25, 20, 20);
    fill(yellowLight);
    ellipse(width / 2, height / 2, 20, 20);
    fill(greenLight);
    ellipse(width / 2, height / 2 + 25, 20, 20);
}

Vandaag heb ik tot en met 1.3 afgemaakt. Het ging prima.

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  rectMode(CENTER);
  
  if ((mouseX > width/2 - 50) && (mouseX < width/2 + 50) &&(mouseY > height / 2 - 50) && (mouseY < height/2 + 50)){ fill(255,0,0);
}
else{
    fill('pink')
  }
      rect(width/2, height/2, 100,100);
}