简体   繁体   English

获取所选SVG元素的边界框

[英]Get the bounding box of a selected SVG element

I want to read: 我想读:

  • width , height , x , y measurements widthheightxy测量值

for a particular SVG element. 对于特定的SVG元素。


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? 我该如何访问?

If you have a reference to the DOM node, use 如果您有对DOM节点的引用,请使用

svgNode.getBoundingClientRect()

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

Edit: SVG Edit has a method to return currently selected elements: 编辑:SVG Edit有一个返回当前所选元素的方法:

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. 有关示例和答案,请参阅http://granite.sru.edu/~ddailey/svg/BBox0M.svg

In brief, this code works for me in Chrome: 简而言之,此代码适用于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(): 我不确定我是否正确理解你,但是如果你想获得jQuery元素的高度或宽度,请使用width()和height():

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

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

假设你有元素的句柄,我会认为这会起作用,不是吗?

box = svgedit.utilities.getBBox(selected);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM