dimanche 19 août 2018

Javascript canvas green ellipse left and red right side

I want to make green ellipses on left side and red ellipses on right side. I use random function to fill the canvas. I use if statements for my purpose. Maybe switch case would be better for this task? This syntax only generate pink dots, whats wrong?

var spotPos = {
  x:300,
  y:200
}

var spotCol = {
  r:0,
  g:0,
  b:0
}

function setup() {
  createCanvas(600,400);
  background(0);
}

function draw() {
  spotPos.x = random(0,width);
  spotPos.y = random(0,height);
  //spotCol.r = random(60,255);
  noStroke()
  fill(spotCol.r, spotCol.g, spotCol.b)
  ellipse(spotPos.x, spotPos.y, 25, 25);
  if(spotPos.x < 300) {
    spotCol.b = 255;
  } else if(spotPos.x > 300) {
    spotCol.r = 255;
  }

}




Aucun commentaire:

Enregistrer un commentaire