Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda