如何使用 JavaScript 获取缩放后的 SVG 元素的宽度?
如果我有内联 SVG,包括已缩放的元素...
<g transform="scale(sX,sY)">
<rect id="myElement" x="107" y="32" width="245" height="31" stroke="#C6C3C6" stroke-width="1px" />
</g>
...或者位于具有 viewBox
属性的 元素中:
<svg viewBox="20 20 5000 1230">
<g transform="scale(sX,sY)">
<rect id="myElement" x="107" y="32" width="245" height="31" stroke="#C6C3C6" stroke-width="1px" />
</g>
<svg>
.. . 如何以编程方式找到 myElement
的新缩放宽度(以像素为单位) - 无需手动检测缩放并进行数学计算?到目前为止 myElement.getBBox().width
返回 245
而不考虑缩放。
If I have inline SVG, including an element which has been scaled...
<g transform="scale(sX,sY)">
<rect id="myElement" x="107" y="32" width="245" height="31" stroke="#C6C3C6" stroke-width="1px" />
</g>
... or which is in a <svg>
element with a viewBox
attribute:
<svg viewBox="20 20 5000 1230">
<g transform="scale(sX,sY)">
<rect id="myElement" x="107" y="32" width="245" height="31" stroke="#C6C3C6" stroke-width="1px" />
</g>
<svg>
... how can I programmatically find the new scaled width in pixels of myElement
- without manually detecting the scaling and doing the math? So far myElement.getBBox().width
returns 245
without accounting for the scaling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请检查这个小提琴http://jsfiddle.net/T723E/。单击矩形并注意 Firebug 控制台。
这里我硬编码了一个数字 .3,它是包含 svg 节点/1000 个用户单位的 300px 宽度的 div。
看到赏金后,这是我返回的函数,无需太多(没有数学我不确定。)maths.Use
matrix.d
获取缩放高度请看这个小提琴完整代码 http://jsfiddle.net/b4KXr/
please check this fiddle http://jsfiddle.net/T723E/. Click on the rectangles and note the firebug console.
Here i have hard coded a number .3 which is 300px width of div containing svg node / 1000 user units.
After seeing the bounty, this is the function i have return to get the scaled width without much (with no maths i'm not sure.)maths.Use
matrix.d
for getting scaled heightPlease look this fiddle for complete code http://jsfiddle.net/b4KXr/
您是否研究过可以与 getBBox() 一起使用的参数?
http://raphaeljs.com/reference.html#Element.getBBox
Have you investigated the parameter you can use with getBBox()?
http://raphaeljs.com/reference.html#Element.getBBox