-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
165 lines (138 loc) · 4.55 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
// SVG Containing Dots
const board = document.getElementById("board");
const namespace = "http://www.w3.org/2000/svg";
const shadows = {
circle: board.firstElementChild,
line: board.lastElementChild,
};
const Dots = [];
const Lines = [];
let clickingDot = false;
let prevLine = false;
function dotOnPos(x, y) {
for (let dot of Dots) {
const xRange = Math.abs(dot.cx.baseVal.value - x);
const yRange = Math.abs(dot.cy.baseVal.value - y);
if (xRange < 15 && yRange < 15) {
return dot;
}
}
}
function reamoveUnnessecary({x1, y1, x2, y2}) {
Lines.forEach((line, i) => {
const opposite = (
line.x1.baseVal.value === x2.baseVal.value &&
line.y1.baseVal.value === y2.baseVal.value &&
line.x2.baseVal.value === x1.baseVal.value &&
line.y2.baseVal.value === y1.baseVal.value
);
const same = (
line.x1.baseVal.value === x1.baseVal.value &&
line.y1.baseVal.value === y1.baseVal.value &&
line.x2.baseVal.value === x2.baseVal.value &&
line.y2.baseVal.value === y2.baseVal.value
);
const zeroLine = (
line.x1.baseVal.value === line.x2.baseVal.value &&
line.y1.baseVal.value === line.y2.baseVal.value
);
if ((i !== Lines.length - 1) && (same || opposite) || zeroLine) {
Lines[i].remove();
Lines[i] = null;
}
})
for (i in Lines) {
if (!Lines[i]) {
Lines.splice(i,1);
}
}
}
function makeDot(e) {
if (e.button) return;
let isLastDot = false, dotHovered = false;
let x = e.clientX;
let y = e.clientY;
if (prevLine) {
if (e.shiftKey) {
const distance = {
h: Math.abs(prevLine.x1.baseVal.value - x),
v: Math.abs(prevLine.y1.baseVal.value - y)
}
if (distance.h < distance.v) {
x = prevLine.x1.baseVal.value;
}
else {
y = prevLine.y1.baseVal.value;
}
} else {
dotHovered = dotOnPos(x, y);
isLastDot = Dots[Dots.length - 1] === dotHovered;
if (dotHovered) {
x = dotHovered.cx.baseVal.value;
y = dotHovered.cy.baseVal.value;
}
}
}
if (!dotHovered) {
const newDot = document.createElementNS(namespace, "circle");
newDot.setAttribute("cx", x);
newDot.setAttribute("cy", y);
board.appendChild(newDot);
Dots.push(newDot);
}
const newLine = document.createElementNS(namespace, "line");
newLine.setAttribute("x1", x);
newLine.setAttribute("y1", y);
newLine.setAttribute("x2", x);
newLine.setAttribute("y2", y);
if (prevLine) {
if (isLastDot || e.type === "mouseup") {
prevLine.setAttribute("x2", x);
prevLine.setAttribute("y2", y);
reamoveUnnessecary(prevLine);
} else {
Lines.splice(-1, 1);
prevLine.remove();
prevLine = false;
}
}
prevLine = newLine;
board.appendChild(newLine);
Lines.push(newLine);
//======================================//
shadows.circle.toggleAttribute("show");
shadows.line.toggleAttribute("show");
clickingDot = (clickingDot)? false : true;
}
board.addEventListener("mousedown", makeDot);
board.addEventListener("mouseup", makeDot);
board.addEventListener("mousemove", function(e) {
if (!clickingDot || !prevLine) return;
let x = e.clientX;
let y = e.clientY;
if (e.shiftKey) {
const distance = {
h: Math.abs(prevLine.x1.baseVal.value - x),
v: Math.abs(prevLine.y1.baseVal.value - y)
}
if (distance.h < distance.v) {
x = prevLine.x1.baseVal.value;
}
else {
y = prevLine.y1.baseVal.value;
}
} else {
const dotHovered = dotOnPos(x, y);
if (dotHovered) {
x = dotHovered.cx.baseVal.value;
y = dotHovered.cy.baseVal.value;
}
}
shadows.circle.setAttribute("cx", x);
shadows.circle.setAttribute("cy", y);
const line = shadows.line;
line.setAttribute("x1", prevLine.x1.baseVal.value);
line.setAttribute("y1", prevLine.y1.baseVal.value);
line.setAttribute("x2", x);
line.setAttribute("y2", y);
});