【问题标题】:editing an inkscape svg in javascript在javascript中编辑inkscape svg
【发布时间】:2012-06-13 13:30:45
【问题描述】:

我在inkscape 中创建了一个svg,上面有两个不同颜色的矩形和4 个文本叠加层。我目前能够存储 svg 并在 javascript 中调用它并让它显示在网页上。但是,我们正在尝试使文本动态化,以便使用一个 svg 多次表示存储在数据库中的数据。这意味着每次在javascript中调用svg时,我们都需要更改inkscape svg参数以具有不同的文本显示。

有什么想法吗?我们在访问 inkscape svg 内部时遇到问题。我们知道每个文本项都有自己的 id,但我们无法在 javascript 代码中更改 inkscape svg 的任何参数。

谢谢!

代码:

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="500"
   height="50"
   id="svg5768"
   version="1.1"
   inkscape:version="0.48.2 r9819"
   sodipodi:docname="fusiongene2.svg">
  <defs
     id="defs5770" />
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="2"
     inkscape:cx="148.31325"
     inkscape:cy="50.507627"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="1440"
     inkscape:window-height="838"
     inkscape:window-x="-8"
     inkscape:window-y="-8"
     inkscape:window-maximized="1" />
  <metadata
     id="metadata5773">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1"
     transform="translate(0,-1002.3622)">
    <rect
       style="fill:#00ff00;fill-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none"
       id="rect4312"
       width="276.7818"
       height="48.487324"
       x="1.0101526"
       y="1.5126777"
       transform="translate(0,1002.3622)" />
    <rect
       style="fill:#0000ff;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.1;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
       id="rect4314"
       width="235.36554"
       height="48.487324"
       x="263.64981"
       y="1003.8749" />
    <text
       xml:space="preserve"
       style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
       x="13.5"
       y="1037.8622"
       id="text4316"
       sodipodi:linespacing="125%"><tspan
         sodipodi:role="line"
         id="tspan4318"
         x="13.5"
         y="1037.8622"
         style="font-size:20px">Gene1</tspan></text>
    <text
       xml:space="preserve"
       style="font-size:40px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
       x="169.5"
       y="1037.8622"
       id="text4320"
       sodipodi:linespacing="125%"><tspan
         sodipodi:role="line"
         id="tspan4322"
         x="169.5"
         y="1037.8622"
         style="font-size:20px">Chrom1</tspan></text>
    <rect
       y="1002.8749"
       x="0.010152578"
       height="49.487324"
       width="277.7818"
       id="rect4324"
       style="fill:#00ff00;fill-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none" />
    <rect
       y="1002.8298"
       x="263.64981"
       height="49.532394"
       width="235.90593"
       id="rect4326"
       style="fill:#0000ff;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.10118789;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />
    <text
       sodipodi:linespacing="125%"
       id="text4328"
       y="1006.8973"
       x="13.915162"
       style="font-size:38.80659103px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
       xml:space="preserve"
       transform="scale(0.97016475,1.0307528)"><tspan
         style="font-size:19.40329552px"
         y="1006.8973"
         x="13.915162"
         id="tspan4330"
         sodipodi:role="line">Gene1</tspan></text>
    <text
       sodipodi:linespacing="125%"
       id="text4332"
       y="1006.8973"
       x="145.32069"
       style="font-size:38.80659103px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
       xml:space="preserve"
       transform="scale(0.97016475,1.0307528)"><tspan
         style="font-size:19.40329552px"
         y="1006.8973"
         x="145.32069"
         id="tspan4334"
         sodipodi:role="line">Chrom1</tspan></text>
    <text
       sodipodi:linespacing="125%"
       id="text4328-1"
       y="1006.8973"
       x="276.59357"
       style="font-size:38.80659103px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
       xml:space="preserve"
       transform="scale(0.97016475,1.0307528)"><tspan
         style="font-size:19.40329552px"
         y="1006.8973"
         x="276.59357"
         id="tspan4330-7"
         sodipodi:role="line">Gene2</tspan></text>
    <text
       sodipodi:linespacing="125%"
       id="text4332-4"
       y="1006.8973"
       x="407.99911"
       style="font-size:38.80659103px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
       xml:space="preserve"
       transform="scale(0.97016475,1.0307528)"><tspan
         style="font-size:19.40329552px"
         y="1006.8973"
         x="407.99911"
         id="tspan4334-0"
         sodipodi:role="line">Chrom2</tspan></text>
  </g>
</svg>

【问题讨论】:

  • 请给我们一些代码,否则我们无能为力...
  • 这里是 inkscape 代码。您可以看到我们试图在底部修改的文本作为通用名称而不是特定名称:
  • 您应该在 Inkscape 中保存为“优化的 svg”,这样会产生更适合放在网络上的输出。

标签: javascript svg inkscape


【解决方案1】:

根据您的 SVG 包含在 HTML 中的方式,您可以像其他任何方法一样简单地使用 getElementById 引用 tspan 元素:

document.getElementById('tspan4330').firstChild.textContent = '1 Rob was here';
document.getElementById('tspan4334').firstChild.textContent = '2 Rob was here';
document.getElementById('tspan4330-7').firstChild.textContent = '3 Rob was here';
document.getElementById('tspan4334-0').firstChild.textContent = '4 Rob was here';

Demo.

如果您已将其包含在 object 中,那么您需要监听 load 事件并获取 contentDocument as described in this answer

【讨论】:

    【解决方案2】:

    我昨天也遇到了同样的问题。对我来说,这与 robertc 的回答相同,只是我做了:

    document.getElementById('元素的 id').textContent = '你的新文本';

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多