-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
155 lines (139 loc) · 4.82 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
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');
//TODO: Filters
document.addEventListener('click', (e) => {
if (e.target.classList.contains('filter-btn')) {
if (e.target.classList.contains('brightness-add')) {
Caman('#canvas', img, function() {
this.brightness(10).render();
})
} else if (e.target.classList.contains('brightness-remove')) {
Caman('#canvas', img, function() {
this.brightness(-10).render();
})
} else if (e.target.classList.contains('contrast-add')) {
Caman('#canvas', img, function() {
this.contrast(10).render();
})
} else if (e.target.classList.contains('contrast-remove')) {
Caman('#canvas', img, function() {
this.contrast(-10).render();
})
} else if (e.target.classList.contains('saturation-add')) {
Caman('#canvas', img, function() {
this.saturation(10).render();
})
} else if (e.target.classList.contains('saturation-remove')) {
Caman('#canvas', img, function() {
this.saturation(-10).render();
})
} else if (e.target.classList.contains('vibrance-add')) {
Caman('#canvas', img, function() {
this.vibrance(10).render();
})
} else if (e.target.classList.contains('vibrance-remove')) {
Caman('#canvas', img, function() {
this.vibrance(-10).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 Filters
revertBtn.addEventListener('click', (e) => {
Caman('#canvas', img, function() {
this.revert();
});
});
// Download Button
downloadBtn.addEventListener('click', (e) => {
// Get file ext
const fileExtension = fileName.slice(-4);
// init new filename
let newFileName;
// Check image type
if(fileExtension === '.jpg' || fileExtension === '.png') {
newFileName = fileName.substring(0, fileName.length - 4) + '-edited.jpg';
}
download(canvas, newFileName)
})
// Call download
function download(canvas, filename) {
// initialize event
let e;
// create link
const link = document.createElement('a');
// set props
link.download = filename;
link.href = canvas.toDataURL('image/jpeg', 0.8);
// new mouse event
e = new MouseEvent('click');
// dispatch
link.dispatchEvent(e);
}
// Upload File
uploadFile.addEventListener('change', (e) => {
// Get File
const file = document.getElementById('upload-file').files[0];
// init FileReader
const reader = new FileReader();
if (file) {
// Set file name
fileName = file.name;
// Read data as URL
reader.readAsDataURL(file);
}
//Add image to canvas
reader.addEventListener(
'load',
() => {
// Create img
img = new Image();
// Set src
img.src = reader.result;
// On img 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);
});