-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpictureTool.js
More file actions
455 lines (392 loc) · 10.6 KB
/
pictureTool.js
File metadata and controls
455 lines (392 loc) · 10.6 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
function pictureTool() {
this.icon = "assets/pictureTool.png";
this.name = "pictureTool";
var deltaX = -1;
var deltaY = -1;
var startMouseX = -1;
var startMouseY = -1;
var shapeWidth = 1;
var shapeHeight = 1;
var originX;
var originY;
var selectedImage = null;
var drawing = false;
var editMode = false;
var moving = false;
var cornerCircles = [];
var sideRectangles = [];
this.draw = function (pg = currentLayer, tpg = layers[layers.length - 1]) {
//display the last save state of pixels
pg.updatePixels();
tpg.updatePixels();
if (selectedImage == null) return;
//mouseIsPressed is a variable that p5 provides for us which tells us in boolean if the mouse is pressed or not
if (mouseIsPressed) {
//if the previous values are -1 set them to the current mouse location and set drawing to true and load the pixels
if (drawing == false) {
startMouseX = mouseX;
startMouseY = mouseY;
drawing = true;
} else if (editMode == false) {
//draw a line from the start position to the current mouse position
shapeWidth = mouseX - startMouseX;
shapeHeight = mouseY - startMouseY;
} else {
calculationsForEditMode();
}
}
//if the mouse isn't pressed reset the previous values to -1 and set drawing to false
else if (drawing == true && editMode == false) {
editMode = true;
moving = false;
calculateValues();
select("#finishButton").elt.style.display = "inline";
} else if (drawing == true) {
moving = false;
}
pg.image(
selectedImage,
startMouseX,
startMouseY,
shapeWidth,
shapeHeight
);
drawVisualizers(tpg);
};
//used to move anchor/picture and resize the picture
var calculationsForEditMode = function () {
//dealing with rectangles
//top rectangle
if (
mouseX > sideRectangles[0].x &&
mouseX < sideRectangles[0].x + sideRectangles[0].width &&
mouseY > sideRectangles[0].y &&
mouseY < sideRectangles[0].y + sideRectangles[0].height
) {
deltaY = mouseY - startMouseY;
startMouseY = mouseY;
shapeHeight -= deltaY;
}
//bottom rectangle
else if (
mouseX > sideRectangles[1].x &&
mouseX < sideRectangles[1].x + sideRectangles[1].width &&
mouseY > sideRectangles[1].y &&
mouseY < sideRectangles[1].y + sideRectangles[1].height
) {
deltaY = mouseY - startMouseY;
shapeHeight = deltaY;
}
//left rectangle
else if (
mouseX > sideRectangles[2].x &&
mouseX < sideRectangles[2].x + sideRectangles[2].width &&
mouseY > sideRectangles[2].y &&
mouseY < sideRectangles[2].y + sideRectangles[2].height
) {
deltaX = mouseX - startMouseX;
startMouseX = mouseX;
shapeWidth -= deltaX;
}
//right rectangle
else if (
mouseX > sideRectangles[3].x &&
mouseX < sideRectangles[3].x + sideRectangles[3].width &&
mouseY > sideRectangles[3].y &&
mouseY < sideRectangles[3].y + sideRectangles[3].height
) {
deltaX = mouseX - startMouseX;
shapeWidth = deltaX;
}
//deal with circles
//deal with top left circle
else if (
dist(mouseX, mouseY, cornerCircles[0].x, cornerCircles[0].y) <= 20
) {
deltaX = mouseX - startMouseX;
startMouseX = mouseX;
shapeWidth -= deltaX;
deltaY = mouseY - startMouseY;
startMouseY = mouseY;
shapeHeight -= deltaY;
}
//deal with top right circle
else if (
dist(mouseX, mouseY, cornerCircles[1].x, cornerCircles[1].y) <= 20
) {
deltaX = mouseX - startMouseX;
shapeWidth = deltaX;
deltaY = mouseY - startMouseY;
startMouseY = mouseY;
shapeHeight -= deltaY;
}
//deal with bottom left circle
else if (
dist(mouseX, mouseY, cornerCircles[2].x, cornerCircles[2].y) <= 20
) {
deltaX = mouseX - startMouseX;
startMouseX = mouseX;
shapeWidth -= deltaX;
deltaY = mouseY - startMouseY;
shapeHeight = deltaY;
}
//deal with bottom right circle
else if (
dist(mouseX, mouseY, cornerCircles[3].x, cornerCircles[3].y) <= 20
) {
deltaX = mouseX - startMouseX;
shapeWidth = deltaX;
deltaY = mouseY - startMouseY;
shapeHeight = deltaY;
}
//dealing with moving of shape by dragging mouse
else if (
((mouseX > startMouseX && mouseX < startMouseX + shapeWidth) ||
(mouseX < startMouseX && mouseX > startMouseX + shapeWidth)) &&
((mouseY > startMouseY && mouseY < startMouseY + shapeHeight) ||
(mouseY < startMouseY && mouseY > startMouseY + shapeHeight))
) {
if (moving == false) {
originX = mouseX;
originY = mouseY;
deltaX = originX - startMouseX;
deltaY = originY - startMouseY;
moving = true;
} else {
startMouseX = mouseX - deltaX;
startMouseY = mouseY - deltaY;
}
}
calculateValues();
};
//calculates location for anchors after changing values
var calculateValues = function () {
//null the values
cornerCircles = [];
sideRectangles = [];
//push corner circle values
//top left circle
cornerCircles.push({ x: startMouseX, y: startMouseY });
//top right circle
cornerCircles.push({
x: startMouseX + shapeWidth,
y: startMouseY,
});
//bottom left circle
cornerCircles.push({
x: startMouseX,
y: startMouseY + shapeHeight,
});
//bottom right circle
cornerCircles.push({
x: startMouseX + shapeWidth,
y: startMouseY + shapeHeight,
});
//push rectangle values
var verticalMidPoint = (2 * startMouseX + shapeWidth) / 2;
var horizontalMidPoint = (2 * startMouseY + shapeHeight) / 2;
var tempWidth = shapeWidth / 5;
var tempHeight = shapeHeight / 5;
//top rectangle
sideRectangles.push({
x: verticalMidPoint - tempWidth,
y: startMouseY - 13.75,
width: tempWidth * 2,
height: 20,
});
//bottom rectangle
sideRectangles.push({
x: verticalMidPoint - tempWidth,
y: startMouseY + shapeHeight - 6.25,
width: tempWidth * 2,
height: 20,
});
//left rectangle
sideRectangles.push({
x: startMouseX - 13.75,
y: horizontalMidPoint - tempHeight,
width: 20,
height: tempHeight * 2,
});
//right rectangle
sideRectangles.push({
x: startMouseX + shapeWidth - 6.25,
y: horizontalMidPoint - tempHeight,
width: 20,
height: tempHeight * 2,
});
};
//draws the visualizers at top most layer
var drawVisualizers = function (tpg) {
tpg.push();
if (editMode) {
tpg.fill(255, 0, 0);
tpg.noStroke();
//corner circles
//top left corner
tpg.ellipse(cornerCircles[0].x, cornerCircles[0].y, 20, 20);
//top right corner
tpg.ellipse(cornerCircles[1].x, cornerCircles[1].y, 20, 20);
//bottom left corner
tpg.ellipse(cornerCircles[2].x, cornerCircles[2].y, 20, 20);
//bottom right corner
tpg.ellipse(cornerCircles[3].x, cornerCircles[3].y, 20, 20);
//side rectangle
//top rectangle
tpg.rect(
sideRectangles[0].x,
sideRectangles[0].y,
sideRectangles[0].width,
sideRectangles[0].height
);
//bottom rectangle
tpg.rect(
sideRectangles[1].x,
sideRectangles[1].y,
sideRectangles[1].width,
sideRectangles[1].height
);
//left rectangle
tpg.rect(
sideRectangles[2].x,
sideRectangles[2].y,
sideRectangles[2].width,
sideRectangles[2].height
);
//right rectangle
tpg.rect(
sideRectangles[3].x,
sideRectangles[3].y,
sideRectangles[3].width,
sideRectangles[3].height
);
}
tpg.pop();
};
//used to hide or show colour swatches
var colourSwatches = document.getElementsByClassName("colourSwatches");
//used to hide or show brush slider
var brushSize = document.getElementById("brush-size");
var brushSizeLabel = document.getElementById("brush-size-label");
//unselects tool, sets values to null, shows size slider and colour pallete
this.unselectTool = function () {
//return colour swatches to original style
for (let i = 0; i < colourSwatches.length; i++) {
colourSwatches[i].style.display = "inline";
}
//show the colour picker
document.getElementById("colour-picker").style = "display: colourP;";
//show brush size and its label
brushSize.style.display = "inline";
brushSizeLabel.style.display = "inline";
//clear options
select(".options").html("");
select("#clearButton").elt.removeEventListener(
"click",
pictureToolClear
);
editMode = false;
drawing = false;
moving = false;
self.draw();
currentLayer.loadPixels();
layers[layers.length - 1].loadPixels();
selectedImage = null;
cornerCircles = [];
sideRectangles = [];
deltaX = -1;
deltaY = -1;
startMouseX = -1;
startMouseY = -1;
shapeWidth = 1;
shapeHeight = 1;
};
//populates options while hiding brush and colour pallete
var self = this;
this.populateOptions = function () {
//hide the colour swatch
for (let i = 0; i < colourSwatches.length; i++) {
colourSwatches[i].style.display = "none";
}
//hide colour picker
document.getElementById("colour-picker").style = "display: none;";
//show brush size and its label
brushSize.style.display = "none";
brushSizeLabel.style.display = "none";
select(".options").html(
"<button id='finishButton''>finish shape</button>"
);
select("#finishButton").mouseClicked(function () {
editMode = false;
drawing = false;
moving = false;
this.elt.style.display = "none";
self.draw();
currentLayer.loadPixels();
layers[layers.length - 1].loadPixels();
cornerCircles = [];
sideRectangles = [];
deltaX = -1;
deltaY = -1;
startMouseX = -1;
startMouseY = -1;
shapeWidth = 1;
shapeHeight = 1;
});
select("#finishButton").elt.style.display = "none";
//input file and use it as a stamp
var input = createFileInput(handleFile);
select(".options").child(input);
select("#clearButton").elt.addEventListener("click", pictureToolClear);
};
//sets values to default
this.refresh = function () {
alert("choose image");
editMode = false;
drawing = false;
moving = false;
selectedImage = null;
cornerCircles = [];
sideRectangles = [];
deltaX = -1;
deltaY = -1;
startMouseX = -1;
startMouseY = -1;
shapeWidth = 1;
shapeHeight = 1;
currentLayer.loadPixels();
};
//adds functionality for clear function
var pictureToolClear = function () {
select("#finishButton").elt.style.display = "none";
editMode = false;
drawing = false;
moving = false;
cornerCircles = [];
sideRectangles = [];
deltaX = -1;
deltaY = -1;
startMouseX = -1;
startMouseY = -1;
shapeWidth = 1;
shapeHeight = 1;
self.draw();
layers[currentLayerIndex].loadPixels();
};
//handles file and only allows pmg, jpg, jpeg image format
var handleFile = function (file) {
if (
!(
file.subtype === "png" ||
file.subtype === "jpg" ||
file.subtype === "jpeg"
)
) {
alert(
"file is not recognised image file, please select either png, jpg or jpeg format"
);
return;
}
selectedImage = loadImage(file.data);
};
}