JavaScript method to limit the size of an image display

  • 2020-05-12 02:14:53
  • OfStack

This example shows how JavaScript can limit the size of a picture display. Share with you for your reference. The specific implementation method is as follows:


/**
 *  Limit image display size.
 * 
 * @param thisobj  Image components 
 * @param limitW  Limit width 
 * @param limitH  Limit height 
 */
function imageResize(thisobj, limitW, limitH) {
  var newW;
  var newH;
  if (thisobj.width > limitW) {
    newW = limitW;
    newH = parseInt(thisobj.height * newW / thisobj.width);
 //  Scale to the width 
    if (newH > limitH) {
      newH = limitH;
      newW = parseInt(thisobj.width * newH / thisobj.height);
    }
    thisobj.width = newW;
    thisobj.height = newH;
  } else if (thisobj.height > limitH) {
    newH = limitH;
    newW = parseInt(thisobj.width * newH / thisobj.height);
    thisobj.width = newW;
    thisobj.height = newH;
  }
}

I hope this article has been helpful to your javascript programming.


Related articles: