-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_field.html
427 lines (427 loc) · 21 KB
/
vector_field.html
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!DOCTYPE html>
<html lang="en">
<!--
. _ _ _____ _ _ _____ _____ _ _ ___________ _____
. | | | || ___| | | | | _ | |_ _| | | || ___| ___ \ ___|
. | |_| || |__ | | | | | | | | | | | |_| || |__ | |_/ / |__
. | _ || __|| | | | | | | | | | | _ || __|| /| __|
. | | | || |___| |____| |___\ \_/ / | | | | | || |___| |\ \| |___
. \_| |_/\____/\_____/\_____/\___/ \_/ \_| |_/\____/\_| \_\____/
-->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="[MAZ01001.github.io] A colored vector field made with P5.js">
<meta name="author" content="MAZ01001">
<link rel="apple-touch-icon" sizes="180x180" href="../img/apple-touch-icon.png">
<link rel="icon" type="image/x-icon" href="../img/MAZ_logo.svg">
<link rel="icon" type="image/png" sizes="32x32" href="../img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../img/favicon-16x16.png">
<link rel="manifest" href="../img/site.webmanifest">
<link rel="mask-icon" href="../img/safari-pinned-tab.svg" color="#ff9900">
<link rel="shortcut icon" href="../img/favicon.ico">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="../img/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<title>Vektor-Field</title>
<style>
body{
background-color: #222;
padding: 0;
margin: 0;
overflow: hidden;
}
</style>
<script src="../libraries/p5.js/v1.4.2/p5.min.js"></script>
<script>
/// <reference path="../libraries/p5.js/intellisense/p5.d.ts"/>
/// <reference path="../libraries/p5.js/intellisense/p5.global-mode.d.ts"/>
/**
* __Vector-Class for drawing one arrow with `p5.js` - lots of variables__
*/
class _Vector{
/** @type {{dir:{min:number;max:number;x:number;y:number;m:number;pl:number;fac:number;am:number;xt:boolean;ang:number;};tri:{width:number;height:number;fill:number[3]|string;m:number;};pos:{x:number;y:number;};col:{min:number;max:number;cmin:number;cmax:number;rgb:number[3];};x:{limit_dist:boolean;line:boolean;triangle:boolean;tri_line:boolean;small_tri:boolean;big_tri:boolean;};stroke_w:number;lm:number;lv:number;}} - values of vector */
values={
/** @type {{min:number;max:number;x:number;y:number;m:number;pl:number;fac:number;am:number;xt:boolean;ang:number;}} - direction - group */
dir:{
/** @type {number} - min-length in % `[0-1]` */
min:0.01,
/** @type {number} - max-length in % `[0-1]` */
max:1,
/** @type {number} - x-pos of vec-arrow in canvas `[0+]` */
x:0,
/** @type {number} - y-pos of vec-arrow in canvas `[0+]` */
y:0,
/** @type {number} - length in pixel `[0+]` */
m:20,
/** @type {number} - length in % (min-max) */
pl:0,
/** @type {number} - used for size length^fac `[0+]` */
fac:1,
/** @type {number} - used for angle (in rad) */
am:0,
/** @type {boolean} - used for angle (right==false) */
xt:false,
/** @type {number} - current angle (in deg) */
ang:0,
},
/** @type {{width:number;height:number;fill:number[3]|string;m:number;}} - triangle/arrow-head - group */
tri:{
/** @type {number} - width in px `[0+]` */
width:10,
/** @type {number} - height in px `[0+]` */
height:10,
/** @type {number[3]|string} - fill `"css-color"`/`[R,G,B]` each `[0-255]` */
fill:[0,0,0]||"black",
/** @type {number} - multi for length `[0.01+]` */
m:0.1,
},
/** @type {{x:number;y:number;}} - pos - group */
pos:{
/** @type {number} - x-pos in canvas in px `[0+]` */
x:0,
/** @type {number} - x-pos in canvas in px `[0+]` */
y:0,
},
/** @type {{min:number;max:number;cmin:number;cmax:number;rgb:number[3]}} - color - group */
col:{
/** @type {number} - min-color-clipping in % `[0-1]` */
min:0,
/** @type {number} - max-color-clipping in % `[0-1]` */
max:1,
/** @type {number} - min-color hue `[0-360]` */
cmin:240,
/** @type {number} - max-color hue `[0-360]` */
cmax:0,
/** @type {number[3]} - current rgb-color `[r,g,b]` */
rgb:[0,0,0],
},
/** @type {{limit_dist:boolean;line:boolean;triangle:boolean;tri_line:boolean;small_tri:boolean;big_tri:boolean;}} - extras - group */
x:{
/** @type {boolean} - uses the `_Vector.dir.min` / `.max` / `.m` and `.fac` vars for size _default `true`_ */
limit_dist:true,
/** @type {boolean} - render line _default `true`_ */
line:true,
/** @type {boolean} - render triangle _default `true`_ */
triangle:true,
/** @type {boolean} - triangle instead of a line _default `false`_ */
tri_line:false,
/**
* @type {boolean} - render the triangle relatively small _default `false`_
* @requires - **(tri_line==false)**
*/
small_tri:false,
/**
* @type {boolean} - render the triangle relatively big _default `false`_
* @requires - **(trie_line==false && small_tri==false)**
*/
big_tri:false,
},
/** @type {number} - stroke weight of arrow in px `[>0]` */
stroke_w:3,
/** @type {number} - distance from origin to mouse _(in canvas)_ */
lm:0,
/** @type {number} - length of vec in px _(% * px-max)_ */
lv:0,
};
/**
* __calc values and color__
* @param {number} _mX - Xpos in canvas to point the vector at - _default `mouseX`_
* @param {number} _mY - Ypos in canvas to point the vector at - _default `mouseY`_
*/
calc_move(_mX=mouseX,_mY=mouseY){
const _g = _mY - this.values.pos.y,
_a = _mX - this.values.pos.x;
this.values.dir.am = atan(_g/_a);
this.values.lm = sqrt(_g*_g+_a*_a);
this.values.dir.xt=_a<0;
if(this.values.x.limit_dist){
const _max_dis=sqrt(
pow(
((width-this.values.pos.x)>this.values.pos.x)?
(width-this.values.pos.x):this.values.pos.x
,2)+pow(
((height-this.values.pos.y)>this.values.pos.y)?
(height-this.values.pos.y):this.values.pos.y
,2)
);
this.values.dir.pl=map(this.values.lm,0,_max_dis,this.values.dir.min,this.values.dir.max);
const _pow=pow(this.values.dir.m*this.values.dir.pl,this.values.dir.fac);
const _cos=cos(this.values.dir.am)*_pow,
_sin=sin(this.values.dir.am)*_pow;
if(this.values.dir.xt){
this.values.dir.x=this.values.pos.x-(_cos);
this.values.dir.y=this.values.pos.y-(_sin);
}else{
this.values.dir.x=this.values.pos.x+_cos;
this.values.dir.y=this.values.pos.y+_sin;
}
this.values.lv = sqrt(
((this.values.dir.x-this.values.pos.x)*
(this.values.dir.x-this.values.pos.x))+
((this.values.dir.y-this.values.pos.y)*
(this.values.dir.y-this.values.pos.y))
);
}else{
this.values.dir.pl=1;
this.values.dir.x=_mX;
this.values.dir.y=_mY;
this.values.lv=this.values.lm;
// this.values.dir.m=200;
}
this.calc_color();
this.calc_ang();
}
/**
* __calc color__
*/
calc_color(){
this.values.col.rgb=hue_rgb(map(this.values.lv/2,
this.values.col.min*this.values.dir.m,
this.values.col.max*this.values.dir.m,
this.values.col.cmin,
this.values.col.cmax
));
}
/**
* __calc real angle in deg__
*/
calc_ang(){
// rad2deg > *(180/PI)
// deg2rad > *(PI/180)
if(this.values.dir.xt){this.values.dir.ang=((-this.values.dir.am)*(180/PI))+180;}
else{
if(this.values.dir.am<0){this.values.dir.ang=(-this.values.dir.am)*(180/PI);}
else if(this.values.dir.am>0){this.values.dir.ang=((-this.values.dir.am)*(180/PI))+360;}
else{this.values.dir.ang=0;}
}
}
/**
* __set vector pos in canvas__
* @param {number} _x - Xpos in canvas - _default `0`_
* @param {number} _y - Ypos in canvas - _default `0`_
*/
set_pos(_x=0,_y=0){
this.values.pos.x=_x;
this.values.pos.y=_y;
this.calc_move();
}
/**
* __print vec as p5-object on canvas__
*/
print(){
strokeWeight(this.values.stroke_w);
stroke(this.values.col.rgb);
noFill();
if(!this.values.x.line&&!this.values.x.triangle){ellipse(this.values.pos.x, this.values.pos.y,1);}
if(this.values.x.line&&!this.values.x.big_tri&&!this.values.x.tri_line){line(this.values.pos.x, this.values.pos.y, this.values.dir.x, this.values.dir.y);}
if(this.values.x.triangle){
fill(this.values.tri.fill);
push();
translate(this.values.dir.x,this.values.dir.y);
rotate(this.values.dir.am);
const tr_h=this.values.x.tri_line?this.values.lv:(this.values.x.small_tri?1:(this.values.dir.m*(this.values.x.big_tri?1:this.values.tri.m))*this.values.dir.pl*this.values.tri.height),
tr_w=this.values.x.tri_line?(this.values.lv*0.5):(this.values.x.small_tri?1:(this.values.dir.m*(this.values.x.big_tri?1:this.values.tri.m))*this.values.dir.pl*(this.values.tri.width*0.5));
triangle(
this.values.dir.xt?tr_h:-tr_h,-tr_w, //__|\
0,0, //__| >
this.values.dir.xt?tr_h:-tr_h,tr_w //__|/
);
pop();
}
}
/**
* __create new vector__
* @param {number} _x - Xpos in canvas `[0+]` - _default `0`_
* @param {number} _y - Ypos in canvas `[0+]` - _default `0`_
* @param {number} _m - max length in pixel `[1-500]` - _default `20`_
* @param {number} _fac - used for size length^fac `[>0]` - _default `1.333`_
* @param {number} _min - min length in % `[0-1]` - _default `0`_
* @param {number} _max - max length in % `[0-1]` - _default `1`_
* @param {number} _cmin - color-clip start in % `[0-1]` - _default `0`_
* @param {number} _cmax - color-clip end in % `[0-1]` - _default `1`_
* @param {number} _ccmin - color-hue start in deg `[0-360]` - _default `240`_
* @param {number} _ccmax - color-hue end in deg `[0-360]` - _default `0`_
* @param {number} _stroke_w - stroke weight of arrow in px `[>0]` - _default `3`_
* @param {number} _tri_width - width in px `[0+]` - _default `10`_
* @param {number} _tri_height - height in px `[0+]` - _default `10`_
* @param {number[3]|string} _tri_fill - fill `"css-color"`/`[R,G,B]` each `[0-255]` - _default `[0,0,0]`_
* @param {number} _tri_m - multi for length `[0.01+]` - _default `0.1`_
*/
constructor(_x=0,_y=0,_m=20,_fac=1.333,_min=0,_max=1,_cmin=0,_cmax=1,_ccmin=240,_ccmax=0,_stroke_w=3,_tri_width=10,_tri_height=10,_tri_fill=[0,0,0],_tri_m=0.1){
this.set_pos(_x,_y);
this.values.dir.m=_m;
this.values.dir.fac=_fac;
this.values.dir.min=_min;
this.values.dir.max=_max;
this.values.col.min=_cmin;
this.values.col.max=_cmax;
this.values.col.cmin=_ccmin;
this.values.col.cmax=_ccmax;
this.values.stroke_w=_stroke_w;
this.values.tri.width=_tri_width;
this.values.tri.height=_tri_height;
this.values.tri.fill=_tri_fill;
this.values.tri.m=_tri_m;
}
};
/** @type {number} - last FPS - used for FPS-text-smoothing */
let last_fps = 0;
/** @type {number} - last mouse-Xpos */
let lastMX = 0;
/** @type {number} - last mouse-Ypos */
let lastMY = 0;
/** @type {_Vector[]} - stores all the vectors this array - live editable */
let varr = [];
/** @type {number} - (max) FPS of canvas - set before start */
const _FPS=60;
/**
* __mouse function - inside `draw()`__
* @type {(_mx:number,_my:number)=>void}
* @param {number} _mx - Mouse-Xpos
* @param {number} _my - Mouse-Ypos
*/
let mouse_f=function(_mx,_my){for(const vec of varr){vec.calc_move(_mx,_my);}};/* Your mouse-function here */
/** @type {()=>void|null} - __once function - inside `draw()`__ */
let once_f=null;/* Your one-time-calc-function here */
function setup() {
createCanvas(windowWidth, windowHeight - 0);
lastMX=mouseX;
lastMY=mouseY;
/* Create field: */
for (let _x = 20; _x < width; _x+=50) {
for (let _y = 20; _y < height; _y+=50) {
varr.push(new _Vector(_x,_y));
}
}
console.log("%cmake functions with\n%c %s \n %s \n%calso try: \n%c %s \n %s \n%cand feel free to modify the vector array \"varr\":%O",
"background-color:#000;color:#0f0;font-size:1.5em;font-family:serif;",
"background-color:#000;color:#0f0;font-family:monospace;",
"mouse_f=function(_mx,_my){/*varr[]*/};",
"once_f=function(){/*varr[]*/};once_f(); /* if once_f != null then mouse_f() pauses calculating */",
"background-color:#000;color:#0f0;font-size:1.5em;font-family:serif;",
"background-color:#000;color:#0f0;font-family:monospace;",
"varr.forEach(e=>e.values.x.big_tri=true);",
"varr.forEach(e=>e.values.x.tri_line=true);",
"background-color:#000;color:#0f0;font-size:1.5em;font-family:serif;",
varr
);
frameRate(_FPS);
}
function draw() {
{/* background */
strokeWeight(3);
stroke('blue');
fill('#202020');
rectMode(CORNER);
rect(0, 0, width, height);
}
if((mouseX!=lastMX||mouseY!=lastMY)&&once_f===null){
mouse_f(mouseX,mouseY);
lastMX=mouseX;lastMY=mouseY;
}
for(const vec of varr){vec.print();}
if(!mouseIsPressed){/* cursor */
strokeWeight(1);
stroke('black');
noCursor();
noFill();
line(mouseX, 0, mouseX, height);
line(0, mouseY, width, mouseY);
textAlign(LEFT, BOTTOM);
noStroke();
textSize(12);
fill('yellow');
text(
`x: ${parseInt(mouseX)}\ny: ${parseInt(mouseY)}`,
((mouseX+50)>width)?mouseX-50:mouseX+4,
((mouseY-50)<0)?mouseY+32:mouseY-4
);
}
{/* fps */
textAlign(LEFT, TOP);
textSize(14);
strokeWeight(4);
stroke('black');
fill('lime');
text(`FPS ${last_fps}/${_FPS}`,4,4);
if(frameCount%7==0){last_fps=round((last_fps+frameRate())*0.5);}
}
}
function mouseWheel(event) {
/* keys (in order):
[]vec.values.tri:
alt > m +-0.01
[]vec.values.col:
ctrl > max +-0.02
shift > min +-0.02
[]vec.values.dir:
normal > m ++--
*/
const _min=1,_max=500,_cmin=0,_cmax=1,_cstep=0.02,_trimin=0.01,_trimax=500,_tristep=0.01;
if(event.altKey){/* tri-m */
for (const vector of varr) {
if(event.delta>0){vector.values.tri.m-=_tristep;}
else if(event.delta<0){vector.values.tri.m+=_tristep;}
if(vector.values.tri.m >_trimax){vector.values.tri.m =_trimax;}
else if(vector.values.tri.m <_trimin){vector.values.tri.m =_trimin;}
}
if(once_f==null){mouse_f(mouseX,mouseY);}else{once_f();}
console.log("%ctri-m: %f","background-color:#000;color:#0f0;font-size:1.5em;",varr[0].values.tri.m);
}else if(event.ctrlKey){/* col.max */
for (const vector of varr) {
if(event.delta>0){vector.values.col.max-=_cstep;}
else if(event.delta<0){vector.values.col.max+=_cstep;}
if(vector.values.col.max >_cmax){vector.values.col.max =_cmax;}
else if(vector.values.col.max <_cmin){vector.values.col.max =_cmin;}
vector.calc_color();
}
console.log("%ccol-max: %f","background-color:#000;color:#0f0;font-size:1.5em;",varr[0].values.col.max);
}else if(event.shiftKey){/* col.min */
for (const vector of varr) {
if(event.delta>0){vector.values.col.min-=_cstep;}
else if(event.delta<0){vector.values.col.min+=_cstep;}
if(vector.values.col.min >_cmax){vector.values.col.min =_cmax;}
else if(vector.values.col.min <_cmin){vector.values.col.min =_cmin;}
vector.calc_color();
}
console.log("%ccol-min: %f","background-color:#000;color:#0f0;font-size:1.5em;",varr[0].values.col.min);
}else{/* dir.m */
for (const vector of varr) {
if(event.delta>0){vector.values.dir.m--;}
else if(event.delta<0){vector.values.dir.m++;}
if(vector.values.dir.m>_max){vector.values.dir.m=_max;}
else if(vector.values.dir.m<_min){vector.values.dir.m=_min;}
}
if(once_f==null){mouse_f(mouseX,mouseY);}else{once_f();}
console.log("%cdir-m: %d","background-color:#000;color:#0f0;font-size:1.5em;",varr[0].values.dir.m);
}
return false;
}
/**
* __calc rgb from hue__
* @param {number} h - hue in deg `[0-360]`
* @returns {number[]} - rgb as array `[R,G,B]`
*/
function hue_rgb(h=240){
// hsl: [h,1,0.5]
const hue_to_rgb=function hue_to_rgb(hue) {
if (hue < 0) hue += 6;
if (hue >= 6) hue -= 6;
if (hue < 1) return hue;
else if(hue < 3) return 1;
else if(hue < 4) return (4 - hue);
else return 0;
}
let r, g, b;
h = h / 60;
r = hue_to_rgb(h + 2) * 255;
g = hue_to_rgb(h) * 255;
b = hue_to_rgb(h - 2) * 255;
return [r,g,b];
}
function windowResized(){resizeCanvas(windowWidth, windowHeight);return false;}
</script>
</head>
<body></body>
</html>