Skip to content

Commit c76c0bf

Browse files
committed
Added Scipt
1 parent f9dd7a2 commit c76c0bf

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

Image-Filter-App/script.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
const canvas = document.getElementById("canvas");
2+
const ctx = canvas.getContext("2d");
3+
4+
let img = new Image();
5+
let fileName = "";
6+
7+
const downloadBtn = document.getElementById("download-btn");
8+
const uploadFile = document.getElementById("upload-file");
9+
const revertBtn = document.getElementById("revert-btn");
10+
11+
// Filter & Effect Handlers
12+
document.addEventListener("click", (e) => {
13+
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+
}
79+
}
80+
});
81+
82+
// Revert Filters
83+
revertBtn.addEventListener("click", (e) => {
84+
Caman("#canvas", img, function () {
85+
this.revert();
86+
});
87+
});
88+
89+
// Upload File
90+
uploadFile.addEventListener("change", () => {
91+
// Get File
92+
const file = document.getElementById("upload-file").files[0];
93+
// Init FileReader API
94+
const reader = new FileReader();
95+
96+
// Check for file
97+
if (file) {
98+
// Set file name
99+
fileName = file.name;
100+
// Read data as URL
101+
reader.readAsDataURL(file);
102+
}
103+
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+
);
122+
});
123+
124+
// Download Event
125+
downloadBtn.addEventListener("click", () => {
126+
// Get ext
127+
const fileExtension = fileName.slice(-4);
128+
129+
// Init new filename
130+
let newFilename;
131+
132+
// Check image type
133+
if (fileExtension === ".jpg" || fileExtension === ".png") {
134+
// new filename
135+
newFilename = fileName.substring(0, fileName.length - 4) + "-edited.jpg";
136+
}
137+
138+
// Call download
139+
download(canvas, newFilename);
140+
});
141+
142+
// Download
143+
function download(canvas, filename) {
144+
// Init event
145+
let e;
146+
// Create link
147+
const link = document.createElement("a");
148+
149+
// Set props
150+
link.download = filename;
151+
link.href = canvas.toDataURL("image/jpeg", 0.8);
152+
// New mouse event
153+
e = new MouseEvent("click");
154+
// Dispatch event
155+
link.dispatchEvent(e);
156+
}

0 commit comments

Comments
 (0)