-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
90 lines (75 loc) · 2.18 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
const imgElement = [];
let currentDegrees = [];
let inputElement = document.getElementById("fileInput");
let imgCount = 0;
let selectedImage;
let selectedImageNumber = 0;
inputElement.addEventListener(
"change",
(e) => {
newImgCreation();
currentDegrees.push(0);
imgElement.push(document.getElementById(`imageSrc-${imgCount}`));
imgElement[imgCount].src = URL.createObjectURL(e.target.files[0]);
imgElement[imgCount].onload = () => {
newCanvasCreation();
let mat = cv.imread(imgElement[imgCount]);
cv.imshow(`canvas-${imgCount}`, mat);
imgCount++;
mat.delete();
};
},
false
);
const rotateImg = (degrees, direction) => {
if (selectedImage == null) {
alert("No image selected");
return;
}
let src = cv.imread(imgElement[selectedImageNumber]);
let dst = new cv.Mat();
let dsize = new cv.Size(src.rows, src.cols);
let center = new cv.Point(src.cols / 2, src.rows / 2);
if (direction === "left") currentDegrees[selectedImageNumber] += degrees;
else currentDegrees[selectedImageNumber] -= degrees;
let M = cv.getRotationMatrix2D(
center,
currentDegrees[selectedImageNumber],
1
);
cv.warpAffine(
src,
dst,
M,
dsize,
cv.INTER_LINEAR,
cv.BORDER_CONSTANT,
new cv.Scalar()
);
cv.imshow(`${selectedImage}`, dst);
src.delete();
dst.delete();
M.delete();
};
const newImgCreation = () => {
const newImg = document.createElement("img");
newImg.id = `imageSrc-${imgCount}`;
newImg.style.display = "none";
document.getElementById("container").appendChild(newImg);
};
const newCanvasCreation = () => {
let randomX = Math.floor(Math.random() * 380);
let randomY = Math.floor(Math.random() * 380);
const newCanvas = document.createElement("canvas");
newCanvas.style.position = "absolute";
newCanvas.style.width = "50px";
newCanvas.style.height = "50px";
newCanvas.style.top = `${randomY}px`;
newCanvas.style.left = `${randomX}px`;
newCanvas.id = `canvas-${imgCount}`;
newCanvas.addEventListener("click", () => {
selectedImage = newCanvas.id;
selectedImageNumber = selectedImage.slice(7);
});
document.getElementById("container").appendChild(newCanvas);
};