-
Notifications
You must be signed in to change notification settings - Fork 0
/
green-magnetic-dots.html
96 lines (73 loc) · 2.5 KB
/
green-magnetic-dots.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
<!DOCTYPE html><html><head>
<script src="https://let-pr.com/wp-content/uploads/2019/09/p5.min.js"></script>
<script src="https://let-pr.com/wp-content/uploads/2019/09/p5.dom.min.js"></script>
<meta charset="utf-8">
<style>
html, body {
margin: 0;
padding: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script>var mass = [];
var positionX = [];
var positionY = [];
var velocityX = [];
var velocityY = [];
/////////////////////////////////////////////////////////////////////////////////////////////////////
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function draw() {
background(32);
for (var particleA = 0; particleA < mass.length; particleA++) {
var accelerationX = 0, accelerationY = 0;
for (var particleB = 0; particleB < mass.length; particleB++) {
if (particleA != particleB) {
var distanceX = positionX[particleB] - positionX[particleA];
var distanceY = positionY[particleB] - positionY[particleA];
var distance = sqrt(distanceX * distanceX + distanceY * distanceY);
if (distance < 1) distance = 1;
var force = (distance - 160) * mass[particleB] / distance;
accelerationX += force * distanceX;
accelerationY += force * distanceY;
}
}
velocityX[particleA] = velocityX[particleA] * 0.99 + accelerationX * mass[particleA];
velocityY[particleA] = velocityY[particleA] * 0.99 + accelerationY * mass[particleA];
}
for (var particle = 0; particle < mass.length; particle++) {
positionX[particle] += velocityX[particle];
positionY[particle] += velocityY[particle];
fill(194, 206, 37,220);
ellipse(positionX[particle], positionY[particle], mass[particle] * 500, mass[particle] * 500);
}
noFill();
strokeWeight(1);
stroke(194, 206, 37);
r = 50;
ellipse((mouseX-r/5), (mouseY-r/5), r, r);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function addNewParticle() {
mass.push(random(0.003, 0.03));
positionX.push(mouseX);
positionY.push(mouseY);
velocityX.push(0);
velocityY.push(0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function mouseClicked() {
addNewParticle();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function mouseDragged() {
addNewParticle();
}</script>
</body></html>