-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
203 lines (170 loc) · 5.31 KB
/
sketch.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
let cellSize = 20;
let columns;
let rows;
let xOffset = 0;
let yOffset = 0;
let zOffset = 0;
let noiseScale = 0.1;
let flowField;
let flowFieldAngleOffset = 0;
let particles = [];
let maxParticleSpeed = 0.1;
let isWrappingParticles = true;
let strokeOpacity = 20;
let strokeColor = 0;
let isFading = false;
function setup() {
createCanvas(windowWidth, windowHeight);
columns = floor(width / cellSize);
rows = floor(height / cellSize);
flowField = new Array(columns * rows);
}
// ===============================================
// WINDOW RESIZING
// ===============================================
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
background(255);
}
// ===============================================
// FLOWFIELD AND PARTICLE
// ===============================================
function draw() {
strokeWeight(1);
stroke(strokeColor, strokeOpacity);
if (isFading) background(255, 5);
xOffset = 0;
for (let x = 0; x < columns; x++) {
yOffset = 0;
for (let y = 0; y < rows; y++) {
// create a noise based vector
let angle = noise(xOffset, yOffset, zOffset) * TWO_PI + flowFieldAngleOffset;
let vector = p5.Vector.fromAngle(angle);
vector.setMag(1) // noramlize length to 0.1
// store vector in flowfield array
let index = (y * columns) + x;
flowField[index] = vector;
yOffset += noiseScale;
zOffset += 0.00001;
}
xOffset += noiseScale
for (let i = 0; i < particles.length; i++) {
particles[i].follow(flowField);
isWrappingParticles && particles[i].keepOnScreen();
particles[i].draw();
particles[i].update();
}
}
}
// ===============================================
// MOUSE AND KEYBOARD INPUT
// ===============================================
function mouseDragged() {
// keep adding particles while the mouse is down
particles.push(new Particle(mouseX, mouseY));
}
function mouseReleased() {
// clear particles when mouse is released
particles = [];
}
function keyPressed() {
switch (keyCode) {
// 1, 2, and 3 control particle speed
case 49: snSetParticleSpeed(0.01); break;
case 50: snSetParticleSpeed(0.1); break;
case 51: snSetParticleSpeed(1); break;
// Q, W, and E change the noise scale
case 81: snSetNoiseScale(1); break;
case 87: snSetNoiseScale(0.1); break;
case 69: snSetNoiseScale(0.01); break;
// A, S and D change the opacity
case 65: snSetOpacity(5); break;
case 83: snSetOpacity(20); break;
case 68: snSetOpacity(100); break;
// X to clear canvas and reset brushes
case 88: clearCanvas(); break;
// R to reset brushes
case 82: resetBrushes(); break;
// C to toggle eraser
case 67: toggleEraser(); break;
// Z to toggle whether particles wrap back around
case 90: toggleParticleWrapping(); break;
// F to toggle trail fading
case 70: toggleFading(); break;
// arrow keys set flowfield direction
case 37: offsetFlowFieldAngle(0); break; // left
case 38: offsetFlowFieldAngle(HALF_PI); break; // up
case 39: offsetFlowFieldAngle(PI); break; // right
case 40: offsetFlowFieldAngle(PI + HALF_PI); break; // down
}
}
function snSetParticleSpeed(speed) { maxParticleSpeed = speed; }
function snSetNoiseScale(scale) { noiseScale = scale; }
function snSetOpacity(opacity) { strokeOpacity = opacity; }
function clearCanvas() { background(255); resetBrushes(); particles = []; }
function resetBrushes() {
snSetParticleSpeed(0.1);
snSetNoiseScale(0.1);
snSetOpacity(20);
strokeColor = 0;
isWrappingParticles = true;
isFading = false;
flowFieldAngleOffset = 0;
}
function toggleEraser() { strokeColor = strokeColor == 0 ? 255 : 0; }
function toggleParticleWrapping() { isWrappingParticles = !isWrappingParticles; }
function toggleFading() { isFading = !isFading; }
function offsetFlowFieldAngle(offsetAngle) { flowFieldAngleOffset = offsetAngle; }
// ===============================================
// PARTICLE LOGIC
// ===============================================
class Particle {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector(0, 0);
this.acceleration = createVector(0, 0);
this.prevPosition = this.position.copy();
}
update() {
this.velocity.add(this.acceleration);
this.velocity.limit(maxParticleSpeed);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
applyForce(force) {
this.acceleration.add(force);
}
draw() {
line(this.position.x, this.position.y, this.prevPosition.x, this.prevPosition.y);
this.updatePrevPosition();
}
updatePrevPosition() {
this.prevPosition.x = this.position.x;
this.prevPosition.y = this.position.y;
}
keepOnScreen() {
if (this.position.x > width) {
this.position.x = 0;
this.updatePrevPosition();
}
if (this.position.x < 0) {
this.position.x = width;
this.updatePrevPosition();
}
if (this.position.y < 0) {
this.position.y = height;
this.updatePrevPosition();
}
if (this.position.y > height) {
this.position.y = 0;
this.updatePrevPosition();
}
}
follow(vectors) {
let x = floor(this.position.x / cellSize);
let y = floor(this.position.y / cellSize);
let index = (y * columns) + x;
let force = vectors[index];
this.applyForce(force);
}
}