xxxxxxxxxx
var num = 2222;
var noiseScale=100, noiseStrength=1;
var particles = [num];
function setup() {
describe('purple sand particles mimicking water flow')
createCanvas(windowWidth, windowHeight);
noStroke();
for (let i=0; i<num; i++) {
var loc = createVector(random(width*1.2), random(height), 2);
var angle = 0; //any value to initialize
var dir = createVector(cos(angle), sin(angle));
// var speed = random(0.5,2);
var speed = random(5,map(mouseX,0,width,5,20)); // faster
particles[i]= new Particle(loc, dir, speed);
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function draw() {
// background(0);
fill(0, 10);
rect(0, 0, width, height);
for (let i=0; i<particles.length; i++) {
particles[i].run();
}
}
class Particle{
constructor(_loc,_dir,_speed){
this.loc = _loc;
this.dir = _dir;
this.speed = _speed;
// var col;
}
run() {
this.move();
this.checkEdges();
this.update();
}
move(){
let angle=noise(this.loc.x/noiseScale, this.loc.y/noiseScale, frameCount/noiseScale)*TWO_PI*noiseStrength; //0-2PI
this.dir.x = sin(angle);
this.dir.y = tan(angle);
var vel = this.dir.copy();
var d = 22; //direction change
vel.mult(this.speed*d); //vel = vel * (speed*d)
this.loc.add(vel); //loc = loc + vel
}
checkEdges(){
if (this.loc.x<0 || this.loc.x>width || this.loc.y<0 || this.loc.y>height) {
this.loc.x = random(width*10);
this.loc.y = random(height);
}
}
update(){
fill(255,200,255);
ellipse(this.loc.x, this.loc.y, this.loc.z);
}
}