xxxxxxxxxx
42
class Star {
constructor() {
this.pos = createVector(random(-width, width), random(-height, height));
this.z = random(width);
this.color = color(random(255), random(255), random(255));
this.speed = 21;
}
update() {
this.z -= this.speed;
}
render() {
let cX = map(this.pos.x / this.z, 0, 1, 0, width);
let cY = map(this.pos.y / this.z, 0, 1, 0, height);
// When the mouse gets the middle of the canvas, the stars will take the form of a triangle:
if(mouseX > width/4 && mouseX < (width - width / 4)) {
let d1 = map(this.z, 0, width, 15, 0);
let d2 = map(this.z, 0, width, 7.5, 0);
noStroke();
fill(this.color);
triangle(cX, cY, cX - d1, cY, cX - d2, cY - d1);
// Otherwise, the stars will take the form of a circle:
} else {
let r = map(this.z, 0, width, 12, 0);
noStroke();
fill(this.color);
noStroke();
ellipse(cX, cY, r, r);
}
}
reset() {
this.pos = createVector(random(-width, width), random(-height, height));
this.z = random(width);
}
}