【问题标题】:Use multiple child containers for SVG rendering?使用多个子容器进行 SVG 渲染?
【发布时间】:2016-03-21 06:21:18
【问题描述】:

目前,当我创建我的 SVG 文档时,我使用单个“画布”svg 对象,如下所示:

<svg id="svgCanvas" xmlns="http://www.w3.org/2000/svg" 
   xlink="http://www.w3.org/1999/xlink" width="100%" height="100%"></svg>

然后我这样称呼它:

var svgCanvas = document.getElementById( "svgCanvas" );

然后将所有 SVG 对象作为子对象添加到该对象。例如,如果我创建一条线,我会这样做:

    function createLine( id, x1, y1, x2, y2, color, width ){
        var svgCanvas = document.getElementById( "svgCanvas" );
        var line  = document.createElementNS( svgNS, "line" );
        line.setAttributeNS( null, "id", id );
        line.setAttributeNS( null, "x1", x1 );
        line.setAttributeNS( null, "y1", y1 );
        line.setAttributeNS( null, "x2", x2 );
        line.setAttributeNS( null, "y2", y2 );
        line.setAttributeNS( null, "stroke", color );
        line.setAttributeNS( null, "stroke-width", width );
        svgCanvas.appendChild( line );
    }       

问题是我的图纸既有静态元素又有动态元素。有些东西会四处移动、被删除和重绘、改变形状等。其他东西是静态的并且永久保持不变。

当我清除我的画布时,我不想删除这些永久对象,我只想删除可变对象。所以,理想情况下,我希望有某种容器,它是 svg 对象(svgCanvas)的子对象,它只包含动态对象,这样当我清除时,我可以迭代该容器并删除它的子对象,然后离开静态对象,它们是主 svgCanvas 的子对象。

有没有办法制作这样一个“容器”,它是 svgCanvas 的子对象,并且仍然在 svg 上渲染容器中的所有对象?

另外,我可以通过移除容器来移除所有这些项目,而不需要遍历容器中的每个对象并单独移除它们吗?

* 更新 *

我添加了一个组如下:

    function initialize(){
        svgCanvas = document.getElementById( "svgCanvas" )
        gDynamic  = document.createElementNS( svgNS, "g" );
        gDynamic.setAttribute( "id", "DynamicGroup" );
        svgCanvas.appendChild( gDynamic );
    }

但是,在这样做之后,添加到 svgCanvas 的任何对象都会出现,但子组中的任何对象都不会出现。

【问题讨论】:

    标签: javascript svg containers element


    【解决方案1】:

    &lt;g&gt; 和 &lt;svg&gt; 元素都充当其他元素的容器。从文档中删除它们也会删除它们的所有子项。

    【讨论】:

      【解决方案2】:

      您需要使用组。并且您将需要修改元素创建函数,以便您可以将它们添加到哪个父元素中。

      var svgCanvas = document.getElementById( "svgCanvas" );
      
      // Create a group
      var svgGroup = createGroup(svgCanvas, "myGroupId");
      // Add a line to the group
      createLine(svgGroup, ...etc...);
      
      // do stuff
      
      // Done with group, so remove it, and its children
      svgGroup.parentNode.removeChild(svgGroup);
      
      
      function createLine( parent, id, x1, y1, x2, y2, color, width ) {
          var line  = document.createElementNS( svgNS, "line" );
          line.setAttribute( "id", id );
          line.setAttribute( "x1", x1 );
          line.setAttribute( "y1", y1 );
          line.setAttribute( "x2", x2 );
          line.setAttribute( "y2", y2 );
          line.setAttribute( "stroke", color );
          line.setAttribute( "stroke-width", width );
          parent.appendChild( line );
      }
      
      function createGroup( parent , id ) {
          var group  = document.createElementNS( svgNS, "g" );
          group.setAttribute( "id", id );
          parent.appendChild( group );
      }
      

      【讨论】:

      • 我做了类似的事情(见有问题的代码),但是动态组中的对象都没有出现,只有主要的 svgCanvas 子项出现。
      • 你是如何向组中添加元素的?我们能看到那个代码吗?事实上,您应该创建一个工作代码 sn-p 或 fiddle,以便我们可以看到整个工作正常。
      猜你喜欢
      • 2010-11-05
      • 2011-04-25
      • 2020-03-29
      • 2021-06-04
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      相关资源
      最近更新 更多