UNPKG

advanced-cropper

Version:

The core of the advanced cropper libraries family

62 lines (57 loc) 2.91 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib = require('tslib'); var utils = require('../service/utils.js'); var helpers = require('../service/helpers.js'); require('../types/index.js'); require('../state/setCoordinates.js'); function defaultVisibleArea(state, settings) { var coordinates = state.coordinates, boundary = state.boundary; var imageSize = helpers.getTransformedImageSize(state); var boundaryRatio = utils.ratio(boundary); if (coordinates) { // Visible area will try to fit reference: var reference = { height: Math.max(coordinates.height, imageSize.height), width: Math.max(coordinates.width, imageSize.width), }; var visibleArea = { left: 0, top: 0, width: utils.ratio(reference) > boundaryRatio ? reference.width : reference.height * boundaryRatio, height: utils.ratio(reference) > boundaryRatio ? reference.width / boundaryRatio : reference.height, }; // Visible area should correspond its restrictions: visibleArea = utils.resizeToSizeRestrictions(visibleArea, helpers.getAreaSizeRestrictions(state, settings)); // Visible area will try to center stencil: visibleArea.left = coordinates.left + coordinates.width / 2 - visibleArea.width / 2; visibleArea.top = coordinates.top + coordinates.height / 2 - visibleArea.height / 2; // TODO: Controversial behavior: // If the coordinates are beyond image visible area will be allowed to be beyond image alike: var coordinatesIntersection = utils.getIntersections(coordinates, utils.coordinatesToPositionRestrictions(tslib.__assign({ left: 0, top: 0 }, imageSize))); var restrictions = {}; if (!coordinatesIntersection.left && !coordinatesIntersection.right && visibleArea.width <= imageSize.width) { restrictions.left = 0; restrictions.right = imageSize.width; } if (!coordinatesIntersection.top && !coordinatesIntersection.bottom && visibleArea.height <= imageSize.height) { restrictions.top = 0; restrictions.bottom = imageSize.height; } return utils.moveToPositionRestrictions(visibleArea, restrictions); } else { var imageRatio = utils.ratio(imageSize); var areaProperties = { height: imageRatio < boundaryRatio ? imageSize.height : imageSize.width / boundaryRatio, width: imageRatio < boundaryRatio ? imageSize.height * boundaryRatio : imageSize.width, }; return { left: imageSize.width / 2 - areaProperties.width / 2, top: imageSize.height / 2 - areaProperties.height / 2, width: areaProperties.width, height: areaProperties.height, }; } } exports.defaultVisibleArea = defaultVisibleArea;