Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the bounding box of a selected SVG element

I want to read:

  • width,height,x,y measurements

for a particular SVG element.


I suppose that easiest way to go about this is to fetch the minimum bounding box first and read it's properties.

How can I access this?

like image 897
nicholaswmin Avatar asked Feb 17 '13 21:02

nicholaswmin


2 Answers

If you have a reference to the DOM node, use

svgNode.getBoundingClientRect()

https://developer.mozilla.org/en-US/docs/DOM/element.getBoundingClientRect

Edit: SVG Edit has a method to return currently selected elements:

svgCanvas.getSelectedElems()

so in the above example:

svgNode = svgCanvas.getSelectedElems()[0];
svgNode.getBoundingClientRect();
like image 146
pawel Avatar answered Oct 12 '22 15:10

pawel


Please see http://granite.sru.edu/~ddailey/svg/BBox0M.svg for the example and answer.

In brief, this code works for me in Chrome:

<script>
function foo(evt)
{
    console.log(evt.target.getBBox());      
}
</script>

<svg>
    <circle onclick="foo(evt)" r="20%" cx="50%" cy="50%"/>
</svg>
like image 42
MSC Avatar answered Oct 12 '22 14:10

MSC