获取svg元素尺寸的正确方法是什么?
http://jsfiddle.net/langdonx/Xkv3X/
Chrome 28:
style x
client 300x100
offset 300x100
Run Code Online (Sandbox Code Playgroud)
IE 10:
stylex
client300x100
offsetundefinedxundefined
Run Code Online (Sandbox Code Playgroud)
FireFox 23:
"style" "x"
"client" "0x0"
"offset" "undefinedxundefined"
Run Code Online (Sandbox Code Playgroud)
有宽度和高度属性svg1,但.width.baseVal.value只有在我设置元素的宽度和高度属性时才设置.
小提琴看起来像这样:
HTML
<svg id="svg1" xmlns="http://www.w3.org/2000/svg" version="1.1">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="1" fill="red" />
<circle cx="150" cy="50" r="40" stroke="black" stroke-width="1" fill="green" />
<circle cx="250" cy="50" r="40" stroke="black" stroke-width="1" fill="blue" />
</svg>
Run Code Online (Sandbox Code Playgroud)
JS
var svg1 = document.getElementById('svg1');
console.log(svg1);
console.log('style', svg1.style.width + 'x' + svg1.style.height);
console.log('client', svg1.clientWidth + 'x' + svg1.clientHeight);
console.log('offset', …Run Code Online (Sandbox Code Playgroud) 我正在使用 d3,我想使用 SVG 元素具有的 getBBox 方法。
我正在使用以下内容:(d3Selection.node() as SVGElement).getBBox(),但是由于标题中的错误,TypeScript 无法编译。
SVGElement 是错误的类型吗?我可以any改用它,但这似乎有点“不干净”的解决方案。