【问题标题】:How to get SVG node at coordinates with Batik如何在 Batik 坐标处获取 SVG 节点
【发布时间】:2017-10-25 22:29:15
【问题描述】:

我有一个 SVG 和点(例如 (x,y) = (250,112))。 BATIK 是否可以在给定点获取节点(DOM 元素)?

【问题讨论】:

    标签: java svg batik


    【解决方案1】:

    我在用户 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。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 2022-01-13
    • 2014-05-03
    • 2013-10-09
    • 2015-02-10
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多