-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (132 loc) · 5.32 KB
/
script.js
File metadata and controls
150 lines (132 loc) · 5.32 KB
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
const fileUpload = document.querySelector(".file-upload");
const chooseImg = document.querySelector(".choose-img");
const saveImgBtn = document.querySelector(".save-img");
const displayImg = document.querySelector(".display-img img");
const mainContainer = document.querySelector(".main-container");
const filterButtons = document.querySelectorAll(".filter button");
const rotateButtons = document.querySelectorAll(".rotate button");
const resetFilterButton = document.querySelector(".reset-filters");
const inputRange = document.querySelector(".slider input");
const inputValue = document.querySelector(".slider .value");
let [brightness, saturation, inversion, grayscale, blur, hueRotate] = [100, 100, 0, 0, 0, 0];
let [rotate, horizontalFlip, verticalFlip] = [0, 1, 1]
const applyFilter = () => {
displayImg.style.transform = `rotate(${rotate}deg) scale(${horizontalFlip}, ${verticalFlip})`;
displayImg.style.filter = `brightness(${brightness}%) saturate(${saturation}%) invert(${inversion}%) grayscale(${grayscale}%) blur(${blur}px) hue-rotate(${hueRotate}deg)`;
}
let file; // Initializing global to get the same file name while downloading
const uploadImg = () => {
resetFilters() // Reset filters while uploading new file
file = fileUpload.files[0]; // Get selected file
if (!file) return; // Return if the file has not selected (if canceled)
displayImg.src = URL.createObjectURL(file); // Passing file url as display img src
displayImg.addEventListener("load", () => { // Removing desable class, after selecting an image
mainContainer.classList.remove("disable");
});
}
// Managing.active class on filter options
filterButtons.forEach((select => {
select.addEventListener("click", () => {
document.querySelector(".filter .active").classList.remove("active");
select.classList.add("active");
document.querySelector(".slider .name").innerHTML = select.innerHTML; // Passing filter name
// Set indivisual selected filter value
if (select.id === "brightness") {
inputRange.max = "200";
inputRange.value = brightness;
inputValue.innerHTML = `${brightness}%`;
} else if (select.id === "saturation") {
inputRange.max = "200";
inputRange.value = saturation;
inputValue.innerHTML = `${saturation}%`;
} else if (select.id === "hueRotate") {
inputRange.max = "360";
inputRange.value = hueRotate;
inputValue.innerHTML = `${hueRotate}deg`;
} else if (select.id === "blur") {
inputRange.max = "50";
inputRange.value = blur;
inputValue.innerHTML = `${blur}px`;
} else if (select.id === "inversion") {
inputRange.max = "100";
inputRange.value = inversion;
inputValue.innerHTML = `${inversion}%`;
} else {
inputRange.max = "100";
inputRange.value = grayscale;
inputValue.innerHTML = `${grayscale}%`;
}
})
}))
// Updating filters
const displayAndUpdateFilter = () => {
inputValue.innerHTML = `${inputRange.value}%`; // Passing inputRage
let filterId = document.querySelector(".filter .active").id; // Get selected filter
if (filterId === "brightness") {
brightness = inputRange.value;
} else if (filterId === "saturation") {
saturation = inputRange.value;
} else if (filterId === "inversion") {
inversion = inputRange.value;
} else if (filterId === "grayscale") {
grayscale = inputRange.value;
} else if (filterId === "hueRotate") {
hueRotate = inputRange.value;
} else {
blur = inputRange.value;
}
applyFilter()
}
// Managing.rotion and flip on rotate options
rotateButtons.forEach((select) => {
select.addEventListener('click', () => {
if (select.id === 'left') {
rotate -= 90;
}
else if (select.id === 'right') {
rotate += 90;
}
else if (select.id === 'horizontal') {
horizontalFlip = horizontalFlip == 1 ? -1 : 1
}
else {
verticalFlip = verticalFlip == 1 ? -1 : 1
}
applyFilter()
})
})
// Resetting filters
const resetFilters = () => {
[brightness, saturation, inversion, grayscale, blur, hueRotate] = [
100, 100, 0, 0, 0, 0];
[rotate, horizontalFlip, verticalFlip] = [0, 1, 1]
filterButtons[0].click() // select brightness by default
applyFilter()
}
const saveImg = () => {
let canvas = document.createElement('canvas')
let ctx = canvas.getContext('2d')
canvas.width = displayImg.naturalWidth
canvas.height = displayImg.naturalHeight
ctx.filter = `brightness(${brightness}%) saturate(${saturation}%) invert(${inversion}%) grayscale(${grayscale}%) blur(${blur}px) hue-rotate(${hueRotate}deg)`;
ctx.translate(canvas.width / 2, canvas.height / 2); // Translate canvas from center
ctx.scale(horizontalFlip, verticalFlip); // Flip canvas
if (rotate !== 0) { //Rotate canvas, if the rotate value is not 0
ctx.rotate(rotate * Math.PI / 180);
}
ctx.drawImage(
displayImg,
-canvas.width / 2,
-canvas.height / 2
);
// For download the image
const downloadLink = document.createElement('a');
downloadLink.download = `${file.name.slice(0, -4)}.jpg`;
downloadLink.href = canvas.toDataURL();
downloadLink.click()
}
resetFilterButton.addEventListener('click', resetFilters)
inputRange.addEventListener('input', displayAndUpdateFilter)
fileUpload.addEventListener("change", uploadImg)
saveImgBtn.addEventListener("click", saveImg);
chooseImg.addEventListener('click', () => fileUpload.click())