直接从Javascript代码访问SVG文件 [英] Accessing SVG file directly from Javascript code

查看:133
本文介绍了直接从Javascript代码访问SVG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML代码,它正在调用我的javascript代码。代码适用于仪表。在javascript代码中,我试图访问SVG文件,并修改针(指标)以显示所需的值。代码工作正常。但是,我不希望在HTML中调用object id。我想直接通过javascript访问SVG文件,而不是在HTML中使用对象ID。我尝试使用el.setAttribute('data','gauge.svg');但是后来svg_doc无法检索SVG图像并修改针。任何帮助都将受到高度赞赏。

I have this HTML code, which is invoking my javascript code. The code is for a gauge. In the javascript code, I am trying to access a SVG file, and modifying the needle (of the gauge) to display the desired value. The code is working fine. However, I do not wish to call "object id" in HTML. I want to access SVG file through javascript directly, instead of using object id in HTML. I tried using el.setAttribute('data', 'gauge.svg'); But then svg_doc isn't able to retrieve the SVG image and modify the needle. Any help would be highly appreciated.

PS:我尽力解释问题。但是,如果我在某处不清楚,请告诉我。

PS : I tried my best to be as thorough in explaining the problem. However, please let me know if I am unclear somewhere.

这是Gauge.png图片,它嵌入在我粘贴在下面的svg代码中 https://sphotos-b.xx.fbcdn.net/hphotos-snc6/179594_10150982737360698_1827200234_n.jpg

This is Gauge.png image which is embedded in the svg code I have pasted below https://sphotos-b.xx.fbcdn.net/hphotos-snc6/179594_10150982737360698_1827200234_n.jpg

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <g name="gauge" width="122px" height="127px">
        <image xlink:href="gauging.png" width="122" height="127"/>
    <circle id="led" cx="39" cy="76" r="5" style="fill: #999; stroke: none">
        <animateColor id="ledAnimation" attributeName="fill" attributeType="css" begin="0s" dur="1s"
        values="none;#f88;#f00;#f88;none;" repeatCount="0"/>
    </circle>
        <g id="needle" transform="rotate(0,62,62)">
            <circle cx="62" cy="62" r="4" style="fill: #c00; stroke: none"/>
            <rect transform="rotate(-130,62,62)" name="arrow"  x="58" y="38" width="8" height="24" style="fill: #c00; stroke: none"/>
            <polygon transform="rotate(-130,62,62)" points="58,39,66,39,62,30,58,39" style="fill: #c00; stroke: none"/>
        </g>
        <text id="value" x="51" y="98" focusable="false" editable="no" style="stroke:none; fill:#fff; font-family: monospace; font-size: 12px"></text>
    </g>
</svg>

HTML + Javascript代码

    <head>
    <title>SVG Gauge example</title>

    <script>
    function update1(){
        var scale=100;
        var value;
        var value1 = 69;

        var el=document.getElementById('gauge1');       
        if (!el) return;

        /* Get SVG document from HTML element */
        var svg_doc = el.contentDocument;
        if (!svg_doc) return;


        /* Rotate needle to display given value */
        var needle_el = svg_doc.getElementById('needle');
        if (!needle_el) return;
        /* Calc rotation angle (0->0%, 260->100%) */
        value = parseInt(value1);
        scale = parseInt(scale);
        if (value > scale) value = scale;
        var angle = value / scale * 260;
        /* On-the-fly SVG transform */
        needle_el.setAttribute('transform','rotate('+angle+',62,62)');
    }
document.addEventListener('load', update1, true);
    </script>

    </head>

<div>
<object id="gauge1" type="image/svg+xml" data="gauge.svg" width="127" height="122"/>
</div>


</html>


推荐答案

正如robertc已经提到的,你可以嵌入javascript代码进入你的SVG文件:

As robertc already mentioned, you can embed the javascript code into your SVG file:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <g name="gauge" width="122px" height="127px">
        <image xlink:href="gauging.png" width="122" height="127"/>
    <circle id="led" cx="39" cy="76" r="5" style="fill: #999; stroke: none">
        <animateColor id="ledAnimation" attributeName="fill" attributeType="css" begin="0s" dur="1s"
        values="none;#f88;#f00;#f88;none;" repeatCount="0"/>
    </circle>
        <g id="needle" transform="rotate(0,62,62)">
            <circle cx="62" cy="62" r="4" style="fill: #c00; stroke: none"/>
            <rect transform="rotate(-130,62,62)" name="arrow"  x="58" y="38" width="8" height="24" style="fill: #c00; stroke: none"/>
            <polygon transform="rotate(-130,62,62)" points="58,39,66,39,62,30,58,39" style="fill: #c00; stroke: none"/>
        </g>
        <text id="value" x="51" y="98" focusable="false" editable="no" style="stroke:none; fill:#fff; font-family: monospace; font-size: 12px"></text>
    </g>

    <script type="text/javascript">
        var scale=100;
        var value;
        var value1 = 69;

        /* Rotate needle to display given value */
        var needle_el = document.getElementById('needle');
        /* Calc rotation angle (0->0%, 260->100%) */
        value = parseInt(value1);
        scale = parseInt(scale);
        if (value > scale) value = scale;
        var angle = value / scale * 260;
        /* On-the-fly SVG transform */
        needle_el.setAttribute('transform','rotate('+angle+',62,62)');

    </script>
</svg>

我把代码放在实际的SVG内容下面,以便在脚本中加载文档执行。

I've put the code below the actual SVG contents so that the document is already loaded when the script is executed.

然后,您可以直接查看SVG文件,例如在Firefox中(我现在已经测试过了。)

Then, you can view the SVG file directly e.g. in Firefox (I've tested it right now).

这篇关于直接从Javascript代码访问SVG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆