-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
102 lines (95 loc) · 2.64 KB
/
draw.js
File metadata and controls
102 lines (95 loc) · 2.64 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
function createGraph(x, y) {
var ok = true;
for (var i = 0; i < nodeArray.length; i++) {
if (checkNodeCollision(nodeArray[i], x, y, size)) {
ok = false;
break;
}
}
if (ok) {
drawCircle(x, y);
nodeArray.push({ x: x, y: y, text: "" });
}
}
function connectGraphs(graph) {
if (selectedGraphOne != null && !compareObjects(selectedGraphOne, graph)) {
selectedGraphTwo = graph;
} else {
selectedGraphOne = graph;
}
if (selectedGraphOne && selectedGraphTwo) {
var ok = true;
for (var i = 0; i < lineArray.length; i++) {
if (
(compareObjects(selectedGraphOne, lineArray[i][0]) &&
compareObjects(selectedGraphTwo, lineArray[i][1])) ||
(compareObjects(selectedGraphOne, lineArray[i][1]) &&
compareObjects(selectedGraphTwo, lineArray[i][0]))
) {
ok = false;
textForLine = i;
createInputBox(
(selectedGraphOne.x + selectedGraphTwo.x) / 2,
(selectedGraphOne.y + selectedGraphTwo.y) / 2
);
}
}
if (ok) {
drawLine(selectedGraphOne.x, selectedGraphOne.y, selectedGraphTwo.x, selectedGraphTwo.y);
lineArray.push([selectedGraphOne, selectedGraphTwo]);
createInputBox(
(selectedGraphOne.x + selectedGraphTwo.x) / 2,
(selectedGraphOne.y + selectedGraphTwo.y) / 2
);
textForLine = lineArray.length - 1;
}
clearSelection();
}
}
function createInputBox(x, y) {
var input = document.createElement("INPUT");
input.setAttribute("type", "text");
input.setAttribute("autofocus", "autofocus");
input.style.position = "absolute";
input.style.top = y;
input.style.left = x;
input.style.width = size;
document.body.appendChild(input);
}
function clearSelection() {
selectedGraphOne = null;
selectedGraphTwo = null;
}
function redrawStuff() {
nodeArray.forEach((element) => {
drawCircle(element.x, element.y);
ctx.font = "15px Arial";
ctx.fillStyle = "green";
ctx.fillText(element.text, element.x - size / 2, element.y);
});
lineArray.forEach((element) => {
drawLine(element[0].x, element[0].y, element[1].x, element[1].y);
ctx.font = "15px Arial";
ctx.fillStyle = "green";
ctx.fillText(
element.text,
(element[0].x + element[1].x) / 2 - size / 2,
(element[0].y + element[1].y) / 2
);
});
}
function drawLine(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
function drawCircle(x, y) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2, true);
ctx.fill();
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}