-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
278 lines (208 loc) · 8.38 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
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
// Importing functions from filter.js library
import { toGrayscale, toCool, toWarm, toWeightedGrayscale, toOil_painting, toSharpen, toFrost, toVignette, toKissMe, toBlackWhite, toIvory,toIvorySecond, toFade, toClassic, toSoft, toBlossom, toCartoon } from "./filter.js";
// console.log("Image processing");
const file = document.getElementById("img"); // File input
let originalImageData; // Variable to store the image data of the uploded file
let filteredImageData; // Variable to store the image data of the filtered image
let brightnessValue = 0; // variable to store the currentBrightness value
let coolValue = 0; // variable to store the currentCool value
let warmValue = 0; // variable to store the current warm value
// canvas element to draw the uploaded image
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
// Variable to store the uploaded image
let uploadedImg = new Image();
// Dictionary to hold different filtes and its values
let addedFilters = [];
// On uploading the file on the file input
file.addEventListener('change', (e) => {
// console.log(e.target.files[0]);
const imgFile = e.target.files[0]; // Getting the firs uploaded file
const reader = new FileReader(); // Filereader to read the file
let imgUrl = reader.readAsDataURL(imgFile); // Reading file using file reader and getting image url
// When the file reader is done reading the uploaded file
reader.onloadend = function (e) {
uploadedImg.src = e.target.result; // Setting src of image as the image url read by file reader
// When image is done loading
uploadedImg.onload = function (element) {
console.log("Image is loaded");
}
}
})
// Upload button function
let uploadBtn = document.querySelector("#upload");
uploadBtn.addEventListener("click", () => {
//calculating image canvas width and height
canvas.width = uploadedImg.width;
canvas.height = uploadedImg.height;
// ctx.drawImage(uploadedImg, 0, 0, canvas.width, canvas.height);
ctx.drawImage(uploadedImg, 0, 0);
// Getting image data from the image
originalImageData = ctx.getImageData(0, 0, uploadedImg.width, uploadedImg.height);
filteredImageData = originalImageData;
brightnessValue = 0;
coolValue = 0;
warmValue = 0;
})
// Coverting the image to grayscale when clicking on grayscale button
let grayscaleBtn = document.querySelector('#grayscale');
grayscaleBtn.addEventListener('click', function () {
// Getting the modified, according to grayscale function, image data
filteredImageData = toGrayscale(originalImageData);
// Drawing the image on canvas
ctx.putImageData(filteredImageData, 0, 0);
})
// To changed image into weightedGrayscale
let weightedGrayscale = document.querySelector("#weightedGrayscale");
weightedGrayscale.addEventListener('click', (e) => {
// Getting the modified, according to grayscale function, image data
filteredImageData = toWeightedGrayscale(originalImageData);
// Drawing the image on canvas
ctx.putImageData(filteredImageData, 0, 0);
})
// For changing the brightness
let brightnessBtn = document.querySelector('#brightness');
brightnessBtn.addEventListener('change', (e) => {
// Getting the current brightness value
let currentBrightness = parseInt(e.target.value);
brightnessImage(currentBrightness);
})
// for changing cool
let coolBtn = document.querySelector('#cool');
coolBtn.addEventListener('change', (e) => {
// warmBtn.value = 0;
// Getting the current cool value
let currentCoolValue = parseInt(e.target.value);
// Getting the modified, according to cool function, image data
filteredImageData = toCool(originalImageData, currentCoolValue);
coolValue = currentCoolValue;
// Drawing the image on canvas
ctx.putImageData(filteredImageData, 0, 0);
})
// for changing cool
let warmBtn = document.querySelector('#warm');
warmBtn.addEventListener('change', (e) => {
// coolBtn.value = 0;
// Getting the current warm value
let currentWarmValue = parseInt(e.target.value);
// Getting the modified, according to cool function, image data
filteredImageData = toWarm(originalImageData, currentWarmValue);
warmValue = currentWarmValue;
// Drawing the image on canvas
ctx.putImageData(filteredImageData, 0, 0);
})
//Sketch filter
let oil_painting = document.querySelector('#oil_painting');
oil_painting.addEventListener('click', function () {
// Getting the modified, according to grayscale function, image data
filteredImageData = toOil_painting(originalImageData);
// Drawing the image on canvas
ctx.putImageData(filteredImageData, 0, 0);
})
// removing all filters
let originalImageBtn = document.querySelector("#original");
originalImageBtn.addEventListener('click', (e) => {
//assigning original image data to filtered image data
filteredImageData = originalImageData;
ctx.putImageData(filteredImageData, 0, 0);
brightnessValue = 0;
coolValue = 0;
warmValue = 0;
})
// Downloading filtered image
let downloadBtn = document.querySelector("#download");
downloadBtn.addEventListener("click", (e) => {
e.target.href = canvas.toDataURL("image/png");
})
// mean Blur filter
let meanBlurBtn = document.querySelector("#meanBlur");
meanBlurBtn.addEventListener("change", (e) => {
blurImage(parseInt(e.target.value));
})
let sharp = document.querySelector('#toSharpen');
sharp.addEventListener('click', function () {
//Getting sharppened image data
filteredImageData = toSharpen(originalImageData);
//drawing image to canvas
ctx.putImageData(filteredImageData, 0, 0);
})
//contrast filter
let contrast = document.querySelector("#contrast");
contrast.addEventListener("change", (e) => {
addContrast(parseInt(e.target.value));
})
// function to set change the blur of the image applicable only for this file
const blurImage = (value) => {
ctx.filter = `blur(${value}px)`;
ctx.drawImage(uploadedImg, 0, 0);
}
const brightnessImage = (value) => {
ctx.filter = `brightness(${value}%)`;
ctx.drawImage(uploadedImg, 0, 0);
}
const addContrast = (value) => {
ctx.filter = `contrast(${value}%)`;
ctx.drawImage(uploadedImg, 0, 0);
}
//soft button filter
let softBtn = document.querySelector("#soft");
softBtn.addEventListener("click", (e) => {
// soft();
ctx.filter = toSoft(originalImageData);
ctx.drawImage(uploadedImg, 0, 0);
})
//frost button filter
let fadedBtn = document.querySelector("#faded");
fadedBtn.addEventListener("click", (e) => {
ctx.filter = toFade(originalImageData);
ctx.drawImage(uploadedImg, 0, 0);
})
let blossomBtn = document.querySelector("#blossom");
blossomBtn.addEventListener("click", (e) => {
ctx.filter = toBlossom(originalImageData);
ctx.drawImage(uploadedImg,0,0);
})
let ivoryBtn = document.querySelector("#ivory");
ivoryBtn.addEventListener("click", (e) => {
ctx.filter = toIvory(originalImageData);
ctx.drawImage(uploadedImg, 0, 0);
})
let blackwhiteBtn = document.querySelector("#blackwhite");
blackwhiteBtn.addEventListener("click", (e) => {
// blackwhite();
ctx.filter = toBlackWhite(originalImageData);
ctx.drawImage(uploadedImg, 0, 0);
})
let classicBtn = document.querySelector("#classic");
classicBtn.addEventListener("click", (e) => {
ctx.filter = toClassic(originalImageData);
ctx.drawImage(uploadedImg, 0, 0);
})
let ivory2Btn = document.querySelector("#ivory2");
ivory2Btn.addEventListener("click", (e) => {
ctx.filter = toIvorySecond(originalImageData);
ctx.drawImage(uploadedImg, 0, 0);
})
let vignetteBtn = document.querySelector("#vignette");
vignetteBtn.addEventListener("click", (e) => {
filteredImageData = toVignette(originalImageData);
ctx.putImageData(filteredImageData, 0, 0);
})
let frostBtn = document.querySelector("#frost");
frostBtn.addEventListener("click", (e) => {
filteredImageData = toFrost(originalImageData);
ctx.putImageData(filteredImageData, 0, 0);
})
// Applying cartoon filter
let cartoonBtn = document.querySelector("#cartoon");
cartoonBtn.addEventListener("click", () => {
filteredImageData = toCartoon(originalImageData);
ctx.putImageData(filteredImageData, 0, 0);
})
// checking a new filter
let kissmeBtn = document.querySelector("#kissme");
kissmeBtn.addEventListener("click", () => {
console.log('filteredImageData');
let filteredImageData = toKissMe(originalImageData);
ctx.putImageData(filteredImageData, 0, 0);
})