如何使用drawImage在画布中平滑地缩放大图像



当我使用 drawImage 缩放大图像(如 5M)时,结果看起来很糟糕,有没有更简单的方法可以使用抗锯齿来调用图像?MDN已经提到过这一点:

【注】放大时图像可能会变得模糊,如果放大太多,图像可能会变得颗粒状。如果您有一些需要保持清晰易读的文本,则最好不要进行缩放。

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);

头像是要缩放的图像。

在缩放图像时,首先获取上传的图像纵横比。

/* 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;
}

如果你这样做,你不能失去你的纵横比,所以缩放看起来不错

最新更新