xxxxxxxxxx
150
class Triangulation{
constructor(x1,y1,x2,y2,fontSize=12,ssMin=50,ssMax=80){
this.maxLifeTime = 30;
this.points = [];
this.spawnRate = 10;
this.shapes = [];
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.fontSize = fontSize;
this.shapeSizeMin = ssMin;
this.shapeSizeMax = ssMax;
append(this.points, new Point
(this.x1,this.y1,this.x2,this.y2,this.maxLifeTime));
}
help(){
console.log('constructor(x1,y1,x2,y2,fontSize,shape size min, shape size max)');
}
run(){
//background
stroke(180);
fill(0);
rect(this.x1,this.y1,this.x2,this.y2);
//spawn new shapes
if(round(random((50))) == 0){
append(this.shapes, new Shape(
this.x1,this.y1,this.x2,this.y2,this.maxLifeTime,this.shapeSizeMin,this.shapeSizeMax));
}
if(this.shapes.size != 0){
for(let i = this.shapes.length-1; i >= 0; i--){
let s = this.shapes[i];
s.update();
s.render(this.fontSize);
if(!s.alive){
this.shapes.splice(i,1);
}
}
}
//spawn new points
if(frameCount % this.spawnRate == 0){
append(this.points, new Point(this.x1,this.y1,this.x2,this.y2,this.maxLifeTime));
}
for(let i = this.points.length-1; i >= 0; i--){
let p = this.points[i];
p.update();
p.render(this.fontSize,this.points);
if(!p.alive){
this.points.splice(i,1);
}
}
// beginShape();
// for(let p of points){
// vertex(p.pos.x,p.pos.y);
// }
// vertex(points[0].pos.x,points[0].pos.y);
// endShape();
}
}
class Shape{
constructor(x1,y1,x2,y2,maxLifeTime,sizeMin,sizeMax){
this.size = random(sizeMin,sizeMax);
this.spacer = random(this.size/4,this.size/2);
this.pos = createVector(random(x1+this.size+2*this.spacer,x2-this.size-2*this.spacer),random(y1+this.size+2*this.spacer,y2-this.size-2*this.spacer));
this.shape = round(random(1));
//this.length = random(this.size/2);
this.rotation = random(2*PI);
this.textSpace = createVector(random(10,this.size-10),random(10,this.size-10));
this.lifeTime = 0;
this.alive = true;
this.maxLifeTime = random(2,4) * maxLifeTime;
}
update(){
if(this.lifeTime < this.maxLifeTime){
this.lifeTime++;
}else{
this.alive = false;
}
}
render(ts){
noFill();
stroke(140);
textSize(ts);
if(this.shape == 1){
circle(this.pos.x,this.pos.y,this.size);
push();
translate(this.pos.x,this.pos.y);
rotate(this.rotation+this.lifeTime/10);
line(0,this.spacer,0,2*this.spacer);
pop();
}else{
square(this.pos.x,this.pos.y,this.size);
push();
translate(this.pos.x,this.pos.y);
text(round(random(100,999)),this.textSpace.x,this.textSpace.y);
pop();
}
}
}
class Point{
constructor(x1,y1,x2,y2,maxLifeTime){
this.size = random(1,10);
this.pos = createVector(random(x1+this.size,x2-this.size),random(y1+this.size,y2-this.size));
this.lifeTime = 0;
this.alive = true;
this.maxLifeTime =
random(-5,5) + maxLifeTime;
}
update(){
if(this.lifeTime < this.maxLifeTime){
this.lifeTime++;
}else{
this.alive = false;
}
}
render(ts,points){
stroke(255,80);
noFill();
textSize(ts);
circle(this.pos.x,this.pos.y,this.size);
for(let p of points){
if(p != this){
stroke(255,40);
line(this.pos.x,this.pos.y,p.pos.x,p.pos.y);
let d = dist(this.pos.x,this.pos.y,p.pos.x,p.pos.y)
noStroke();
fill(255,60);
text(round(d),(this.pos.x+p.pos.x)/2,(this.pos.y+p.pos.y)/2)
}
}
}
}