-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdrawGrid.js
313 lines (248 loc) · 9.59 KB
/
drawGrid.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
// this p5 sketch is written in instance mode
// read more here: https://github.com/processing/p5.js/wiki/Global-and-instance-mode
function sketch(parent) { // we pass the sketch data from the parent
return function( p ) { // p could be any variable name
// p5 sketch goes here
let canvas;
let grid, spacing, multiplier, rotate;
let recentHover = false;
let recentlySelectedLines = [];
let adding = true;
let selectedAngle;
let prevX = 0;
let prevY = 0;
p.setup = function() {
let target = parent.$el.parentElement;
let width = target.clientWidth;
let height = target.clientHeight;
canvas = p.createCanvas(width, height);
canvas.parent(parent.$el);
parent.$emit('update:resize-completed');
parent.$emit('update:width', width);
parent.$emit('update:height', height);
p.pixelDensity(2);
p.stroke(255,0,0);
p.noLoop();
drawLines(p, parent.data);
};
p.draw = function() {
};
// this is a new function we've added to p5
// it runs only if the data changes
p.dataChanged = function(data, oldData) {
// console.log('data changed');
// console.log('x: ', val.x, 'y: ', val.y);
if (data.display == 'none') {
// measure parent without canvas
let target = parent.$el.parentElement;
let width = target.clientWidth;
let height = target.clientHeight;
// resize canvas
p.resizeCanvas(width, height);
parent.$emit('update:resize-completed');
parent.$emit('update:width', width);
parent.$emit('update:height', height);
}
if (data.download > oldData.download) {
let target = parent.$el.parentElement;
let width = target.clientWidth;
let height = target.clientHeight;
let q = p.createGraphics(width, height, p.SVG);
q.clear();
drawLines(q, parent.data);
q.save('Grid Pattern.svg');
}
drawLines(p, data);
};
p.mouseMoved = function() {
if (p.mouseX > 0 && p.mouseX < p.width && p.mouseY > 0 && p.mouseY < p.height) {
recentHover = true;
drawLines(p, parent.data);
let xprime = (p.mouseX - p.width/2) * Math.cos(-rotate) - (p.mouseY - p.height/2) * Math.sin(-rotate) + p.width/2;
let yprime = (p.mouseX - p.width/2) * Math.sin(-rotate) + (p.mouseY - p.height/2) * Math.cos(-rotate) + p.height/2;
let selectedLine = getNearestLine(xprime, yprime);
if (JSON.stringify(selectedLine) !== JSON.stringify({})) {
p.push();
p.translate(p.width / 2, p.height / 2);
p.stroke(0, 255, 0);
p.rotate(rotate);
drawLine(p, multiplier * selectedLine.angle, spacing * selectedLine.index);
p.pop();
}
prevX = p.mouseX;
prevY = p.mouseY;
} else if (recentHover) {
recentHover = false;
drawLines(p, parent.data);
}
};
p.mousePressed = function() {
if (p.mouseX > 0 && p.mouseX < p.width && p.mouseY > 0 && p.mouseY < p.height) {
let xprime = (p.mouseX - p.width/2) * Math.cos(-rotate) - (p.mouseY - p.height/2) * Math.sin(-rotate) + p.width/2;
let yprime = (p.mouseX - p.width/2) * Math.sin(-rotate) + (p.mouseY - p.height/2) * Math.cos(-rotate) + p.height/2;
let selectedLine = getNearestLine(xprime, yprime);
if (JSON.stringify(selectedLine) !== JSON.stringify({})) {
let index = parent.data.selectedLines.findIndex(e => e.angle == selectedLine.angle && e.index == selectedLine.index);
adding = index < 0;
selectedAngle = selectedLine.angle;
updateSelectedLines(selectedLine, adding);
recentlySelectedLines.push(lineToString(selectedLine));
}
prevX = p.mouseX;
prevY = p.mouseY;
}
};
p.mouseDragged = function() {
if (p.mouseX > 0 && p.mouseX < p.width && p.mouseY > 0 && p.mouseY < p.height) {
let xprime = (p.mouseX - p.width/2) * Math.cos(-rotate) - (p.mouseY - p.height/2) * Math.sin(-rotate) + p.width/2;
let yprime = (p.mouseX - p.width/2) * Math.sin(-rotate) + (p.mouseY - p.height/2) * Math.cos(-rotate) + p.height/2;
let selectedLine = getNearestLine(xprime, yprime);
let lineString = lineToString(selectedLine);
if (lineString !== JSON.stringify({})) {
if (!recentlySelectedLines.includes(lineString)) {
if (adding) {
if (selectedLine.angle == selectedAngle) {
updateSelectedLines(selectedLine, adding);
recentlySelectedLines.push(lineString);
}
} else {
updateSelectedLines(selectedLine, adding);
recentlySelectedLines.push(lineString);
}
}
}
let mouseDistance = p.dist(p.mouseX, p.mouseY, prevX, prevY);
let stepSize = 1;
if (mouseDistance > stepSize) {
for (let i = 0; i <= mouseDistance; i++) {
let cursorX = p.map(i, 0, mouseDistance, p.mouseX, prevX, true);
let cursorY = p.map(i, 0, mouseDistance, p.mouseY, prevY, true);
let xprime = (cursorX - p.width/2) * Math.cos(-rotate) - (cursorY - p.height/2) * Math.sin(-rotate) + p.width/2;
let yprime = (cursorX - p.width/2) * Math.sin(-rotate) + (cursorY - p.height/2) * Math.cos(-rotate) + p.height/2;
let intermediateLine = getNearestLine(xprime, yprime);
if (JSON.stringify(intermediateLine) !== JSON.stringify({})) {
let lineString = lineToString(intermediateLine);
if (!recentlySelectedLines.includes(lineString)) {
if (adding) {
if (intermediateLine.angle == selectedAngle) {
updateSelectedLines(intermediateLine, adding);
recentlySelectedLines.push(lineString);
}
} else {
updateSelectedLines(intermediateLine, adding);
recentlySelectedLines.push(lineString);
}
}
}
}
}
prevX = p.mouseX;
prevY = p.mouseY;
}
};
p.mouseReleased = function() {
recentlySelectedLines = [];
};
// algorithm for identifying closest line
// needs testing, could be optimized
function getNearestLine(mouseX, mouseY) {
let minDist = spacing;
let minLine = {};
for (let line of grid) {
let angle = line.angle;
let index = line.index;
let dist = p.abs(getXVal(multiplier * angle, spacing * index, mouseY - p.height/2) - (mouseX - p.width/2));
if (!isNaN(dist)) {
if (dist < minDist) {
minLine = {
angle: angle,
index: index
};
minDist = dist;
}
}
dist = p.abs(getYVal(multiplier * angle, spacing * index, mouseX - p.width/2) - (mouseY - p.height/2));
if (!isNaN(dist)) {
if (dist < minDist) {
minLine = {
angle: angle,
index: index
};
minDist = dist;
}
}
}
if (minDist < 10 && minDist < spacing) {
return minLine;
} else {
return {};
}
}
function updateSelectedLines(line, addMode) {
if (addMode) {
parent.$emit('update:add-line', line);
} else {
parent.$emit('update:remove-line', line);
}
}
function lineToString(line) {
return JSON.stringify(line);
}
function drawLines(instance, data) {
grid = data.grid;
spacing = data.spacing;
multiplier = data.multiplier;
rotate = instance.radians(data.rotate);
instance.push();
instance.background(0, 0, 0.2 * 255);
instance.strokeWeight(1);
instance.translate(instance.width / 2, instance.height / 2);
instance.rotate(rotate);
let selectedLines = parent.data.selectedLines;
for (let line of grid) {
if (selectedLines.filter(e => e.angle == line.angle && e.index == line.index).length > 0) {
instance.stroke(0, 255, 0);
} else {
instance.stroke(255, 0, 0);
}
drawLine(instance, multiplier * line.angle, spacing * line.index);
}
if (data.showIntersections) {
instance.noStroke();
instance.fill(255);
for (let pt of Object.values(data.intersectionPoints)) {
instance.ellipse(pt.x * spacing, pt.y * spacing, 4);
}
}
// intersections corresponding to selected tiles
instance.strokeWeight(2);
instance.stroke(0, 191, 255);
instance.noFill();
for (let tile of data.selectedTiles) {
instance.ellipse(tile.x * spacing, tile.y * spacing, 10);
}
instance.pop();
}
// angle, index
function drawLine(instance, angle, index) {
let x0 = getXVal(angle, index, -instance.height);
let x1 = getXVal(angle, index, instance.height);
//console.log(x0);
if (!isNaN(x0) && !isNaN(x1) && Math.abs(x0) < 1000000 && Math.abs(x1) < 1000000) {
instance.line(x0, -instance.height, x1, instance.height);
} else {
let y0 = getYVal(angle, index, -instance.width);
let y1 = getYVal(angle, index, instance.width);
instance.line(-instance.width, y0, instance.width, y1);
}
}
// angle, index
function getXVal(angle, index, y) {
return (index - y * p.sin(angle))/p.cos(angle);
}
// angle, index
function getYVal(angle, index, x) {
return (index - x * p.cos(angle))/p.sin(angle);
}
};
}