SVG-> getBBox失败,但只有一半时间 [英] SVG -> getBBox fails but only half the time
问题描述
我正在尝试使用getBBox方法获取SVG渲染中文本元素的宽度,但是出于某种奇怪的原因,它仅在一半时间内起作用,而另一半则失败..
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..
这是我正在使用的JS代码:
Here's my JS code I'm using for that:
console.log(iElement.find('.yLabel2:eq(2)')[0].getBBox());
var passWidth = numberTitle2.node().getBBox().width;
那么一半时间它将记录[object SVGRect]
并将passWidth设置为宽度,这真棒.但是其他时候它记录[object Exception]
,不记录宽度,并破坏跟随它的其余JavaScript.
So half the time it will log [object SVGRect]
and set passWidth to the width, that's awesome. BUT the other times it logs [object Exception]
, doesn't log the width, and breaks the rest of the JavaScript following it.
在Exception对象的内部,有name: "NS_ERROR_FAILURE"
和result: 2147500037
Inside the Exception object there's name: "NS_ERROR_FAILURE"
and result: 2147500037
关于如何使其平稳运行或获得svg元素宽度的更好方法的任何想法?
Any ideas on how to get this to work smoothly or a better way to get the width of an svg element?
推荐答案
如果未呈现文本,则在Firefox中将失败.例如.它显示为:none或您尚未将其附加到文档.
It fails in Firefox if the text is not rendered. E.g. it is display:none or you've not attached it to the document.
这篇关于SVG-> getBBox失败,但只有一半时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!