-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
156 lines (143 loc) · 4.58 KB
/
script.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
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");
// Filter & Effect Handlers
document.addEventListener("click", (e) => {
if (e.target.classList.contains("filter-btn")) {
if (e.target.classList.contains("brightness-add")) {
Caman("#canvas", img, function () {
this.brightness(2).render();
});
} else if (e.target.classList.contains("brightness-remove")) {
Caman("#canvas", img, function () {
this.brightness(-2).render();
});
} else if (e.target.classList.contains("contrast-add")) {
Caman("#canvas", img, function () {
this.contrast(1).render();
});
} else if (e.target.classList.contains("contrast-remove")) {
Caman("#canvas", img, function () {
this.contrast(-1).render();
});
} else if (e.target.classList.contains("saturation-add")) {
Caman("#canvas", img, function () {
this.saturation(1.5).render();
});
} else if (e.target.classList.contains("saturation-remove")) {
Caman("#canvas", img, function () {
this.saturation(-1).render();
});
} else if (e.target.classList.contains("vibrance-add")) {
Caman("#canvas", img, function () {
this.vibrance(2).render();
});
} else if (e.target.classList.contains("vibrance-remove")) {
Caman("#canvas", img, function () {
this.vibrance(-1).render();
});
} else if (e.target.classList.contains("vintage-add")) {
Caman("#canvas", img, function () {
this.vintage(0.05).render();
});
} else if (e.target.classList.contains("lomo-add")) {
Caman("#canvas", img, function () {
this.lomo(0.07).render();
});
} else if (e.target.classList.contains("clarity-add")) {
Caman("#canvas", img, function () {
this.clarity(0.1).render();
});
} else if (e.target.classList.contains("sincity-add")) {
Caman("#canvas", img, function () {
this.sinCity(0.2).render();
});
} else if (e.target.classList.contains("crossprocess-add")) {
Caman("#canvas", img, function () {
this.crossProcess(0.1).render();
});
} else if (e.target.classList.contains("pinhole-add")) {
Caman("#canvas", img, function () {
this.pinhole(0.2).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();
});
});
// Upload File
uploadFile.addEventListener("change", () => {
// Get File
const file = document.getElementById("upload-file").files[0];
// Init FileReader API
const reader = new FileReader();
// Check for file
if (file) {
// Set file name
fileName = file.name;
// Read data as URL
reader.readAsDataURL(file);
}
// Add image to canvas
reader.addEventListener(
"load",
() => {
// Create image
img = new Image();
// Set image src
img.src = reader.result;
// On image 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
);
});
// Download Event
downloadBtn.addEventListener("click", () => {
// Get ext
const fileExtension = fileName.slice(-4);
// Init new filename
let newFilename;
// Check image type
if (fileExtension === ".jpg" || fileExtension === ".png") {
// new filename
newFilename = fileName.substring(0, fileName.length - 4) + "-edited.jpg";
}
// Call download
download(canvas, newFilename);
});
// Download
function download(canvas, filename) {
// Init 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 event
link.dispatchEvent(e);
}