圖片等比縮放

function DrawImage(ImgD,FitWidth,FitHeight){
     var image=new Image();

     image.src=ImgD.src;

     /*

     image.width 圖片原寬

     image.height 圖片原高

     FiWidth  顯示圖片區域寬

     FitHeight 顯示圖片區域高

     ImgD.width 縮放後圖片的寬

     ImgD.height 縮放後圖片的高

    */

     if(image.width>0 && image.height>0){
         if(image.width/image.height>= FitWidth/FitHeight){//圖片原 寬>=高
             if(image.width>FitWidth){
                 ImgD.width=FitWidth;
                 ImgD.height=(image.height*FitWidth)/image.width;
             }else{
                 ImgD.width=image.width; 
                ImgD.height=image.height;
             }
         } else{
             if(image.height>FitHeight){
                 ImgD.height=FitHeight;
                 ImgD.width=(image.width*FitHeight)/image.height;
             }else{
                 ImgD.width=image.width; 
                ImgD.height=image.height;
             } 
        }
     }
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章