xxxxxxxxxx
108
/**
* Laura Tessin
* prompt: lush green
*
* based on: https://editor.p5js.org/ada10086/sketches/r1gmVaE07
*
*/
var num = 1800; // number of particles
var noiseScale = 500; // higher number -> being pulled in similar direction
var noiseStrength = -0.5; // change direction
var particles = [num];
var direction = 1;
let canvas_size;
var noise_slider;
var speed_slider;
const colors = ['#B7CF0C',
'#92C43C',
'#85B72F',
'#CDDD55',
'#86BD26',
'#B2C718',
'#939730',
'#809108',
'#84A54A',
'#F9CA00']
function setup() {
// set up canvas
canvas_size = windowHeight;
createCanvas(canvas_size, canvas_size);
noStroke();
// set up slider
noise_slider = createSlider(5, 500 ,500);
noise_slider.position(10, canvas_size - 30);
speed_slider = createSlider(0.5, 1.5, 1, 0.5);
speed_slider.position(160, canvas_size - 30);
// create particles
for (let i = 0; i < num; i++) {
var loc = createVector(random(-20, canvas_size + 20), random(-20, canvas_size + 20), 5);
var angle = 0;
var dir = createVector(cos(angle), sin(angle));
var speed = random(0.5,2);
var particle_col = random(colors);
particles[i] = new Particle(loc, dir, speed, particle_col);
}
}
function draw() {
// draw background
fill(54, 76, 15, 10)
noStroke();
rect(0, 0, width, height);
// update slider values
const n = noise_slider.value();
noiseScale = n;
const s = speed_slider.value();
direction = s;
// update particles
for (let i = 0; i < particles.length; i++) {
particles[i].run();
}
}
class Particle{
constructor(_loc,_dir,_speed, _color){
this.loc = _loc;
this.dir = _dir;
this.speed = _speed;
this.color = _color;
}
run() {
this.move();
this.checkEdges();
this.update();
}
move() {
let angle = noise(this.loc.x/noiseScale, this.loc.y/noiseScale, frameCount/noiseScale) * TWO_PI * noiseStrength;
this.dir.x = cos(angle);
this.dir.y = sin(angle);
var vel = this.dir.copy();
var d = direction; //direction change
vel.mult(this.speed * d);
this.loc.add(vel);
}
checkEdges() {
if (this.loc.x<0 || this.loc.x > canvas_size || this.loc.y < 0 || this.loc.y > canvas_size) {
this.loc.x = random(-20, canvas_size + 20);
this.loc.y = random(-50, canvas_size);
}
}
update() {
fill(this.color);
ellipse(this.loc.x, this.loc.y, this.loc.z, 5);
}
}