UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

223 lines 13.2 kB
import { EqualsOfFloatNumbers, RotatedRectangleF } from "@aurigma/design-atoms-model/Math"; import { GripsResizeType, ResizeHelper, ResizeType } from "../Utils/Grips"; var ImageCropperHandler = /** @class */ (function () { function ImageCropperHandler() { } Object.defineProperty(ImageCropperHandler.prototype, "selection", { get: function () { var _a; return (_a = this._canvas) === null || _a === void 0 ? void 0 : _a.selection; }, enumerable: true, configurable: true }); Object.defineProperty(ImageCropperHandler.prototype, "canvas", { set: function (cnv) { this._canvas = cnv; }, enumerable: true, configurable: true }); ImageCropperHandler.prototype.isRotated = function (curRect, newRect) { return curRect.angle != newRect.angle; }; ImageCropperHandler.prototype.isMoved = function (curRect, newRect) { return EqualsOfFloatNumbers(curRect.width, newRect.width) && EqualsOfFloatNumbers(curRect.height, newRect.height) && !this.isRotated(curRect, newRect); }; ImageCropperHandler.prototype.modifyRectangle = function (placeholder, currentRect, newRect) { var _a, _b, _c; this._rectangle = currentRect; this._placeholder = placeholder; if (this.isRotated(currentRect, newRect)) { return { rectangle: newRect, needUpdate: false }; } if (((_a = this.selection) === null || _a === void 0 ? void 0 : _a.isDragging) || this.isMoved(currentRect, newRect)) { return { rectangle: newRect, needUpdate: false }; } var gripsResizeType = ResizeHelper.geTypeOfResizeByGrips(this._placeholder.rectangle, this._placeholder.previousRectangle, (_b = this.selection) === null || _b === void 0 ? void 0 : _b.resizeIndex); if (gripsResizeType !== GripsResizeType.none) { return { rectangle: this._resizeByGrips(gripsResizeType), needUpdate: false }; } var resizeType = ResizeHelper.getTypeResize(newRect, this._rectangle); if (gripsResizeType === GripsResizeType.none && ((_c = this.selection) === null || _c === void 0 ? void 0 : _c.resizeIndex) == null && resizeType !== ResizeType.none) { return { rectangle: this._resize(resizeType, newRect), needUpdate: true }; } return { rectangle: this._rectangle, needUpdate: false }; }; ImageCropperHandler.prototype._resizeByGrips = function (type) { var placeholderRect = this._placeholder.rectangle; var _a = placeholderRect.bounds, containerTop = _a.top, containerRight = _a.right, containerLeft = _a.left, containerBottom = _a.bottom; var rectangle = this._rectangle.clone().rotateAt(-this._rectangle.angle, placeholderRect.center); var _b = rectangle.bounds, imageTop = _b.top, imageRight = _b.right, imageLeft = _b.left, imageBottom = _b.bottom; var _c = this._placeholder.startRectangle.bounds, startTop = _c.top, startBottom = _c.bottom, startLeft = _c.left, startRight = _c.right; switch (type) { case GripsResizeType.increaseTop: { if (containerTop > imageTop) { return this._rectangle; } else { var deltaTop = imageTop - containerTop; var finalHeight = this._rectangle.bounds.height + deltaTop; var scale = finalHeight / this._rectangle.height; var resized = this._resizeProportional(rectangle, scale); var alignmentRect = RotatedRectangleF.FromLTRB(resized.bounds.left, containerTop, resized.bounds.right, containerTop + resized.bounds.height); return alignmentRect.rotateAt(this._rectangle.angle, alignmentRect.center); } } case GripsResizeType.decreaseTop: { if (containerTop < startTop) { var scale = placeholderRect.bounds.height / this._rectangle.bounds.height; var diff = imageBottom - containerBottom; var resized = this._resizeProportional(rectangle, scale); var final = RotatedRectangleF.FromLTRB(resized.bounds.left, containerTop, resized.bounds.right, containerTop + resized.height); if (final.width < placeholderRect.bounds.width || final.height < placeholderRect.bounds.height) { return this._rectangle; } else { return RotatedRectangleF.FromLTRB(final.bounds.left, final.bounds.top + diff, final.bounds.right, final.bounds.bottom + diff).rotateAt(this._rectangle.angle, final.center); } } else { return this._rectangle; } } case GripsResizeType.increaseLeft: { if (containerLeft > imageLeft) { return this._rectangle; } else { var deltaLeft = imageLeft - containerLeft; var finalHeight = this._rectangle.bounds.width + deltaLeft; var scale = finalHeight / this._rectangle.width; var resized = this._resizeProportional(rectangle, scale); var alignmentRect = RotatedRectangleF.FromLTRB(containerLeft, resized.bounds.top, containerLeft + resized.bounds.width, resized.bounds.bottom); return alignmentRect.rotateAt(this._rectangle.angle, alignmentRect.center); } } case GripsResizeType.decreaseLeft: { if (containerLeft < startLeft) { var scale = placeholderRect.bounds.width / this._rectangle.bounds.width; var diff = imageRight - containerRight; var bounds = this._resizeProportional(rectangle, scale).bounds; var final = RotatedRectangleF.FromLTRB(containerLeft, bounds.top, containerLeft + bounds.width, bounds.bottom); if (final.width < placeholderRect.bounds.width || final.height < placeholderRect.bounds.height) { return this._rectangle; } else { return RotatedRectangleF.FromLTRB(final.bounds.left + diff, final.bounds.top, final.bounds.right + diff, final.bounds.bottom).rotateAt(this._rectangle.angle, final.center); } } else { return this._rectangle; } } case GripsResizeType.increaseRight: { if (containerRight < imageRight) { return this._rectangle; } else { var deltaRight = containerRight - imageRight; var finalHeight = this._rectangle.bounds.width + deltaRight; var scale = finalHeight / this._rectangle.width; var resized = this._resizeProportional(rectangle, scale); var alignmentRect = RotatedRectangleF.FromLTRB(containerRight - resized.bounds.width, resized.bounds.top, containerRight, resized.bounds.bottom); return alignmentRect.rotateAt(this._rectangle.angle, alignmentRect.center); } } case GripsResizeType.decreaseRight: { if (containerRight > startRight) { var scale = placeholderRect.width / this._rectangle.bounds.width; var diff = imageLeft - containerLeft; var bounds = this._resizeProportional(rectangle, scale).bounds; var final = RotatedRectangleF.FromLTRB(containerRight - bounds.width, bounds.top, containerRight, bounds.bottom); if (final.width < placeholderRect.bounds.width || final.height < placeholderRect.bounds.height) { return this._rectangle; } else { return RotatedRectangleF.FromLTRB(final.bounds.left + diff, final.bounds.top, final.bounds.right + diff, final.bounds.bottom).rotateAt(this._rectangle.angle, final.center); } } else { return this._rectangle; } } case GripsResizeType.increaseBottom: { if (containerBottom < imageBottom) { return this._rectangle; } else { var deltaBottom = containerBottom - imageBottom; var finalHeight = this._rectangle.bounds.height + deltaBottom; var scale = finalHeight / this._rectangle.height; var resized = this._resizeProportional(rectangle, scale); // ?? var alignmentRect = RotatedRectangleF.FromLTRB(resized.bounds.left, containerBottom - resized.bounds.height, resized.bounds.right, containerBottom); return alignmentRect.rotateAt(this._rectangle.angle, alignmentRect.center); } } case GripsResizeType.decreaseBottom: { if (containerBottom > startBottom) { var scale = placeholderRect.height / this._rectangle.bounds.height; var diff = imageTop - containerTop; var resized = this._resizeProportional(rectangle, scale); var final = RotatedRectangleF.FromLTRB(resized.bounds.left, containerBottom - resized.bounds.height, resized.bounds.right, containerBottom); if (final.width < placeholderRect.bounds.width || final.height < placeholderRect.bounds.height) { return this._rectangle; } else { return RotatedRectangleF.FromLTRB(final.bounds.left, final.bounds.top + diff, final.bounds.right, final.bounds.bottom + diff).rotateAt(this._rectangle.angle, final.center); } } else { return this._rectangle; } } case GripsResizeType.cornerTopLeftDecrease: case GripsResizeType.cornerTopRightDecrease: case GripsResizeType.cornerBottomLeftDecrease: case GripsResizeType.cornerBottomRightDecrease: case GripsResizeType.cornerTopLeftIncrease: case GripsResizeType.cornerTopRightIncrease: case GripsResizeType.cornerBottomLeftIncrease: case GripsResizeType.cornerBottomRightIncrease: { var diffX = this._placeholder.previousRectangle.center.x - placeholderRect.center.x; var diffY = this._placeholder.previousRectangle.center.y - placeholderRect.center.y; var scale = placeholderRect.width / this._placeholder.previousRectangle.width; var result = this._resizeProportional(rectangle, scale); result.translate(-diffX, -diffY); return result.rotateAt(this._rectangle.angle, result.center); } case GripsResizeType.none: return this._rectangle; default: return this._rectangle; } }; ImageCropperHandler.prototype._resize = function (type, newRect) { switch (type) { case ResizeType.incWidth: case ResizeType.decWidth: { var scale = newRect.width / this._rectangle.width; var resized = this._resizeProportional(this._rectangle.clone(), scale); return RotatedRectangleF.FromLTRB(this._rectangle.bounds.left, this._rectangle.bounds.top, this._rectangle.bounds.left + resized.width, this._rectangle.bounds.top + resized.height); } case ResizeType.incHeight: case ResizeType.decHeight: case ResizeType.decHeightAndWidth: case ResizeType.incHeightAndWidth: { var scale = newRect.height / this._rectangle.height; var resized = this._resizeProportional(this._rectangle.clone(), scale); return RotatedRectangleF.FromLTRB(this._rectangle.bounds.left, this._rectangle.bounds.top, this._rectangle.bounds.left + resized.width, this._rectangle.bounds.top + resized.height); } default: return newRect; } }; ImageCropperHandler.prototype._resizeProportional = function (rectangle, scale) { rectangle.width = rectangle.width * scale; rectangle.height = rectangle.height * scale; return rectangle.clone(); }; return ImageCropperHandler; }()); export { ImageCropperHandler }; //# sourceMappingURL=ImageCropperHandler.js.map