@gorpacrate/core-graphics
Version:
A core library for creating shape-based graphic editors
176 lines (173 loc) • 7.16 kB
JavaScript
"use strict";
exports.__esModule = true;
var invariant = require("invariant");
var declarations_1 = require("../declarations");
var math_1 = require("./math");
function normalizeBB(bb) {
var x1 = bb.x1, y1 = bb.y1, x2 = bb.x2, y2 = bb.y2, width = bb.width, height = bb.height;
var nx1 = Math.min(x1, x2);
var ny1 = Math.min(y1, y2);
var nx2 = Math.max(x1, x2);
var ny2 = Math.max(y1, y2);
return {
x1: nx1, y1: ny1,
x2: nx2, y2: ny2,
width: width, height: height
};
}
exports.normalizeBB = normalizeBB;
function padBB(padParams, bb) {
var _a = padParams.left, left = _a === void 0 ? 0 : _a, _b = padParams.right, right = _b === void 0 ? 0 : _b, _c = padParams.top, top = _c === void 0 ? 0 : _c, _d = padParams.bottom, bottom = _d === void 0 ? 0 : _d;
var x1 = bb.x1, y1 = bb.y1, x2 = bb.x2, y2 = bb.y2;
var nx1 = x1 - left;
var ny1 = y1 - top;
var nx2 = x2 + right;
var ny2 = y2 + bottom;
var nwidth = Math.abs(nx1 - nx2);
var nheight = Math.abs(ny1 - ny2);
return {
x1: nx1, y1: ny1,
x2: nx2, y2: ny2,
width: nwidth, height: nheight
};
}
exports.padBB = padBB;
function roundBB(bb) {
var x1 = Math.round(bb.x1);
var y1 = Math.round(bb.y1);
var x2 = Math.round(bb.x2);
var y2 = Math.round(bb.y2);
var width = Math.abs(bb.x1 - bb.x2);
var height = Math.abs(bb.y1 - bb.y2);
return { x1: x1, y1: y1, x2: x2, y2: y2, width: width, height: height };
}
exports.roundBB = roundBB;
function padBBEqual(p, bb) {
return padBB({ left: p, right: p, top: p, bottom: p }, bb);
}
exports.padBBEqual = padBBEqual;
function getBBCenter(bb) {
var x1 = bb.x1, y1 = bb.y1, x2 = bb.x2, y2 = bb.y2;
return {
x: (x1 + x2) / 2,
y: (y1 + y2) / 2
};
}
exports.getBBCenter = getBBCenter;
function getBoundingBoxFromTwoPoints(a, b) {
var x1 = a.x, y1 = a.y;
var x2 = b.x, y2 = b.y;
var width = Math.abs(x2 - x1);
var height = Math.abs(y2 - y1);
return { x1: x1, y1: y1, x2: x2, y2: y2, width: width, height: height };
}
exports.getBoundingBoxFromTwoPoints = getBoundingBoxFromTwoPoints;
function doBoundingBoxesCollide(a, b) {
var _a = normalizeBB(a), ax1 = _a.x1, ay1 = _a.y1, ax2 = _a.x2, ay2 = _a.y2;
var _b = normalizeBB(b), bx1 = _b.x1, by1 = _b.y1, bx2 = _b.x2, by2 = _b.y2;
var mx1 = Math.max(ax1, bx1);
var mx2 = Math.min(ax2, bx2);
var my1 = Math.max(ay1, by1);
var my2 = Math.min(ay2, by2);
var xCollides = (mx1 <= mx2);
var yCollides = (my1 <= my2);
return (xCollides && yCollides);
}
exports.doBoundingBoxesCollide = doBoundingBoxesCollide;
function getBoundingBoxFromNPoints(points) {
var xs = points.map(function (p) { return p.x; });
var ys = points.map(function (p) { return p.y; });
var minX = xs.reduce(function (a, b) { return Math.min(a, b); }, +Infinity);
var minY = ys.reduce(function (a, b) { return Math.min(a, b); }, +Infinity);
var maxX = xs.reduce(function (a, b) { return Math.max(a, b); }, -Infinity);
var maxY = ys.reduce(function (a, b) { return Math.max(a, b); }, -Infinity);
var width = maxX - minX;
var height = maxY - minY;
return {
x1: minX, y1: minY,
x2: maxX, y2: maxY,
width: width, height: height
};
}
exports.getBoundingBoxFromNPoints = getBoundingBoxFromNPoints;
function unionBoundingBoxes(bbs) {
var points = bbs.map(function (_a) {
var x1 = _a.x1, y1 = _a.y1, x2 = _a.x2, y2 = _a.y2;
return ([{ x: x1, y: y1 }, { x: x2, y: y2 }]);
}).reduce(function (total, curr) { return total.concat(curr); }, []);
return getBoundingBoxFromNPoints(points);
}
exports.unionBoundingBoxes = unionBoundingBoxes;
function getShapesBoundingBoxes(params) {
var shapesDeclarations = params.shapesDeclarations, ids = params.ids, shapes = params.shapes;
return ids.map(function (id) {
var shapeData = shapes[id];
var type = shapeData.type;
return {
id: id,
bb: shapesDeclarations[type].getBoundingBox(shapeData)
};
});
}
exports.getShapesBoundingBoxes = getShapesBoundingBoxes;
function getSceneBB(params) {
var scene = params.scene, shapesDeclarations = params.shapesDeclarations;
return getShapesBoundingBox({ scene: scene, shapesDeclarations: shapesDeclarations, ids: scene.shapesOrder });
}
exports.getSceneBB = getSceneBB;
function getShapeResizeHandleCoords(bb, handle) {
var width = bb.width, height = bb.height, x1 = bb.x1, y1 = bb.y1, x2 = bb.x2, y2 = bb.y2;
var relX = handle.relX, relY = handle.relY;
var mx = (x1 + x2) / 2;
var my = (y1 + y2) / 2;
var x = mx + relX * width / 2;
var y = my + relY * height / 2;
return { x: x, y: y };
}
exports.getShapeResizeHandleCoords = getShapeResizeHandleCoords;
function getShapesResizeHandlesCoords(params) {
var bb = params.bb, offset = params.offset, handles = params.handles;
return handles.map(function (handle) {
var relX = handle.relX, relY = handle.relY;
var vX = math_1.sign(relX);
var vY = math_1.sign(relY);
var _a = getShapeResizeHandleCoords(bb, handle), x = _a.x, y = _a.y;
var handleX = x + vX * offset;
var handleY = y + vY * offset;
return { handle: handle, x: handleX, y: handleY };
});
}
exports.getShapesResizeHandlesCoords = getShapesResizeHandlesCoords;
function getShapesBoundingBox(params) {
var ids = params.ids, shapesDeclarations = params.shapesDeclarations, scene = params.scene;
var shapes = scene.shapes;
var boundingBoxes = getShapesBoundingBoxes({ shapesDeclarations: shapesDeclarations, shapes: shapes, ids: ids }).map(function (indexedBB) { return indexedBB.bb; });
return unionBoundingBoxes(boundingBoxes);
}
exports.getShapesBoundingBox = getShapesBoundingBox;
function getSelectedOrTransformedShapesBoundingBox(params) {
var mode = params.mode;
if (declarations_1.isSelectedMode(mode) || declarations_1.isResizingMode(mode) || declarations_1.isMovingMode(mode)) {
var shapesDeclarations = params.shapesDeclarations, scene = params.scene;
var shapes = scene.shapes;
var ids = mode.payload.ids;
var boundingBoxes = getShapesBoundingBoxes({ shapesDeclarations: shapesDeclarations, shapes: shapes, ids: ids }).map(function (indexedBB) { return indexedBB.bb; });
return unionBoundingBoxes(boundingBoxes);
}
return invariant(false, 'not in selected, resizing or moving mode');
}
exports.getSelectedOrTransformedShapesBoundingBox = getSelectedOrTransformedShapesBoundingBox;
/*
export function getSelectedShapesBoundingBox(params: { shapesDeclarations: IShapesDeclarations, scene: IScene, mode: ISceneMode }) : IBoundingBox {
const { mode } = params;
if (isSelectedMode(mode)) {
const { shapesDeclarations, scene } = params;
const { shapes } = scene;
const { ids } = mode.payload;
const boundingBoxes = getShapesBoundingBoxes({ shapesDeclarations, shapes, ids }).map( indexedBB => indexedBB.bb );
return unionBoundingBoxes(boundingBoxes);
}
return undefined;
}
*/
//# sourceMappingURL=bounding-box.js.map