Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda