forked from soumyadip1188/image_editer_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
199 lines (173 loc) · 5.17 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
193
194
195
196
197
198
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const downloadBtn = document.getElementById("download-btn");
const uploadFile = document.getElementById("upload-file");
const revertBtn = document.getElementById("revert-btn");
/*-----------------------------------Add Effects--------------------------------*/
var dictionary = {"xpro2-add":"contrast(1.3) brightness(0.8) sepia(0.3) saturate(1.5) hue-rotate(-20deg)",
"toaster-add":"sepia(0.4) saturate(2.5) hue-rotate(-30deg) contrast(0.67)",
"sutro-add":"brightness(0.75) contrast(1.3) sepia(0.5) hue-rotate(-25deg)",
"mayfair-add":"saturate(1.4) contrast(1.1)",
"kelvin-add":"sepia(0.4) saturate(2.4) brightness(1.3) contrast(1)",
'inkwell-add':"grayscale(1) brightness(1.2) contrast(1.05)",
"brannan-add":'sepia(0.5) contrast(1.4)',
"amaro-add":" hue-rotate(-10deg) contrast(0.9) brightness(1.1) saturate(1.5)",
"none":"none"}
// dictionary contain proprty : class of the button , value : css effects add on canvas
function add_effects(filterclass){
const file = uploadFile.files[0];
const reader = new FileReader();
if (file){
fileName = file.name;
reader.readAsDataURL(file);
}
//Add image to a canvas
reader.addEventListener("load",() =>{
img = new Image();
img.src = reader.result;
img.addEventListener("load" , () => {
canvas.width = img.width;
canvas.height = img.height;
ctx.filter = dictionary[filterclass];
ctx.drawImage(img,0,0,img.width,img.height);
add_filter_value();
});
},false);
};
const finding_value = (value,index) =>{
var a='';
var i=index;
while (1){
if (value[i]!==")"){
a+=value[i]
i+=1
}else{
break
}
}
return a;
}
var fil = document.querySelectorAll(".filter-btn")
fil.forEach(function(item){
item.addEventListener("click",(event) => {
add_effects(event.target.classList[1]);
});
});
revertBtn.addEventListener("click",(e) => {
add_effects("none");
});
const filtersOption = {
bright:[document.getElementById("brightness"),"brightness",11],
contrast:[document.getElementById("contrast"),"contrast",9],
saturation:[document.getElementById("saturation"),"saturate",9],
grayscale:[document.getElementById("grayscale"),"grayscale",10]
};
const add_filter_value = () => {
for (x in filtersOption){
var a = ctx.filter;
var index = a.search(filtersOption[x][1])
if (index!==-1){
index = index+filtersOption[x][2]
var val = finding_value(a,index);
filtersOption[x][0].value=String(parseFloat(val)*100);
}else{
if(x!=="grayscale"){
filtersOption[x][0].value="100";
}else{
filtersOption[x][0].value="0";
}
}
}
}
const repeat_func = (a) => {
const file = uploadFile.files[0];
const reader = new FileReader();
if (file){
fileName = file.name;
reader.readAsDataURL(file);
}
//Add image to a canvas
reader.addEventListener("load",() =>{
img = new Image();
img.src = reader.result;
img.addEventListener("load" , () => {
canvas.width = img.width;
canvas.height = img.height;
ctx.filter = a;
ctx.drawImage(img,0,0,img.width,img.height);
});
},false);
}
const on_filter_change = (item) => {
var a = ctx.filter;
var index = a.search(filtersOption[item][1]);
if (a==="none"){
a="";
}
if (index!==-1){
var s = filtersOption[item][1]+"("+String(finding_value(a,index+filtersOption[item][2]))+")";
var val = filtersOption[item][0].value;
val = val/100;
a = a.replace(s,filtersOption[item][1]+"("+String(val)+")");
repeat_func(a);
}else{
var val = filtersOption[item][0].value;
val = val/100;
a+=(" "+(filtersOption[item][1]+"("+String(val)+")"));
repeat_func(a);
}
}
Object.keys(filtersOption).forEach(function(item){
filtersOption[item][0].addEventListener("change",(event) => {
on_filter_change(item);
});
});
/*-----------------------------Upload File--------------------------------*/
uploadFile.addEventListener("change", (event) => {
const file = uploadFile.files[0];
const reader = new FileReader();
if (file){
fileName = file.name;
reader.readAsDataURL(file);
}
//Add image to a canvas
reader.addEventListener("load",() =>{
img = new Image();
img.src = reader.result;
img.addEventListener("load" , () => {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img,0,0,img.width,img.height);
});
},false);
});
/*------------------------------- Download Event-------------------------------*/
downloadBtn.addEventListener("click", () => {
// Get ext
const fileExtension = fileName.slice(-4);
// Init new filename
let newFilename;
// Check image type
if (fileExtension === ".jpg" || fileExtension === ".png") {
// new filename
newFilename = fileName.substring(0, fileName.length - 4) + "-edited.jpg";
}
// Call download
download(canvas, newFilename);
});
// Download
function download(canvas, filename) {
// Init event
let e;
// Create link
const link = document.createElement("a");
// Set props
link.download = filename;
// imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
link.href = canvas.toDataURL("image/jpeg", 0.8);
console.log(canvas,link.href)
// New mouse event
e = new MouseEvent("click");
// Dispatch event
link.dispatchEvent(e);
}