Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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 在“调整大小”的较低值上设置一个限制,同时借助动态js保持纵横比_Javascript_Jquery_Html_Kineticjs_Image Resizing - Fatal编程技术网

Javascript 在“调整大小”的较低值上设置一个限制,同时借助动态js保持纵横比

Javascript 在“调整大小”的较低值上设置一个限制,同时借助动态js保持纵横比,javascript,jquery,html,kineticjs,image-resizing,Javascript,Jquery,Html,Kineticjs,Image Resizing,我已经在这上面呆了很长一段时间了,我正在尝试允许对图像进行拖动和调整大小,我在这个链接上找到了一些示例: 但我还想保持图像的纵横比,我想给图片的大小设置一个下限,以便调整图片的大小(缩小),也就是说,我不希望图片的大小超过一个固定的最小值。我可以通过做一些更改来保持纵横比,但我无法在不改变纵横比超出限制的情况下设置调整大小的下限。到目前为止,我已经做到了这一点 我可以更新这些行之间的链接提供的示例中提到的代码: newHeight = bottomLeft.getY() - topL

我已经在这上面呆了很长一段时间了,我正在尝试允许对图像进行拖动和调整大小,我在这个链接上找到了一些示例:

但我还想保持图像的纵横比,我想给图片的大小设置一个下限,以便调整图片的大小(缩小),也就是说,我不希望图片的大小超过一个固定的最小值。我可以通过做一些更改来保持纵横比,但我无法在不改变纵横比超出限制的情况下设置调整大小的下限。到目前为止,我已经做到了这一点

我可以更新这些行之间的链接提供的示例中提到的代码:

    newHeight = bottomLeft.getY() - topLeft.getY();
    newWidth = image.getWidth() * newHeight / image.getHeight();
    newHeight = bottomLeft.getY() - topLeft.getY();
    if(newHeight < 50){
        newHeight = 50;
    };

    newWidth = image.getWidth() * newHeight / image.getHeight();
    if(newWidth < 50){
        newWidth = 50;
    };
通过以下方式:

    newHeight = bottomLeft.getY() - topLeft.getY();
    newWidth = image.getWidth() * newHeight / image.getHeight();
    newHeight = bottomLeft.getY() - topLeft.getY();
    if(newHeight < 50){
        newHeight = 50;
    };

    newWidth = image.getWidth() * newHeight / image.getHeight();
    if(newWidth < 50){
        newWidth = 50;
    };
newHeight=bottomLeft.getY()-topLeft.getY();
如果(新高度<50){
新高度=50;
};
newWidth=image.getWidth()*newHeight/image.getHeight();
如果(新宽度<50){
新宽度=50;
};

但是我无法保持纵横比。请说明您的回答。

首先,您需要计算
活动手柄的原始位置(即如果用户没有拖动它,它会在哪里)。如果我们知道大小太小,需要将
activeHandle
移回,这一点在以后会很重要

第二,您可以使用如下函数来确定适当的比例因子,以保持纵横比(有关详细信息,请阅读关于如何操作的文章):

第三,调整大小后,需要检查新大小是否太小。如果是,则必须将
activeHandle
移回我们在步骤1中计算的位置,然后调整所有内容的大小

我在JS Bin中编辑了您的代码-您可以在以下位置看到工作示例:

这里是:

JS:

函数更新(组、activeHandle){
var topLeft=group.get(“.topLeft”)[0],
topRight=group.get(“.topRight”)[0],
bottomRight=group.get(“.bottomRight”)[0],
bottomLeft=group.get(“.bottomLeft”)[0],
image=group.get(“.image”)[0],
activeHandleName=activeHandle.getName(),
新宽度,
新高度,
最小宽度=32,
最小高度=32,
奥德克斯,
古老的,
imageX,
影像学;
//在拖动过程中更新控制柄的位置。
//需要这样做,以便尺寸计算可以使用
//控制手柄位置以确定新的宽度/高度。
开关(activeHandleName){
案例“左上”:
oldY=topRight.getY();
oldX=bottomLeft.getX();
setY(activeHandle.getY());
setX(activeHandle.getX());
打破
案例“topRight”:
oldY=topLeft.getY();
oldX=bottomRight.getX();
setY(activeHandle.getY());
setX(activeHandle.getX());
打破
案例“右下角”:
oldY=bottomLeft.getY();
oldX=topRight.getX();
setY(activeHandle.getY());
setX(activeHandle.getX());
打破
案例“左下角”:
oldY=bottomRight.getY();
oldX=topLeft.getX();
setY(activeHandle.getY());
setX(activeHandle.getX());
打破
}
//计算新尺寸。高度只是手柄的尺寸。
//宽度根据高度变化的程度增加/减少。
newHeight=bottomLeft.getY()-topLeft.getY();
newWidth=image.getWidth()*newHeight/image.getHeight();
//太小:将活动手柄移回原来的位置
if(新宽度<最小宽度| |新高度<最小高度){
activeHandle.setY(旧的);
activeHandle.setX(oldX);
开关(activeHandleName){
案例“左上”:
右上角。塞蒂(老调);
左下角.setX(oldX);
打破
案例“topRight”:
左上角。塞蒂(旧);
右下角.setX(oldX);
打破
案例“右下角”:
左下角。塞蒂(老调);
右上角.setX(oldX);
打破
案例“左下角”:
右下角。塞蒂(老调);
左上角.setX(oldX);
打破
}
}
newHeight=bottomLeft.getY()-topLeft.getY();
//注释下一行并取消注释下一行,以允许自由调整图像大小,因为下一行保留比例和纵横比
newWidth=image.getWidth()*newHeight/image.getHeight();//用于限制大小调整
//newWidth=topRight.getX()-topLeft.getX();//用于自由调整大小
//移动图像以调整新尺寸。
//位置计算将根据其定位的位置而变化。
//即,在右侧拖动时,它将定位到左上角,
//在左侧拖动时,它将定位到右上角。
如果(activeHandleName==“右上角”| | activeHandleName===“右下角”){
setPosition(topLeft.getX(),topLeft.getY());
}else if(activeHandleName==“左上”| | activeHandleName===“左下”){
setPosition(topRight.getX()-newWidth,topRight.getY());
}
imageX=image.getX();
imageY=image.getY();
//更新控制柄位置以反映新的图像尺寸
左上。设置位置(imageX,imageY);
右上角。设置位置(imageX+新宽度,imageY);
右下角。设置位置(imageX+newWidth,imageY+newHeight);