File size: 1,565 Bytes
a8eab52 |
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 |
function generateRandomColor(){
let maxVal = 0xFFFFFF; // 16777215
let randomNumber = Math.random() * maxVal;
randomNumber = Math.floor(randomNumber);
randomNumber = randomNumber.toString(16);
let randColor = randomNumber.padStart(6, 0);
return `#${randColor.toUpperCase()}`
}
function getScaledCoordinates(box, img) {
// Get the original dimensions of the image
const originalWidth = img.naturalWidth;
const originalHeight = img.naturalHeight;
// Get the scaled dimensions of the image
const scaledWidth = img.offsetWidth; // The image should be exactly 400px wide
const scaledHeight = img.offsetHeight;
// Calculate the scaling factor
const scale = scaledWidth / originalWidth;
// Scale the box boundaries according to the scaled image
let ymax = box.ymax * scale;
let xmax = box.xmax * scale;
let ymin = box.ymin * scale;
let xmin = box.xmin * scale;
// Make sure the minimum values are are at least 0 and the maximum values
// are less than the width/height of the image
ymin = Math.max(0, box.ymin * scale)
xmin = Math.max(0, box.xmin * scale)
ymax = Math.min(scaledHeight, ymax)
xmax = Math.min(scaledWidth, xmax)
return {
ymin,
xmin,
ymax,
xmax
}
}
function removeElements(className) {
const HTMLcollection = document.getElementsByClassName(className)
Array.from(HTMLcollection).forEach(element => element.remove())
}
export { generateRandomColor, removeElements, getScaledCoordinates } |