Create utils.js
Browse files
utils.js
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
function generateRandomColor(){
|
2 |
+
let maxVal = 0xFFFFFF; // 16777215
|
3 |
+
let randomNumber = Math.random() * maxVal;
|
4 |
+
randomNumber = Math.floor(randomNumber);
|
5 |
+
randomNumber = randomNumber.toString(16);
|
6 |
+
let randColor = randomNumber.padStart(6, 0);
|
7 |
+
return `#${randColor.toUpperCase()}`
|
8 |
+
}
|
9 |
+
|
10 |
+
|
11 |
+
function getScaledCoordinates(box, img) {
|
12 |
+
// Get the original dimensions of the image
|
13 |
+
const originalWidth = img.naturalWidth;
|
14 |
+
const originalHeight = img.naturalHeight;
|
15 |
+
|
16 |
+
// Get the scaled dimensions of the image
|
17 |
+
const scaledWidth = img.offsetWidth; // The image should be exactly 400px wide
|
18 |
+
const scaledHeight = img.offsetHeight;
|
19 |
+
|
20 |
+
// Calculate the scaling factor
|
21 |
+
const scale = scaledWidth / originalWidth;
|
22 |
+
|
23 |
+
// Scale the box boundaries according to the scaled image
|
24 |
+
let ymax = box.ymax * scale;
|
25 |
+
let xmax = box.xmax * scale;
|
26 |
+
let ymin = box.ymin * scale;
|
27 |
+
let xmin = box.xmin * scale;
|
28 |
+
|
29 |
+
// Make sure the minimum values are are at least 0 and the maximum values
|
30 |
+
// are less than the width/height of the image
|
31 |
+
ymin = Math.max(0, box.ymin * scale)
|
32 |
+
xmin = Math.max(0, box.xmin * scale)
|
33 |
+
ymax = Math.min(scaledHeight, ymax)
|
34 |
+
xmax = Math.min(scaledWidth, xmax)
|
35 |
+
|
36 |
+
return {
|
37 |
+
ymin,
|
38 |
+
xmin,
|
39 |
+
ymax,
|
40 |
+
xmax
|
41 |
+
}
|
42 |
+
}
|
43 |
+
|
44 |
+
|
45 |
+
function removeElements(className) {
|
46 |
+
const HTMLcollection = document.getElementsByClassName(className)
|
47 |
+
Array.from(HTMLcollection).forEach(element => element.remove())
|
48 |
+
}
|
49 |
+
|
50 |
+
export { generateRandomColor, removeElements, getScaledCoordinates }
|