UNPKG

advanced-cropper

Version:

The core of the advanced cropper libraries family

33 lines (30 loc) 1.68 kB
import { __assign } from 'tslib'; import { getCenter, rotateSize, rotatePoint, fitToPositionRestrictions, applyMove } from '../../service/utils.js'; import '../../types/index.js'; import '../../state/setCoordinates.js'; import { BoundingBoxType } from './types.js'; import { getBoundingBox } from './boundingBox.js'; function fitToImage(coordinates, image, boundingBox) { if (boundingBox === void 0) { boundingBox = BoundingBoxType.Rectangle; } var imageCenter = getCenter(__assign({ left: 0, top: 0 }, rotateSize(image, -image.angle))); var center = rotatePoint(getCenter(coordinates), -image.angle, imageCenter); var boundingBoxSize = getBoundingBox(coordinates, image.angle, boundingBox); var boundingBoxCoordinates = __assign(__assign({}, boundingBoxSize), { left: center.left - boundingBoxSize.width / 2, top: center.top - boundingBoxSize.height / 2 }); var vector = fitToPositionRestrictions(boundingBoxCoordinates, { left: imageCenter.left - image.width / 2, top: imageCenter.top - image.height / 2, right: imageCenter.left + image.width / 2, bottom: imageCenter.top + image.height / 2, }); var leftVector = rotatePoint({ left: vector.left, top: 0 }, image.angle); var topVector = rotatePoint({ left: 0, top: vector.top }, image.angle); return { left: leftVector.left + topVector.left, top: leftVector.top + topVector.top, }; } function moveToImage(coordinates, image, boundingBox) { if (boundingBox === void 0) { boundingBox = BoundingBoxType.Rectangle; } return applyMove(coordinates, fitToImage(coordinates, image, boundingBox)); } export { fitToImage, moveToImage };