-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (170 loc) · 6.62 KB
/
index.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
window.onload = function () {
const canvas = document.querySelector("canvas");
canvas.width = 600;
canvas.height = 300;
const context = canvas.getContext("2d");
const img = new Image();
img.src = "iStock_3357562_600x300.jpg";
let imageData;
const kernelAlgo = (imageData, weights, opaque) => {
let side = Math.round(Math.sqrt(weights.length));
let halfSide = Math.floor(side / 2);
let src = imageData.data;
let sw = imageData.width;
let sh = imageData.height;
let imageDataCopy = context.createImageData(600, 300);
imageDataCopy.data = imageData.data;
imageDataCopy.width = imageData.width;
imageDataCopy.height = imageData.height;
let w = sw;
let h = sh;
let alphaFac = opaque ? 1 : 0;
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
let sy = y;
let sx = x;
let dstOff = (y * w + x) * 4;
let r = 0,
g = 0,
b = 0,
a = 0;
for (let cy = 0; cy < side; cy++) {
for (let cx = 0; cx < side; cx++) {
let scy = sy + cy - halfSide;
let scx = sx + cx - halfSide;
if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
let srcOff = (scy * sw + scx) * 4;
let wt = weights[cy * side + cx];
r += src[srcOff] * wt;
g += src[srcOff + 1] * wt;
b += src[srcOff + 2] * wt;
a += src[srcOff + 3] * wt;
}
}
}
imageDataCopy.data[dstOff] = r;
imageDataCopy.data[dstOff + 1] = g;
imageDataCopy.data[dstOff + 2] = b;
imageDataCopy.data[dstOff + 3] = a + alphaFac * (255 - a);
}
}
return imageDataCopy;
};
const originalbtn = document.getElementById("originalbtn");
const sharpenbtn = document.getElementById("sharpenbtn");
const blurbtn = document.getElementById("blurbtn");
const sepiabtn = document.getElementById("sepiabtn");
const BWbtn = document.getElementById("BWbtn");
const mirrorbtn = document.getElementById("mirrorbtn");
const sketchbtn = document.getElementById("sketchbtn");
const brightform = document.getElementById("brightform");
const negativebtn = document.getElementById("negativebtn");
img.onload = () => {
context.drawImage(img, 0, 0, 600, 300);
imageData = context.getImageData(0, 0, 600, 300);
originalbtn.addEventListener("click", () => {
context.putImageData(imageData, 0, 0);
});
sharpenbtn.addEventListener("click", () => {
let weights = [-1, -1, -1, -1, 9, -1, -1, -1, -1];
let opaque = 0;
const imageDataCopy = kernelAlgo(imageData, weights, opaque);
context.putImageData(imageDataCopy, 0, 0);
});
blurbtn.addEventListener("click", () => {
let weights = [
1 / 9,
1 / 9,
1 / 9,
1 / 9,
1 / 9,
1 / 9,
1 / 9,
1 / 9,
1 / 9,
];
let opaque = 0;
const imageDataCopy = kernelAlgo(imageData, weights, opaque);
context.putImageData(imageDataCopy, 0, 0);
});
sepiabtn.addEventListener("click", () => {
let imageDataCopy = context.createImageData(600, 300);
for (i = 0; i < imageData.data.length; i += 4) {
imageDataCopy.data[i] = imageData.data[i] * 1.07; //RED
imageDataCopy.data[i + 1] = imageData.data[i + 1] * 0.74; //GREEN
imageDataCopy.data[i + 2] = imageData.data[i + 2] * 0.43; //BLUE
imageDataCopy.data[i + 3] = imageData.data[i + 3]; //Alpha
}
context.putImageData(imageDataCopy, 0, 0);
});
BWbtn.addEventListener("click", () => {
let imageDataCopy = context.createImageData(600, 300);
for (i = 0; i < imageData.data.length; i += 4) {
const avg =
imageData.data[i] * 0.29 +
imageData.data[i + 1] * 0.58 +
imageData.data[i + 2] * 0.11;
imageDataCopy.data[i] = avg; //RED
imageDataCopy.data[i + 1] = avg; //GREEN
imageDataCopy.data[i + 2] = avg; //BLUE
imageDataCopy.data[i + 3] = imageData.data[i + 3]; //Alpha
}
context.putImageData(imageDataCopy, 0, 0);
});
negativebtn.addEventListener("click", () => {
let imageDataCopy = context.createImageData(600, 300);
for (i = 0; i < imageData.data.length; i += 4) {
imageDataCopy.data[i] = 255 - imageData.data[i]; //RED
imageDataCopy.data[i + 1] = 255 - imageData.data[i + 1]; //GREEN
imageDataCopy.data[i + 2] = 255 - imageData.data[i + 2]; //BLUE
imageDataCopy.data[i + 3] = imageData.data[i + 3]; //Alpha
}
context.putImageData(imageDataCopy, 0, 0);
});
mirrorbtn.addEventListener("click", () => {
let imageDataCopy = context.createImageData(600, 300);
for (let i = 0; i < imageData.data.length; i++) {
imageDataCopy.data[i] = imageData.data[i];
}
for (var i = 0; i < imageData.height; i++) {
for (var j = 0; j < imageData.width / 2; j++) {
var off = (i * imageData.width + j) * 4;
var dstOff = (i * imageData.width + (imageData.width - j - 1)) * 4;
imageDataCopy.data[dstOff] = imageData.data[off];
imageDataCopy.data[dstOff + 1] = imageData.data[off + 1];
imageDataCopy.data[dstOff + 2] = imageData.data[off + 2];
imageDataCopy.data[dstOff + 3] = imageData.data[off + 3];
}
}
context.putImageData(imageDataCopy, 0, 0);
});
sketchbtn.addEventListener("click", () => {
let imageDataCopy = context.createImageData(600, 300);
for (i = 0; i < imageData.data.length; i += 4) {
const threshold = 150;
const affect =
(imageData.data[i] + imageData.data[i] + imageData.data[i]) / 3 >=
threshold
? 255
: 0;
imageDataCopy.data[i] = affect; //RED
imageDataCopy.data[i + 1] = affect; //GREEN
imageDataCopy.data[i + 2] = affect; //BLUE
imageDataCopy.data[i + 3] = imageData.data[i + 3]; //Alpha
}
context.putImageData(imageDataCopy, 0, 0);
});
brightform.addEventListener("submit", (e) => {
let imageDataCopy = context.createImageData(600, 300);
e.preventDefault();
const brightness = document.getElementById("brightness").value;
for (i = 0; i < imageData.data.length; i += 4) {
imageDataCopy.data[i] = imageData.data[i] + (brightness - 0); //RED
imageDataCopy.data[i + 1] = imageData.data[i + 1] + (brightness - 0); //GREEN
imageDataCopy.data[i + 2] = imageData.data[i + 2] + (brightness - 0); //BLUE
imageDataCopy.data[i + 3] = imageData.data[i + 3]; //Alpha
}
context.putImageData(imageDataCopy, 0, 0);
});
};
};