Function and Vector3

This sketch looks exactly like the one from last week but uses Vector3 to store colour and also keep the setup() and draw() function as small as possible.

let w = 20;

function drawRandLine(x,y,c){
  y *= 20;
  x *= 20;
  pop();
  fill(0);
  stroke(0);
  rect(x,y,20,20);
  push();
  stroke(c.x,c.y,c.z);
  if (random(1) < 0.5) {
      line(x, y, x + 20, y+20);
    } else {
      line(x, y+20, x+20, y);
    }
}

function getRandomColour(){
  let r = random(255);
  let g = random(255);
  let b = random(255);
  return createVector(r,g,b);
}

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

function draw() {
  frameRate(6);
    let randColour = getRandomColour();
    for(let i=0;i<25;i++){
    drawRandLine( round(random(0, 20)), round(random(0, 20)),randColour);
  }
}
Code language: JavaScript (javascript)

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *