adjustPerspective method
Implementation
Quality? adjustPerspective(Point<num> mouse, bool click) {
//if (click) return null ;
if (_perspective == null) return null;
//print('--- adjustPerspective ---');
Point<num> point = _getMousePointInCanvas(mouse, false);
var points = _perspective.value ?? _defaultPerspective();
if (points.length != 4) points = _defaultPerspective();
var initialBounds = _getPointsBounds(points);
var target = nearestPoint(points, point)!;
var targetIdx = points.indexOf(target);
//print('target: $target #$targetIdx');
var pointsAdjusted = copyPoints(points);
pointsAdjusted[targetIdx] = point;
var bounds = _getPointsBounds(pointsAdjusted);
if (bounds != initialBounds) {
var tolerance = max(10, max(width, height) / 50);
var wDiff = initialBounds.width - bounds.width;
var hDiff = initialBounds.height - bounds.height;
var xDiff = target.x - point.x;
var yDiff = target.y - point.y;
if (wDiff < 0) wDiff = -wDiff;
if (hDiff < 0) hDiff = -hDiff;
if (xDiff < 0) xDiff = -xDiff;
if (yDiff < 0) yDiff = -yDiff;
//print('Changing bounds> tolerance: $tolerance > whDiff: $wDiff , $hDiff > xyDiff: $xDiff , $yDiff >> $bounds != $initialBounds') ;
var pointFixed = point;
if (xDiff < tolerance && yDiff < tolerance) {
if (wDiff > 0 && xDiff < yDiff || xDiff < tolerance) {
pointFixed = Point<num>(target.x, point.y);
} else if (hDiff > 0 && yDiff < xDiff || yDiff < tolerance) {
pointFixed = Point<num>(point.x, target.y);
}
}
if (point != pointFixed) {
pointsAdjusted[targetIdx] = pointFixed;
bounds = _getPointsBounds(pointsAdjusted);
point = pointFixed;
}
}
var scaleX = width / bounds.width;
var scaleY = height / bounds.height;
//print('scaleX: $scaleX ; scaleY: $scaleY >> $bounds');
var pointsScaled =
translatePoints(pointsAdjusted, -bounds.left, -bounds.top);
pointsScaled = scalePointsXY(pointsScaled, scaleX, scaleY);
var spaceW = max(5, width / 20);
var spaceH = max(5, height / 20);
var pointsInBounds = [
_boundPoint(pointsScaled[0], Point(0, 0),
Point(width / 2 - spaceW, height / 2 - spaceH)),
_boundPoint(pointsScaled[1], Point(width / 2 + spaceW, 0),
Point(width, height / 2 - spaceH)),
_boundPoint(pointsScaled[2],
Point(width / 2 + spaceW, height / 2 + spaceH), Point(width, height)),
_boundPoint(pointsScaled[3], Point(0, height / 2 + spaceH),
Point(width / 2 - spaceW, height)),
];
/*
print('points: $points >> ${_getPointsBounds(points)}');
print(
'pointsAdjusted: $pointsAdjusted >> ${_getPointsBounds(pointsAdjusted)}');
print('pointsScaled: $pointsScaled >> ${_getPointsBounds(pointsScaled)}');
print(
'pointsInBounds: $pointsInBounds >> ${_getPointsBounds(pointsInBounds)}');
*/
_perspective.value = pointsInBounds;
return Quality.medium;
}