Javascript: Resize a rectangle to fit into the bounds of another rectangle with proper ratio
Blog Logo
Tom Raithel
on
read
Sometimes you have an image or something else that has to be resized to fit into the bounds of another object.
Following function returns an object that contains the new width and height:
Positionize image in the center of a frame
To positionize an image in the center of another element (#imageFrame in this case), you can use following code.
The code only works if the image has been loaded already. Also note, that IMG.naturalSize does not work in IE8. However, this is just an example code to demonstrate how to use the function above.