-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING refactor(util):
boundingBoxFromPoints
, removed transform (#…
- Loading branch information
Showing
3 changed files
with
38 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,26 @@ | ||
import { Point } from '../../point.class'; | ||
import { TMat2D } from '../../typedefs'; | ||
import { transformPoint } from './matrix'; | ||
|
||
/** | ||
* Returns coordinates of points's bounding rectangle (left, top, width, height) | ||
* This function does not make sense. | ||
* - it mutates the input in case transform is present | ||
* - is used in 2 instances of the app one with the transform one without | ||
* Calculates bounding box (left, top, width, height) from given `points` | ||
* @static | ||
* @memberOf fabric.util | ||
* @param {Point[]} points 4 points array | ||
* @param {TMat2D} [transform] an array of 6 numbers representing a 2x3 transform matrix | ||
* @param {Point[]} points | ||
* @return {Object} Object with left, top, width, height properties | ||
*/ | ||
export const makeBoundingBoxFromPoints = (points: Point[], transform: TMat2D) => { | ||
if (transform) { | ||
for (let i = 0; i < points.length; i++) { | ||
points[i] = transformPoint(points[i], transform); | ||
export const makeBoundingBoxFromPoints = (points: Point[]) => { | ||
const { min, max } = points.reduce(({ min, max }, curr) => { | ||
return { | ||
min: min.min(curr), | ||
max: max.max(curr) | ||
} | ||
} | ||
const left = Math.min(points[0].x, points[1].x, points[2].x, points[3].x), | ||
right = Math.max(points[0].x, points[1].x, points[2].x, points[3].x), | ||
width = right - left, | ||
top = Math.min(points[0].y, points[1].y, points[2].y, points[3].y), | ||
bottom = Math.max(points[0].y, points[1].y, points[2].y, points[3].y), | ||
height = bottom - top; | ||
}, { min: points[0], max: points[0] }); | ||
|
||
const size = max.subtract(min); | ||
|
||
return { | ||
left, | ||
top, | ||
width, | ||
height, | ||
left: min.x, | ||
top: min.y, | ||
width: size.x, | ||
height: size.y, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters