Obtaining an original SVG viewBox via javascript

后端 未结 4 1796
南笙
南笙 2021-01-07 17:53

Our system loads SVG files programmatically into HTML via AJAX. A typical SVG file begins with:

 

        
4条回答
  •  情歌与酒
    2021-01-07 18:38

    You'll want to take a look at the SVGFitToViewBox interface, which specifies the viewBox property. The interface for svg elements, SVGSVGElement, extends that interface, so this property is available on the element objects:

    const svgElement = document.getElementById("example-svg");
    const {x, y, width, height} = svgElement.viewBox.baseVal;
    

提交回复
热议问题