Get the real size of a SVG/G element

后端 未结 4 1205
耶瑟儿~
耶瑟儿~ 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 02:54

    Here is an example using D3.js:

    Starting with a div:

    The javascript code looks like this:

    var myDiv = d3.select('div');
    var mySvg = myDiv.append('svg');
    var myPath = mySvg.append('path');
    myPath.attr({
        'fill': '#F7931E',
        'd': 'M37,17v15H14V17H37z M50,0H0v50h50V0z'
    });
    
    // Get height and width.
    console.log(myPath.node().getBBox());
    

提交回复
热议问题