core-graphics
Version:
A core library for creating shape-based graphic editors
266 lines (239 loc) • 7.98 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.normalizeBB = normalizeBB;
exports.padBB = padBB;
exports.roundBB = roundBB;
exports.padBBEqual = padBBEqual;
exports.getBBCenter = getBBCenter;
exports.getBoundingBoxFromTwoPoints = getBoundingBoxFromTwoPoints;
exports.doBoundingBoxesCollide = doBoundingBoxesCollide;
exports.getBoundingBoxFromNPoints = getBoundingBoxFromNPoints;
exports.unionBoundingBoxes = unionBoundingBoxes;
exports.getShapesBoundingBoxes = getShapesBoundingBoxes;
exports.getSceneBB = getSceneBB;
exports.getShapeResizeHandleCoords = getShapeResizeHandleCoords;
exports.getShapesResizeHandlesCoords = getShapesResizeHandlesCoords;
exports.getShapesBoundingBox = getShapesBoundingBox;
exports.getSelectedOrTransformedShapesBoundingBox = getSelectedOrTransformedShapesBoundingBox;
var _declarations = require('../declarations');
var _math = require('./math');
function normalizeBB(bb) {
var x1 = bb.x1;
var y1 = bb.y1;
var x2 = bb.x2;
var y2 = bb.y2;
var width = bb.width;
var 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
};
}
function padBB(padParams, bb) {
var _padParams$left = padParams.left;
var left = _padParams$left === undefined ? 0 : _padParams$left;
var _padParams$right = padParams.right;
var right = _padParams$right === undefined ? 0 : _padParams$right;
var _padParams$top = padParams.top;
var top = _padParams$top === undefined ? 0 : _padParams$top;
var _padParams$bottom = padParams.bottom;
var bottom = _padParams$bottom === undefined ? 0 : _padParams$bottom;
var x1 = bb.x1;
var y1 = bb.y1;
var x2 = bb.x2;
var 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
};
}
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 };
}
function padBBEqual(p, bb) {
return padBB({ left: p, right: p, top: p, bottom: p }, bb);
}
function getBBCenter(bb) {
var x1 = bb.x1;
var y1 = bb.y1;
var x2 = bb.x2;
var y2 = bb.y2;
return {
x: (x1 + x2) / 2,
y: (y1 + y2) / 2
};
}
function getBoundingBoxFromTwoPoints(a, b) {
var x1 = a.x;
var y1 = a.y;
var x2 = b.x;
var 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 };
}
function doBoundingBoxesCollide(a, b) {
var _normalizeBB = normalizeBB(a);
var ax1 = _normalizeBB.x1;
var ay1 = _normalizeBB.y1;
var ax2 = _normalizeBB.x2;
var ay2 = _normalizeBB.y2;
var _normalizeBB2 = normalizeBB(b);
var bx1 = _normalizeBB2.x1;
var by1 = _normalizeBB2.y1;
var bx2 = _normalizeBB2.x2;
var by2 = _normalizeBB2.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;
}
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
};
}
function unionBoundingBoxes(bbs) {
var points = bbs.map(function (_ref) {
var x1 = _ref.x1;
var y1 = _ref.y1;
var x2 = _ref.x2;
var y2 = _ref.y2;
return [{ x: x1, y: y1 }, { x: x2, y: y2 }];
}).reduce(function (total, curr) {
return total.concat(curr);
}, []);
return getBoundingBoxFromNPoints(points);
}
function getShapesBoundingBoxes(params) {
var shapesDeclarations = params.shapesDeclarations;
var ids = params.ids;
var shapes = params.shapes;
return ids.map(function (id) {
var shapeData = shapes[id];
var type = shapeData.type;
return {
id: id,
bb: shapesDeclarations[type].getBoundingBox(shapeData)
};
});
}
function getSceneBB(params) {
var scene = params.scene;
var shapesDeclarations = params.shapesDeclarations;
return getShapesBoundingBox({ scene: scene, shapesDeclarations: shapesDeclarations, ids: scene.shapesOrder });
}
function getShapeResizeHandleCoords(bb, handle) {
var width = bb.width;
var height = bb.height;
var x1 = bb.x1;
var y1 = bb.y1;
var x2 = bb.x2;
var y2 = bb.y2;
var relX = handle.relX;
var 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 };
}
function getShapesResizeHandlesCoords(params) {
var bb = params.bb;
var offset = params.offset;
var handles = params.handles;
return handles.map(function (handle) {
var relX = handle.relX;
var relY = handle.relY;
var vX = (0, _math.sign)(relX);
var vY = (0, _math.sign)(relY);
var _getShapeResizeHandle = getShapeResizeHandleCoords(bb, handle);
var x = _getShapeResizeHandle.x;
var y = _getShapeResizeHandle.y;
var handleX = x + vX * offset;
var handleY = y + vY * offset;
return { handle: handle, x: handleX, y: handleY };
});
}
function getShapesBoundingBox(params) {
var ids = params.ids;
var shapesDeclarations = params.shapesDeclarations;
var 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);
}
function getSelectedOrTransformedShapesBoundingBox(params) {
var mode = params.mode;
if ((0, _declarations.isSelectedMode)(mode) || (0, _declarations.isResizingMode)(mode) || (0, _declarations.isMovingMode)(mode)) {
var shapesDeclarations = params.shapesDeclarations;
var 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 null;
}
/*
export function getSelectedShapesBoundingBox(params: { shapesDeclarations: IShapesDeclarations, scene: IScene, mode: IMode }) : 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 null;
}
*/