@macrostrat/column-components
Version:
React rendering primitives for stratigraphic columns
85 lines (84 loc) • 2.39 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const h = require("@macrostrat/hyper");
const React = require("react");
const classNames = require("classnames");
const uiComponents = require("@macrostrat/ui-components");
const d3Path = require("d3-path");
const column = require("../context/column.cjs");
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
const h__default = /* @__PURE__ */ _interopDefault(h);
const classNames__default = /* @__PURE__ */ _interopDefault(classNames);
function ColumnBox(props) {
const { offsetTop, absolutePosition, className, ...rest } = props;
const { pixelsPerMeter, zoom } = React.useContext(column.ColumnContext);
const marginTop = offsetTop * pixelsPerMeter * zoom;
let pos = { marginTop };
if (absolutePosition) {
pos = {
position: "absolute",
top: marginTop
};
}
return h__default.default(uiComponents.Box, {
className: classNames__default.default("column-box", className),
...pos,
...rest
});
}
function zigZagBoxPath(x, y, width, height, top, bottom) {
const d = d3Path.path();
const x1 = x + width;
d.moveTo(x, y);
if (top) {
drawZigZagAtConstantHeight(d, x, x1, y);
} else {
d.lineTo(x1, y);
}
const yBottom = y + height;
d.lineTo(x1, yBottom);
if (bottom) {
drawZigZagAtConstantHeight(d, x1, x, yBottom);
} else {
d.lineTo(x, yBottom);
}
d.closePath();
return d.toString();
}
function drawZigZagAtConstantHeight(d, x0, x1, y) {
const zigZagWidth = 10;
const zigZagHeight = 4;
const deltaX = x1 - x0;
const width = Math.abs(deltaX);
const nZigZags = Math.floor(width / zigZagWidth - 0.5);
const _zigZagWidth = width / (nZigZags + 0.5);
let dy = zigZagHeight / 2;
let dx = _zigZagWidth / 4;
let cx = x0;
let cy = y;
if (deltaX < 0) {
dx = -dx;
dy = -dy;
}
const doZigZag = (last = false) => {
cx += dx;
cy -= dy;
d.lineTo(cx, cy);
let scalar = last ? 1 : 2;
cx += dx * scalar;
cy += dy * scalar;
d.lineTo(cx, cy);
cx += dx;
cy -= dy;
};
d.lineTo(x0, y);
for (let i = 0; i < nZigZags; i++) {
doZigZag();
}
doZigZag(true);
d.lineTo(x1, y);
}
exports.ColumnBox = ColumnBox;
exports.drawZigZagAtConstantHeight = drawZigZagAtConstantHeight;
exports.zigZagBoxPath = zigZagBoxPath;
//# sourceMappingURL=column-box.cjs.map