Here is the code I found and I was trying to convert this into javascript code
pathfinder[] paths;
void setup() {
size(800, 600);
background(0);
ellipseMode(CENTER);
fill(255);
noStroke();
smooth();
paths = new pathfinder[1];
paths[0] = new pathfinder();
}
void draw() {
for (int i=0; i<paths.length; i++) {
PVector loc = paths[i].location;
float diam = paths[i].diameter;
ellipse(loc.x, loc.y, diam, diam);
paths[i].update();
}
}
void mousePressed() {
background(0);
paths = new pathfinder[1];
paths[0] = new pathfinder();
}
class pathfinder {
PVector location;
PVector velocity;
float diameter;
pathfinder() {
location = new PVector(width/2, height);
velocity = new PVector(0, -1);
diameter = 32;
}
pathfinder(pathfinder parent) {
location = parent.location.get();
velocity = parent.velocity.get();
float area = PI*sq(parent.diameter/2);
float newDiam = sqrt(area/2/PI)*2;
diameter = newDiam;
parent.diameter = newDiam;
}
void update() {
if (diameter>0.5) {
location.add(velocity);
PVector bump = new PVector(random(-1, 1), random(-1, 1));
bump.mult(0.1);
velocity.add(bump);
velocity.normalize();
if (random(0, 1)<0.02) {
paths = (pathfinder[]) append(paths, new pathfinder(this));
}
}
}
}
Here is my p5js code
let branchs = [];
function setup() {
createCanvas(600, 400);
background(0);
fill(255);
noStroke();
let l = createVector(width/2,height);
branchs.push(new Branch(l,20));
}
function draw() {
for(let i=0;i<branchs.length;i++){
branchs[i].update();
ellipse(branchs[i].loc.x,branchs[i].loc.y,branchs[i].dia,branchs[i].dia);
}
}
function Branch(parent){
this.loc=0;
this.vel=0;
this.dia=0;
if(parent){
this.loc = parent.loc;
this.vel = parent.vel;
let area = PI*sq(parent.dia/2);
let newDia = sqrt(area/2/PI)*2;
this.dia = newDia;
parent.dia = newDia;
return parent;
}else{
this.loc = loc;
this.dia = dia;
this.vel = createVector(0,-1);
}
this.update = function(){
if(this.dia>5){
this.loc.add(this.vel) ;
let bp = createVector(random(-1,1),random(-1,1));
bp.mult(random(-0.5,0.5));
this.vel.add(bp);
this.vel.normalize();
this.dia-=0.005;
if(random(1)<0.002){
for(let i=0;i<random(1,6);i++){
append(branchs, new Branch(this))
}
}
}
}
}
update the code according to the feedback and it throwing error now
TypeError: branchs[i].update is not a function (sketch: line 14)