xxxxxxxxxx
43
// https://discourse.processing.org/t/make-object-array-move-as-one-unit/15994
let lines = [];
let many = 10;
function setup() {
createCanvas(600, 600, WEBGL);
for (let i = 0; i < many; i++) lines[i] = new Line();
}
function draw() {
background(255, 255, 255);
for (i = 0; i < lines.length; i++) lines[i].display();
}
class Line {
constructor() {
this.x1 = random(-150, -160);
this.y1 = random(50, 100);
this.x2 = random(-135, -175);
this.y2 = 150;
this.speed = 3;
}
display() {
this.move();
stroke(0);
line(this.x1, this.y1, this.x2, this.y2);
}
move() {
this.x1 += random(-this.speed, this.speed);
this.y1 += random(-this.speed, this.speed);
this.x2 += random(-this.speed, this.speed);
this.y2 += random(-this.speed, this.speed);
this.x1 = constrain(this.x1, 0, width);
this.x2 = constrain(this.x2, 0, width);
this.y1 = constrain(this.y1, 0, height);
this.y2 = constrain(this.y2, 0, width);
}
}