-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
281 lines (250 loc) · 8.22 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
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
// initialize global variables and assign default values
const pointRadius = 6;
let gridDensity = 10;
let borderCoordinates = {
x1: 100,
y1: 100,
x2: 300,
y2: 300,
};
// define 4 border points from borderCoordinates
let controlPoint0 = [borderCoordinates.x1, borderCoordinates.y1];
let controlPoint1 = [borderCoordinates.x2, borderCoordinates.y1];
let controlPoint2 = [borderCoordinates.x2, borderCoordinates.y2];
let controlPoint3 = [borderCoordinates.x1, borderCoordinates.y2];
let controlPoints = [
controlPoint0,
controlPoint1,
controlPoint2,
controlPoint3,
];
let currentCoordinates = {};
let pointCoordinates = {};
let activePointId = "";
let circle0;
let circle1;
let circle2;
let circle3;
let circles = [];
// create svg content
const areaDefinitionSvg = d3.select("#areaDefinitionSvg");
const area = areaDefinitionSvg.node();
const svgRect = area.getBoundingClientRect();
let g = areaDefinitionSvg.append("g");
let pattern = g.append("pattern");
let gridPath = pattern.append("path");
let borderPath = g.append("path");
// define function initialGrid to draw initial svg on clicking the button
function initialGrid() {
// disable button
document.getElementById("defineArea").disabled = true;
// assign svg attributes
pattern
.attr("id", "grid")
.attr("width", gridDensity)
.attr("height", gridDensity)
.attr("x", controlPoint0[0])
.attr("y", controlPoint0[1])
.attr("patternUnits", "userSpaceOnUse");
gridPath
.attr("d", `M ${gridDensity} 0 L 0 0 0 ${gridDensity}`)
.attr("class", "gridInnerLines");
borderPath
.attr(
"d",
`M ${controlPoint0} L ${controlPoint1} L ${controlPoint2} L ${controlPoint3} Z`
)
.attr("id", "gridBorder");
controlPoints.forEach(function (controlPoint, index) {
g.append("circle")
.attr("class", "controlCircle")
.attr("id", `circle${index}`)
.attr("cx", controlPoint[0])
.attr("cy", controlPoint[1])
.attr("r", pointRadius);
});
// assign control circles
circle0 = d3.select("#circle0").node();
circle1 = d3.select("#circle1").node();
circle2 = d3.select("#circle2").node();
circle3 = d3.select("#circle3").node();
circles = [circle0, circle1, circle2, circle3];
// create event listeners
createEventListeners();
}
// define function to create event listeners when svg grid was created
function createEventListeners() {
// add wheel event listener
document.addEventListener("wheel", changeGridDensity);
// add mousedown listener for whole grid to move
let grid = document.getElementById("gridBorder");
grid.addEventListener("mousedown", function (event) {
grid.classList.add("grabbing");
let mouseX = screenToSvgCoords(event).X;
let mouseY = screenToSvgCoords(event).Y;
pointCoordinates = {
X: mouseX,
Y: mouseY,
};
document.removeEventListener("wheel", changeGridDensity);
document.addEventListener("mousemove", moveGrid);
document.addEventListener("mouseup", mouseup);
});
// add event listener mousedown to circles
circles.forEach(function (circle) {
circle.addEventListener("mousedown", function (event) {
document.removeEventListener("wheel", changeGridDensity);
// make point coordinates equal to center of targeted event
let pointX = event.target.getAttribute("cx");
let pointY = event.target.getAttribute("cy");
pointCoordinates = {
X: pointX,
Y: pointY,
};
activePointId = event.target.id;
document.addEventListener("mousemove", mousemove);
document.addEventListener("mouseup", mouseup);
});
});
}
// define mousemove function
function mousemove(event) {
setCurrentCoordinates(event);
// if point coordinates and current coordinates distance is bigger then half of the grid cell -> update point coordinates
let diffX = Math.abs(pointCoordinates.X - currentCoordinates.X);
let diffY = Math.abs(pointCoordinates.Y - currentCoordinates.Y);
if (diffX >= gridDensity / 2 || diffY >= gridDensity / 2) {
pointCoordinates = {
X:
Math.round(
(currentCoordinates.X - borderCoordinates.x1) / gridDensity
) *
gridDensity +
borderCoordinates.x1,
Y:
Math.round(
(currentCoordinates.Y - borderCoordinates.y1) / gridDensity
) *
gridDensity +
borderCoordinates.y1,
};
updateGrid(pointCoordinates.X, pointCoordinates.Y);
}
}
// define mouseup function
function mouseup() {
currentCoordinates = {};
pointCoordinates = {};
activePointId = "";
document.removeEventListener("mousemove", mousemove);
document.removeEventListener("mouseup", mouseup);
document.removeEventListener("mousemove", moveGrid);
document.addEventListener("wheel", changeGridDensity);
document.getElementById("gridBorder").classList.remove("grabbing");
}
// define screenToSvgCoords function to translate coordinates from DOM to svg
function screenToSvgCoords(event) {
let x = event.clientX - svgRect.x;
let y = event.clientY - svgRect.y;
return {
X: x,
Y: y,
};
}
// define updateGrid function -> update variable values and redraw grid
function updateGrid(x, y) {
// update border coordinates
if (activePointId === "circle0") {
borderCoordinates.x1 = x;
borderCoordinates.y1 = y;
} else if (activePointId === "circle1") {
borderCoordinates.x2 = x;
borderCoordinates.y1 = y;
} else if (activePointId === "circle2") {
borderCoordinates.x2 = x;
borderCoordinates.y2 = y;
} else if (activePointId === "circle3") {
borderCoordinates.x1 = x;
borderCoordinates.y2 = y;
} else {
console.log("Error: point not selected");
}
updateControlPoints();
redrawGrid();
}
// define change Grid density function listening to wheel event
function changeGridDensity(event) {
// increase or decrease grid density by 5 px, set bottom and top limits
if (event.deltaY < 0 && gridDensity > 5) {
gridDensity -= 5;
} else if (event.deltaY > 0 && gridDensity < 45) {
gridDensity += 5;
}
// redraw grid
pattern
.attr("width", gridDensity)
.attr("height", gridDensity)
.attr("x", borderCoordinates.x1)
.attr("y", borderCoordinates.y1);
gridPath.attr("d", `M ${gridDensity} 0 L 0 0 0 ${gridDensity}`);
// adapt point2 coordinates to fit the grid
activePointId = "circle2";
let rx = (borderCoordinates.x2 - borderCoordinates.x1) % gridDensity;
let ry = (borderCoordinates.y2 - borderCoordinates.y1) % gridDensity;
if (rx < gridDensity / 2) {
pointCoordinates.X = borderCoordinates.x2 - rx;
} else {
pointCoordinates.X = borderCoordinates.x2 - rx + gridDensity;
}
if (ry < gridDensity / 2) {
pointCoordinates.Y = borderCoordinates.y2 - ry;
} else {
pointCoordinates.Y = borderCoordinates.y2 - ry + gridDensity;
}
updateGrid(pointCoordinates.X, pointCoordinates.Y);
pointCoordinates = {};
activePointId = "";
}
function moveGrid(event) {
setCurrentCoordinates(event);
let diffX = pointCoordinates.X - currentCoordinates.X;
let diffY = pointCoordinates.Y - currentCoordinates.Y;
// update variables
borderCoordinates.x1 -= diffX;
borderCoordinates.x2 -= diffX;
borderCoordinates.y1 -= diffY;
borderCoordinates.y2 -= diffY;
updateControlPoints();
redrawGrid();
pointCoordinates = currentCoordinates;
document.addEventListener("mouseup", mouseup);
}
function updateControlPoints() {
controlPoint0 = [borderCoordinates.x1, borderCoordinates.y1];
controlPoint1 = [borderCoordinates.x2, borderCoordinates.y1];
controlPoint2 = [borderCoordinates.x2, borderCoordinates.y2];
controlPoint3 = [borderCoordinates.x1, borderCoordinates.y2];
controlPoints = [controlPoint0, controlPoint1, controlPoint2, controlPoint3];
}
function redrawGrid() {
// redraw border
borderPath.attr(
"d",
`M ${controlPoint0} L ${controlPoint1} L ${controlPoint2} L ${controlPoint3} Z`
);
//set grid origin
pattern.attr("x", controlPoint0[0]).attr("y", controlPoint0[1]);
// redraw points
circles.forEach(function (circle, index) {
circle.setAttribute("cx", controlPoints[index][0]);
circle.setAttribute("cy", controlPoints[index][1]);
});
}
function setCurrentCoordinates(event) {
let mouseX = screenToSvgCoords(event).X;
let mouseY = screenToSvgCoords(event).Y;
currentCoordinates = {
X: mouseX,
Y: mouseY,
};
}