调整HTML 5画布中的图像大小我试图使用javascript和Canvas元素在客户端创建缩略图,但是当我缩小图像时,它看起来很糟糕。看起来就像它在Photoshop中被缩小了,重新采样设置为“最近的邻居”,而不是Bicubic。我知道有可能让这个看起来正确,因为本站用画布也能做得很好。我试过使用“[Source]”链接中显示的相同的代码,但是看起来仍然很糟糕。是不是有什么东西我错过了,有些设置需要设置或其他什么?编辑:我在试着调整一个jpg的大小。我试过在链接的站点和Photoshop上调整相同的jpg大小,当缩小大小时,它看起来很好。以下是相关代码:reader.onloadend = function(e){
var img = new Image();
var ctx = canvas.getContext("2d");
var canvasCopy = document.createElement("canvas");
var copyContext = canvasCopy.getContext("2d");
img.onload = function()
{
var ratio = 1;
if(img.width > maxWidth)
ratio = maxWidth / img.width;
else if(img.height > maxHeight)
ratio = maxHeight / img.height;
canvasCopy.width = img.width;
canvasCopy.height = img.height;
copyContext.drawImage(img, 0, 0);
canvas.width = img.width * ratio;
canvas.height = img.height * ratio;
ctx.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvas.width, canvas.height);
};
img.src = reader.result;}
添加回答
举报
0/150
提交
取消