UNPKG

@aurigma/design-atoms-model

Version:

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

263 lines 11 kB
import { PointF } from "./PointF"; import { EqualsOfFloatNumbers } from "./Common"; //import { traceLog } from "../Utils/TraceLog"; import { Matrix } from "./Matrix"; import { ArgumentException } from "../Exception"; import * as _ from "underscore"; import { EventWithSenderArg } from "../EventObject"; var RectangleF = /** @class */ (function () { function RectangleF(left, top, width, height) { this._propertyChanged = new EventWithSenderArg(); this._left = typeof left == "number" ? left : 0; this._top = typeof top == "number" ? top : 0; this._width = (typeof width == "number") ? width : 2; this._height = (typeof height == "number") ? height : 2; this._id = Math.random().toString(); } RectangleF.prototype.toJSON = function () { return { left: this.left, top: this.top, width: this.width, height: this.height, id: this._id }; }; Object.defineProperty(RectangleF.prototype, "left", { get: function () { return this._left; }, set: function (value) { if (EqualsOfFloatNumbers(value, this._left, RectangleF._tolerance)) return; this._left = value; this._propertyChanged.notify(this, "left"); }, enumerable: true, configurable: true }); Object.defineProperty(RectangleF.prototype, "top", { get: function () { return this._top; }, set: function (value) { if (EqualsOfFloatNumbers(value, this._top, RectangleF._tolerance)) return; this._top = value; this._propertyChanged.notify(this, "top"); }, enumerable: true, configurable: true }); Object.defineProperty(RectangleF.prototype, "width", { get: function () { return this._width; }, set: function (value) { if (EqualsOfFloatNumbers(value, this._width, RectangleF._tolerance)) return; this._width = value; this._propertyChanged.notify(this, "width"); }, enumerable: true, configurable: true }); Object.defineProperty(RectangleF.prototype, "height", { get: function () { return this._height; }, set: function (value) { if (EqualsOfFloatNumbers(value, this._height, RectangleF._tolerance)) return; this._height = value; this._propertyChanged.notify(this, "height"); }, enumerable: true, configurable: true }); Object.defineProperty(RectangleF.prototype, "id", { get: function () { return this._id; }, enumerable: true, configurable: true }); RectangleF.FromLTRB = function (left, top, right, bottom) { var width = Math.abs(right - left); var height = Math.abs(bottom - top); return new RectangleF(Math.min(left, right), Math.min(top, bottom), width, height); }; RectangleF.fromLtrbObject = function (obj) { return RectangleF.FromLTRB(obj.left, obj.top, obj.right, obj.bottom); }; RectangleF.FromObject = function (object) { return new RectangleF(object.left, object.top, object.width, object.height); }; /** * Creates a rectangle that represents the intersetion between a and b. * If there is no intersection, empty rectangle is returned. */ RectangleF.intersect = function (a, b) { var maxLeft = Math.max(a.left, b.left); var minRight = Math.min(a.right, b.right); var maxTop = Math.max(a.top, b.top); var minBottom = Math.min(a.bottom, b.bottom); if (minRight >= maxLeft && minBottom >= maxTop) return new RectangleF(maxLeft, maxTop, minRight - maxLeft, minBottom - maxTop); return RectangleF.empty; }; ; RectangleF.union = function (a, b) { var x1 = Math.min(a.left, b.left); var x2 = Math.max(a.left + a.width, b.left + b.width); var y1 = Math.min(a.top, b.top); var y2 = Math.max(a.top + a.height, b.top + b.height); return new RectangleF(x1, y1, x2 - x1, y2 - y1); }; ; Object.defineProperty(RectangleF, "empty", { get: function () { return new RectangleF(0, 0, -1, -1); }, enumerable: true, configurable: true }); RectangleF.prototype.equals = function (other) { return RectangleF.isEqual(this, other); }; RectangleF.isEqual = function (a, b, tolerance) { if (tolerance === void 0) { tolerance = 0.0001; } if (a == null && b == null) return true; if (a == null || b == null) return false; return EqualsOfFloatNumbers(a.left, b.left, tolerance) && EqualsOfFloatNumbers(a.top, b.top, tolerance) && EqualsOfFloatNumbers(a.width, b.width, tolerance) && EqualsOfFloatNumbers(a.height, b.height, tolerance); }; RectangleF.prototype.contains = function (point, includeBorder, tolerance) { if (includeBorder == null) includeBorder = false; if (includeBorder) { if (typeof tolerance != "number") tolerance = 0.0001; var left = this.left, top = this.top, right = this.left + this.width, bottom = this.top + this.height; return (point.x > left || EqualsOfFloatNumbers(point.x, left, tolerance)) && (point.y > top || EqualsOfFloatNumbers(point.y, top, tolerance)) && (point.x < right || EqualsOfFloatNumbers(point.x, right, tolerance)) && (point.y < bottom || EqualsOfFloatNumbers(point.y, bottom, tolerance)); } else { return point.x > this.left && point.y > this.top && point.x < this.left + this.width && point.y < this.top + this.height; } }; RectangleF.prototype.containsRectangle = function (rectangle) { var leftTop = new PointF(rectangle.left, rectangle.top); var rightBottom = new PointF(rectangle.right, rectangle.bottom); return this.contains(leftTop, true) && this.contains(rightBottom, true); }; RectangleF.prototype.clone = function () { return new RectangleF(this.left, this.top, this.width, this.height); }; RectangleF.prototype.isEmpty = function () { return (this.width <= 0) || (this.height <= 0); }; RectangleF.prototype.inflate = function (size) { this.left -= size; this.top -= size; this.width += size * 2; this.height += size * 2; }; /** * Determines if this rectangle intersects with rect */ RectangleF.prototype.intersectsWith = function (rect) { return (rect.left < this.right) && (this.left < rect.right) && (rect.top < this.bottom) && (this.top < rect.bottom); }; RectangleF.prototype.updateByMatrix = function (matrix) { var deltaAngle = Math.abs(matrix.angle % 90); if (!EqualsOfFloatNumbers(deltaAngle, 0) && !EqualsOfFloatNumbers(deltaAngle, 90)) throw new ArgumentException("RectangleF cannot be transformed by angle (" + matrix.angle + "!"); var oldLeftTopPoint = new PointF(this.left, this.top); var oldRightBottomPoint = new PointF(this.right, this.bottom); var newLeftTopPoint = matrix.transformPoint(oldLeftTopPoint); var newRightBottomPoint = matrix.transformPoint(oldRightBottomPoint); var oldRect = this.clone(); this._left = newLeftTopPoint.x; this._top = newLeftTopPoint.y; this._width = newRightBottomPoint.x - this._left; this._height = newRightBottomPoint.y - this._top; if (!oldRect.equals(this)) this._propertyChanged.notify(this); return this; }; RectangleF.prototype.getExpanded = function (margin) { return RectangleF.FromLTRB(this.left - margin.left, this.top - margin.top, this.right + margin.right, this.bottom + margin.bottom); }; RectangleF.prototype.toString = function () { var r = [this.left, this.top, this.width, this.height]; return r.join(","); }; RectangleF.fromString = function (str) { var parts = str.replace(", ", ",").split(","); return new RectangleF(parseFloat(parts[0]), parseFloat(parts[1]), parseFloat(parts[2]), parseFloat(parts[3])); }; RectangleF.fromPoints = function (point1, point2) { return RectangleF.FromLTRB(Math.min(point1.x, point2.x), Math.min(point1.y, point2.y), Math.max(point1.x, point2.x), Math.max(point1.y, point2.y)); }; RectangleF.getMatrix = function (initialRect, targetRect) { var result = new Matrix(); var initialRectCenter = initialRect.center; var targetRectCenter = targetRect.center; result.scaleRelativeToPoint(targetRect.width / initialRect.width, targetRect.height / initialRect.height, initialRectCenter); result.translate((targetRectCenter.x - initialRectCenter.x) / result.scaleX, (targetRectCenter.y - initialRectCenter.y) / result.scaleY); return result; }; RectangleF.getOverallBounds = function (rects) { if (_.isEmpty(rects)) return RectangleF.empty; return rects.reduce(function (result, value) { if (result == null) return result = value.clone(); return RectangleF.union(result, value); }); }; Object.defineProperty(RectangleF.prototype, "right", { get: function () { return this.left + this.width; }, set: function (value) { this.width = value - this.left; }, enumerable: true, configurable: true }); Object.defineProperty(RectangleF.prototype, "bottom", { get: function () { return this.top + this.height; }, set: function (value) { this.height = value - this.top; }, enumerable: true, configurable: true }); Object.defineProperty(RectangleF.prototype, "center", { get: function () { return new PointF(this.left + this.width / 2, this.top + this.height / 2); }, enumerable: true, configurable: true }); RectangleF.prototype.addPropertyChanged = function (listener) { this._propertyChanged.add(listener); }; RectangleF.prototype.removePropertyChanged = function (listener) { this._propertyChanged.remove(listener); }; RectangleF._tolerance = 0.000001; return RectangleF; }()); export { RectangleF }; //# sourceMappingURL=RectangleF.js.map