-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravityfield.js
462 lines (373 loc) · 14.1 KB
/
gravityfield.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// A cool interactive background effect designed by Mason BD Hieb
// Copyright 2021 Mason BD Hieb
// masonhieb.com
function gravityfieldMain(radius=2) {
const gravityfield = document.getElementById('gravityfield');
const gf_ctx = gravityfield.getContext('2d');
const mousePos = {
x: 0,
y: 0
}
const dotSep = 70; // distance (in pixels) between each dot
const cardinalRadius = 8; // Radius that the cardinal spline is built into
const gravityMultiplier = 15; // increase global strength of gravity
const fixedPosMass = 3;
const gfDotMass = 1;
const mouseMass = 20;
const colorizeAttracted = true;
const drawLines = false;
const colorVec = colorVecGenerate(80, 80, 80, 0 ,6, 120, 0, 0.6);
const debugMode = false;
setupScreen = () => {
allDots = [];
gravityfield.width = window.innerWidth;
gravityfield.height = window.innerHeight;
// allDots.push(new gfDot(gravityfield.width/2,gravityfield.height/2));
const margin = dotSep*2;
const width = margin+document.body.clientWidth;
const height = margin+document.body.clientHeight;
let hx = -margin;
let hy = -margin;
while (hx <= width) {
hy = -margin;
let isFirst = true;
while (hy <= height) {
allDots.push(new gfDot(hx,hy,radius,!isFirst,(isFirst ? null : allDots[allDots.length-1])));
hy+=dotSep;
isFirst = false;
}
hx+=dotSep;
}
return allDots
}
var allDots = setupScreen();
function unit_vector(x1,y1,x2,y2) {
const magnitude = dist(x1,y1,x2,y2);
return {
magnitude: magnitude,
x: (x2-x1)/magnitude,
y: (y2-y1)/magnitude
}
}
function calcGravity(m1,m2,r,forMouse) {
// A modified gravity equation to remote erratic behavior
// and to prevent the mouse from having too much influence
let denom;
if (r < 1) {
denom = Math.pow(r,2);
} else if (forMouse) {
denom = r;
} else {
denom = Math.sqrt(r);
}
const out = ((m1*m2)/denom)*gravityMultiplier;
if (out > 100) {
return 0;
}
return out
}
function calcGravityForceVector(x1,y1,x2,y2,m1,m2,forMouse) {
const uv = unit_vector(x1,y1,x2,y2);
if (uv.magnitude == 0) {
return {
x:0,
y:0
}
} else {
const g = calcGravity(m1,m2,uv.magnitude,forMouse);
return {
x:g*uv.x,
y:g*uv.y
}
}
}
const minPullDistance = 10;
const magmult = minPullDistance/window.innerWidth;
setupGravityField();
updateGravityField();
function pointInCircle(xIn,yIn,radius,t) {
return {
x:xIn+(radius*Math.cos(t)),
y:yIn+(radius*Math.sin(t))
}
}
function randomCardinalSpline(bx,by,x,y) {
// https://www.cubic.org/docs/hermite.htm
const init_t = 0.1;
this.t = 0;
this.tvel = 1/(100+Math.random()*200);
const a = 0.5;
this.p0x = x;
this.p0y = y;
// let pmc = pointInCircle(bx,by,Math.random()*cardinalRadius*5,Math.random()*2*Math.PI);
// this.t0x = pmc.x;
// this.t0y = pmc.y;
this.t0x = init_t;
this.t0y = init_t;
let pmc = pointInCircle(bx,by,Math.random()*cardinalRadius,Math.random()*2*Math.PI);
this.p1x = pmc.x;
this.p1y = pmc.y;
// pmc = pointInCircle(bx,by,Math.random()*cardinalRadius*5,Math.random()*2*Math.PI);
// this.t1x = pmc.x;
// this.t1y = pmc.y;
this.t1x = init_t;
this.t1y = init_t;
this.last_p = {x:0,y:0};
this.pos = () => {
if (this.t > 1) {
this.t = 0;
this.t0x = a*(this.p1x - this.p0x);
this.t0y = a*(this.p1y - this.p0y);
old_p1x = this.p1x;
old_p1y = this.p1y;
pmc = pointInCircle(bx,by,Math.random()*cardinalRadius,Math.random()*2*Math.PI);
this.p1x = pmc.x;
this.p1y = pmc.y;
this.t1x = a*(this.p1x - old_p1x);
this.t1y = a*(this.p1y - old_p1y);
this.p0x = this.last_p.x;
this.p0y = this.last_p.y;
}
const t2 = Math.pow(this.t,2);
const t3 = Math.pow(this.t,3);
const h1 = (2*t3)-(3*t2)+1;
const h2 = (t3)-(2*t2)+this.t;
const h3 = (-2*t3)+(3*t2);
const h4 = t3-t2;
// console.log('t2',t2,'t3',t3,'h1',h1,'h2',h2,'h3',h3,'h4',h4,this);
this.t+=this.tvel;
this.last_p = {
x:(h1*this.p0x)+(h2*this.t0x)+(h3*this.p1x)+(h4*this.t1x),
y:(h1*this.p0y)+(h2*this.t0y)+(h3*this.p1y)+(h4*this.t1y)
}
return this.last_p;
}
}
function colorFromForce(t,colorVec,base_opacity) {
let r = colorVec.rb+(colorVec.rv*t);
let g = colorVec.gb+(colorVec.gv*t);
let b = colorVec.bb+(colorVec.bv*t);
let o = colorVec.ob+(colorVec.ov*t);
if (r > 255) {
r = 255;
}
if (g > 255) {
g = 255;
}
if (b > 255) {
b = 255;
}
if (o > 1) {
o = 1;
}
if (r < 0) {
r = 0;
}
if (g < 0) {
g = 0;
}
if (b < 0) {
b = 0;
}
if (o < 0) {
o = 0;
}
return "rgba("+r+", "+g+", "+b+", " + (base_opacity+o) + ")";
}
function colorVecGenerate(c1r,c1g,c1b,c1o,c2r,c2g,c2b,c2o) {
// Creates a vector in RGB space for easy interpolation
return {
rb:c1r,
gb:c1g,
bb:c1b,
ob:c1o,
rv: c2r-c1r,
gv: c2g-c1g,
bv: c2b-c1b,
ov: c2o-c1o
}
}
// let test_hc = new randomHermiteCurve(0,0,5,5);
// console.log(test_hc.pos(0));
function gfDot(x,y,radius,hasAbove,aboveDot) {
this.fpx = x; // fixed position, doesn't move, this is what the base moves around
this.fpy = y;
this.bx = x; // base position, has gravitational pull
this.by = y;
this.x = x+10+Math.random()*(dotSep-10);
this.y = y+10+Math.random()*(dotSep-10);
this.radius = radius;
this.hasAbove = hasAbove;
this.aboveDot = aboveDot;
this.path = new randomCardinalSpline(x,y,x,y);
this.opacity = 0.25+(Math.random()*(colorizeAttracted ? 0.5 : 0.75));
this.color = "rgba(100, 100, 100, " + this.opacity + ")";
this.lastFrameTime = Date.now();
this.singlerun = true;
this.fx = 0; // force
this.fy = 0;
// for calculating average seconds to get the correct acceleration on each frame
this.mCtr = 0;
this.averageElapsedSeconds = 0;
this.aesSum = 0;
// x and y == bx and by, until mouse reaches minPullDistance. Once that value is reached, dot moves towards mouse, with a
this.vx = 0; // velocity
this.vy = 0;
this.pd = 0;
this.calcForce = () => {
const gFp = calcGravityForceVector(this.x,this.y,this.bx,this.by,gfDotMass,fixedPosMass,false);
this.fx = gFp.x;
this.fy = gFp.y;
const gMp = calcGravityForceVector(this.x,this.y,mousePos.x,mousePos.y,gfDotMass,mouseMass,true);
this.fx += gMp.x;
this.fy += gMp.y;
if (colorizeAttracted) {
const gmag = Math.sqrt(Math.pow(gMp.x,2)+Math.pow(gMp.y,2));
this.color = colorFromForce(gmag/10,colorVec,this.opacity-0.4);
}
}
this.move = () => {
//if (!document.hidden && document.hasFocus) { // otherwise acceleration experiences an "integral windup moment" and everything explodes if you tab out, wait a while, and then tab back in
if (this.mCtr < 10) { // get an average after so many cycles, and then use this from that point on, so that if user tabs out we don't get what you might call an "integral windup moment"
const now = Date.now();
const elapsedS = (now - this.lastFrameTime)/1000; // acceleration is per this time unit
this.aesSum += elapsedS;
this.lastFrameTime = now;
this.mCtr++;
this.averageElapsedSeconds = this.aesSum/this.mCtr;
}
this.calcForce();
let ax = (this.fx/gfDotMass)*this.averageElapsedSeconds; // acceleration
let ay = (this.fy/gfDotMass)*this.averageElapsedSeconds;
const maxAcceleration = 0.07;
const maxVelocity = 3;
if (Math.abs(ax) > maxAcceleration) {
ax = (ax > 0) ? maxAcceleration : -maxAcceleration;
}
if (Math.abs(ay) > maxAcceleration) {
ay = (ay > 0) ? maxAcceleration : -maxAcceleration;
}
this.vx+=ax;
this.vy+=ay;
if (Math.abs(this.vx) > maxVelocity) {
if (this.vx < 0) {
this.vx = -maxVelocity;
} else {
this.vx = maxVelocity;
}
}
if (Math.abs(this.vy) > maxVelocity) {
if (this.vy < 0) {
this.vy = -maxVelocity;
} else {
this.vy = maxVelocity;
}
}
if (this.t >= 1) {
this.t = 0;
this.path = new randomCardinalSpline(this.fpx,this.fpy,this.x,this.y);
} else {
const p = this.path.pos();
this.bx = p.x;
this.by = p.y;
}
this.t += this.tvel;
this.x += this.vx;
this.y += this.vy;
//}
}
this.show = () => {
// gf_ctx.beginPath();
// // gf_ctx.fillStyle = "rgba(255, 255, 255, " + opacity + ")";
// gf_ctx.fillStyle = "limegreen";
// gf_ctx.arc(this.fpx, this.fpy, 3, 0, Math.PI * 2);
// gf_ctx.fill();
if (debugMode) {
// gf_ctx.beginPath();
// gf_ctx.fillStyle = "limegreen";
// gf_ctx.arc(this.path.p0x, this.path.p0y, 3, 0, Math.PI * 2);
// gf_ctx.fill();
// gf_ctx.beginPath();
// gf_ctx.fillStyle = "green";
// gf_ctx.arc(this.path.p1x, this.path.p1y, 3, 0, Math.PI * 2);
// gf_ctx.fill();
gf_ctx.beginPath();
gf_ctx.fillStyle = "rgba(50, 160, 50, 0.25)";
gf_ctx.arc(this.bx, this.by, 3, 0, Math.PI * 2);
gf_ctx.fill();
}
if (drawLines && this.hasAbove) {
gf_ctx.beginPath();
gf_ctx.strokeStyle = "rgba(100, 100, 100, 0.25)";
gf_ctx.moveTo(this.aboveDot.x, this.aboveDot.y);
gf_ctx.lineTo(this.x, this.y);
gf_ctx.stroke();
}
gf_ctx.beginPath();
gf_ctx.fillStyle = this.color;
gf_ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
gf_ctx.fill();
}
}
window.addEventListener('mousemove', e => {
mousePos.x = e.clientX;
mousePos.y = e.clientY;
});
window.addEventListener('drag', e => {
mousePos.x = e.touches[0].clientX;
mousePos.y = e.touches[0].clientY;
});
function setupGravityField() {
window.cancelAnimationFrame(updateGravityField);
}
function updateGravityField() {
const grad = gf_ctx.createRadialGradient(gravityfield.width/2, gravityfield.height/2, gravityfield.width/15, gravityfield.width/2, gravityfield.height/2, gravityfield.width/2);
grad.addColorStop(0,"rgb(224, 224, 224)");
grad.addColorStop(1,"rgb(120, 120, 120)");
gf_ctx.fillStyle = grad;
gf_ctx.fillRect(0, 0, gravityfield.width, gravityfield.height);
allDots.forEach(e => {
e.move();
e.show();
});
window.requestAnimationFrame(updateGravityField);
}
/*
function bezierCurve(bx,by,x,y) {
const bezierRadius = divv;
// random quadratic bezier curve, starts at x,y
// and never leaves the circle of given radius
this.p0x = x;
this.p0y = y;
let pmc = paramCircle(bx,by,cardinalRadius,Math.random()*2*Math.PI);
this.p1x = pmc.xOut;
this.p1y = pmc.yOut;
pmc = paramCircle(bx,by,cardinalRadius,Math.random()*2*Math.PI);
this.p2x = pmc.xOut;
this.p2y = pmc.yOut;
// gf_ctx.beginPath();
// // gf_ctx.fillStyle = "rgba(255, 255, 255, " + opacity + ")";
// gf_ctx.fillStyle = "darkgreen";
// gf_ctx.arc(this.p1x, this.p1y, 8, 0, Math.PI * 2);
// gf_ctx.fill();
this.pos = (t) => {
return {
x:(1-t)*((1-t)*this.p0x+t*this.p1x)+t*((1-t)*this.p1x+t*this.p2x),
y:(1-t)*((1-t)*this.p0y+t*this.p1y)+t*((1-t)*this.p1y+t*this.p2y),
}
}
}
this.setDvec = () => {
const magnitude = dist(this.fpx,this.fpy,mousePos.x,mousePos.y);
this.dvx = (mousePos.x - this.fpx)/magnitude;
this.dvy = (mousePos.y - this.fpy)/magnitude;
if (magnitude > minPullDistance) {
this.dvx+=magmult*(magnitude);
this.dvy+=magmult*(magnitude);
} else {
this.dvx = 0;
this.dvy = 0;
}
}
*/
}