1

Let's say I have the following code:

http://bl.ocks.org/mbostock/1160929

And I know that instead of "Hello, getBBox!" my string will be much longer and overflow the svg. One option to get the width of the overflow is to render it and do getBBox().width on the element. Mike Bostock does this in the link above.

Is there a way to precompute the width of the overflow before actually rendering the svg?

5
  • 3
    What's wrong with rendering it? You can always render it visibility:hidden and then show it when you've done the calculations. Jun 5, 2014 at 8:15
  • Problem with rendering is with sufficient size data (and in turn many dom elements created), rendering the svg may be a bit slow. In this small case I'm only rendering a string, but in my actual use-case there would be much, much more. So I'd prefer to only render once. Hoping there's a more elegant solution, and seems like there should be.
    – bumpkin
    Jun 5, 2014 at 8:26
  • 1
    But you're going to render it anyway once it's calculated right? In orer to calculate the dimensions the browser will have to do the same amount of work as if you attach it as visibility:hidden. Jun 5, 2014 at 8:33
  • The context may influence the size, e.g inherited font-family or similar. Inserting the element into the document tree is the proper way to go. Jun 11, 2014 at 13:57
  • thanks. i ended up drawing, measuring, and then re-drawing. works well.
    – bumpkin
    Jun 12, 2014 at 8:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.