xxxxxxxxxx
73
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
animate() {
this.x = this.x + random(5) - 3;
this.y = this.y + random(5) - 3;
if (this.x > width) {
this.x = 0;
}
if (this.x < 0) {
this.x = width;
}
if (this.y > height) {
this.y = 0;
}
if (this.y < 0) {
this.y = height;
}
}
}
class Triangle {
constructor(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
drawTriangle() {
line(this.a.x, this.a.y, this.b.x, this.b.y);
line(this.b.x, this.b.y, this.c.x, this.c.y);
line(this.c.x, this.c.y, this.a.x, this.a.y);
}
animate() {
this.a.animate();
this.b.animate();
this.c.animate();
}
}
let tris = [];
function setup() {
createCanvas(400, 400);
for(let i = 0; i < 10; i++) {
tris[i] = new Triangle(
new Point(random(width), random(height)),
new Point(random(width), random(height)),
new Point(random(width), random(height)));
}
}
function draw() {
background(220);
for(t of tris) {
t.animate();
t.drawTriangle();
}
}