开发者

SVG: Calculating bounding box without displaying object

I need to get a text bounding box to adjust my layout before rendering anything. With some experimenting, I found that I had to actually render the text before 'getBBox' (or 'getComputedTextLength') will return a non-zero value:

   var group = svgDocument.createElementNS(svgns, "g");
   for(i=0; i <= nYblocks; ++i) {
      str = svgDocument.createTextNode(strings[i]);
      obj = tnode.cloneNode(true);
      obj.setAttributeNS(null, "y", y1);
      obj.appendChild(str);
      group.appendChild(obj);
      y1 += yBlockPx;
   }

   svgDocument.documentElement.appendChild(group);  // **REQUIRED**
   bb = vgroup.getBBox();

Problem: is there a good way to render the text so that it doesn't actually display? Should I just adjust the colours or opacity, or is there something clever I can do 开发者_开发百科to render somewhere else, perhaps in a different tree?

Thanks -

Al


I think the easiest option is to draw it with the visibility set to hidden:

obj.setAttributeNS(null, "visibility", "hidden");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜