Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/custom/canvas-init.js
1293 views
1
(function() {
2
3
var width, height, largeHeader, canvas, ctx, points, target, animateHeader = true;
4
5
// Main
6
initHeader();
7
initAnimation();
8
addListeners();
9
10
function initHeader() {
11
width = window.innerWidth;
12
height = window.innerHeight;
13
target = {x: width/2, y: height/2};
14
15
largeHeader = document.getElementById('large-header');
16
largeHeader.style.height = height+'px';
17
18
canvas = document.getElementById('demo-canvas');
19
canvas.width = width;
20
canvas.height = 500;
21
ctx = canvas.getContext('2d');
22
23
// calculate divisors
24
x_div = width * 0.015;
25
y_div = height * 0.015;
26
27
// create points
28
points = [];
29
for(var x = 0; x < width; x = x + width/x_div) {
30
for(var y = 0; y < height; y = y + height/y_div) {
31
var px = x + Math.random()*width/x_div;
32
var py = y + Math.random()*height/y_div;
33
var p = {x: px, originX: px, y: py, originY: py };
34
points.push(p);
35
}
36
}
37
38
// for each point find the 5 closest points
39
for(var i = 0; i < points.length; i++) {
40
var closest = [];
41
var p1 = points[i];
42
for(var j = 0; j < points.length; j++) {
43
var p2 = points[j]
44
if(!(p1 == p2)) {
45
var placed = false;
46
for(var k = 0; k < 5; k++) {
47
if(!placed) {
48
if(closest[k] == undefined) {
49
closest[k] = p2;
50
placed = true;
51
}
52
}
53
}
54
55
for(var k = 0; k < 5; k++) {
56
if(!placed) {
57
if(getDistance(p1, p2) < getDistance(p1, closest[k])) {
58
closest[k] = p2;
59
placed = true;
60
}
61
}
62
}
63
}
64
}
65
p1.closest = closest;
66
}
67
68
// assign a circle to each point
69
for(var i in points) {
70
var c = new Circle(points[i], 2+Math.random()*2, 'rgba(255,255,255,0.3)');
71
points[i].circle = c;
72
}
73
}
74
75
// Event handling
76
function addListeners() {
77
if(!('ontouchstart' in window)) {
78
window.addEventListener('mousemove', mouseMove);
79
}
80
window.addEventListener('scroll', scrollCheck);
81
window.addEventListener('resize', resize);
82
}
83
84
function mouseMove(e) {
85
var posx = posy = 0;
86
if (e.pageX || e.pageY) {
87
posx = e.pageX;
88
posy = e.pageY;
89
}
90
else if (e.clientX || e.clientY) {
91
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
92
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
93
}
94
target.x = posx;
95
target.y = posy;
96
}
97
98
function scrollCheck() {
99
if(document.body.scrollTop > height) animateHeader = false;
100
else animateHeader = true;
101
}
102
103
function resize() {
104
width = window.innerWidth;
105
height = window.innerHeight;
106
largeHeader.style.height = height+'px';
107
canvas.width = width;
108
canvas.height = height;
109
}
110
111
// animation
112
function initAnimation() {
113
animate();
114
for(var i in points) {
115
shiftPoint(points[i]);
116
}
117
}
118
119
function animate() {
120
if(animateHeader) {
121
ctx.clearRect(0,0,width,height);
122
for(var i in points) {
123
// detect points in range
124
if(Math.abs(getDistance(target, points[i])) < 4000) {
125
points[i].active = 0.3;
126
points[i].circle.active = 0.6;
127
} else if(Math.abs(getDistance(target, points[i])) < 20000) {
128
points[i].active = 0.1;
129
points[i].circle.active = 0.3;
130
} else if(Math.abs(getDistance(target, points[i])) < 40000) {
131
points[i].active = 0.02;
132
points[i].circle.active = 0.1;
133
} else {
134
points[i].active = 0;
135
points[i].circle.active = 0;
136
}
137
138
drawLines(points[i]);
139
points[i].circle.draw();
140
}
141
}
142
requestAnimationFrame(animate);
143
}
144
145
function shiftPoint(p) {
146
TweenLite.to(p, 1+1*Math.random(), {x:p.originX-50+Math.random()*100,
147
y: p.originY-50+Math.random()*100, ease:Circ.easeInOut,
148
onComplete: function() {
149
shiftPoint(p);
150
}});
151
}
152
153
// Canvas manipulation
154
function drawLines(p) {
155
if(!p.active) return;
156
for(var i in p.closest) {
157
ctx.beginPath();
158
ctx.moveTo(p.x, p.y);
159
ctx.lineTo(p.closest[i].x, p.closest[i].y);
160
ctx.strokeStyle = 'rgba(156,217,249,'+ p.active+')';
161
ctx.stroke();
162
}
163
}
164
165
function Circle(pos,rad,color) {
166
var _this = this;
167
168
// constructor
169
(function() {
170
_this.pos = pos || null;
171
_this.radius = rad || null;
172
_this.color = color || null;
173
})();
174
175
this.draw = function() {
176
if(!_this.active) return;
177
ctx.beginPath();
178
ctx.arc(_this.pos.x, _this.pos.y, _this.radius, 0, 2 * Math.PI, false);
179
ctx.fillStyle = 'rgba(156,217,249,'+ _this.active+')';
180
ctx.fill();
181
};
182
}
183
184
// Util
185
function getDistance(p1, p2) {
186
return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
187
}
188
189
})();
190