Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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 通过精确裁剪图像删除while循环_Javascript_Image_Crop - Fatal编程技术网

Javascript 通过精确裁剪图像删除while循环

Javascript 通过精确裁剪图像删除while循环,javascript,image,crop,Javascript,Image,Crop,我已经盯着/玩这个代码太久了 使用currentCrop(x、y、宽度、高度)剪裁图像当前裁剪是根据图像必须位于的可用尺寸预先计算的 cropCentre(x,y)是存储作物中心点的临时变量 desiredPoint(x,y)是图像中希望显示的点,因此一旦计算了裁剪,最好移动图像裁剪,以便CropCenter(从而currentCrop)尽可能接近desiredPoint,但要确保currentCrop不会超出图像的范围 下面的代码可以工作,但我确信,不需要任何while循环也可以实现同样的功能

我已经盯着/玩这个代码太久了

使用
currentCrop
(x、y、宽度、高度)剪裁图像<代码>当前裁剪是根据图像必须位于的可用尺寸预先计算的

cropCentre
(x,y)是存储作物中心点的临时变量

desiredPoint
(x,y)是图像中希望显示的点,因此一旦计算了裁剪,最好移动图像裁剪,以便
CropCenter
(从而
currentCrop
)尽可能接近
desiredPoint
,但要确保
currentCrop
不会超出图像的范围

下面的代码可以工作,但我确信,不需要任何while循环也可以实现同样的功能:

if ( cropCentre.x < desiredPoint.x ) {
    while( currentCrop.x + currentCrop.width < sourceWidth && cropCentre.x < desiredPoint.x ) {
        cropCentre.x = currentCrop.x + currentCrop.width / 2;
        currentCrop.x ++;
    }
} else if ( cropCentre.x > desiredPoint.x ) {
    while( currentCrop.x > 0 && cropCentre.x > desiredPoint.x ) {
        cropCentre.x = currentCrop.x + currentCrop.width / 2;
        currentCrop.x --;
    }
}

// code for vertical omitted - if the problem can be solved for x, then the solution for y is identical...
if(cropCentre.xdesiredPoint.x){
而(currentCrop.x>0&&CropCenter.x>desiredPoint.x){
CropCenter.x=currentCrop.x+currentCrop.width/2;
currentCrop.x--;
}
}
//忽略垂直方向的代码-如果x的问题可以解决,那么y的解决方案是相同的。。。

currentCrop.x=Math.min(Math.max(desiredPoint.x-currentCrop.width/2,0),sourceWidth-currentCrop.width)