Html 如何使用drawImage在画布中平滑缩放大图像?
当我使用drawImage缩放大图像(如5M)时,结果看起来很糟糕,有没有更简单的方法可以使用抗锯齿缩放图像?他提到: 注意:图像在放大时可能会变得模糊,如果放大过多,图像可能会变得颗粒状。如果你有一些需要保持清晰的文本,最好不要进行缩放 EIDT:我想将图像缩放到90x90,我的代码如下:Html 如何使用drawImage在画布中平滑缩放大图像?,html,canvas,Html,Canvas,当我使用drawImage缩放大图像(如5M)时,结果看起来很糟糕,有没有更简单的方法可以使用抗锯齿缩放图像?他提到: 注意:图像在放大时可能会变得模糊,如果放大过多,图像可能会变得颗粒状。如果你有一些需要保持清晰的文本,最好不要进行缩放 EIDT:我想将图像缩放到90x90,我的代码如下: that.avatar_height >= that.avatar_width ? that.context.drawImage($(this)[0], 86, 2, 90, that.avatar_
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;
}
/*计算纵横比*/
var imageAspectRatio=imgWidth/imgHeight;
/*给出图像所需的宽度和高度*/
/*例如,你需要这样做*/
var newWidth=1024;
var newHeight=768;
如果(imgWidth=newHeight){
var newHeight=newWidth/imageAspectRatio;
}
如果您这样做,您不会丢失纵横比,因此缩放看起来很好谢谢,但请参见我的编辑,我的方式与您刚才提供的相同,它不起作用:(