-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
295 lines (243 loc) · 10.7 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
///////////////////////////
// Canvas drawing //
///////////////////////////
let nbPoints = 2000; // amount of points
let points = []; // list of all points
let speed = 0.5; // the speed represents how fast the points will move each frame
let size = 1; // the size represents how big the points will be (better to keep at low values)
let positionScale = 200; // the scale represents how much the noise will be zoomed
let rotationScale = 1; // how much a point can change it's direction at total (1 = 2PI)
let opacity = 20; // how visible is the point (0 = invisible, 255 = opaque)
let fadeSpeed = 0; // how fast the points will fade out (0 = no fade, 255 = instant fade)
let borderRule = "randomTeleport"; // determines what happens when a point goes off-screen (randomTeleport, linkedTeleport, none)
let mainWidth, mainHeight; // size of the canvas
let currentColor; // color of the points
let currentColor2; // 2nd color of the points (if needed)
function setup() {
// get size of div
if (window.innerWidth > 3000) {
mainWidth = document.getElementById("main").clientWidth;
mainHeight = window.innerHeight - 400; // test
} else {
mainWidth = document.getElementById("main").clientWidth;
mainHeight = document.getElementById("setterCanvaHeight").clientHeight + 25; // + 25 is a manual offset to make canvas slightly bigger than the sidebar
}
// initialize canvas
createCanvas(mainWidth, mainHeight); // set canva size
background(0); // set background color in A
currentColor = color(0,255,210, opacity); // set base color in RGBA
currentColor2 = null; // set secondary color in RGBA
stroke(currentColor); // set color in RGBA
strokeWeight(size); // set size of points
// create all points
for (let i = 0; i < nbPoints; i++) {
points.push(createVector(random(mainWidth), random(mainHeight))); // add a new point with random position
}
}
function draw() {
// add fade effect if fade is not 0
if (fadeSpeed > 0) {
background(0, fadeSpeed);
}
for (vector of points) {
// if there are 2 colors, change color at the middle of the list
if (points[Math.floor(points.length/2)] == vector && currentColor2 != null) stroke(currentColor2)
// set the direction of the point with semi-random noise
let direction = 2 * rotationScale * Math.PI * noise(vector.x / positionScale, vector.y / positionScale);
// apply the movement
vector.x += Math.cos(direction) * speed;
vector.y += Math.sin(direction) * speed;
// check if the point is off-screen and apply border rule
switch (borderRule) {
case "randomTeleport": // teleport to random position
if (vector.x < 0 || vector.x > mainWidth || vector.y < 0 || vector.y > mainHeight) {
vector.x = random(mainWidth);
vector.y = random(mainHeight);
}
break;
case "linkedTeleport": // teleport to opposite side (bottom = top, left = right)
if (vector.x < 0) vector.x = mainWidth;
if (vector.x > mainWidth) vector.x = 0;
if (vector.y < 0) vector.y = mainHeight;
if (vector.y > mainHeight) vector.y = 0;
break;
case "none": // let the point go off-screen
default:
break;
}
// draw the point
point(vector.x, vector.y)
}
stroke(currentColor); // reset color
}
function windowResized() {
if (mainWidth != document.getElementById("main").clientWidth) { // check that it is not only the height that changed (we don't care in this case)
// get new div size
if (window.innerWidth > 1920) {
mainWidth = document.getElementById("main").clientWidth;
mainHeight = innerHeight - 250; // make the canva take most screen space
} else {
mainWidth = document.getElementById("main").clientWidth;
mainHeight = document.getElementById("setterCanvaHeight").clientHeight + 25; // + 25 is a manual offset to make canvas slightly bigger than the sidebar
}
// resize canvas to correct size
resizeCanvas(mainWidth, mainHeight);
reset();
}
}
function reset() {
//refresh values
stroke(currentColor);
strokeWeight(size);
background(0);
// replace all points to random positions
for (vector of points) {
vector.x = random(mainWidth);
vector.y = random(mainHeight);
}
}
///////////////////////////
// Setters Values HTML //
///////////////////////////
function inputNbParticle(event) {
// check if enter is pressed
if (event.keyCode === 13) {
points = []; // destroy all points
nbPoints = document.getElementById("nbPoints").value; // get new amount of points
// create points at random positions
for (let i = 0; i < nbPoints; i++) {
points.push(createVector(random(mainWidth), random(mainHeight)));
}
reset();
}
}
function inputSpeed(event) {
if (event.keyCode === 13) { // check if enter is pressed
speed = document.getElementById("speed").value; // change speed
reset();
}
}
function inputSize(event) {
if (event.keyCode === 13) { // check if enter is pressed
size = document.getElementById("size").value; // change size
strokeWeight(size); // apply size to stroke
reset();
}
}
function inputPositionScale(event) {
if (event.keyCode === 13) { // check if enter is pressed
positionScale = document.getElementById("positionScale").value; // change positionScale
reset();
}
}
function inputRotationScale(event) {
if (event.keyCode === 13) { // check if enter is pressed
rotationScale = document.getElementById("rotationScale").value; // change rotationScale
reset();
}
}
function inputOpacity(event) {
if (event.keyCode === 13) { // check if enter is pressed
opacity = Math.floor(document.getElementById("opacity").value); // change opacity (USE MATH.FLOOR BECAUSE STROKE DOESN'T ACCEPT FLOATS)
currentColor.setAlpha(opacity); // set opacity in color
if (currentColor2 != null) currentColor2.setAlpha(opacity); // set opacity in color2 (if needed)
stroke(currentColor); // set color in RGBA
reset();
}
}
function inputFade(event) {
if (event.keyCode === 13) { // check if enter is pressed
fadeSpeed = Math.floor(document.getElementById("fadeSpeed").value); // change fadeSpeed (USE MATH.FLOOR BECAUSE STROKE DOESN'T ACCEPT FLOATS)
reset();
}
}
function inputBorderRule(inputBorderRule) {
if (inputBorderRule != null) { // makes it so that it doesn't change borderRule if inputBorderRule is null
borderRule = inputBorderRule // change borderRule
}
reset();
}
function inputColor(inputColor, inputColor2) {
if (inputColor != null) { // makes it so that it doesn't change color if inputColor is null (serves for apply all inputs when called with no values in html)
currentColor = inputColor; // change color
currentColor2 = inputColor2; // change 2nd color
}
reset();
}
function applyAllInputs(newBorderRule, newColor, newColor2) {
// inputColor needs to be called before inputOpacity and inputFadeSpeed
inputBorderRule(newBorderRule);
inputColor(newColor, newColor2);
// calls all inputsFunctions with correct event.keycode (enter)
inputNbParticle({keyCode: 13});
inputSpeed({keyCode: 13});
inputSize({keyCode: 13});
inputPositionScale({keyCode: 13});
inputRotationScale({keyCode: 13});
inputOpacity({keyCode: 13});
inputFade({keyCode: 13});
reset();
}
///////////////////////////
// Presets //
///////////////////////////
function presetSandStream() {
// modify all values in html
document.getElementById("nbPoints").value = 2000;
document.getElementById("speed").value = 50;
document.getElementById("size").value = 1;
document.getElementById("positionScale").value = 40000;
document.getElementById("rotationScale").value = 200;
document.getElementById("opacity").value = 100;
document.getElementById("fadeSpeed").value = 4;
noiseSeed(2992); // change noise seed to a certain seed to have a nice result
applyAllInputs("randomTeleport", color(255,255,200, opacity), null);
}
function presetHighway() {
// modify all values in html
document.getElementById("nbPoints").value = 1000;
document.getElementById("speed").value = 10;
document.getElementById("size").value = 1;
document.getElementById("positionScale").value = 5000;
document.getElementById("rotationScale").value = 200;
document.getElementById("opacity").value = 200;
document.getElementById("fadeSpeed").value = 15;
noiseSeed(5175); // change noise seed to a certain seed to have a nice result
applyAllInputs("randomTeleport", color(130,0,255, opacity), null);
}
function presetGenerativeMap() {
// modify all values in html
document.getElementById("nbPoints").value = 3000;
document.getElementById("speed").value = 0.2;
document.getElementById("size").value = 1;
document.getElementById("positionScale").value = 200;
document.getElementById("rotationScale").value = 200;
document.getElementById("opacity").value = 20;
document.getElementById("fadeSpeed").value = 0;
noiseSeed(8517); // change noise seed to a certain seed to have a nice result
applyAllInputs("randomTeleport", color(115, 64, 50, opacity), null);
}
function presetGlitch() {
// modify all values in html
document.getElementById("nbPoints").value = 2000;
document.getElementById("speed").value = 150;
document.getElementById("size").value = 0.1;
document.getElementById("positionScale").value = 200000000000;
document.getElementById("rotationScale").value = 700000000000000000;
document.getElementById("opacity").value = 100;
document.getElementById("fadeSpeed").value = 0;
noiseSeed(4610); // change noise seed to a certain seed to have a nice result
applyAllInputs("randomTeleport", color(150, 255, 150, opacity), null);
}
function presetDuoTone() {
// modify all values in html
document.getElementById("nbPoints").value = 10000;
document.getElementById("speed").value = 0.3;
document.getElementById("size").value = 1;
document.getElementById("positionScale").value = 500;
document.getElementById("rotationScale").value = 0.5;
document.getElementById("opacity").value = 7;
document.getElementById("fadeSpeed").value = 0;
noiseSeed(8201); // change noise seed to a certain seed to have a nice result
applyAllInputs("linkedTeleport", color(255, 255, 100, opacity), color(255, 100, 180, opacity));
}