HTML5画布图像缩放到更小的尺寸并恢复原始状态

时间:2012-12-07 19:56:09

标签: javascript html5 canvas image-resizing

我正试图解决的一种情况。我正在将图像加载到画布上,然后对其进行一些编辑(调整大小,裁剪等)并将结果图像保存到文件夹中。现在,如果原始图像大于画布尺寸,则裁剪结果图像。因此,我试图将图像缩放到适合画布的较小尺寸,应用效果,然后将其缩放回原始尺寸并保存。有没有办法做到这一点?

我的JS伪代码有点像这样:

//load the image using drawimage
//compare image dimensions with canvas dimensions, scale down the image if large
//apply the effects
(I have done till here)

//scale it back to original size
//save

如何使用canvas scale方法来回缩放它?我假设我必须计算宽度比,以便按比例缩小和缩小:

scale_down_width = image_orig_width / canvas_width
scale_down_height = image_orig_height / canvas_height

//For scaling down
scale(1*scale_down_width, 1*scale_down_height)

//For scaling up
scale(1/scale_down_width, 1/scale_down_height)

我这样做是对的吗? 缩小工作正常,但放大不起作用,我该怎么做呢?

0 个答案:

没有答案