xxxxxxxxxx
218
let pnum = 3000;
let particles = [];
let colours = [];
let colourNum = 10;
let selectedColour = 0;
let noiseScale = 500;
let noiseStrength = 80;
let minSpeed = 1;
let maxSpeed = 4;
let jitter = 5;
let particleSize = 3;
let zOff = 0.25;
let image = null;
let selectedMode = "A";
// let combination = ''
let colourModes = [0, 1, 2, 3, 4];
function setup() {
image = createCanvas(windowWidth, windowHeight);
background(0);
colorMode(HSB);
strokeWeight(particleSize);
noiseStrength = random(1, 5);
selectedColour = random(colourModes);
jitter = random(1, 1.5);
zOff = random(0.6, 1);
pushParticles();
setColours();
}
function draw() {
fill("white");
noStroke();
textSize(12);
text("Use the keys:", 10, height - 55);
text(' - "a s d f g" to change the intensity', 10, height - 40);
text(' - "1 2 3 4 5" to change the colours', 10, height - 25);
text(' - "x" so save the image.', 10, height - 10);
background(0, 0.05);
let i = 0;
let j = 0;
for (const p of particles) {
// Itereate trough colours
if (j > colourNum - 1) {
j = 0;
} else {
stroke(colours[selectedColour][j], 80, 80, 0.7);
j++;
}
// Render Particle
p.move();
p.edges();
p.show();
i++;
}
}
class Particle {
constructor(posV, dirV, spdV) {
// Set local variables
this.pos = posV;
this.dir = dirV;
this.speed = spdV;
}
move() {
// Angle based on noise space value
let angle =
noise(
this.pos.x / noiseScale,
this.pos.y / noiseScale,
(frameCount * zOff) / noiseScale
) *
TWO_PI *
noiseStrength;
this.dir.x = cos(angle);
this.dir.y = sin(angle);
// Change in velocity
let vel = this.dir.copy();
vel.mult(this.speed * jitter);
this.pos.add(vel);
}
edges() {
if (
this.pos.x < 0 ||
this.pos.x > width ||
this.pos.y < 0 ||
this.pos.y > height
) {
this.pos.x = random(width);
this.pos.y = random(height);
}
}
show() {
point(this.pos.x, this.pos.y);
}
}
function pushParticles() {
for (let i = 0; i < pnum; i++) {
// Create vectors
let position = createVector(random(width), random(height));
let direction = createVector(cos(0), sin(0));
let speed = random(minSpeed, maxSpeed);
particles[i] = new Particle(position, direction, speed);
}
}
function colourChange(e, lowC, highC) {
colours[e] = [];
for (let i = 0; i < colourNum; i++) {
colours[e][i] = floor(random(lowC, highC));
}
}
function setColours() {
for (let i = 0; i < 5; i++) {
switch (i) {
case 0:
colourChange(0, 0, 30);
break;
case 1:
colourChange(1, 30, 52);
break;
case 2:
colourChange(2, 70, 160);
break;
case 3:
colourChange(3, 165, 195);
break;
case 4:
colourChange(4, 210, 250);
break;
}
}
}
// Keys
function keyTyped() {
switch (key) {
// Save Canvas
case "x":
saveCanvas(image, "fields_emergent", "png");
break;
// New set
case " ":
background(0);
pushParticles();
break;
// Stress
case "a":
noiseStrength = 80;
// selectedColour = 0
jitter = 3;
zOff = 0.1;
selectedMode = "A";
break;
case "s":
noiseStrength = 40;
// selectedColour = 1
jitter = 2.4;
zOff = 0.25;
selectedMode = "S";
break;
// Neutral
case "d":
noiseStrength = 20;
// selectedColour = 2
jitter = 1.75;
zOff = 0.5;
selectedMode = "D";
break;
case "f":
noiseStrength = 5;
// selectedColour = 3
jitter = 1.3;
zOff = 0.75;
selectedMode = "F";
break;
// Calm
case "g":
noiseStrength = 1;
// selectedColour = 4
jitter = 1;
zOff = 1;
selectedMode = "G";
break;
case "1":
selectedColour = 0;
break;
case "2":
selectedColour = 1;
break;
case "3":
selectedColour = 2;
break;
case "4":
selectedColour = 3;
break;
case "5":
selectedColour = 4;
break;
case "o":
noiseStrength++;
break;
case "l":
noiseStrength--;
break;
}
}