javascript - 直接从 Javascript 代码访问 SVG 文件

标签 javascript html svg web

我有这个 HTML 代码,它正在调用我的 javascript 代码。该代码用于仪表。在 javascript 代码中,我尝试访问 SVG 文件,并修改(仪表的)指针以显示所需的值。该代码运行良好。但是,我不想在 HTML 中调用“对象 id”。我想直接通过javascript访问SVG文件,而不是在HTML中使用对象id。我尝试使用 el.setAttribute('data', 'gauge.svg');但是 svg_doc 无法检索 SVG 图像并修改针。任何帮助将不胜感激。

PS:我尽力尽可能彻底地解释问题。不过,如果我有不清楚的地方,请告诉我。

这是嵌入在我粘贴在下面的 svg 代码中的 Gauge.png 图像 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 文件中:

<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 内容下方,以便在执行脚本时已加载文档。

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

关于javascript - 直接从 Javascript 代码访问 SVG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11729868/

相关文章:

javascript - 使用带有 CSS 模块的 ReactJS 对 SVG 元素进行样式化

javascript - 根据当前 View 边界加载 map 对象

javascript - Chrome 以百分比正确显示自适应图像和宽度,Firefox 不正确

javascript - 有没有办法隐藏窗口属性?

html - div 上的滚动条溢出 :auto and percentage height

javascript - 是否可以从 javascript 访问 SMIL 计时器?

javascript - 如何解决一系列 promise ?

javascript - 扩展 Chrome,javascript 在页面中写入

javascript - 想要在向下滚动到顶部时更改悬停时的导航链接颜色 --jquery

javascript - 如何在 SVG 中包含 jQuery SVG 源?