Get the bounding box of a selected SVG element

北慕城南 提交于 2019-12-05 13:28:38

Assuming you have a handle to the element, I would think that this would work, no?

box = svgedit.utilities.getBBox(selected);

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();

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>

I'm not sure if I'm understanding you correctly, but if you're looking to get the height or width of a jQuery element, use width() and height():

log("The svg is " + $("img").width() + "px wide.");
log("The svg is " + $("img").height() + "px tall.");

JSFiddle example: http://jsfiddle.net/gGWU4/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!