/** * Converts a point in screen coordinates to a point in document * coordinates. * * @param locatable * @param screenPoint * the point in screen coordinates * @return the point in document coordinates */ public static SVGOMPoint screenToDocument(SVGLocatable locatable, SVGOMPoint screenPoint) { SVGMatrix mat = ((SVGLocatable) locatable.getFarthestViewportElement()).getScreenCTM() .inverse(); return (SVGOMPoint) screenPoint.matrixTransform(mat); }
SVGMatrix svgICTM = svgCTM.inverse();
SVGMatrix svgICTM = svgCTM.inverse();
public SVGPoint convertCoords(float x, float y) { SVGMatrix m = getSVGRootElement().getScreenCTM().inverse(); return new Point( m.getComponent(0) * x + m.getComponent(2) * y + m.getComponent(4), m.getComponent(1) * x + m.getComponent(3) * y + m.getComponent(5)); }
/** * Convert the coordinates of an DOM Event from screen into element * coordinates. * * @param doc Document context * @param tag Element containing the coordinate system * @param evt Event to interpret * @return coordinates */ public static SVGPoint elementCoordinatesFromEvent(Document doc, Element tag, Event evt) { try { DOMMouseEvent gnme = (DOMMouseEvent) evt; SVGMatrix mat = ((SVGLocatable) tag).getScreenCTM(); SVGMatrix imat = mat.inverse(); SVGPoint cPt = ((SVGDocument) doc).getRootElement().createSVGPoint(); cPt.setX(gnme.getClientX()); cPt.setY(gnme.getClientY()); return cPt.matrixTransform(imat); } catch(Exception e) { LoggingUtil.warning("Error getting coordinates from SVG event.", e); return null; } }
/** * Convert the coordinates of an DOM Event from screen into element * coordinates. * * @param doc Document context * @param tag Element containing the coordinate system * @param evt Event to interpret * @return coordinates */ public static SVGPoint elementCoordinatesFromEvent(Document doc, Element tag, Event evt) { try { DOMMouseEvent gnme = (DOMMouseEvent) evt; SVGMatrix mat = ((SVGLocatable) tag).getScreenCTM(); SVGMatrix imat = mat.inverse(); SVGPoint cPt = ((SVGDocument) doc).getRootElement().createSVGPoint(); cPt.setX(gnme.getClientX()); cPt.setY(gnme.getClientY()); return cPt.matrixTransform(imat); } catch(Exception e) { LoggingUtil.warning("Error getting coordinates from SVG event.", e); return null; } }
/** * Get the relative coordinates of a point within the coordinate system of a * particular SVG Element. * * @param evt Event, needs to be a DOMMouseEvent * @param reference SVG Element the coordinate system is used of * @return Array containing the X and Y values */ public static double[] getRelativeCoordinates(Event evt, Element reference) { if(evt instanceof DOMMouseEvent && reference instanceof SVGLocatable && reference instanceof SVGElement) { // Get the screen (pixel!) coordinates DOMMouseEvent gnme = (DOMMouseEvent) evt; SVGMatrix mat = ((SVGLocatable) reference).getScreenCTM(); SVGMatrix imat = mat.inverse(); SVGPoint cPt = ((SVGElement) reference).getOwnerSVGElement().createSVGPoint(); cPt.setX(gnme.getClientX()); cPt.setY(gnme.getClientY()); // Have Batik transform the screen (pixel!) coordinates into SVG element // coordinates cPt = cPt.matrixTransform(imat); return new double[] { cPt.getX(), cPt.getY() }; } return null; }
/** * Get the relative coordinates of a point within the coordinate system of a * particular SVG Element. * * @param evt Event, needs to be a DOMMouseEvent * @param reference SVG Element the coordinate system is used of * @return Array containing the X and Y values */ public static double[] getRelativeCoordinates(Event evt, Element reference) { if(evt instanceof DOMMouseEvent && reference instanceof SVGLocatable && reference instanceof SVGElement) { // Get the screen (pixel!) coordinates DOMMouseEvent gnme = (DOMMouseEvent) evt; SVGMatrix mat = ((SVGLocatable) reference).getScreenCTM(); SVGMatrix imat = mat.inverse(); SVGPoint cPt = ((SVGElement) reference).getOwnerSVGElement().createSVGPoint(); cPt.setX(gnme.getClientX()); cPt.setY(gnme.getClientY()); // Have Batik transform the screen (pixel!) coordinates into SVG element // coordinates cPt = cPt.matrixTransform(imat); return new double[] { cPt.getX(), cPt.getY() }; } return null; }