A grid of points that expand to move away from the mouse position.. Link to source files.
let inertia = 0.9;
let k = 0.04;
let wind = 0;
let oldmouse;
let finalwind = 0;
let xp = 1;
let yp = 1;
let origX = [];
let origY = [];
let coordinatesX = [];
let coordinatesY = [];
let CR = [];
let colorG = [];
let colorB = [];
let gridnumX = 28;
let gridnumY = 12;
let xspace = 50;
let yspace = 50;
let framecount = 0;
function setup() {
createCanvas(800,800);
for (var x = 0; x < gridnumX; x++) {
coordinatesX[x] = []; // create nested array
coordinatesY[x] = [];
origX[x] = [];
origY[x] = [];
for (var y = 0; y < gridnumY; y++) {
coordinatesX[x][y] = x * xspace-200;
coordinatesY[x][y] = y * yspace-200;
origX[x][y] = x * xspace-200;
origY[x][y] = y * xspace-200;
append(CR,color(random(255), random(255), random(255)));
}
}
}
function draw() {
background(255);
for (let x = 0; x < gridnumX; x++) {
for (let y = 0; y < gridnumY; y++) {
if(framecount > 10){
var distx = (origX[x][y] - mouseX);
let disty = (origY[x][y] - mouseY);
let distance = sqrt((distx * distx) + (disty * disty));
let radius = distance + (300 - distance)/1.5;
if(radius < distance){
radius = distance;
}
let newx = 0;
let newy = 0;
let theta = 0;
if(disty != 0 && distx != 0){
theta = atan(disty/distx);
}
if(distx < 0){
newx = mouseX - radius * cos(theta);
newy = mouseY - radius * sin(theta);
}else{
newx = mouseX + radius * cos(theta);
newy = mouseY + radius * sin(theta);
}
let posx = newx;
let posy = newy;
let x1 = -1 * coordinatesX[x][y] + posx;
let y1 = -1 * coordinatesY[x][y] + posy;
xp = xp * inertia + x1*k ;
yp = yp * inertia + y1*k ;
coordinatesX[x][y] += xp ;
coordinatesY[x][y] += yp ;
}
framecount++;
noStroke();
fill(0);
ellipse(coordinatesX[x][y], coordinatesY[x][y] , 5, 5);
}
}
var ccount = 0;
for (let x = 0; x < gridnumX-1; x++) {
for (let y = 0; y < gridnumY-1; y++) {
fill(CR[ccount]);
stroke(CR[ccount]);
quad(coordinatesX[x][y],coordinatesY[x][y],coordinatesX[x+1][y],coordinatesY[x+1][y],coordinatesX[x+1][y+1],coordinatesY[x+1][y+1],coordinatesX[x][y+1],coordinatesY[x][y+1]);
ccount++;
}
}
}