-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplexus.js
More file actions
110 lines (94 loc) · 2.7 KB
/
Copy pathplexus.js
File metadata and controls
110 lines (94 loc) · 2.7 KB
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
(function () {
'use strict';
var canvas = document.createElement('canvas');
canvas.id = 'plexus-bg';
canvas.style.cssText = 'position:fixed;inset:0;width:100%;height:100%;z-index:0;pointer-events:none;';
document.body.insertBefore(canvas, document.body.firstChild);
var ctx = canvas.getContext('2d');
var W, H, nodes;
var NODE_COUNT = 80;
var SPEED = 0.38;
var MAX_DIST = 155;
var BG_COLOR = '#020a02';
var LINE_COLOR = [0, 200, 50];
var GLOW_COLOR = '#cfffcf';
var GLOW_CHANCE = 0.13;
function resize() {
W = canvas.width = window.innerWidth;
H = canvas.height = window.innerHeight;
}
function randomNode() {
var glow = Math.random() < GLOW_CHANCE;
return {
x: Math.random() * W,
y: Math.random() * H,
vx: (Math.random() - 0.5) * SPEED * 2,
vy: (Math.random() - 0.5) * SPEED * 2,
r: glow ? 1.8 + Math.random() * 0.8 : 0.6 + Math.random() * 1.0,
glow: glow
};
}
function init() {
resize();
nodes = [];
for (var i = 0; i < NODE_COUNT; i++) nodes.push(randomNode());
}
function draw() {
ctx.fillStyle = BG_COLOR;
ctx.fillRect(0, 0, W, H);
// Lines
for (var i = 0; i < nodes.length; i++) {
for (var j = i + 1; j < nodes.length; j++) {
var dx = nodes[i].x - nodes[j].x;
var dy = nodes[i].y - nodes[j].y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < MAX_DIST) {
var alpha = (1 - dist / MAX_DIST) * 0.55;
ctx.beginPath();
ctx.strokeStyle = 'rgba(' + LINE_COLOR[0] + ',' + LINE_COLOR[1] + ',' + LINE_COLOR[2] + ',' + alpha + ')';
ctx.lineWidth = 0.8;
ctx.moveTo(nodes[i].x, nodes[i].y);
ctx.lineTo(nodes[j].x, nodes[j].y);
ctx.stroke();
}
}
}
// Nodes
for (var k = 0; k < nodes.length; k++) {
var n = nodes[k];
ctx.beginPath();
if (n.glow) {
ctx.shadowBlur = 14;
ctx.shadowColor = GLOW_COLOR;
} else {
ctx.shadowBlur = 0;
ctx.shadowColor = 'transparent';
}
ctx.arc(n.x, n.y, n.r, 0, Math.PI * 2);
ctx.fillStyle = n.glow
? GLOW_COLOR
: 'rgba(' + LINE_COLOR[0] + ',' + LINE_COLOR[1] + ',' + LINE_COLOR[2] + ',0.75)';
ctx.fill();
}
ctx.shadowBlur = 0;
}
function update() {
for (var i = 0; i < nodes.length; i++) {
var n = nodes[i];
n.x += n.vx;
n.y += n.vy;
if (n.x < 0 || n.x > W) n.vx *= -1;
if (n.y < 0 || n.y > H) n.vy *= -1;
}
}
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
window.addEventListener('resize', function () {
resize();
});
init();
loop();
})();