简体   繁体   English

我如何确定任何的viewBox <svg> 来自JavaScript的元素?

[英]How can I determine the viewBox of any <svg> Element from JavaScript?

I'd like to determine the viewBox of an element (ie an SVGSVGElement) from a JavaScript function. 我想从JavaScript函数确定一个元素(即SVGSVGElement)的viewBox。 Calling: 致电:

svgElement.getAttribute("viewBox")

works great when the viewBox is explicitly set on the element. 在元素上明确设置viewBox时,效果很好。 But I cannot assume this will always be the case. 但是我不能认为情况总是如此。 For instance, I'd like my function to work on all of these tags: 例如,我希望我的函数可以在所有这些标签上使用:

(400, 100) <svg width=400 height=100></svg>
(400, 100) <svg width="400px" height="100px"></svg>
(400, 100) <svg style="width: 400px; height: 100px"></svg>
(100,  25) <svg viewBox="0 0 100 25" style="width: 400px; height: 100px"></svg>
(100,  25) <svg viewBox="0 0 100 25" width=400 height=100></svg>

(The values in parens are what the function should return in each case). (parens中的值是函数在每种情况下应返回的值)。 I'd like to be able to do this across all browsers which support SVG and at all zoom levels. 我希望能够在所有支持SVG和所有缩放级别的浏览器中执行此操作。

Here's a JSFiddle with one (failed) attempt: http://jsfiddle.net/w4BTD/1/ 这是一个尝试(失败)的JSFiddle: http : //jsfiddle.net/w4BTD/1/

The viewbox is to specify that a given set of graphics stretch to fit a particular container element. 视图框将指定一组给定的图形进行拉伸以适合特定的容器元素。 This attribute should be set when you want that behaviour. 需要该行为时应设置此属性。

Setting the attribute to the same size of the svg element would be a little bit redundant (if you are not going to change it ever), you would make the code smaller but it would be unnecessary or even wrong. 将属性设置为svg元素的大小相同会有点多余(如果您永远都不会更改它),则可以使代码变小,但是这是不必要的甚至是错误的。

I would check if the viewbox attribute is set, if not, ask for the size of the svg which will be the same. 我将检查是否设置了viewbox属性,如果没有设置,则要求svg的大小相同。 The SVG spec will not help you with that... SVG规范将无法帮助您...

This question is rather old I know.. yet I recently started to work with SVG on crossbrowser and ran into the same problem. 我知道这个问题比较老。.但是我最近开始在跨浏览器上使用SVG,遇到了同样的问题。

Given a bunch of SVG files some with viewBox and some without, every browser reacts a bit differently on displaying them. 给定一堆SVG文件,有些带有viewBox,有些没有,每个浏览器在显示它们时的反应都有些不同。

The key part and also the part which I could not find yet on this site was, that it is possible to assume the viewBox by using the DOMParser(). 关键部分以及我在该站点上尚未找到的部分是,可以通过使用DOMParser()来假定viewBox。

With this, the missing viewBox can bet set before displaying the svg. 这样,可以在显示svg之前设置缺少的viewBox。

This is a simplified example, however it might help others who ended up having the same problem: 这是一个简化的示例,但是它可以帮助最终遇到相同问题的其他人:

//Parsing a svg-string ('<svg ...></svg>') to an actual element
var tP = new DOMParser().parseFromString(s, 'image/svg+xml');

//Assuming no errors happened, this is going to be the <svg>
tS = document.importNode(tP.documentElement, true);

//Check for a viewBox attribute
var tV = tS.getAttribute('viewBox');

//If it does not exists, it gets set to the actual document dimensions which are provided by the parser
(!tV) && tS.setAttribute('viewBox', [0, 0, tP.documentElement.attributes.width.value, tP.documentElement.attributes.height.value].join(' '));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM