Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.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_Zooming_Crop - Fatal编程技术网

放大图像的矩形选择(javascript)

放大图像的矩形选择(javascript),javascript,image,zooming,crop,Javascript,Image,Zooming,Crop,我为这个问题头痛了好几天 经过大量的搜索和各种比率算法的实验,我意识到我大脑中的数学部分缺失或死亡 我在做一个裁剪工具 在图像上选择矩形裁剪区域后,我想放大裁剪区域段 为此,我想放大图像和裁剪框的尺寸,直到裁剪框达到工作空间的最大高度或最大宽度 裁剪框的顶部、左侧、宽度和高度值应保持与缩放图像的相对比例和值 为此,我想我需要将它们乘以比例因子=(c) 这是一次可悲的尝试。但是比例因子是不存在的。因为我选择了85%的图像,所以图像的比例应该只增加15% 我尝试了各种方法来获得根据新尺寸缩放图像和

我为这个问题头痛了好几天

经过大量的搜索和各种比率算法的实验,我意识到我大脑中的数学部分缺失或死亡

我在做一个裁剪工具

在图像上选择矩形裁剪区域后,我想放大裁剪区域段

为此,我想放大图像和裁剪框的尺寸,直到裁剪框达到工作空间的最大高度或最大宽度

裁剪框的顶部、左侧、宽度和高度值应保持与缩放图像的相对比例和值

为此,我想我需要将它们乘以比例因子=(c

这是一次可悲的尝试。但是比例因子是不存在的。因为我选择了85%的图像,所以图像的比例应该只增加15%

我尝试了各种方法来获得根据新尺寸缩放图像和裁剪框所需的比例:

 const c = Math.min( croppingAreaNewWidth  / currentImageWidth, croppingAreaNewHeight / currentImageHeight);

// OR

 const c = Math.min( croppingAreaOldWidth  / croppingAreaNewWidth, croppingAreaOldHeight / croppingAreaNewHeight );
//或

我知道我走远了

我无法计算我必须按比例放大的高度/宽度来保持每件事的比例

计算比例因子的正确方法是什么,可以放大裁剪区域


任何提示都将不胜感激。

好的,我已经解决了。我会离开这个帖子,以防其他人感兴趣

放大图像和调整裁剪框大小的比率为:

// Using: Math.min( maxWidth / srcWidth, maxHeight / srcHeight )
const ratio = Math.min( workspaceWidth  / croppingAreaNewHeight, workspaceHeight / croppingAreaNewHeight);
我使用比率来变换图像:

 const newImageScaleValue = currentImageScaleValue * ratio;

 const newImageTranslateY =  (-1 * (croppingAreaNewOffsetTop * ratio )) + (currentImageTranslateY * ratio);

 const translateX =  (-1 * (croppingAreaNewOffsetLeft * ratio )) + (currentImageTranslateX * ratio);

 const newImageWidth = currentImageWidth * ratio;

 const newImageHeight = currentImageHeight * ratio;


工作区是裁剪空间周围相对位置的容器。

您的应用程序是开源的吗?它将是。。。一旦完成了。你可以等一会儿!:)还在等。@BoganJustice你可以接受你的帖子作为答案:)
 const newImageScaleValue = currentImageScaleValue * ratio;

 const newImageTranslateY =  (-1 * (croppingAreaNewOffsetTop * ratio )) + (currentImageTranslateY * ratio);

 const translateX =  (-1 * (croppingAreaNewOffsetLeft * ratio )) + (currentImageTranslateX * ratio);

 const newImageWidth = currentImageWidth * ratio;

 const newImageHeight = currentImageHeight * ratio;