@core-graphics/rect
Version:
JS utilities for managing rects
423 lines (368 loc) • 9.75 kB
JavaScript
import { Point } from '@core-graphics/point';
import { Size } from '@core-graphics/size';
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _typeof(obj) {
"@babel/helpers - typeof";
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
/**
* A structure that contains the location and dimensions of a rectangle.
*/
var Rect = /*#__PURE__*/function () {
/**
* A point that specifies the coordinates of the rectangle’s origin.
*/
/**
* A size that specifies the height and width of the rectangle.
*/
/* -----------------------------------------------------------------------------
* Creating a Rectangle
* -----------------------------------------------------------------------------*/
/**
* Creates a rectangle with the specified origin and size.
*/
function Rect(value) {
var _this = this;
_classCallCheck(this, Rect);
_defineProperty(this, "origin", void 0);
_defineProperty(this, "size", void 0);
_defineProperty(this, "withOrigin", function (origin) {
return new Rect(_objectSpread2(_objectSpread2({}, _this.value), origin));
});
_defineProperty(this, "clone", function () {
return new Rect(_this.value);
});
_defineProperty(this, "toString", function () {
return JSON.stringify(_this.value);
});
_defineProperty(this, "toJSON", function () {
return _objectSpread2(_objectSpread2({}, _this.origin.value), _this.size.value);
});
_defineProperty(this, "set", function (rect) {
_this.origin.set(rect);
_this.size.set(rect);
return _this;
});
_defineProperty(this, "reset", function () {
_this.origin.reset();
_this.size.reset();
return _this;
});
_defineProperty(this, "toIntegral", function () {
var rect = _this.clone();
rect.origin.toIntegral();
rect.size.toIntegral();
return rect;
});
_defineProperty(this, "round", function (decimal) {
_this.origin.round(decimal);
_this.size.round(decimal);
return _this;
});
this.origin = new Point(value);
this.size = new Size(value);
}
/**
* Creates a Rect from specified point and size as arguments
*/
_createClass(Rect, [{
key: "withSize",
value:
/**
* Returns a new rect with updated size
*/
function withSize(size) {
return new Rect(_objectSpread2(_objectSpread2({}, this.value), size));
}
/**
* Create a copy of the current rectangle
*/
}, {
key: "value",
get: function get() {
return this.toJSON();
}
/* -----------------------------------------------------------------------------
* Geometric Properties
* -----------------------------------------------------------------------------*/
}, {
key: "area",
get: function get() {
return this.width * this.height;
}
}, {
key: "x",
get: function get() {
return this.origin.x;
}
}, {
key: "y",
get: function get() {
return this.origin.y;
}
}, {
key: "width",
get: function get() {
return this.size.width;
}
}, {
key: "height",
get: function get() {
return this.size.height;
}
}, {
key: "minX",
get: function get() {
return this.x;
}
}, {
key: "midX",
get: function get() {
return this.x + this.width / 2;
}
}, {
key: "maxX",
get: function get() {
return this.x + this.width;
}
}, {
key: "minY",
get: function get() {
return this.y;
}
}, {
key: "midY",
get: function get() {
return this.y + this.height / 2;
}
}, {
key: "maxY",
get: function get() {
return this.y + this.height;
}
}, {
key: "topLeftPoint",
get: function get() {
return new Point({
x: this.minX,
y: this.minY
});
}
}, {
key: "topRightPoint",
get: function get() {
return new Point({
x: this.maxX,
y: this.minY
});
}
}, {
key: "bottomLeftPoint",
get: function get() {
return new Point({
x: this.minX,
y: this.maxY
});
}
}, {
key: "bottomRightPoint",
get: function get() {
return new Point({
x: this.maxX,
y: this.maxY
});
}
}, {
key: "centerPoint",
get: function get() {
return new Point({
x: this.midX,
y: this.midY
});
}
}, {
key: "cornerPoints",
get: function get() {
return [this.topLeftPoint, this.topRightPoint, this.bottomRightPoint, this.bottomLeftPoint];
}
}, {
key: "topCenterPoint",
get: function get() {
return new Point({
x: this.midX,
y: this.minY
});
}
}, {
key: "rightCenterPoint",
get: function get() {
return new Point({
x: this.maxX,
y: this.midY
});
}
}, {
key: "bottomCenterPoint",
get: function get() {
return new Point({
x: this.midX,
y: this.maxY
});
}
}, {
key: "leftCenterPoint",
get: function get() {
return new Point({
x: this.minX,
y: this.midY
});
}
}, {
key: "centerPoints",
get: function get() {
return [this.topCenterPoint, this.rightCenterPoint, this.bottomCenterPoint, this.leftCenterPoint];
}
}, {
key: "isEmpty",
get: function get() {
return this.size.isEmpty;
}
}, {
key: "edges",
get: function get() {
return {
top: [this.topLeftPoint, this.topRightPoint],
right: [this.topRightPoint, this.bottomRightPoint],
bottom: [this.bottomLeftPoint, this.bottomRightPoint],
left: [this.topLeftPoint, this.bottomLeftPoint]
};
}
}, {
key: "getPoint",
value: function getPoint(point) {
var map = {
"top-left": this.topLeftPoint,
"top-right": this.topRightPoint,
"top-center": this.topCenterPoint,
"bottom-left": this.bottomLeftPoint,
"bottom-center": this.bottomCenterPoint,
"bottom-right": this.bottomRightPoint,
"left-center": this.leftCenterPoint,
"right-center": this.rightCenterPoint,
center: this.centerPoint
};
return map[point];
}
/**
* Updates the values for the rect
*/
}], [{
key: "zero",
get:
/* -----------------------------------------------------------------------------
* Special Values
* -----------------------------------------------------------------------------*/
/**
* The rectangle whose origin and size are both zero.
*/
function get() {
return new Rect({
x: 0,
y: 0,
width: 0,
height: 0
});
}
/**
* Creates a rectangle with origin (0,0) and size (0,0).
*/
}]);
return Rect;
}();
_defineProperty(Rect, "create", function (rect) {
return new Rect(rect);
});
_defineProperty(Rect, "init", function () {
return new Rect(Rect.zero);
});
_defineProperty(Rect, "isEqual", function (a, b) {
if (!a || !b) return false;
return a.origin.isEqual(b.origin) && a.size.isEqual(b.size);
});
_defineProperty(Rect, "is", function (value) {
return _typeof(value) === "object" && Size.is(value) && Point.is(value);
});
_defineProperty(Rect, "cast", function (value) {
return value instanceof Rect ? value : new Rect(value);
});
export { Rect as R, _defineProperty as _, _classCallCheck as a };