-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
184 lines (149 loc) · 4.57 KB
/
main.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
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let img = new Image();
let filename = '';
const downloadBtn = document.getElementById('download-btn');
const uploadFile = document.getElementById('upload-file');
const revertBtn = document.getElementById('revert-btn');
//Add Filter and Effects
document.addEventListener('click', (e)=>{
if(e.target.classList.contains('filter-btn')){
if(e.target.classList.contains('brightness-add')){
Caman('#canvas', img, function(){
this.brightness(5).render();
});
} else if(e.target.classList.contains('brightness-remove')){
Caman('#canvas', img, function(){
this.brightness(-5).render();
});
}
else if(e.target.classList.contains('contrast-add')){
Caman('#canvas', img, function(){
this.contrast(5).render();
});
}
else if(e.target.classList.contains('contrast-remove')){
Caman('#canvas', img, function(){
this.contrast(-5).render();
});
}
else if(e.target.classList.contains('saturation-add')){
Caman('#canvas', img, function(){
this.saturation(5).render();
});
}
else if(e.target.classList.contains('saturation-remove')){
Caman('#canvas', img, function(){
this.saturation(-5).render();
});
}
else if(e.target.classList.contains('vibrance-add')){
Caman('#canvas', img, function(){
this.vibrance(5).render();
});
}
else if(e.target.classList.contains('vibrance-remove')){
Caman('#canvas', img, function(){
this.vibrance(-5).render();
});
}
else if(e.target.classList.contains('vintage-add')){
Caman('#canvas', img, function(){
this.vintage().render();
});
}
else if(e.target.classList.contains('lomo-add')){
Caman('#canvas', img, function(){
this.lomo().render();
});
}
else if(e.target.classList.contains('clarity-add')){
Caman('#canvas', img, function(){
this.clarity().render();
});
}
else if(e.target.classList.contains('sincity-add')){
Caman('#canvas', img, function(){
this.sincity().render();
});
}
else if(e.target.classList.contains('crossprocess-add')){
Caman('#canvas', img, function(){
this.crossprocess().render();
});
}
else if(e.target.classList.contains('pinhole-add')){
Caman('#canvas', img, function(){
this.pinhole().render();
});
}
else if(e.target.classList.contains('nostalgia-add')){
Caman('#canvas', img, function(){
this.nostalgia().render();
});
}
else if(e.target.classList.contains('hermajesty-add')){
Caman('#canvas', img, function(){
this.hermajesty().render();
});
}
}
});
//Revert the filters
revertBtn.addEventListener('click', ()=>{
Caman('#canvas', img, function(){
this.revert();
});
});
//Upload File
uploadFile.addEventListener('change', () => {
//Get file
const file = document.getElementById('upload-file').files[0];
//Init FileReader
const reader = new FileReader();
if(file){
//Set the filename
filename = file.name;
//Read data as URL
reader.readAsDataURL(file);
}
reader.addEventListener('load', ()=>{
//Create a img variable
img = new Image();
//Set src
img.src = reader.result;
//on image load, add to canvas
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
canvas.removeAttribute('data-caman-id');
}
}, false);
});
//Download Event
downloadBtn.addEventListener('click', (e)=>{
//Get the file extension
const fileExtension = filename.slice(-4);
//Init newFile name variable
let newFileName;
//Check the image type
if(fileExtension === '.jpg' || fileExtension === '.png'){
newFileName = filename.substring(0, filename.length-4) + '-edited.jpg';
}
//Call download
download(canvas, newFileName);
});
//Download function
function download(canvas, filename){
//Init Event
let e;
//Create Link
const link = document.createElement('a');
//Set Some Properties
link.download = filename;
link.href = canvas.toDataURL('image/jpeg', 0.8);
//New mouse event
e = new MouseEvent('click');
link.dispatchEvent(e);
}