-
Notifications
You must be signed in to change notification settings - Fork 18
/
drawTiles.js
333 lines (250 loc) · 9.87 KB
/
drawTiles.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
// 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 preFactor;
let rotate;
let selectedTile = {};
let recentHover = true;
let recentlySelectedTiles = [];
let adding = true;
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);
//window.addEventListener('mousemove', mouseMoved);
parent.$emit('update:resize-completed');
parent.$emit('update:width', width);
parent.$emit('update:height', height);
p.pixelDensity(2);
p.noLoop();
drawTiles(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();
drawTiles(q, parent.data);
q.save('Tiling Pattern.svg');
}
drawTiles(p, data);
};
function whichSide(xp, yp, x1, y1, x2, y2) {
return Math.sign((yp - y1) * (x2 -x1) - (xp - x1) * (y2 - y1));
}
function tileToString(tile) {
return JSON.stringify({
x: tile.x,
y: tile.y
});
}
p.mouseDragged = function() {
if (p.mouseX > 0 && p.mouseX < p.width && p.mouseY > 0 && p.mouseY < p.height) {
recentHover = true;
let xprime = (p.mouseX - (p.width/2 + pan)) * Math.cos(-rotate) - (p.mouseY - p.height/2) * Math.sin(-rotate);
let yprime = (p.mouseX - (p.width/2 + pan)) * Math.sin(-rotate) + (p.mouseY - p.height/2) * Math.cos(-rotate);
selectedTile = getSelectedTile(xprime, yprime);
drawTiles(p, parent.data);
if (Object.keys(selectedTile).length > 0) {
let tileString = tileToString(selectedTile);
if (!recentlySelectedTiles.includes(tileString)) {
updateSelectedTiles(selectedTile, adding);
recentlySelectedTiles.push(tileString);
}
}
let mouseDistance = p.dist(p.mouseX, p.mouseY, prevX, prevY);
let stepSize = p.max(1, preFactor/10);
if (mouseDistance > stepSize) {
for (let i = 0; i <= mouseDistance; i += stepSize) {
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 + pan)) * Math.cos(-rotate) - (cursorY - p.height/2) * Math.sin(-rotate);
let yprime = (cursorX - (p.width/2 + pan)) * Math.sin(-rotate) + (cursorY - p.height/2) * Math.cos(-rotate);
let intermediateTile = getSelectedTile(xprime, yprime);
if (Object.keys(intermediateTile).length > 0) {
let tileString = tileToString(intermediateTile);
if (!recentlySelectedTiles.includes(tileString)) {
updateSelectedTiles(intermediateTile, adding);
recentlySelectedTiles.push(tileString);
}
}
}
}
prevX = p.mouseX;
prevY = p.mouseY;
}
}
p.mouseMoved = function() {
if (p.mouseX > 0 && p.mouseX < p.width && p.mouseY > 0 && p.mouseY < p.height) {
recentHover = true;
let xprime = (p.mouseX - (p.width/2 + pan)) * Math.cos(-rotate) - (p.mouseY - p.height/2) * Math.sin(-rotate);
let yprime = (p.mouseX - (p.width/2 + pan)) * Math.sin(-rotate) + (p.mouseY - p.height/2) * Math.cos(-rotate);
selectedTile = getSelectedTile(xprime, yprime);
drawTiles(p, parent.data);
if (Object.keys(selectedTile).length > 0) {
p.push();
p.translate(p.width/2 + pan, p.height/2);
p.fill(128, 215, 255);
p.rotate(rotate);
p.beginShape();
for (let pt of selectedTile.dualPts) {
p.vertex(preFactor * pt.x, preFactor * pt.y);
}
p.endShape(p.CLOSE);
p.pop();
}
prevX = p.mouseX;
prevY = p.mouseY;
} else if (recentHover) {
recentHover = false;
drawTiles(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 + pan)) * Math.cos(-rotate) - (p.mouseY - p.height/2) * Math.sin(-rotate);
let yprime = (p.mouseX - (p.width/2 + pan)) * Math.sin(-rotate) + (p.mouseY - p.height/2) * Math.cos(-rotate);
selectedTile = getSelectedTile(xprime, yprime);
if (Object.keys(selectedTile).length > 0) {
let tileString = tileToString(selectedTile);
if (!recentlySelectedTiles.includes(tileString)) {
let index = parent.data.selectedTiles.findIndex(e => e.x == selectedTile.x && e.y == selectedTile.y);
adding = index < 0;
updateSelectedTiles(selectedTile, adding);
recentlySelectedTiles.push(tileString);
}
}
prevX = p.mouseX;
prevY = p.mouseY;
}
};
p.mouseReleased = function() {
recentlySelectedTiles = [];
};
function getSelectedTile(mouseX, mouseY) {
let x = mouseX / preFactor;
let y = mouseY / preFactor;
let inside = false;
let mySelectedTile = {};
let nearbyTiles = Object.values(parent.data.tiles).filter(e => p.dist(x, y, e.mean.x, e.mean.y) < 1);
for (let tile of nearbyTiles) {
if (!inside) {
let vertices = tile.dualPts;
let numVertices = vertices.length;
let a = whichSide(x, y, vertices[0].x, vertices[0].y, vertices[1].x, vertices[1].y);
inside = true;
for (let i = 1; i < numVertices; i++) {
if (a !== whichSide(x, y, vertices[i].x, vertices[i].y, vertices[(i + 1) % numVertices].x, vertices[(i + 1) % numVertices].y)) {
inside = false;
}
}
if (inside) {
mySelectedTile = tile;
}
}
}
return mySelectedTile;
}
function updateSelectedTiles(tile, addMode) {
if (addMode) {
parent.$emit('update:add-tile', tile);
} else {
parent.$emit('update:remove-tile', tile);
}
}
function drawTiles(instance, data) {
let steps = data.steps;
let multiplier = data.multiplier;
let spacing = instance.min(instance.width, instance.height) / (steps);
preFactor = spacing * data.multiplier / Math.PI;
preFactor = preFactor * data.zoom;
let stroke = data.stroke;
rotate = instance.radians(data.rotate);
instance.strokeWeight( Math.min(instance.sqrt(preFactor) / 4.5, 1));
pan = - data.zoom * instance.min(instance.width, instance.height) * data.pan;
instance.push();
instance.background(0, 0, 0.2 * 255);
instance.translate(instance.width / 2 + pan, instance.height / 2);
instance.rotate(rotate);
for (let tile of Object.values(data.tiles)) {
let tileIsSelected = false;
if (data.selectedTiles.length > 0) {
tileIsSelected = data.selectedTiles.filter(e => e.x == tile.x && e.y == tile.y).length > 0;
}
let tileInSelectedLine = false;
let numLinesPassingThroughTile = 0;
if (data.selectedLines.length > 0) {
for (let l of tile.lines) {
if (data.selectedLines.filter(e => e.angle == l.angle && e.index == l.index).length > 0) {
tileInSelectedLine = true;
numLinesPassingThroughTile++;
}
}
}
if (data.colorTiles) {
//let onScreenColors = data.colors.filter(e => e.onScreen && e.symmetry == data.symmetry);
let color = data.colors.filter(e => data.orientationColoring ? e.angles == tile.angles : e.area == tile.area)[0];
instance.fill(color.fill);
if (data.showStroke) {
instance.stroke(stroke, stroke, stroke);
} else {
instance.noStroke();
}
if (tileInSelectedLine) {
instance.fill(0, 255, 0);
if (numLinesPassingThroughTile > 1) {
instance.fill(60, 179, 113);
}
}
if (tileIsSelected) {
instance.fill(128, 215, 255);
}
} else {
instance.stroke(0, 255, 0);
instance.noFill();
if (tileInSelectedLine) {
instance.fill(0, 255, 0, 150);
if (numLinesPassingThroughTile > 1) {
instance.fill(60, 179, 113, 150);
}
}
if (tileIsSelected) {
instance.fill(110, 110, 255);
}
}
instance.beginShape();
for (let pt of tile.dualPts) {
instance.vertex(preFactor * pt.x, preFactor * pt.y);
}
instance.endShape(instance.CLOSE);
}
instance.pop();
}
};
}