通过javascript进行渲染

Rendering through javascript

本文关键字:javascript 通过      更新时间:2023-09-26

我一直在尝试通过javascript渲染svg图像。尝试了各种选择,但最终还是要解决这个问题。

我已经在html中声明了一个对象id,就像这个

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

现在,我一直在尝试通过javascript访问这个gauge.svg。我已经创建了一个嵌入类型的元素,比如这个

this.m_svg = document.createElement('embed');

然后尝试使用setAttribute访问图像,但没有成功。有什么建议吗?

还将svg代码与图像一起粘贴到此处。

图像:https://sphotos-b.xx.fbcdn.net/hphotos-snc6/179594_10150982737360698_1827200234_n.jpgsvg代码:

<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>

PS:已经尝试过this.m_svg.innerHTML=svg代码(不在safari上工作)

这将向您展示如何访问作为<object>标记内容的元素。

确保您的标头设置正确,我知道webkit的SVG实现可能会有点选择性,除非标头设置为:

<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />

有关更多信息,请查看此处:https://stackoverflow.com/a/6410814/873836