首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用drawImage在画布上平滑地缩放大图像?

如何用drawImage在画布上平滑地缩放大图像?
EN

Stack Overflow用户
提问于 2012-10-09 03:14:00
回答 1查看 696关注 0票数 0

当我使用drawImage缩放一个大图像(比如5M)时,结果看起来很糟糕,有什么更简单的方法可以用反混叠来破坏图像呢?MDN提到了这一点:

注意:图片在缩放时会变得模糊,如果缩小得太多,图像会变得模糊。如果你有一些文本需要保持清晰,那么缩放可能是最好的选择。

EIDT:我想将图像缩放到90x90,我的代码如下所示:

代码语言:javascript
复制
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);

“阿凡达”是要缩放的图像。

EN

回答 1

Stack Overflow用户

发布于 2012-10-09 05:04:25

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

代码语言:javascript
复制
/* 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;
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12792342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档