如何使用 JavaScript 获取缩放后的 SVG 元素的宽度?

发布于 2025-01-03 07:07:22 字数 776 浏览 2 评论 0原文

如果我有内联 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个少女永远怀春 2025-01-10 07:07:22

请检查这个小提琴http://jsfiddle.net/T723E/。单击矩形并注意 Firebug 控制台。
这里我硬编码了一个数字 .3,它是包含 svg 节点/1000 个用户单位的 300px 宽度的 div。

看到赏金后,这是我返回的函数,无需太多(没有数学我不确定。)maths.Use matrix.d 获取缩放高度

    var svg = document.getElementById('svg'); 
    function getTransformedWidth(el){
        var matrix = el.getTransformToElement(svg);
        return matrix.a*el.width.animVal.value;
    }

    var ele = document.getElementById('myElement_with_scale')
    console.log("scale width----", getTransformedWidth(ele))

请看这个小提琴完整代码 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 height

    var svg = document.getElementById('svg'); 
    function getTransformedWidth(el){
        var matrix = el.getTransformToElement(svg);
        return matrix.a*el.width.animVal.value;
    }

    var ele = document.getElementById('myElement_with_scale')
    console.log("scale width----", getTransformedWidth(ele))

Please look this fiddle for complete code http://jsfiddle.net/b4KXr/

暗藏城府 2025-01-10 07:07:22

您是否研究过可以与 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文