-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
212 lines (167 loc) · 6.06 KB
/
script.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
let activeColor = "#79cef4";
let defaultColor = "#336666";
let activeItem = ""; // last clicked element
let elementList = []; // array with all the elements based on hovered/clicked element
let lineCoords = []; // 2 dim array[startX, startY, endX, endY] for the lines drawn between the elements
//canvas
var width = document.getElementById("main").offsetWidth;
var height = document.getElementById("main").offsetHeight;
const canvas = document.getElementById("lineCanvas");
const ctx = canvas.getContext("2d");
$(document).ready(function () {
// get size of div for drawing the line. update canvas if user resizes the window.
$(window).resize(function () {
width = document.getElementById("main").offsetWidth;
height = document.getElementById("main").offsetHeight;
updateCanvas();
drawLine();
});
$("li span").hover(
function () {
var leftElements = this.parentElement.className;
var currentElement = $(this).parent().attr("id");
var rightElements = this.className;
// if there is no selected element
if (activeItem === "") {
$(this).css({ color: "#79cef4", "text-shadow": "#fff 1px 0 10px" }); // set hovered element to activeState
updateCanvas();
if (leftElements != "") {
// if hovered item contains elements to the left, than loop through all of them and add them to the elementList
leftElementsArray = leftElements.split(" ");
leftRecursion(leftElementsArray, currentElement);
}
if (rightElements != "") {
rightElementsArray = rightElements.split(" ");
rightRecursion(rightElementsArray, currentElement);
}
changeState(activeColor);
}
},
function () {
if (this.innerHTML != activeItem) {
$(this).css({ color: "", "text-shadow": "" }); // set unhovered element to defaultState
if (activeItem === "") {
changeState(defaultColor);
updateCanvas();
}
}
}
);
$("li span").click(function () {
var leftElements = this.parentElement.className;
var currentElement = $(this).parent().attr("id");
var rightElements = this.className;
$("#" + currentElement).css({ color: "#79cef4", "text-shadow": "#fff 1px 0 10px", }); // set clicked element to activeState
if (activeItem === "") {
// if user clicks an element
changeState(activeColor);
activeItem = currentElement;
} else if (activeItem != currentElement) {
// else if user clicks something else
elementList.push(activeItem);
changeState(defaultColor);
if (leftElements != "") {
leftElementsArray = leftElements.split(" ");
leftRecursion(leftElementsArray, currentElement);
}
if (rightElements != "") {
rightElementsArray = rightElements.split(" ");
rightRecursion(rightElementsArray, currentElement);
}
changeState(activeColor);
activeItem = currentElement;
} else {
// else if user clicks the same element
$("#" + activeItem).css({ color: "#336666", "text-shadow": "" });
changeState(defaultColor);
}
});
// randomize colors
const randomHexColor = () => {
let n = (Math.random() * 0xfffff * 1000000).toString(16);
return "#" + n.slice(0, 6);
};
function updateCanvas() {
ctx.canvas.width = window.width - 3;
ctx.canvas.height = window.height;
}
function changeState(color) {
let i = 0;
while (i < elementList.length) {
let item = elementList[i];
document.getElementById(item).style.color = color;
document.getElementById(item).style.textShadow = "";
i++;
}
if (color === defaultColor) {
updateCanvas();
activeItem = "";
elementList = [];
lineCoords = [];
} else {
drawLine();
}
}
// Recursion Logic:
// After hovering/clicking an element, we iterate through all the elements to the left.
// If those elements also have left elements, we recursively do the same thing again.
// This way we can add all the elements we need to the elementList and all the coordinates to lineCoords.
function leftRecursion(leftElementsArray, middle) {
var midOffset = document.getElementById(middle).getBoundingClientRect();
var startX = midOffset.left;
var startY = midOffset.top - 90;
leftElementsArray.forEach((element) => {
if (element.className != "") {
elementList.push(element);
var elementOffset = document
.getElementById(element)
.getBoundingClientRect();
var endX = elementOffset.right;
var endY = elementOffset.top - 90;
lineCoords.push([startX, startY, endX, endY]);
let leftElement = document.getElementById(element);
elementArray = leftElement.className.split(" ");
if (elementArray != "") {
leftRecursion(elementArray, element);
}
}
});
}
function rightRecursion(rightElementsArray, middle) {
var midOffset = document.getElementById(middle).getBoundingClientRect();
var startX = midOffset.right;
var startY = midOffset.top - 90;
rightElementsArray.forEach((element) => {
if (element != "") {
var elementOffset = document
.getElementById(element)
.getBoundingClientRect();
var endX = elementOffset.left;
var endY = elementOffset.top - 90;
lineCoords.push([startX, startY, endX, endY]);
elementList.push(element);
let rightElement = document.getElementById(element).firstChild;
elementArray = rightElement.className.split(" ");
if (elementArray != "") {
rightRecursion(elementArray, element);
}
}
});
}
function drawLine() {
let i = 0;
let strokeColor = randomHexColor();
while (i < lineCoords.length) {
let startX = lineCoords[i][0];
let startY = lineCoords[i][1];
let endX = lineCoords[i][2];
let endY = lineCoords[i][3];
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
ctx.strokeStyle = strokeColor;
ctx.lineWidth = 1;
ctx.stroke();
i++;
}
}
});