-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
178 lines (164 loc) · 6.13 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
onload = function () {
const editor = document.getElementById("editor");
const context = editor.getContext("2d");
const toolbar = document.getElementById("toolbar");
const tools = {
"upload" : function () {
const upload = document.createElement('input');
upload.type = "file";
upload.click();
upload.onchange = function() {
const img = new Image();
img.onload = () => {
editor.width = img.width;
editor.height = img.height;
context.drawImage(img, 0,0);
};
img.onerror = () => {
console.error("The provided file couldn't be loaded as an Image media");
};
img.src = URL.createObjectURL(this.files[0]);
};
},
"save" : function(){
const image = editor.toDataURL();
const link = document.createElement('a');
link.download = 'image.png';
link.href = image;
link.click();
},
"flipHor" : function(){
let cols = editor.width; // Width is number of columns
let rows = editor.height; // Height is number of rows
let image = getRGBArray(rows, cols);
for(let i=0;i<Math.floor(rows/2);i++){
for(let j=0;j<cols;j++){
let tmp = image[i][j];
image[i][j] = image[rows-1-i][j];
image[rows-1-i][j] = tmp;
}
}
setImageData(image, rows, cols);
},
"flipVert" : function(){
let cols = editor.width; // Width is number of columns
let rows = editor.height; // Height is number of rows
let image = getRGBArray(rows, cols);
for(let i=0;i<rows;i++){
for(let j=0;j<Math.floor(cols/2);j++){
let tmp = image[i][j];
image[i][j] = image[i][cols-1-j];
image[i][cols-1-j] = tmp;
}
}
setImageData(image, rows, cols);
},
"rotateL" : function () {
let cols = editor.width; // Width is number of columns
let rows = editor.height; // Height is number of rows
let image = getRGBArray(rows, cols);
let limage = [];
for(let i=cols-1;i>=0;i--){
let row = [];
for(let j=0;j<rows;j++){
row.push(image[j][i]);
}
limage.push(row);
}
setImageData(limage, cols, rows);
},
"rotateR" : function () {
let cols = editor.width; // Width is number of columns
let rows = editor.height; // Height is number of rows
let image = getRGBArray(rows, cols);
let rimage = [];
for(let i=0;i<cols;i++){
let row = [];
for(let j=rows-1;j>=0;j--){
row.push(image[j][i]);
}
rimage.push(row);
}
setImageData(rimage, cols, rows);
},
"resize" : function(){
let cols = editor.width; // Width is number of columns
let rows = editor.height; // Height is number of rows
let image = getRGBArray(rows, cols);
let inp = prompt('Current Width : '+cols + '\n' + 'Current Height : '+rows + '\n' + 'Give the new width and height in a space separated manner').split(' ');
if(inp.length!==2){
alert('Incorrect dimensions in input');
return;
}
let ncols = parseInt(inp[0]);
let nrows = parseInt(inp[1]);
if(isNaN(ncols) || isNaN(nrows)){
alert('Input is not a proper number');
return;
}
let hratio = rows/nrows;
let wratio = cols/ncols;
let nimage = [];
for(let i=0;i<nrows;i++){
let row = [];
for(let j=0;j<ncols;j++){
row.push(image[Math.floor(i*hratio)][Math.floor(j*wratio)]);
}
nimage.push(row);
}
setImageData(nimage, nrows, ncols);
},
"greyscale" : function(){
let cols = editor.width; // Width is number of columns
let rows = editor.height; // Height is number of rows
let image = getRGBArray(rows, cols);
for(let i=0;i<rows;i++){
for(let j=0;j<cols;j++){
let pixel = image[i][j];
let shade = Math.floor(0.3*pixel[0]+0.59*pixel[1]+0.11*pixel[2]);
image[i][j][0] = image[i][j][1] = image[i][j][2] = shade;
}
}
setImageData(image, rows, cols);
}
};
for(let button of toolbar.children){
if(button.nodeName==="BUTTON") {
button.onclick = function (event) {
event.preventDefault();
tools[this.id].call(this);
}
}
}
function setImageData(data, rows, cols) {
const Image = Array.from({ length: rows*cols*4 });
for(let i = 0;i < rows; i++) {
for (let j = 0; j < cols; j++) {
for (let k = 0; k < 4; k++) {
Image[( i*cols + j ) * 4 + k ] = data[i][j][k];
}
}
}
const idata = context.createImageData(cols, rows);
idata.data.set(Image);
editor.width = cols;
editor.height = rows;
context.putImageData(idata, 0, 0);
}
function getRGBArray(rows, cols) {
let data = context.getImageData(0, 0, cols, rows).data;
const RGBImage = [];
for(let i=0;i<rows;i++){
let row = [];
for(let j=0;j<cols;j++){
let pixel = [];
for(let k=0;k<4;k++){
pixel.push( data[ ( i*cols + j ) * 4 + k ] );
}
row.push(pixel);
}
RGBImage.push(row);
}
return RGBImage;
}
};