@core-graphics/rect
Version:
JS utilities for managing rects
75 lines (64 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
/* -----------------------------------------------------------------------------
* Alignment methods
* -----------------------------------------------------------------------------*/
/**
* Align this rect to a reference rect horizontally
*
* @param rect - The rect to align
* @param ref - The reference rect to align to
* @param align - The horizontal alignment to use
*/
function alignHorizontal(rect, ref, align) {
var x = ref.x;
var width = ref.width;
if (align === "left-inside") x = ref.x;
if (align === "left-outside") x = ref.x - rect.width;
if (align === "right-inside") x = ref.maxX - rect.width;
if (align === "right-outside") x = ref.maxX;
if (align === "center") x = ref.midX - rect.width / 2;
if (align === "stretch") {
x = ref.x;
width = ref.width;
}
return rect.clone().set({
x: x,
width: width
});
}
/**
* Align this rect to a reference rect vertically
*
* @param rect - The rect to align
* @param ref - The reference rect to align to
* @param align - The horizontal alignment to use
*/
function alignVertical(rect, ref, align) {
var y = ref.y;
var height = ref.height;
if (align === "top-inside") y = ref.y;
if (align === "top-outside") y = ref.y - rect.height;
if (align === "bottom-inside") y = ref.maxY - rect.height;
if (align === "bottom-outside") y = ref.maxY;
if (align === "center") y = ref.midY - rect.height / 2;
if (align === "stretch") {
y = ref.y;
height = ref.height;
}
return rect.clone().set({
y: y,
height: height
});
}
/**
* Align this rect to a reference rect based on the specifed placement
*/
function align(rect, anchor, options) {
var horizontal = options.horizontal,
vertical = options.vertical;
return alignVertical(alignHorizontal(rect, anchor, horizontal), anchor, vertical);
}
exports.align = align;
exports.alignHorizontal = alignHorizontal;
exports.alignVertical = alignVertical;