-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
192 lines (161 loc) · 6.31 KB
/
Copy pathnode.js
File metadata and controls
192 lines (161 loc) · 6.31 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
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
// NODEBACK - A node based background script for HTML5
// Nodeback is a script for web browsers which draws various nodes
// in the background which float in different directions and connect
// with each other occasionally if they get close
//
// Nodeback uses the HTML5 canvas element to draw these graphics and uses
// javascript to move them around and hold variables about the nodes.
//
// VARIABLES START____________________________
// General Variables
nb_var_container = null;
nb_var_nodearray = [];
// Container Variables
nb_cont_colour = "#000";
// Node variables
nb_node_colour = "#AAA";
nb_node_size = 5;
nb_node_amount = 70;
nb_node_speed = 0.25;
// Line Variables
nb_line_colour = "#555";
nb_line_mindist = 200;
nb_line_width = 1;
//Canvas variables
nb_canvas = null;
// VARIABLES END______________________________
// ======================================================================
// Purpose: Checks if jQuery is present in this webpage
// Returns: Returns 0 if jQuery if found, 1 if errors are encountered
// ======================================================================
function nb_func_checkjquery() {
if(!window.jQuery) {
console.error("[Nodeback] jQuery is required to function correctly.");
return false;
}
return true;
}
// ======================================================================
// Purpose: Creates the container or finds one present on the page
// Returns: Returns the element for variable assignment
// ======================================================================
function nb_func_initcontainer() {
// Create or find an existing container
if ($('#nodeback-container').length > 0) {
nd_var_container = $('#nodeback-container')[0];
} else {
nb_var_container = document.createElement("canvas");
nb_var_container.id = "nodeback-container";
}
nb_var_container.width = $(window).width();
nb_var_container.height = $(window).height();
nb_var_container.style.position = "fixed";
nb_var_container.style.zIndex = "-1";
nb_var_container.style.backgroundColor = nb_cont_colour;
document.body.insertBefore(nb_var_container, document.body.firstChild);
nb_canvas = nb_var_container.getContext("2d");
return nb_var_container;
}
// ======================================================================
// Purpose: Creates all nodes, controlled by variable node_amount
// Returns:
// ======================================================================
function nb_func_initnodes(n) {
nb_var_nodearray.length = 0;
for ( var i = 0 ; i < n ; i++ ) {
// Create a new node, init coords in screen
// and given a random velocity
var nb_tmp_node = new nb_class_node(
parseInt($(window).width() * Math.random()),
parseInt($(window).height() * Math.random()),
(Math.random() - 0.5) * nb_node_speed,
(Math.random() - 0.5) * nb_node_speed
);
nb_var_nodearray.push(nb_tmp_node);
}
}
// ======================================================================
// Purpose: Main loop of nodeback
// Returns: 0 on exit
// ======================================================================
function nb_func_loop() {
requestAnimationFrame(nb_func_loop);
for ( var i = 0 ; i < nb_var_nodearray.length ; i++ ){
if ( nb_var_nodearray[i].xpos < 0 || nb_var_nodearray[i].xpos > $(window).width()) {
nb_var_nodearray[i].xpeed *= -1; Math.min(Math.max(nb_var_nodearray[i].xpos, 0), $(window).width());
}
if ( nb_var_nodearray[i].ypos < 0 || nb_var_nodearray[i].ypos > $(window).height()) {
nb_var_nodearray[i].ypeed *= -1; Math.min(Math.max(nb_var_nodearray[i].ypos, 0), $(window).height());
}
nb_var_nodearray[i].xpos += nb_var_nodearray[i].xpeed;
nb_var_nodearray[i].ypos += nb_var_nodearray[i].ypeed;
}
nb_func_redraw();
}
// ======================================================================
// Purpose: Enumerates all the nodes and draws them on screen
// Returns: 0 on success
// ======================================================================
function nb_func_redraw() {
// Clear the screen first
// This is so the previous nodes are not retained
nb_canvas.clearRect(0, 0, $(window).width(), $(window).height());
nb_canvas.fillStyle = nb_node_colour;
nb_canvas.strokeStyle = nb_line_colour;
nb_canvas.lineWidth = nb_line_width;
// Draw Nodes
for ( var i = 0 ; i < nb_var_nodearray.length ; i++ ) {
// Draw lines between nodes
// We should use pythagorean theorem for this
// c^2 = a^2 + b^2
// c^2 = (x1 - x2)^2 + (y1 - y2)^2
for ( var a = i + 1 ; a < nb_var_nodearray.length ; a++ ) {
var dist = Math.sqrt(
Math.pow((nb_var_nodearray[i].xpos - nb_var_nodearray[a].xpos), 2) +
Math.pow((nb_var_nodearray[i].ypos - nb_var_nodearray[a].ypos), 2)
);
if (dist < nb_line_mindist) {
nb_canvas.beginPath();
nb_canvas.moveTo(nb_var_nodearray[i].xpos, nb_var_nodearray[i].ypos);
nb_canvas.lineTo(nb_var_nodearray[a].xpos, nb_var_nodearray[a].ypos);
nb_canvas.stroke();
nb_canvas.closePath();
}
}
nb_canvas.fillRect(nb_var_nodearray[i].xpos - (nb_node_size / 2), nb_var_nodearray[i].ypos - (nb_node_size / 2), nb_node_size, nb_node_size);
}
}
// ======================================================================
// Purpose: Class default of node elements
// Returns: Nothing
// ======================================================================
function nb_class_node(xpos, ypos, xpeed, ypeed) {
this.xpos = xpos;
this.ypos = ypos;
this.xpeed = xpeed;
this.ypeed = ypeed;
}
// ======================================================================
// Purpose: Called when a resize event is detected
// Returns: Nothing
// ======================================================================
function nodeback_func_resize() {
nb_func_initnodes(nb_node_amount);
nb_var_container.width = $(window).width();
nb_var_container.height = $(window).height();
}
// ======================================================================
// Purpose:
// Returns:
// ======================================================================
function nb_func_init() {
// Print out legal information before continuing...
console.log("[Nodeback] Licenced under WTFPL 2016-2021.");
if(!nb_func_checkjquery())
return -1;
nb_func_initcontainer();
nb_func_initnodes(nb_node_amount);
nb_func_loop();
}
$(document).ready (nb_func_init);
$(window).resize(nodeback_func_resize);