EML
EML

Reputation: 10280

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

Upvotes: 2

Views: 1529

Answers (1)

Peter Collingridge
Peter Collingridge

Reputation: 10979

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

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

Upvotes: 1

Related Questions