I am working on a project with very specific criteria. I have a button that will open a random image from an array when clicked, into a new window located randomly on the screen, and size the window/image height relative to the size of the monitor while maintaining the original image's aspect ratio.
The issue is all of the images in the array are different resolutions and aspect ratios, and I would like the window they open into to fit nicely to the image.
Here is the code I am working with so far:
function openWin() {
var myWindow;
var rand = Math.random();
var l = screen.width * Math.floor(Math.random());
var t = screen.height * Math.floor(Math.random());
var w = screen.width / 5;
var h = screen.height / 3;
var arr = [
"images/testpic.png",
"images/testpic2.png",
"images/testpic3.png",
"images/testpic4.png",
"images/testpic5.png"];
var value = arr[Math.floor(Math.random() * arr.length)];
myWindow = window.open(value, "", "left= "+ l +", top= "+ t +", width= " + w +", height=" + h +" ");
}
At this point I can open the random image from an array, place it randomly on screen, with the height of the new window scaled to the height of the monitor. BUT the width of the new window is also scaled to the width of the monitor and not the width of the image. This either gives black bars or cuts off parts of the image.
I have been trying to get an answer for a while and will preempt a couple of things:
I realize this is very niche but any help is appreciated and sorry if my code is a mess
EDIT: Here is a gif visualizing the kind of functionality I am looking for. Note: Some of the images are HD but resized relative to the screen size See Here