Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Open Image in New Window, Scaled relative to Monitor Size While Retaining Original Aspect Ratio

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:

  • This is for a personal project, not a customer or any other users so it doesn't matter if it is annoying
  • When I say the images are different aspect ratios I mean VERY different. Some are 3:1, 16:9, 1:1, 1:2, etc.
  • There are 1,500 images so I am not keen on brute force sizing each one

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As I couldn't get it work here, I mage a JSFiddle for you:

Sample

Here my idea. Get all the images on the page in imagesArr. Then for each of them make an object Image() with its features. Add an event listener to each image to open the openWindow function.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!