I'm trying to make the following:
Fit a HUGE image within a small div element keeping aspect ratio.
But without using the CSS trick like max-width. Manually settings the width/height to the correct values.
This is the easiest way of me explaining what I'm trying to do.
I got the following values:
Original width/height of the image.
Target div's width/height.
I tried the following example to make it fit within the parent element.
if(ratio < 1) {
setWidth = Math.round((targetH * ratio));
} else {
setHeight = Math.round((targetW / ratio));
}
Which looks on the aspect ratio and resize it using that.
Works fine as long as aspect ratio is under 1. If it goes above it height starts to be all messed up. Also seems to jump when you got from 0.99 to 1 with quite a amount of extra height.