-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
331 lines (302 loc) · 7.67 KB
/
Copy pathscript.js
File metadata and controls
331 lines (302 loc) · 7.67 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
const choose = document.querySelector(".choose");
const reset = document.querySelector(".reset");
const download = document.querySelector(".download");
const original = document.querySelector(".original");
const vintage = document.querySelector(".vintage");
const cold = document.querySelector(".cold");
const warm = document.querySelector(".warm");
const dramatic = document.querySelector(".dramatic");
const darken = document.querySelector(".darken");
const brightness = document.querySelector("#brightness");
const contrast = document.querySelector("#contrast");
const exposure = document.querySelector("#exposure");
const saturation = document.querySelector("#saturation");
const hue = document.querySelector("#hue-rotate");
const blurr = document.querySelector("#blur");
const grayscale = document.querySelector("#grayscale");
const sepia = document.querySelector("#sepia");
const opacity = document.querySelector("#opacity");
const invert = document.querySelector("#invert");
let image = null;
let imageSrc = null;
let brightnessValue = 100;
let contrastValue = 100;
let saturationValue = 100;
let hueValue = 0;
let blurrValue = 0;
let grayscaleValue = 0;
let sepiaValue = 0;
let opacityValue = 100;
let invertValue = 0;
choose.addEventListener("change", (elem) => {
if (!elem.target.files[0].type.startsWith("image/")) {
alert("Please select an image file.");
return;
} else {
imageSrc = elem.target.files[0];
image = document.createElement("img");
image.src = URL.createObjectURL(imageSrc);
image.style.display = "block";
image.setAttribute("alt", "image");
image.classList.add("image");
document.querySelector("#main").appendChild(image);
document.querySelector(".choose").style.display = "none";
const uploadLabel = document.querySelector('.custom-btn.primary') || document.querySelector('label[for="fileInput"]');
if (uploadLabel) uploadLabel.style.display = 'none';
}
});
function applyFilters() {
image.style.filter = `
brightness(${brightnessValue}%)
contrast(${contrastValue}%)
saturate(${saturationValue}%)
hue-rotate(${hueValue}deg)
blur(${blurrValue}px)
grayscale(${grayscaleValue}%)
sepia(${sepiaValue}%)
opacity(${opacityValue}%)
invert(${invertValue}%)
`;
}
function resetFilters() {
brightnessValue = 100;
contrastValue = 100;
saturationValue = 100;
hueValue = 0;
blurrValue = 0;
grayscaleValue = 0;
sepiaValue = 0;
opacityValue = 100;
invertValue = 0;
}
function resetValues() {
brightness.value = brightnessValue;
contrast.value = contrastValue;
exposure.value = brightnessValue;
saturation.value = saturationValue;
hue.value = hueValue;
blurr.value = blurrValue * 5;
grayscale.value = grayscaleValue;
sepia.value = sepiaValue;
opacity.value = opacityValue;
invert.value = invertValue;
}
brightness.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust brightness.");
resetValues();
return;
} else {
brightnessValue = elem.target.value;
applyFilters();
}
});
contrast.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust contrast.");
resetValues();
return;
} else {
contrastValue = elem.target.value;
applyFilters();
}
});
exposure.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust exposure.");
resetValues();
return;
} else {
brightnessValue = elem.target.value;
applyFilters();
}
});
saturation.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust saturation.");
resetValues();
return;
} else {
saturationValue = elem.target.value;
applyFilters();
}
});
hue.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust hue.");
resetValues();
return;
} else {
hueValue = elem.target.value;
applyFilters();
}
});
blurr.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust blur.");
resetValues();
return;
} else {
blurrValue = elem.target.value / 5;
applyFilters();
}
});
grayscale.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust grayscale.");
resetValues();
return;
} else {
grayscaleValue = elem.target.value;
applyFilters();
}
});
sepia.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust sepia.");
resetValues();
return;
} else {
sepiaValue = elem.target.value;
applyFilters();
}
});
opacity.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust opacity.");
resetValues();
return;
} else {
opacityValue = elem.target.value;
applyFilters();
}
});
invert.addEventListener("input", (elem) => {
if (!image) {
alert("Please select an image to adjust invert.");
resetValues();
return;
} else {
invertValue = elem.target.value;
applyFilters();
}
});
reset.addEventListener("click", () => {
if (!image) {
alert("Please select an image to reset.");
return;
} else {
resetFilters();
resetValues();
applyFilters();
}
});
function downloadImage() {
const img = document.querySelector(".image");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
// Apply filters
ctx.filter = `
brightness(${brightnessValue}%)
contrast(${contrastValue}%)
saturate(${saturationValue}%)
hue-rotate(${hueValue}deg)
blur(${blurrValue}px)
grayscale(${grayscaleValue}%)
sepia(${sepiaValue}%)
opacity(${opacityValue}%)
invert(${invertValue}%)
`;
ctx.drawImage(img, 0, 0);
const link = document.createElement("a");
link.download = "edited-image.png";
link.href = canvas.toDataURL("image/png");
link.click();
}
download.addEventListener("click", () => {
if (!image) {
alert("Please select an image to download.");
return;
} else {
downloadImage();
window.location.reload();
}
});
original.addEventListener("click", () => {
if (!image) {
alert("Please select an image to view original.");
return;
} else {
resetFilters();
resetValues();
applyFilters();
}
});
vintage.addEventListener("click", () => {
if (!image) {
alert("Please select an image to apply vintage filter.");
return;
} else {
brightnessValue = 110;
contrastValue = 120;
saturationValue = 90;
hueValue = -10;
sepiaValue = 30;
applyFilters();
resetValues();
}
});
cold.addEventListener("click", () => {
if (!image) {
alert("Please select an image to apply cold filter.");
return;
} else {
brightnessValue = 90;
contrastValue = 110;
saturationValue = 120;
hueValue = 20;
applyFilters();
resetValues();
}
});
warm.addEventListener("click", () => {
if (!image) {
alert("Please select an image to apply warm filter.");
return;
} else {
brightnessValue = 110;
contrastValue = 120;
saturationValue = 120;
hueValue = -20;
applyFilters();
resetValues();
}
});
dramatic.addEventListener("click", () => {
if (!image) {
alert("Please select an image to apply dramatic filter.");
return;
} else {
brightnessValue = 80;
contrastValue = 150;
saturationValue = 80;
hueValue = 0;
applyFilters();
resetValues();
}
});
darken.addEventListener("click", () => {
if (!image) {
alert("Please select an image to apply darken filter.");
return;
} else {
brightnessValue = 70;
contrastValue = 130;
saturationValue = 100;
hueValue = 0;
applyFilters();
resetValues();
}
});