Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何使用drawImage在画布中平滑缩放大图像?_Html_Canvas - Fatal编程技术网

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

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_

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

注意:图像在放大时可能会变得模糊,如果放大过多,图像可能会变得颗粒状。如果你有一些需要保持清晰的文本,最好不要进行缩放

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;
}
/*计算纵横比*/
var imageAspectRatio=imgWidth/imgHeight;
/*给出图像所需的宽度和高度*/
/*例如,你需要这样做*/
var newWidth=1024;
var newHeight=768;
如果(imgWidth=newHeight){
var newHeight=newWidth/imageAspectRatio;
}

如果您这样做,您不会丢失纵横比,因此缩放看起来很好

谢谢,但请参见我的编辑,我的方式与您刚才提供的相同,它不起作用:(