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)