xxxxxxxxxx
58
let Lines = [];
let linecount = 0; // zero lines in the beginning
let randomColor;
function setup() {
createCanvas(400, 400);
frameRate(2);
}
function draw() {
background(0);
for (var i = 0; i < Lines.length; i++) {
Lines[i].move();
Lines[i].display();
Lines[i].checkBoundaries();
}
}
function mousePressed() {
let choice = random(0, 4);
let myLine = new Line(random(0, width), random(0, height), random(-5, 5), random(50));
//Lines[linecount++] = myLine;
Lines.push(myLine);
linecount++;
}
class Line {
constructor(x, y, dx, dy) {
this.x = x;
this.y = y;
//this.c = c
this.directionX = dx;
this.directionY = dy;
}
move() {
this.x = this.x + this.directionX;
this.y = this.y + this.directionY;
}
checkBoundaries() {
if (this.x > width || this.x < 0) {
this.directionX = -this.directionX;
}
if (this.y > width || this.y < 0) {
this.directionY = -this.directionY;
}
}
display() {
randomColor = color(random(255), random(255), random(255));
stroke(color(randomColor));
line(this.x, this.y, random(0, 400), random(0, 400));
}
}