简体   繁体   English

SVG-> getBBox失败,但只有一半时间

[英]SVG -> getBBox fails but only half the time

I'm trying to use the getBBox method to get the width of a text element inside an SVG render, BUT for some odd reason it only works half the time, the other half it fails.. 我正在尝试使用getBBox方法来获取SVG渲染中文本元素的宽度,但是出于某种奇怪的原因,它只能工作一半时间,而另一半则失败。

Here's my JS code I'm using for that: 这是我正在使用的JS代码:

console.log(iElement.find('.yLabel2:eq(2)')[0].getBBox());
var passWidth = numberTitle2.node().getBBox().width;

So half the time it will log [object SVGRect] and set passWidth to the width, that's awesome. 因此,一半的时间它将记录[object SVGRect]并将passWidth设置为width,这太棒了。 BUT the other times it logs [object Exception] , doesn't log the width, and breaks the rest of the JavaScript following it. 但是其他时候它记录[object Exception] ,不记录宽度,并破坏跟随它的其余JavaScript。

Inside the Exception object there's name: "NS_ERROR_FAILURE" and result: 2147500037 在Exception对象中, name: "NS_ERROR_FAILURE"result: 2147500037

Any ideas on how to get this to work smoothly or a better way to get the width of an svg element? 关于如何使其平稳运行或获得svg元素宽度的更好方法的任何想法?

It fails in Firefox if the text is not rendered. 如果未呈现文本,则在Firefox中将失败。 Eg it is display:none or you've not attached it to the document. 例如,它显示为:none,或者您尚未将其附加到文档中。

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

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