/
molecule.js
140 lines (111 loc) · 4.58 KB
/
molecule.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
(function(window, document){
window.onload = function(){
var canvas;
var ctx;
var Width,
Height,
i,j,
molecules = [],
moleculesTimer,
cursor,
//default values: Change according to your need
numMolecules = 50,
minDistance = 50,
moleculeColor = "rgba(255, 255, 255, .5)",
moleculeSpeed = 0.5,
moleculeSizeMax = 3,
moleculeSizeMin = 1;
createCanvas();
setCanvas();
// Generate the molecules
generateMolecules();
//start the timer here
draw();
// moleculesTimer = setInterval(draw, 100);
document.getElementsByTagName("body")[0].addEventListener("mousemove", function(e){
cursor.x = e.pageX;
cursor.y = e.pageY;
});
function draw(){
//clear canvas
ctx.clearRect(0, 0, Width, Height);
// draw the molecules and their bonds
ctx.strokeStyle = moleculeColor;
drawMolecules();
requestAnimationFrame(draw);
}
function generateRandom(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateMolecules(){
for(i = 0; i < numMolecules; i++){
var newMolecule = {
size :generateRandom(moleculeSizeMin, moleculeSizeMax),
y: generateRandom(0,Height),
x: generateRandom(0, Width),
vx: Math.random()/2,// generateRandom(-moleculeSpeed, moleculeSpeed),
vy: Math.random()/2,//generateRandom(-moleculeSpeed, moleculeSpeed),
angle: generateRandom(-Math.PI, Math.PI)
};
molecules.push(newMolecule);
}
}
function drawMolecules() {
for(var length = molecules.length-1, i = length; i >= 0; i--){
var m1 = molecules[i];
m1.x += Math.cos(m1.angle) * m1.vx;
m1.y += Math.sin(m1.angle) * m1.vy;
ctx.beginPath();
ctx.arc(m1.x, m1.y, m1.size, 0, 360);
ctx.stroke();
ctx.beginPath();
ctx.fillStyle = moleculeColor;
ctx.arc(m1.x, m1.y, m1.size-1, 0, 360);
ctx.fill();
let offset = 20;
if(m1.x < -offset || m1.x > Width+offset) m1.vx *= -1;
else if(m1.y < -offset || m1.y > Height+offset) m1.vy *= -1;
for(var length2 = molecules.length-1, j = length2; j >= 0; j--){
var m2 = molecules[j];
var x1 = m1.x;
var y1 = m1.y;
var x2 = m2.x;
var y2 = m2.y;
var distance = Math.sqrt(Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2));
if(distance < minDistance){
ctx.lineWidth = 0.4;
ctx.beginPath();
ctx.moveTo(m1.x, m1.y);
ctx.lineTo(m2.x, m2.y);
ctx.stroke();
ctx.lineWidth = 1;
}
}
}
}
function createCanvas(){
canvas = document.createElement("canvas");
ctx = canvas.getContext('2d');
document.getElementsByTagName('body')[0].appendChild(canvas);
}
function setCanvas(){
Width = window.innerWidth;
Height = window.innerHeight;
canvas.setAttribute("width", Width);
canvas.setAttribute("height", Height);
canvas.style.position = "absolute";
canvas.style.left = 0;
canvas.style.top = 0;
canvas.style.zIndex = -1;
canvas.style.cursor = "default";
molecules = [];
generateMolecules();
// create and push the push the cursor molecule initially
cursor = {size: 1, y: 0, x: 0, speedX: 0, speedY: 0};
molecules.push(cursor);
}
window.onresize = function(){
setCanvas();
}
};
}(window, document));