formula to autocenter and scale any sized image in sketch window
in
Programming Questions
•
2 years ago
With several projects I wish to display several images that have different width's and height's in the center of the sketch window. Many of the images are larger than the screen resolution (at times in the sketch parts parts of may be displayed at higher resolutions though other functions.) Sometimes the widths are wider than the screen resolution; sometimes the heights are; sometimes both are.
I need to center and scale different sized images in the sketch. Since all we are dealing with is rescaling images according to what the sketch height and width height are, it MUST be possible to construct a forumula that would always display an image in the center of the sketch, scaled to fit inside the screen, when it wider or higher than the sketch window. Like it would scale by whatever the largest dimension is.
I've been picking my brain writing all sorts of different variations of ideas and haven't come up with a formula that scales the images correctly. E.G.:
<snip>
It seems to me that this would be a fairly common project requirement and that someone could share their autoimage center solution. Before spending another evennining trying to figure this one out, I thought it wouldn't hurt to ask.
I think by the time I come back to this question, I'll have worked out a solution but I'm still interested in how advanced coders would do it.
I need to center and scale different sized images in the sketch. Since all we are dealing with is rescaling images according to what the sketch height and width height are, it MUST be possible to construct a forumula that would always display an image in the center of the sketch, scaled to fit inside the screen, when it wider or higher than the sketch window. Like it would scale by whatever the largest dimension is.
I've been picking my brain writing all sorts of different variations of ideas and haven't come up with a formula that scales the images correctly. E.G.:
<snip>
- int srcratio = srcImg.height/srcImg.width;
- int screenratio = height/width;
- //if (srcImg.height >= height) {srcImg = srcImg.resize( ((width/srcImg.width)*width), ((height/srcImg.height)*height) );}
- //image(srcImg,width/2-srcImg.width/2,0,((width/srcImg.width)*width),((height/srcImg.height)*height));
- //image(srcImg,100,0,((width/srcImg.width)*srcImg.width), height );
- //if (wratio <= hratio) {
- image(srcImg,width/2-srcImg.width/2,0,srcImg.height*srcratio, srcImg.width );
- //} else {image(srcImg,width/2-srcImg.width/2,0,srcImg.width*hratio, srcImg.height*hratio );}
It seems to me that this would be a fairly common project requirement and that someone could share their autoimage center solution. Before spending another evennining trying to figure this one out, I thought it wouldn't hurt to ask.
I think by the time I come back to this question, I'll have worked out a solution but I'm still interested in how advanced coders would do it.
1