Line segments that rotate based on mouse movement. Each segment compounds its p[argents rotation producing a “curl.” Each blade has an original goal rotation it moves back to elastically when there is no mouse movement. Link to source files.
https://editor.p5let inertia = 0.9;
let k = 0.02;
let wind = 0;
let oldmouse;
let finalwind = 0;
let xp = 1;
let oldacc;
let grasslimit = 400;
let stemlimit = 18;
let grassX = [];
let grassS = [];
let rigidness = [];
let Gcolor = [];
let origrot= [];
let gcount = 11;
let gnum = 0;
let mrot = 0;
let rotlist = [];
function setup() {
createCanvas(800,800);
oldmouse = mouseX;
oldacc = accelerationX;
for(var i = 0; i < stemlimit; i++){
rotlist[i] = 0;
}
}
function draw() {
background(0);
if(gcount > 10){
if(gnum < grasslimit){
createGrass();
gcount = 0;
gnum++;
}
}
wind = -1*finalwind + mouseX + (accelerationX*10);
xp = xp * inertia + wind*k ;
finalwind += xp ;
if((finalwind < 0.0003) && (finalwind > -0.0003)){
finalwind = 0;
}
mrot = (mouseX + (accelerationX*10) - finalwind )/20;
inertia = 0.9 - (mouseY/windowHeight);
oldmouse = mouseX;
oldacc = accelerationX;
let revc = stemlimit - 1;
for(let i = 0; i < stemlimit - 1; i++){
rotlist[revc] = rotlist[revc-1];
revc = revc - 1;
}
rotlist[0] = mrot;
for (let i = 0; i < grassX.length; i++) {
let xpos = grassX[i];
let ypos = windowHeight;
let sizec = 100;
let stw = 5;
let rot = 270;
for (let j = 0; j < grassS[i]; j++) {
let nrot = rotlist[j] * rigidness[i];
rot = rot + nrot + origrot[i];
strokeWeight(stw);
stroke(Gcolor[i]);
let newx = xpos + sizec * cos(radians(rot));
let newy = ypos + sizec * sin(radians(rot));
line(xpos,ypos,newx,newy);
ypos = newy;
xpos = newx;
nrot = nrot *1.1;
sizec = sizec * 0.90;
stw = stw * 0.90;
}
if(grassS[i] < 18){
grassS[i]++;
}else{
grassS[i] = 18;
}
}
gcount++;
}
function createGrass(){
append(grassX,int(random(windowWidth)));
append(grassS,1);
append(rigidness, random() * 0.4 + 0.6);
append(Gcolor, color(random(255), random(255),random(255)));
append(origrot, random(-5,5));
}js.org/mszivos/sketches/2xc8_6hjd