2012-10-09 110 views
0

當我使用drawImage方法來scalle一個大圖像(如5M),結果看起來很可怕,是有可以scalling圖像抗鋸齒任何更簡單的方法? MDN提到這一點:如何用drawImage在畫布上平滑地縮放大圖像?

注:擴大或呈顆粒狀,如果他們按比例縮小太多,當圖像可以變得非常模糊。如果你有一些需要保持清晰的文字,縮放可能是最好的。

EIDT:我想一個圖像擴展到90x90,和我的代碼是這樣的:

that.avatar_height >= that.avatar_width ? 
that.context.drawImage($(this)[0], 86, 2, 90, that.avatar_height*90/that.avatar_width) : 
that.context.drawImage($(this)[0], 86, 2, that.avatar_width*90/that.avatar_height, 90); 

頭像是要縮放圖像。

回答

1

儘管縮放圖像首先獲取上傳的圖像縱橫比。

/* Calculating aspect ratio */ 
var imageAspectRatio = imgWidth/imgHeight; 

/* Give the needed width and height of the image*/ 
/*For example you need like this */ 
var newWidth = 1024; 
var newHeight = 768; 

if(imgWidth <= 1024) { 
    var newWidth = newHeight * imageAspectRatio; 
} else if(imgHeight <= 768) { 
    var newHeight = newWidth/imageAspectRatio; 
} else if(imgWidth >= newWidth) { 
    var newWidth = newHeight * imageAspectRatio; 
} else if(imgHeight >= newHeight) { 
    var newHeight = newWidth/imageAspectRatio; 
} 

如果你這樣做,你可以這麼比例看起來不錯,不虧你寬高比

+0

謝謝你,但看到我的編輯,像你剛纔提供我的方式是一樣的,它不工作: ( – jiguang

+0

我使用上述方式完成了它爲我工作。 – vvr02