-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
368 lines (319 loc) · 8.98 KB
/
sketch.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
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
// INSTRUCTIONS
/* -- "PROPY": Simple image editor --
>> 1. Start by pressing the play button on top left, and press the “Click to start editing!” button, in the "Preview" side on the right. That will go to the upload page.
>> 2. For this page, you need to upload through the editor. Click the side arrow next to “sketch.js” on the left. From there, click the dropdown of “Sketch Files”, upload your picture by pressing “Upload file” from the dropdown.
>> 3. Once uploaded, change the default picture name “gg.png” on line 24 in the preload() function. Then press the play button agian, without doing the upload process agian, move to the next step.
>> 4. Then press the green button on the screen to continue to the editing screen.
>> 5. In the editing page, press the blue buttons to edit, and press the “Done!” button once completed.
>> 6. That will give the saving screen with the edited image with its original size. Press the key "s" or "S" to download image.
>> NOTE: Pressing the "reflect" and "Invert(-)" will do and undo
BUT the other buttons, will ONLY increase the application of the filters. To reset the filters (change the image back to its orginal filter), press the key "r" or "R".
>> NOTE: You can press the key "D" or "d" to go to the Saving screen
*/
let img;
function preload() {
// Upload Picture
img = loadImage("gg.png"); // ===> CHANGE THIS "gg.png" ONLY
// to "name.type" of your uploaded picture
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
let currentscreen = 0;
let orgimgH;
let orgimgW;
let gryscle = false;
let invert = false;
let cntrst = false;
let tinted = false;
let reflct = false;
function setup() {
createCanvas(500, 486);
pixelDensity(1);
noStroke();
textAlign(CENTER, CENTER);
// Save orginal image size
orgimgH = img.height;
orgimgW = img.width;
}
function draw() {
createCanvas(500, 486);
background("pink");
// Load pixels and resize to fit the screen
img.loadPixels();
img.resize(width - 100, height - 130);
// Define the screens
if (currentscreen === 0) {
main_screen();
} else if (currentscreen === 1) {
upload_screen();
} else if (currentscreen === 2) {
editing_screen();
} else if (currentscreen === 3) {
end_screen();
} else {
currentscreen = 0;
}
}
function mousePressed() {
if (currentscreen === 0) {
// Main screen
if (
mouseY >= height - 175 &&
mouseY <= height - 175 + 50 &&
mouseX >= width / 2 - 100 &&
mouseX <= width / 2 - 100 + 200
) {
currentscreen = 1; // Go to Upload screen
}
}
if (currentscreen === 1) {
// Upload screen
if (
mouseY >= height / 2 - 30 &&
mouseY <= height / 2 - 30 + 50 &&
mouseX >= 150 &&
mouseX <= 150 + 200
) {
currentscreen = 2; // Go to Editing screen
}
}
if (currentscreen === 2) {
// Editing screen
let hs = 22;
let ws = 80;
let ys = height - 75;
if (mouseY >= ys && mouseY <= ys + hs && mouseX >= 63 && mouseX < 63 + ws) {
// GreyScale
gryscle = !gryscle;
}
if (
mouseY >= ys + 40 &&
mouseY <= ys + 40 + hs &&
mouseX >= 213 &&
mouseX <= 213 + ws
) {
// Invert(-)
invert = !invert;
}
if (
mouseY >= ys + 40 &&
mouseY <= ys + 40 + hs &&
mouseX >= 63 &&
mouseX <= 63 + ws
) {
// Contrast
cntrst = !cntrst;
}
if (
mouseY >= ys &&
mouseY <= ys + hs &&
mouseX >= 213 &&
mouseX <= 213 + ws
) {
// Tint
tinted = !tinted;
}
if (
mouseY >= ys &&
mouseY < ys + hs &&
mouseX >= 363 &&
mouseX <= 363 + ws
) {
// Reflect
reflct = !reflct;
}
if (
mouseY >= ys + 40 &&
mouseY <= ys + 40 + hs &&
mouseX >= 363 &&
mouseX <= 363 + ws
) {
// Save button
saveCanvas("ProPy_image", "png");
currentscreen = 3; // Go to Save screen
}
}
}
function keyPressed() {
if (key === "d" || key === "D") {
currentscreen = 3; // Go to Save screen
}
if (currentscreen === 3) {
// Save/download the edited image on the end_screen
if (key === "s" || key === "S") {
saveCanvas("ProPy_image", "png");
}
if (key === "R" || key === "r") {
// reset filters ONLY
preload();
}
}
}
function main_screen() {
// Welcome sign
fill("white");
rect(90, 50, 310, 200, 10);
fill(0);
textSize(40);
text("Welcome to", width / 2 - 20, height / 2 - 135);
textSize(55);
text("ProPy", width / 2, height / 2 - 50);
// Start button
fill("blue");
rect(width / 2 - 100, height - 175, 200, 50, 18);
fill("white");
textSize(18);
text("Click to start editing!", width / 2, height - 150);
}
function upload_screen() {
// Image holderplace
fill("white");
rect(50, 50, width - 100, height - 100, 20);
// Upload button => *must upload through the editor
fill("green");
rect(150, height / 2 - 30, 200, 50, 15);
textSize(18);
fill("white");
text("Upload image!", width / 2, height / 2 - 5);
// Note:
textSize(13);
fill(0);
text(
"***Must follow the instructions written\n in description before pressing!\n *Uploads using this editor",
width / 2,
height / 2 + 55
);
}
const rgb = 255;
function editing_screen() {
// Image placeholder
fill("white");
rect(30, 10, width - 60, height - 100, 20);
// Tools placeholder
fill(0);
rect(30, height - 84, width - 55, 80, 20);
// Tools buttons
let c = 0;
textSize(15);
for (let i = 63; i < width - 55 + 30; i += 150) {
for (let j = height - 75; j < height - 30; j += 40) {
// Draw the buttons
fill("blue");
rect(i, j, 80, 22, 5);
// Label tools buttons
fill("white");
if (c === 0) {
text("GreyScale", i + 40, j + 12);
} else if (c === 1) {
text("Contrast", i + 40, j + 12);
} else if (c === 2) {
text("Tint", i + 40, j + 12);
} else if (c === 3) {
text("Invert(-)", i + 40, j + 12);
} else if (c === 4) {
text("Reflect", i + 40, j + 12);
} else {
text("Done!", i + 40, j + 12);
}
c++;
}
}
// Edit image on-click
for (let x = 0; x < img.width; x++) {
for (let y = 0; y < img.height; y++) {
let idx = (x + y * img.width) * 4;
// (r, g, b) vector
let r = img.pixels[idx];
let g = img.pixels[idx + 1];
let b = img.pixels[idx + 2];
// Preform operations based on their predicates
// GreyScale
if (gryscle) {
greyscale(r, g, b, idx);
}
// Contrast
if (cntrst) {
contrast(r, g, b, idx);
}
// Tint
if (tinted) {
tinting(r, g, b, idx);
}
// Negative Invert
if (invert) {
negativeInvert(r, g, b, idx);
}
}
}
if (invert) {
// Can set the image back/ undo invert
invert = false;
}
if (tinted) {
tinted = false;
}
if (gryscle) {
gryscle = false;
}
if (cntrst) {
cntrst = false;
}
// UPdate pixels and draw the image
img.updatePixels();
image(img, 50, 25);
// Reflect image
if (reflct) {
push();
scale(-1, 1);
image(img, -50 * 10 + 50, 25);
pop();
}
}
function greyscale(r, g, b, idx) {
// Multiplies the (r, g, b) vector by a matrix with all entries (1/3); takes the average of the vector/pixel
let grs = (r + g + b) / 3;
img.pixels[idx] = constrain(grs, grs - 1, grs + 1);
img.pixels[idx + 1] = constrain(grs, grs - 1, grs + 1);
img.pixels[idx + 2] = constrain(grs, grs - 1, grs + 1);
}
function contrast(r, g, b, idx) {
// Multiplies scalar f by the subtraction of vector (r, g, b) by the vector (255/2, 255/2, 255/2) and the product is added to the (255/2, 255/2, 255/2) vector
const contrasted = 100;
let f = (259 * (contrasted + rgb)) / (rgb * (259 - contrasted));
let w = rgb / 2;
img.pixels[idx] = constrain(
f * (r - w) + w,
f * (r - w) + w + 1,
f * (r - w) + w - 1
);
img.pixels[idx + 1] = constrain(
f * (g - w) + w,
f * (g - w) + w + 1,
f * (g - w) + w - 1
);
img.pixels[idx + 2] = constrain(
f * (b - w) + w,
f * (b - w) + w + 1,
f * (b - w) + w - 1
);
}
function tinting(r, g, b, idx) {
// Adds the (r, g, b) vector to the vector (255/2, 0, 255/2)
img.pixels[idx] = constrain(rgb / 2 + r, rgb / 2 + r - 1, rgb / 2 + r + 1);
img.pixels[idx + 1] = constrain(g, g - 1, g + 1);
img.pixels[idx + 2] = constrain(rgb / 2 + b, rgb / 2 + b, rgb / 2 + b);
}
function negativeInvert(r, g, b, idx) {
// Subtracts the (r, g, b) vector from vector (255, 255, 255)
img.pixels[idx] = rgb - r;
img.pixels[idx + 1] = rgb - g;
img.pixels[idx + 2] = rgb - b;
}
function end_screen() {
// Display image only with its orginal size
clear();
createCanvas(orgimgW, orgimgH);
img.resize(orgimgW, orgimgH);
image(img, 0, 0);
}