Get pixel length of String in Svg

后端 未结 2 405
太阳男子
太阳男子 2020-12-25 11:28

I\'m currently working with SVG. I need to know the string length in pixels in order to do some alignment. How can I do to get the length of a string in pixel ?

2条回答
  •  被撕碎了的回忆
    2020-12-25 12:21

    I've been wondering this too, and I was pleasantly surprised to find that, according to the SVG spec, there is a specific function to return this info: getComputedTextLength()

    // access the text element you want to measure
    var el = document.getElementsByTagName('text')[3];
    el.getComputedTextLength(); // returns a pixel integer
    

    Working fiddle (only tested in Chrome): http://jsfiddle.net/jyams/

提交回复
热议问题