xxxxxxxxxx
let sli;
let btn;
let bv = []
let v = []
let c = 0
function setup() {
createCanvas(windowWidth, windowHeight);
ellipseMode(CENTER);
sli = createSlider(0,10,1)
sli.position(20,20);
btn = createButton('のびる');
btn.position(20,60);
btn.mousePressed(grow);
bv[0] = createVector(width/2,height/2)
v[0] = createVector(0,0)
}
function draw() {
background(255);
let sp = sli.value();
c++
if(c == 10){
v[0].x = random(-10,10);
v[0].y = random(-10,10);
c = 0
}
fill(255,230,230);
for(let i = 1 ;i < bv.length ; i++){
let j = bv.length - i;
ellipse(bv[j].x,bv[j].y,30,30);
v[j].x = (bv[j-1].x - bv[j].x)/sp;
v[j].y = (bv[j-1].y - bv[j].y)/sp;
bv[j].x += v[j].x * sp;
bv[j].y += v[j].y * sp;
}
fill(200,100,100);
ellipse(bv[0].x,bv[0].y,40,40);
bv[0].x += v[0].x * sp;
bv[0].y += v[0].y * sp;
if(bv[0].x>width||bv[0].x<0){
v[0].x=v[0].x*-1;
}
if(bv[0].y>height||bv[0].y<0){
v[0].y=v[0].y*-1;
}
}
function grow(){
bv.push(createVector(bv[bv.length-1].x,bv[bv.length-1].y));
v.push(createVector(0,0));
}