📜  如何停止调整图像大小的画布大小 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:47.989000             🧑  作者: Mango

代码示例1
//You just need to calculate the image aspect ratio:
var f = image.height / image.width;
var newHeight = canvas.width * f;
//And then draw using the recalculated height of image for destination:
ctx.drawImage(image, 0, 0, image.width, image.height, // source size
                     0, 0, canvas.width, newHeight);  // destination size
//Canvas will do the clipping for you.
//If you want to lets say center the destination vertical position you can do:
var destY = (canvas.height - image.height) / 2;
ctx.drawImage(image, 0, 0, image.width, image.height,    // source size
                     0, destY, canvas.width, newHeight); // destination size