Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将图像缩放和居中到正方形尺寸?_Javascript_Image_Canvas_Html5 Canvas_Resize - Fatal编程技术网

Javascript 如何将图像缩放和居中到正方形尺寸?

Javascript 如何将图像缩放和居中到正方形尺寸?,javascript,image,canvas,html5-canvas,resize,Javascript,Image,Canvas,Html5 Canvas,Resize,这些问题类似,但没有帮助:、和 目标是在正方形画布上绘制图像,同时保留原始纵横比,如果原始纵横比不是正方形,则将图像居中 例如,以附带的1262x2688图像为例。下面的代码将其大小调整为100x100,但扭曲了纵横比 代码应:(1)缩放图像以适合100x100画布;(2) 保持纵横比;以及(3)在画布内垂直和水平居中图像 // Create canvas element. var canvas = $(document.createElement("canvas"));

这些问题类似,但没有帮助:、和

目标是在正方形画布上绘制图像,同时保留原始纵横比,如果原始纵横比不是正方形,则将图像居中

例如,以附带的1262x2688图像为例。下面的代码将其大小调整为100x100,但扭曲了纵横比

代码应:(1)缩放图像以适合100x100画布;(2) 保持纵横比;以及(3)在画布内垂直和水平居中图像

    // Create canvas element.
    var canvas = $(document.createElement("canvas"));

    // Get canvas context.
    var context = canvas[0].getContext("2d");

    // Set canvas size.
    canvas[0].width = 100;
    canvas[0].height = 100;

    // Write image to canvas.
    context.drawImage(image, 0, 0, newWidth, newHeight);
形象


假设我的计算方法正确,下面的方法可能有效

ratio = image.width/image.height;
if (image.width > image.height) {
    output.height = ( 100 / ratio ) 
    output.width = 100
    output.x = 0
    output.y = (100 - output.height) / 2
} else {
    output.height = 100
    output.width = 100 * ratio
    output.x = (100 - output.width) / 2
    output.y = 0
}

要在保留外观的同时将图像适配到画布,请使用以下命令

const w = image.naturalWidth;
const h = image.naturalHeight;

// Get the min scale to fit the image to the canvas
const scale = Math.min(canvas.width / w, canvas.height / h);

// Set the transform to scale the image, and center to the canvas
ctx.setTransform(scale, 0, 0, scale, canvas.width / 2, canvas.height / 2);

// draw the image offset by half its width and height to center and fit
ctx.drawImage(image, -w / 2, -h / 2, w, h);

// to reset the transform
// ctx.setTransform(1,0,0,1,0,0);

下面是我们使用的代码:

    // Create canvas element.
    var canvas = $(document.createElement("canvas"));

    // Get canvas context.
    var context = canvas[0].getContext("2d");

    // Set canvas size.
    canvas[0].width = canvasWidth;
    canvas[0].height = canvasHeight;

    // Set image size, must use image.naturalWidth and image.naturalHeight -- not image.width and image.height.
    const imageWidth = image.naturalWidth;
    const imageHeight = image.naturalHeight;

    // Set scale to fit image to canvas, 
    const scale = Math.min(canvasWidth/imageWidth, canvasHeight/imageHeight);

    // Set new image dimensions.
    const scaledWidth = imageWidth * scale;
    const scaledHeight = imageHeight * scale;

    // Draw image in center of canvas.
    context.drawImage(image, (canvasWidth - scaledWidth)/2, (canvasHeight - scaledHeight)/2, scaledWidth, scaledHeight);

如果您想保留整个图像而不将其切断,这是不可能的。为了使图像不失真,你必须保持纵横比,但是如果可以将图像的一部分切掉,那么这是可能的。特拉维斯是正确的。在不扭曲图像的情况下,无法更改图像的纵横比。您需要确定哪个更大(宽度或高度),然后使用最大的一个填充画布并使其居中,在边框之间留出空间,或者您需要部分剪切图像以使其呈方形。@icecub很抱歉造成混淆。应缩放图像以适合方形画布,同时保持纵横比。因此,图像不会变成正方形,但会保留其纵横比(使用两个比例因子中最小的一个),并且需要水平或垂直居中。感谢这段代码!我们最终使用了不同的代码,但您的代码有所帮助。:)@Crashalot如果使用
setTranform(xAxisXScale、xAxisYscale、yAxisXScale、yAxisYScale、canvasXOrigin、canvasOrigin)
则无需进行额外的计算来转换和缩放图像(如您在回答中所做的那样)。您可以在图像坐标中渲染到画布。我们最初使用您的版本,但当画布小于图像时,它不起作用。如果您更新您的答案,这将是伟大的奖励您的分数!再次感谢您的帮助。@Crashalot我不关心要点,只是觉得您使用更复杂的解决方案很奇怪。答案中的代码确实有效,我看不到任何拼写错误,如果它不适用于较小的画布,那么它也不适用于较大的画布。我只想感谢您保存了我的理智所剩的一点东西。