Search code examples
svginkscape

How to calculate the y coordinate of a rectangle without transforms in Inkscape?


Imagine I have an Inkscape file with the following rectangle:

Screenshot

I want to calculate the Y coordinate reported by Inkscape (596.654).

How can I do it (manually) ?

I tried this:

  1. The top of the page seems to have a Y coordinate of 744.
  2. I subtract from that number the y coordinate of the rectangle in the XML editor (417) and its height (37) and get 744 - 417 - 37 = 290.

Note that the rectangle doesn't have any transforms and doesn't belong to a group.


Solution

  • Here's the simplified version of your SVG with only the relevant information included:

    <svg width="297mm" height="210mm"
         viewBox="0 0 1052.3622 744.09448">
      <g transform="translate(0,-308.26772)">
        <rect x="216.1537" y="417.34927"
              width="385.25827" height="37.859257"
              style="stroke-width:1;"
       />
      </g>
    </svg>
    

    Despite what you thought, there is a transform in there (in the group).

    SVG internal coordinates have their origin in the top left. Whereas Inkscape displays a converted value relative to the more normal origin at bottom left. The displayed value also takes into account the stroke width.

    Your rectangle is drawn (internal coords) at

    y = rectY + translateY
      = 417.34927 - 308.26772
      = 109.08155
    

    The page has a height of 744.09448. So the displayed ccoordinate will be:

    y = pageHeight - rectY - rectH - strokeWidth/2
      = 744.09448 - 109.08155 - 37.859257 - 0.5
      = 596.653673