Get the real size of a SVG/G element

后端 未结 4 1211
耶瑟儿~
耶瑟儿~ 2020-12-24 02:31

Is there any accurate way to get the real size of a svg element that includes stroke, filters or other elements contributing to the element\'s real size from within Javascri

4条回答
  •  孤独总比滥情好
    2020-12-24 03:14

    You can't get the values directly. However, you can get the dimensions of the bounding rectangle:

    var el   = document.getElementById("yourElement"); // or other selector like querySelector()
    var rect = el.getBoundingClientRect(); // get the bounding rectangle
    
    console.log( rect.width );
    console.log( rect.height);
    

    It is supported at least in the actual versions of all major browser.

    Check fiddle

提交回复
热议问题