-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
192 lines (159 loc) · 5.18 KB
/
script.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
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker
.register("/serviceWorker.js")
.then((res) => console.log("service worker registered"))
.catch((err) => console.log("service worker not registered", err));
});
}
let upload_img_box = document.querySelector(".upload_img_box");
let selectedImage = document.querySelector("#selectedImage");
let choose_image = document.querySelector(".choose_image");
let image_holder = document.querySelector(".image_holder");
let image = document.querySelector("#image");
let slider = document.querySelectorAll(".slider");
let show_value = document.querySelectorAll(".show_value");
let list_options = document.querySelectorAll("ul li");
let options = document.querySelector(".options");
let option = document.querySelectorAll(".option");
let clearAll = document.querySelector("#clearAll");
let remove_img_btn = document.querySelector("#remove_img_btn");
let canvas = document.querySelector("#image_canvas");
const context = canvas.getContext("2d");
let File_Name;
let Edited = false;
/*handle choose image event*/
upload_img_box.addEventListener("click", function () {
selectedImage.click();
});
/*choose image event*/
selectedImage.addEventListener("change", function () {
const file = this.files[0];
if (file) {
const reader = new FileReader();
File_Name = file.name;
choose_image.style.display = "none";
image_holder.style.display = "block";
reader.addEventListener("load", function () {
image.setAttribute("src", this.result);
});
reader.readAsDataURL(file);
remove_img_btn.style.display = "block";
}
if (Edited == false) {
Edited = true;
}
});
/*function call when slider value change*/
for (let i = 0; i <= slider.length - 1; i++) {
slider[i].addEventListener("input", editImage);
}
function editImage() {
let bright = document.querySelector("#brightness");
let blur = document.querySelector("#blur");
let grey = document.querySelector("#greyScale");
let hue = document.querySelector("#hue");
let saturation = document.querySelector("#saturation");
let brightValShow = document.querySelector("#brightVal");
let blurValShow = document.querySelector("#blurVal");
let greyValShow = document.querySelector("#greyVal");
let hueValShow = document.querySelector("#hueVal");
let saturationValShow = document.querySelector("#saturationVal");
let brightVal = bright.value;
let greyVal = grey.value;
let blurVal = blur.value;
let hueVal = hue.value;
let satuVal = saturation.value;
brightValShow.innerHTML = brightVal;
blurValShow.innerHTML = blurVal;
greyValShow.innerHTML = greyVal;
hueValShow.innerHTML = hueVal;
saturationValShow.innerHTML = satuVal;
image.style.filter =
"grayscale(" +
greyVal +
"%) hue-rotate(" +
hueVal +
"deg) brightness(" +
brightVal +
"%) blur(" +
blurVal +
"px) saturate(" +
satuVal +
")";
context.filter =
"grayscale(" +
greyVal +
"%) hue-rotate(" +
hueVal +
"deg) brightness(" +
brightVal +
"%) blur(" +
blurVal +
"px) saturate(" +
satuVal +
")";
clearAll.style.transform = "translateY(0px)";
}
/*handle each option click even*/
list_options.forEach((list_option, index) => {
list_option.addEventListener("click", function () {
if (image.getAttribute("src") == "") {
alert("Choose Image First");
} else {
if (index === 5) return;
options.style.transform = "translateY(0px)";
if (Edited == true) {
canvas.height = image.naturalHeight;
canvas.width = image.naturalWidth;
for (let i = 0; i <= 4; i++) {
if (index != i) {
list_options[i].classList.remove("active_option");
option[i].classList.remove("active_controller");
} else {
this.classList.add("active_option");
option[i].classList.add("active_controller");
}
}
} else {
alert("Edit Your Image First");
}
}
});
});
/*download image btn click*/
function Download_btn() {
if (image.getAttribute("src") != "") {
if (Edited == true) {
context.drawImage(image, 0, 0, canvas.width, canvas.height);
var jpegUrl = canvas.toDataURL("image/jpg");
const link = document.createElement("a");
document.body.appendChild(link);
link.setAttribute("href", jpegUrl);
link.setAttribute("download", File_Name);
link.click();
document.body.removeChild(link);
}
}
}
/*clear or reset range value*/
clearAll.addEventListener("click", function () {
clearAllRangeValue();
});
function clearAllRangeValue() {
image.style.filter = "none";
context.filter = "none";
for (let i = 0; i <= slider.length - 1; i++) {
if (i == 0) {
slider[i].value = "100";
} else {
slider[i].value = "0";
}
}
editImage();
clearAll.style.transform = "translateY(150px)";
}
/*remove image btn click*/
remove_img_btn.addEventListener("click", function () {
window.location.reload();
});