【发布时间】:2017-10-25 22:29:15
【问题描述】:
我有一个 SVG 和点(例如 (x,y) = (250,112))。 BATIK 是否可以在给定点获取节点(DOM 元素)?
【问题讨论】:
我有一个 SVG 和点(例如 (x,y) = (250,112))。 BATIK 是否可以在给定点获取节点(DOM 元素)?
【问题讨论】:
我在用户 templth 开发的batik users forum 上找到了答案,我不相信我只是在这里重新发布解决方案,以便它可以有更多的曝光。
public Element elementAtPosition(Document doc, Point2D point) {
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext context = new BridgeContext(userAgent, loader);
context.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
GraphicsNode rootGraphicsNode = builder.build(context, doc);
// rootGraphicsNode can be offseted relative to coordinate system
// that means calling this method on coordinates taken directly from the svg file might not work
// check the bounds of rootGraphicsNode to determine where the elements actually are
// System.out.println(rootGraphicsNode.getBounds());
GraphicsNode graphicsNode = rootGraphicsNode.nodeHitAt(point);
if (graphicsNode != null) {
return context.getElement(graphicsNode);
} else {
// if graphicsNode is null there is no element at this position
return null;
}
}
在 Batik 1.9 上测试。此方法仅返回指定位置的最顶部元素。作为一种解决方法,您可以删除该元素并再次调用 nodeHitAt。
【讨论】: