最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the bounding box of a selected SVG element - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Nov 1, 2018 at 6:49 nicholaswmin asked Feb 17, 2013 at 21:19 nicholaswminnicholaswmin 22.9k16 gold badges101 silver badges173 bronze badges 1
  • It would be helpful to say in the question above that you're using svg-edit: code.google.com/p/svg-edit – Matt Coughlin Commented Feb 18, 2013 at 0:31
Add a comment  | 

4 Answers 4

Reset to default 8

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>

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

box = svgedit.utilities.getBBox(selected);

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/

发布评论

评论列表(0)

  1. 暂无评论