-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp5.js
70 lines (64 loc) · 2.02 KB
/
p5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
let particles = [];
const num = 100;
const noiseScale = 0.01;
let angleOffset = 0;
let mousePressedFlag = false;
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialize particles with position vectors and random sizes
for (let i = 0; i < num; i++) {
particles.push({
pos: createVector(random(width), random(height)),
size: random(5, 40) // Random size between 5 and 40
});
}
}
function draw() {
setGradient(0, 0, width, height, color(0, 0, 20), color(7, 62, 93), 'Y');
particles.forEach(particle => {
let particleHue = noise(particle.pos.x * noiseScale, particle.pos.y * noiseScale) * 255;
stroke(0, particleHue, 255 - particleHue, 95);
strokeWeight(particle.size);
point(particle.pos.x, particle.pos.y);
if (mousePressedFlag) {
let mousePos = createVector(mouseX, mouseY);
let mouseDir = p5.Vector.sub(mousePos, particle.pos);
mouseDir.setMag(1); // Adjust speed
particle.pos.add(mouseDir);
} else {
let n = noise(particle.pos.x * noiseScale, particle.pos.y * noiseScale, frameCount * noiseScale);
let a = TAU * n + angleOffset;
let v = p5.Vector.fromAngle(a);
v.mult(1); // Adjust movement speed
particle.pos.add(v);
}
// Wrap particles to appear on the opposite
if (!onScreen(particle.pos)) {
particle.pos.x = random(width);
particle.pos.y = random(height);
}
});
}
function mousePressed() {
mousePressedFlag = true;
}
function mouseReleased() {
mousePressedFlag = false;
}
function onScreen(v) {
return v.x >= 0 && v.x <= width && v.y >= 0 && v.y <= height;
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function setGradient(x, y, w, h, c1, c2, axis) {
noFill();
if (axis === 'Y') { // Top to bottom gradient
for (let i = y; i <= y + h; i++) {
let inter = map(i, y, y + h, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x + w, i);
}
}
}