在javascript中编辑inkscape svg

时间:2012-06-13 13:30:46

标签: javascript svg inkscape

我在inkscape中创建了一个带有两个不同颜色矩形和顶部有4个文本覆盖的svg。我目前能够存储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>

2 个答案:

答案 0 :(得分:0)

根据您的SVG包含在HTML中的方式,您可能只能像tspan一样引用getElementById元素:

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

答案 1 :(得分:0)

昨天我遇到了同样的问题。对我来说,它与罗伯特的回答一样,只有我这样做:

document.getElementById(' element's id ')。textContent =' your new text ';