【问题标题】:Rendering through javascript通过 javascript 渲染
【发布时间】:2012-08-15 22:13:05
【问题描述】:

我一直在尝试通过 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');

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

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

图片:https://sphotos-b.xx.fbcdn.net/hphotos-snc6/179594_10150982737360698_1827200234_n.jpg 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>
</svg>

PS : 已经尝试过 this.m_svg.innerHTML = svg 代码(不适用于 safari)

【问题讨论】:

标签: javascript image web svg render


【解决方案1】:

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

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

更多信息请点击此处: https://stackoverflow.com/a/6410814/873836

【讨论】:

  • 啊,好的。我一直对 SVG 有疑问,现在通常只使用 Raphael 来简化它
【解决方案2】:

This 向您展示了如何访问作为&lt;object&gt; 标记内容的元素。

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2012-02-03
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多