Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// perform a fast bounds check
const { left, right, top, bottom } = nodeRef.current.getBBox();
if (x < left || x > right || y < top || y > bottom) {
return false;
}
// Rounding the coordinates due to an issue with `point-in-svg-path` returning false
// when the coordinates clearly are within the path.
const point = Point.singleUse(Math.round(x), Math.round(y));
// Translate to this element's coordinates.
// Assumes the node is not within an svg element containing another transform.
elementRef.current.translateFromAbsolute(point);
if (nodeRef.current instanceof SVGPathElement) {
const d = nodeRef.current.getAttribute('d');
return pointInSvgPath(d, point.x, point.y);
}
if (nodeRef.current instanceof SVGCircleElement) {
const { cx, cy, r } = nodeRef.current;
return (
Math.sqrt((point.x - cx.animVal.value) ** 2 + (point.y - cy.animVal.value) ** 2) <
r.animVal.value
);
}
if (nodeRef.current instanceof SVGEllipseElement) {
const { cx, cy, rx, ry } = nodeRef.current;
return (
(point.x - cx.animVal.value) ** 2 / rx.animVal.value ** 2 +
(point.y - cy.animVal.value) ** 2 / ry.animVal.value ** 2 <=
1
);
}
public isPointInGroup = (point: PointTuple): boolean => {
const { containerPath } = this.state;
return pointInSvgPath(containerPath, point[0], point[1]);
};