-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparator.js
411 lines (391 loc) · 15 KB
/
comparator.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*!
* Comparator.js v0.2
*
* Copyright (c) 2015, Ejvind Hansen
* http://ejvindh.net/
*
* Distributed under the GPL v3.0 license. See LICENSE file for details.
*
* All rights reserved.
*
* Date: Wed Apr 29 21:00:00 2015
*
***/
var canvas, numberOfVites, animationSpeed;
var vites = new Array();
var comparisons = new Array();
var currentComparisons = new Array();
var oldconnectorHit = -1;
var radiusNodeCircleW, radiusNodeCircleH, connectorButtonRadius;
var centerIndex, centerViewX, centerViewY, radiusViewW, radiusViewH;
var clickSizeX, clickSizeY;
var txtProperties, bodyProperties;
function init() {
canvas = document.getElementById("myCanvas");
setSizes();
initializeVites();
initializeComparisons();
centerIndex = -1;
initializeTextContainer();
draw();
animate();
txtProperties = document.querySelector(".textContainer");
txtProperties.style.fontFamily = layout.comparisonTextFont;
bodyProperties = document.querySelector("body");
bodyProperties.style.fontSize = layout.comparisonTextSize;
var onresize = function(e) {
setSizes();
}
window.addEventListener("resize", onresize);
canvas.addEventListener('click', function(event) {
//Determine what has been clicked -- nodes or connector-selector
var mousePos = getMousePos(canvas, event);
var nodeHit = findClickedNodes(mousePos);
var connectorHit = findClickedConnectors(mousePos);
if (nodeHit >=0) {
if (centerIndex == nodeHit) {
centerIndex = -1;
} else {
centerIndex = nodeHit;
}
calculateNewXY();
extractComparisons(nodeHit);
initializeTextContainer();
}
if (connectorHit >=0) {
showComparisons(connectorHit);
}
}, false);
}
function getMousePos(canvas, event) {
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
function findClickedNodes(mousePos) {
// Did the mouse click a node?
var resizeRatioX = clickSizeX / (centerViewX * 2);
var resizeRatioY = clickSizeY / (centerViewY * 2);
var clickedNode = -1;
var i = 0;
while (clickedNode==-1 && i < numberOfVites) {
var xMin = (vites[i].curX - radiusNodeCircleW) * resizeRatioX;
var xMax = (vites[i].curX + radiusNodeCircleW) * resizeRatioX;
var yMin = (vites[i].curY - radiusNodeCircleH) * resizeRatioY;
var yMax = (vites[i].curY + radiusNodeCircleH) * resizeRatioY;
if (mousePos.x > xMin && mousePos.x < xMax &&
mousePos.y > yMin && mousePos.y < yMax) {
clickedNode = i;
}
i++;
}
return clickedNode;
}
function findClickedConnectors(mousePos) {
// Did the mouse click a connectorbutton?
var resizeRatioX = clickSizeX / (centerViewX * 2);
var resizeRatioY = clickSizeY / (centerViewY * 2);
var clickedConnector = -1;
var i = 0;
while (clickedConnector==-1 && i < numberOfVites) {
if (i != centerIndex) {
var xMin = (vites[i].connectorMiddleX - connectorButtonRadius) * resizeRatioX;
var xMax = (vites[i].connectorMiddleX + connectorButtonRadius) * resizeRatioX;
var yMin = (vites[i].connectorMiddleY - connectorButtonRadius) * resizeRatioY;
var yMax = (vites[i].connectorMiddleY + connectorButtonRadius) * resizeRatioY;
if (mousePos.x > xMin && mousePos.x < xMax &&
mousePos.y > yMin && mousePos.y < yMax &&
vites[i].comparison > 0) {
clickedConnector = i;
}
}
i++;
}
return clickedConnector;
}
function initializeTextContainer() {
// Reset content of the textcontainer (upon firstRun and when clicking af new node into the center)
var currentVite = "";
var currentLinks = "";
var currentDiff = "";
var currentSimil = "";
var currentComments = "";
if (centerIndex != -1) {
currentVite = vites[centerIndex].name + " <small>" + messages.clickConnector + "</small>";
currentLinks = vites[centerIndex].links;
currentComments = vites[centerIndex].comments;
} else {
currentVite = "<small>" + messages.clickNode + "</small>";
}
document.getElementById("vitesContainer").innerHTML = currentVite;
document.getElementById("linksContainer").innerHTML = currentLinks;
document.getElementById("diffContainer").innerHTML = currentDiff;
document.getElementById("similaContainer").innerHTML = currentSimil;
document.getElementById("commentContainer").innerHTML = currentComments;
oldconnectorHit = -1;
}
function showComparisons(connectorHit) {
// ConnectorButton has been clicked -- fill content into the textcontainer
if (oldconnectorHit != connectorHit) {
var vitesText = vites[centerIndex].name + " " + messages.versus + " " + vites[connectorHit].name;
var linksText = "<h1>" + messages.links + "</h1>";
var linksTextLength = linksText.length;
var diffText = "<h1>" + messages.differences + "</h1>";
var diffTextLength = diffText.length;
var simText = "<h1>" + messages.similarities + "</h1>";
var simTextLength = simText.length;
var commentText = "<h1>" + messages.comments + "</h1>";
var commentTextLength = commentText.length;
var connectorHitId = vites[connectorHit].id;
for (var i=0; i<currentComparisons.length; i++) {
if ((currentComparisons[i].ids.indexOf("<p>"+connectorHitId+"</p>") > -1)) {
linksText = linksText + currentComparisons[i].links;
diffText = diffText + currentComparisons[i].differences;
simText = simText + currentComparisons[i].similarities;
commentText = commentText + currentComparisons[i].comments;
}
}
document.getElementById("vitesContainer").innerHTML = vitesText;
document.getElementById("linksContainer").innerHTML = linksText;
document.getElementById("diffContainer").innerHTML = diffText;
document.getElementById("similaContainer").innerHTML = simText;
document.getElementById("commentContainer").innerHTML = commentText;
oldconnectorHit = connectorHit;
} else {
initializeTextContainer()
}
}
function calculateNewXY() {
// When new motion has been activated, calculate where the nodes should end
var offset = 0;
var numberOfNodes, x, y;
if (centerIndex == -1) {
numberOfNodes = numberOfVites;
} else {
numberOfNodes = numberOfVites - 1;
}
for (var i = 0; i < numberOfVites; i++) {
if (i == centerIndex) {
offset = 1;
x = centerViewX;
y = centerViewY;
} else {
x = Math.round(centerViewX + radiusViewW * Math.cos(2*Math.PI * (i-offset)/numberOfNodes));
y = Math.round(centerViewY + radiusViewH * Math.sin(2*Math.PI * (i-offset)/numberOfNodes));
}
vites[i].rightX = x;
vites[i].rightY = y;
}
}
function animate() {
// Upon each screenframe: Are any animations called for? If yes, go to draw()
/*reqAnimFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame;
reqAnimFrame(animate);*/
window.requestAnimationFrame(animate);
var changeOffset = false;
for (var i = 0; i < numberOfVites; i++) {
var trivialityLimit = 5; // stop animation if nodes are "close enough" to their "right" x,y
if (Math.abs(vites[i].curX - vites[i].rightX) > trivialityLimit || Math.abs(vites[i].curY - vites[i].rightY) > trivialityLimit) {
changeOffset = true;
//Calculate new positions for nodes
vites[i].curX = vites[i].curX + ((vites[i].rightX - vites[i].curX)/animationSpeed);
vites[i].curY = vites[i].curY + ((vites[i].rightY - vites[i].curY)/animationSpeed);
vites[i].connectorMiddleX = vites[i].curX + ((centerViewX - vites[i].curX) / 2);
vites[i].connectorMiddleY = vites[i].curY + ((centerViewY - vites[i].curY) / 2);
}
}
if (changeOffset) {draw()};
}
function draw() {
// Clear canvas and do a new drawing
var context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < numberOfVites; i++) {
connector(context, vites[i].curX, vites[i].curY, vites[i].connectorMiddleX, vites[i].connectorMiddleY, vites[i].comparison);
}
for (var i = 0; i < numberOfVites; i++) {
oval(context,vites[i].curX,vites[i].curY,radiusNodeCircleW,radiusNodeCircleH,vites[i].name);
}
}
function oval(con, centerX, centerY, width, height, nodeName) {
//Draw the ovals
con.beginPath();
con.moveTo(centerX, centerY - height);
con.bezierCurveTo(
centerX + width*1.3, centerY - height,
centerX + width*1.3, centerY + height,
centerX, centerY + height
);
con.bezierCurveTo(
centerX - width*1.3, centerY + height,
centerX - width*1.3, centerY - height,
centerX, centerY - height
);
con.fillStyle = layout.nodeColor;
con.fill();
con.font = layout.nodeTextSize + " " + layout.nodeTextFont;
con.fillStyle = layout.nodeTextColor;
con.textAlign = "center";
con.textBaseline = 'middle';
con.fillText(nodeName, centerX, centerY,(radiusNodeCircleW*2)-20);
con.closePath();
}
function connector(con, centerX, centerY, conMidX, conMidY, compFlag) {
//Draw connectors and connector buttons
con.beginPath();
con.lineWidth=parseInt(layout.connectorWidth);
con.moveTo(centerViewX, centerViewY);
con.lineTo(centerX, centerY);
if (compFlag > 0) {
con.moveTo(conMidX, conMidY);
con.arc(conMidX, conMidY, connectorButtonRadius, 0, 2 * Math.PI, false);
}
con.fillStyle= layout.comparisonButtonColor;
con.strokeStyle = layout.connectorColor;
con.stroke();
con.fill();
con.closePath();
}
function initializeVites() {
//Get content of nodes.xml into the vites[] array
centerIndex = -1;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","nodes.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("POSITION");
numberOfVites = x.length;
for (i=0;i<numberOfVites;i++) {
var posId = x[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue;
var posName = x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue;
var posComments = "";
for (var j=0; j<x[i].getElementsByTagName("COMMENT").length;j++) {
posComments = posComments + "<p>" + x[i].getElementsByTagName("COMMENT")[j].childNodes[0].nodeValue + "</p>";
}
var posLinks = "";
for (var j=0; j<x[i].getElementsByTagName("HTML").length;j++) {
var linkName = "";
if( x[i].getElementsByTagName("LINKNAME")[j] != null ) {
linkName = x[i].getElementsByTagName("LINKNAME")[j].childNodes[0].nodeValue;
} else {
linkName = messages.externalLinkString;
}
var posLinks = posLinks + "<p><a href='" + x[i].getElementsByTagName("HTML")[j].childNodes[0].nodeValue + "' target='_blank'><strong>" + linkName + "</strong></a></p>";
}
var posX = centerViewX + (radiusViewW * Math.cos(2*Math.PI * i/numberOfVites));
var posY = centerViewY + (radiusViewH * Math.sin(2*Math.PI * i/numberOfVites));
var connectorMiddleX = posX + ((centerViewX - posX) / 2);
var connectorMiddleY = posY + ((centerViewY - posY) / 2);
vites[i] = {
id:posId,
name:posName,
comments:posComments,
links:posLinks,
curX:posX,
curY:posY,
connectorMiddleX: connectorMiddleX,
connectorMiddleY: connectorMiddleY,
rightX:posX,
rightY:posY,
comparison:0
};
}
}
function initializeComparisons() {
//Get content of relations.xml into the comparisons[] array
xmlhttp.open("GET","relations.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("COMPARISON");
for (i=0;i<x.length;i++) {
var ids = "";
var links = "";
var similarities = "";
var differences = "";
var comments = "";
for (var j=0; j<x[i].getElementsByTagName("ID").length;j++) {
ids = ids + "<p>" + x[i].getElementsByTagName("ID")[j].childNodes[0].nodeValue + "</p>";
}
for (var j=0; j<x[i].getElementsByTagName("AUDIO").length;j++) {
links = links + "<p><audio id='SoundaudioButton" + j + "' src='"
+ x[i].getElementsByTagName("AUDIO")[j].childNodes[0].nodeValue
+ "'></audio><button id='audioButton" + j + "' onclick='playSound(this.id)'>"
+ messages.audioButtonBegin + "</button></p>";
}
for (var j=0; j<x[i].getElementsByTagName("HTML").length;j++) {
var linkName = "";
if( x[i].getElementsByTagName("NAME")[j] != null ) {
linkName = x[i].getElementsByTagName("NAME")[j].childNodes[0].nodeValue;
} else {
linkName = messages.externalLinkString;
}
links = links + "<p><a href='" + x[i].getElementsByTagName("HTML")[j].childNodes[0].nodeValue + "' target='_blank'> <strong>" + linkName + "</strong></a></p>";
}
for (var j=0; j<x[i].getElementsByTagName("SIMILARITY").length;j++) {
similarities = similarities + "<p>" + x[i].getElementsByTagName("SIMILARITY")[j].childNodes[0].nodeValue + "</p>";
}
for (var j=0; j<x[i].getElementsByTagName("DIFFERENCE").length;j++) {
differences = differences + "<p>" + x[i].getElementsByTagName("DIFFERENCE")[j].childNodes[0].nodeValue + "</p>";
}
for (var j=0; j<x[i].getElementsByTagName("COMMENT").length;j++) {
comments = comments + "<p>" + x[i].getElementsByTagName("COMMENT")[j].childNodes[0].nodeValue + "</p>";
}
comparisons[i] = {ids:ids, links:links, similarities:similarities, differences:differences, comments:comments};
}
}
function playSound( buttonId ) {
var audio = document.getElementById("Sound" + buttonId);
if (!audio.paused) {
audio.pause();
document.getElementById(buttonId).textContent = messages.audioButtonBegin;
} else {
audio.play();
document.getElementById(buttonId).textContent = messages.audioButtonStop;
}
audio.addEventListener("ended", function () {
document.getElementById(buttonId).textContent = messages.audioButtonBegin;
}, false);
}
function extractComparisons(nodeHit) {
// New node has been clicked to the center, move the potential comparisons in to currentComparisons[] array
currentComparisons.length = 0;
if (centerIndex == -1) {
for (i=0;i<numberOfVites;i++) {
vites[i].comparison = 0;
}
} else {
var found = 0;
for (i=0;i<numberOfVites;i++) {
vites[i].comparison = 0;
}
for (var i=0; i< comparisons.length; i++) {
if (comparisons[i].ids.indexOf("<p>"+vites[nodeHit].id+"</p>") > -1) {
for (var j=0; j<numberOfVites; j++) {
if (comparisons[i].ids.indexOf("<p>"+vites[j].id+"</p>") > -1) {
vites[j].comparison++;
}
}
currentComparisons[found] = comparisons[i];
found++;
}
}
}
}
function setSizes() {
//Set and Calculate sizes of nodes and circles
radiusNodeCircleW = parseInt(layout.nodeSizeW);
radiusNodeCircleH = parseInt(layout.nodeSizeH);
connectorButtonRadius = parseInt(layout.comparisonButtonSize);
centerViewX = Math.round(canvas.width / 2);
centerViewY = Math.round(canvas.height / 2);
radiusViewW = Math.round(canvas.width / 2) - radiusNodeCircleW;
radiusViewH = Math.round(canvas.height / 2) - radiusNodeCircleH;
clickSizeX = canvas.scrollWidth;
clickSizeY = canvas.scrollHeight;
animationSpeed = parseInt(layout.animationSpeed); //small numbers == faster
}