【问题标题】:Combine two SVG elements into one using Javascript?将两个SVG元素组合为使用JavaScript?
【发布时间】:2019-08-12 15:33:02
【问题描述】:

假设我有以下 html 上下文,我无权访问它的创建:

<div id="outer">
  <div id="chart-div">
    <svg id="chart"></svg>
  </div>
  <div id="legend-div">
    <svg id="legend"></svg>
  </div>
</div>

我要做的是使用canvg库将此SVG导出到图像,这里的问题是它们是分开的,所以我得到两个画布,canvg库接受一个SVG定义字符串作为输入。

如何修改上面的 html 元素,使它们只是一个 SVG?使用 Javascript,因为这是一个浏览器扩展。

我尝试将 DIVs 标签切换为 SVG,但它破坏了一切,SVG 变为空白

【问题讨论】:

  • 请附上示例 svg 内容
  • 更简单的方法是将两个画布混合到一个图像中

标签: javascript html svg html5-canvas canvg


【解决方案1】:

这是我对您的问题的解决方案:我制作了外部 div display:none,我正在创建另一个 svg 元素(您可以动态地这样做)在 #new svg 内结束我正在使用 #chart#legend。希望对你有帮助。

svg{border:1px solid;}
#outer {display:none}
#outer div{position:absolute; width:500px;}
<div id="outer">
  <div id="chart-div">
    <svg id="chart" viewBox="0 0 300 150">
      <circle stroke="gold" fill="none" cx="100" cy="75" stroke-width="40" stroke-dasharray="124.85" stroke-dashoffset="20" r="20"  />
    </svg>
  </div>
  <div id="legend-div">
    <svg id="legend"  viewBox="0 0 300 150">
      <rect fill="skyBlue" x="200" y="100" width="80" height ="30" />
    </svg>
  </div>
</div>


<svg id="new" viewBox="0 0 300 150" width="500"> 
  <use xlink:href="#chart" />
  <use xlink:href="#legend" />
</svg>

【讨论】:

    【解决方案2】:

    这是一个 Javascript 解决方案,用于通过 DOM 操作合并文档中的两个 svg 可访问项。

    var svgNS = "http://www.w3.org/2000/svg";  
    var outer = document.getElementById('outer');
    
    // get chart content
    var chart = document.getElementById('chart-div');
    var chartSvg = chart.getElementsByTagName('svg')[0];
    var chartContent = Array.from(chartSvg.childNodes);
    
    // get legend content
    var legend = document.getElementById('legend-div');
    var legendSvg = legend.getElementsByTagName('svg')[0];
    var legendContent = Array.from(legendSvg.childNodes);
    
    // create a merged-div where we are going to merge the svgs
    var merged = document.createElement('div');
    merged.setAttribute('id', 'merged-div');
    outer.appendChild(merged);
    
    // createElementNS for svg
    var mergedSvg = document.createElementNS(svgNS, 'svg');
    mergedSvg.setAttribute('id', 'merged');
    // keep the viewBox of the chart
    mergedSvg.setAttribute('viewBox', chartSvg.getAttribute('viewBox'));
    merged.appendChild(mergedSvg);
    
    // adding the content of both svgs
    for (let i = 0; i < chartContent.length; i++) {
      mergedSvg.appendChild(chartContent[i]);
    }
    for (let i = 0; i < legendContent.length; i++) {
      mergedSvg.appendChild(legendContent[i]);
    }
    
    // the unmerged svgs can be removed
    chart.remove();
    legend.remove();
    <div id="outer">
      <div id="chart-div">
        <svg id="chart" viewBox="0 0 300 150">
          <circle stroke="gold" fill="none" cx="100" cy="75" stroke-width="40" stroke-dasharray="124.85" stroke-dashoffset="20" r="20"  />
        </svg>
      </div>
      <div id="legend-div">
        <svg id="legend"  viewBox="0 0 300 150">
          <rect fill="skyBlue" x="200" y="100" width="80" height ="30" />
        </svg>
      </div>
    </div>

    结果标记:

    
        <div id="outer">
            <div id="merged-div">
                <svg id="merged" viewBox="0 0 300 150">
                    <circle stroke="gold" fill="none" cx="100" cy="75" stroke-width="40" stroke-dasharray="124.85" stroke-dashoffset="20" r="20"></circle>
                    <rect fill="skyBlue" x="200" y="100" width="80" height="30"></rect>
                </svg>
            </div>
        </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      相关资源
      最近更新 更多