2012-10-09 103 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