Skip to content

Commit 2617278

Browse files
Merge pull request #783 from IniyalPalanisamy/patch-11
Update script.js
2 parents fa484aa + 2ddfd41 commit 2617278

File tree

1 file changed

+75
-106
lines changed

1 file changed

+75
-106
lines changed

Image-Filter-App/script.js

Lines changed: 75 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -11,146 +11,115 @@ const revertBtn = document.getElementById("revert-btn");
1111
// Filter & Effect Handlers
1212
document.addEventListener("click", (e) => {
1313
if (e.target.classList.contains("filter-btn")) {
14-
if (e.target.classList.contains("brightness-add")) {
15-
Caman("#canvas", img, function () {
16-
this.brightness(5).render();
17-
});
18-
} else if (e.target.classList.contains("brightness-remove")) {
19-
Caman("#canvas", img, function () {
20-
this.brightness(-5).render();
21-
});
22-
} else if (e.target.classList.contains("contrast-add")) {
23-
Caman("#canvas", img, function () {
24-
this.contrast(5).render();
25-
});
26-
} else if (e.target.classList.contains("contrast-remove")) {
27-
Caman("#canvas", img, function () {
28-
this.contrast(-5).render();
29-
});
30-
} else if (e.target.classList.contains("saturation-add")) {
31-
Caman("#canvas", img, function () {
32-
this.saturation(5).render();
33-
});
34-
} else if (e.target.classList.contains("saturation-remove")) {
35-
Caman("#canvas", img, function () {
36-
this.saturation(-5).render();
37-
});
38-
} else if (e.target.classList.contains("vibrance-add")) {
39-
Caman("#canvas", img, function () {
40-
this.vibrance(5).render();
41-
});
42-
} else if (e.target.classList.contains("vibrance-remove")) {
43-
Caman("#canvas", img, function () {
44-
this.vibrance(-5).render();
45-
});
46-
} else if (e.target.classList.contains("vintage-add")) {
47-
Caman("#canvas", img, function () {
48-
this.vintage().render();
49-
});
50-
} else if (e.target.classList.contains("lomo-add")) {
51-
Caman("#canvas", img, function () {
52-
this.lomo().render();
53-
});
54-
} else if (e.target.classList.contains("clarity-add")) {
55-
Caman("#canvas", img, function () {
56-
this.clarity().render();
57-
});
58-
} else if (e.target.classList.contains("sincity-add")) {
59-
Caman("#canvas", img, function () {
60-
this.sinCity().render();
61-
});
62-
} else if (e.target.classList.contains("crossprocess-add")) {
63-
Caman("#canvas", img, function () {
64-
this.crossProcess().render();
65-
});
66-
} else if (e.target.classList.contains("pinhole-add")) {
67-
Caman("#canvas", img, function () {
68-
this.pinhole().render();
69-
});
70-
} else if (e.target.classList.contains("nostalgia-add")) {
71-
Caman("#canvas", img, function () {
72-
this.nostalgia().render();
73-
});
74-
} else if (e.target.classList.contains("hermajesty-add")) {
75-
Caman("#canvas", img, function () {
76-
this.herMajesty().render();
77-
});
78-
}
14+
const filterAction = e.target.classList.item(1); // Get the second class which indicates the filter
15+
Caman("#canvas", img, function () {
16+
switch (filterAction) {
17+
case "brightness-add":
18+
this.brightness(5).render();
19+
break;
20+
case "brightness-remove":
21+
this.brightness(-5).render();
22+
break;
23+
case "contrast-add":
24+
this.contrast(5).render();
25+
break;
26+
case "contrast-remove":
27+
this.contrast(-5).render();
28+
break;
29+
case "saturation-add":
30+
this.saturation(5).render();
31+
break;
32+
case "saturation-remove":
33+
this.saturation(-5).render();
34+
break;
35+
case "vibrance-add":
36+
this.vibrance(5).render();
37+
break;
38+
case "vibrance-remove":
39+
this.vibrance(-5).render();
40+
break;
41+
case "vintage-add":
42+
this.vintage().render();
43+
break;
44+
case "lomo-add":
45+
this.lomo().render();
46+
break;
47+
case "clarity-add":
48+
this.clarity().render();
49+
break;
50+
case "sincity-add":
51+
this.sinCity().render();
52+
break;
53+
case "crossprocess-add":
54+
this.crossProcess().render();
55+
break;
56+
case "pinhole-add":
57+
this.pinhole().render();
58+
break;
59+
case "nostalgia-add":
60+
this.nostalgia().render();
61+
break;
62+
case "hermajesty-add":
63+
this.herMajesty().render();
64+
break;
65+
default:
66+
break;
67+
}
68+
});
7969
}
8070
});
8171

8272
// Revert Filters
83-
revertBtn.addEventListener("click", (e) => {
73+
revertBtn.addEventListener("click", () => {
8474
Caman("#canvas", img, function () {
8575
this.revert();
8676
});
8777
});
8878

8979
// Upload File
9080
uploadFile.addEventListener("change", () => {
91-
// Get File
92-
const file = document.getElementById("upload-file").files[0];
93-
// Init FileReader API
81+
const file = uploadFile.files[0];
9482
const reader = new FileReader();
9583

96-
// Check for file
9784
if (file) {
98-
// Set file name
85+
if (!file.type.startsWith("image/")) {
86+
alert("Please upload a valid image file.");
87+
return;
88+
}
9989
fileName = file.name;
100-
// Read data as URL
10190
reader.readAsDataURL(file);
10291
}
10392

104-
// Add image to canvas
105-
reader.addEventListener(
106-
"load",
107-
() => {
108-
// Create image
109-
img = new Image();
110-
// Set image src
111-
img.src = reader.result;
112-
// On image load add to canvas
113-
img.onload = function () {
114-
canvas.width = img.width;
115-
canvas.height = img.height;
116-
ctx.drawImage(img, 0, 0, img.width, img.height);
117-
canvas.removeAttribute("data-caman-id");
118-
};
119-
},
120-
false
121-
);
93+
reader.addEventListener("load", () => {
94+
img = new Image();
95+
img.src = reader.result;
96+
img.onload = function () {
97+
canvas.width = img.width;
98+
canvas.height = img.height;
99+
ctx.drawImage(img, 0, 0);
100+
};
101+
}, false);
122102
});
123103

124104
// Download Event
125105
downloadBtn.addEventListener("click", () => {
126-
// Get ext
127106
const fileExtension = fileName.slice(-4);
128-
129-
// Init new filename
130107
let newFilename;
131108

132-
// Check image type
133109
if (fileExtension === ".jpg" || fileExtension === ".png") {
134-
// new filename
135110
newFilename = fileName.substring(0, fileName.length - 4) + "-edited.jpg";
111+
} else {
112+
alert("Unsupported file type. Please upload a .jpg or .png file.");
113+
return;
136114
}
137115

138-
// Call download
139116
download(canvas, newFilename);
140117
});
141118

142-
// Download
119+
// Download Function
143120
function download(canvas, filename) {
144-
// Init event
145-
let e;
146-
// Create link
147121
const link = document.createElement("a");
148-
149-
// Set props
150122
link.download = filename;
151123
link.href = canvas.toDataURL("image/jpeg", 0.8);
152-
// New mouse event
153-
e = new MouseEvent("click");
154-
// Dispatch event
155-
link.dispatchEvent(e);
124+
link.click(); // Simulate a click to download
156125
}

0 commit comments

Comments
 (0)