简体   繁体   English

使用 HTML5 画布调整图像大小

[英]Resizing image with HTML5 canvas

I am trying to size an image with canvas.我正在尝试用画布调整图像的大小。 My goal is to have an image of dimensions A x B sized to M x N without changing proportions - like CSS contain.我的目标是在不改变比例的情况下将尺寸为 A x B 的图像调整为 M x N - 就像 CSS 包含一样。 For example, if the source image is 1000x1000 and the destination is 400x300, it should cut off a piece 100 pixels toll at the bottom, and that should correspond to 250 pixels in the source image.例如,如果源图像是1000x1000,目标是400x300,它应该在底部切掉一块100像素的toll,这应该对应源图像中的250像素。

My code is below:我的代码如下:

const canvas = document.createElement('canvas');
const img = new Image();
img.src = promotedImage;
const FINAL_WIDTH = 400;
const FINAL_HEIGHT = 250;
const width = img.width;
const height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, width, height, 0, 0, FINAL_WIDTH, FINAL_HEIGHT);
const finalImage = b64toFile(canvas.toDataURL("image/jpg"));

This is not working, like I want to, though.但是,这不起作用,就像我想要的那样。 I am obviously using drawImage incorrectly.我显然错误地使用了 drawImage。 For me, if copies source to destination without sizing.对我来说,如果将源复制到目标而不调整大小。

Is this because I need to size (change dimensions) for canvas prior to drawing?这是因为我需要在绘图之前调整画布的尺寸(更改尺寸)吗? Please advise.请指教。

I have also tried something like Mozilla image upload .我也尝试过类似Mozilla image upload 的东西。 It does even scale the image, but it does not crop.它甚至可以缩放图像,但不会裁剪。 Plus, it sizes a source square to the smaller target side, instead of clipping it.另外,它将源方块调整为较小的目标侧,而不是剪裁它。

Setting an image source is asynchronous, can be very fast, but often not fast enough to keep up with still-running code.设置图像源是异步的,可以非常快,但通常不足以跟上仍在运行的代码。 Generally, to make them work reliably, you set an onload handler first and then set src .通常,为了使它们可靠地工作,您首先设置一个onload处理程序,然后设置src The canvas element defaults to 300x150 so would also need to be sized.画布元素默认为 300x150,因此也需要调整大小。 (Canvas obeys CORS. .crossOrigin = '' sets us as anonymous and imgur has a permissive CORS policy. We wouldn't be able to convert the canvas to an image while using a third-party image in this snippet otherwise.) (画布遵守.crossOrigin = ''将我们设置为匿名,而 imgur 有一个宽松的 CORS 策略。否则,在此代码段中使用第三方图像时,我们将无法将画布转换为图像。)

 const MAX_WIDTH = 400; const MAX_HEIGHT = 300; const img = new Image(); img.crossOrigin = ''; img.onload = () => { const wRatio = MAX_WIDTH / img.width; const hRatio = MAX_HEIGHT / img.height; var width, height; if(wRatio > hRatio) { width = MAX_WIDTH; height = wRatio * img.height; } else { width = hRatio * img.width; height = MAX_HEIGHT; } const canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0, width, height); //const finalImage = b64toFile(canvas.toDataURL("image/jpg")); const imgElement = document.createElement('img'); imgElement.src = canvas.toDataURL('image/jpg'); document.body.appendChild(imgElement); }; img.src = 'https://i.imgur.com/TMeawxt.jpeg';
 img { border: 1px solid red; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM