将子节点移动到另一个节点 元素弄乱了设计

问题描述 投票:0回答:2

我有一个带有根节点和4个子节点的静态svg图。我想分别对子节点进行分组,但这样设计就会被分解。这是工作代码

<svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
        <g id="root_node" transform="matrix(1 0 0 1 250 100)">
          <rect width="200" height="60" rx="5" ry="5">
          </rect>
          <text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
            Root Node
          </text>   
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(0, 0) rotate(-135)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(200 0) rotate(-45)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(0 60) rotate(135)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(200 60) rotate(45)" stroke="black" stroke-width="3"/>
        </g>
        <g id="child_node">
          <rect width="180" height="60" x="0" y="0" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="500" y="0" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="0" y="200" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="500" y="200" rx="5" ry="5">
          </rect>
          <text x="100" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
          <text x="600" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
          <text x="100" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
          <text x="600" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
        </g>
        <g id="edges">
          
        </g>
      </svg>

当我将上面的代码更改为以下我希望将子节点分组到一个地方时,我没有得到上述设计

 <svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
        <g id="root_node" transform="matrix(1 0 0 1 250 100)">
          <rect width="200" height="60" rx="5" ry="5">
          </rect>
          <text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
            Root Node
          </text>
        </g>
        <g id="child_node">
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(0, 0) rotate(-135)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(200 0) rotate(-45)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(0 60) rotate(135)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(200 60) rotate(45)" stroke="black" stroke-width="3"/>
          
          <rect width="180" height="60" x="0" y="0" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="500" y="0" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="0" y="200" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="500" y="200" rx="5" ry="5">
          </rect>
          <text x="100" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
          <text x="600" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
          <text x="100" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
          <text x="600" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
        </g>
        
      </svg>

我想知道这背后的机制。有人可以解释一下吗?我是否必须在child_nodes组中使用转换才能使其正常工作?

javascript html svg
2个回答
0
投票

小实验,请问是否有你不理解的东西,我会通过更新来回答它。我还不能发表评论。对不起,我的英语不好 :)

<svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
 <!-- THIS IS THE ROOT OBJECT-->
 <!-- transform used for [scaling,rotating,translating,etc] -->
 <g id="root_node" transform="matrix(1 0 0 1 290 0)" fill="blue">
          
  <!-- THIS FOR SET OBJECT (rx & ry for set border-radius) -->
  <rect width="200" height="60" rx="5" ry="5"></rect>
  <!--  OBJECT TEXT -->
  <text x="100" y="35" font-size="14" text-anchor="middle" fill="white">Root Node</text>
 </g>
        
 <!-- THIS IS THE CHILD OBJECT-->
 <g id="child_node">
  <!-- THIS IS FOR LINE (delete if you dont want to use)
  <line x1="0" y1="0" x2="200" y2="0" transform="translate(0, 0) rotate(-135)" stroke="black" stroke-width="3"/>
  <line x1="0" y1="0" x2="100" y2="0" transform="translate(200 0) rotate(-45)" stroke="black" stroke-width="3"/>
  <line x1="0" y1="0" x2="100" y2="0" transform="translate(0 60) rotate(135)" stroke="black" stroke-width="3"/>
  <line x1="0" y1="0" x2="100" y2="0" transform="translate(200 60) rotate(45)" stroke="black" stroke-width="3"/> -->
          
  <!-- THIS FOR SET OBJECT & move position foreach child -->
  <!-- change value x or y to move object-->
  <rect width="180" height="60" x="0" y="100" rx="5" ry="5"></rect>
  <rect width="180" height="60" x="200" y="100" rx="5" ry="5"></rect>
  <rect width="180" height="60" x="400" y="100" rx="5" ry="5"></rect>
  <rect width="180" height="60" x="600" y="100" rx="5" ry="5"></rect>
  <!--  OBJECT TEXT -->
  <!-- dont forget move the text position ( x , y ) to move it into the object -->
  <text x="100" y="135" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
  <text x="300" y="135" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
  <text x="500" y="135" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
  <text x="700" y="135" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
 </g>
</svg>

0
投票

在您的示例中将子注释移动到其他组会混淆输出,因为您在一个组上使用矩阵转换而不在其他组上 - 原始输出因此导致子节点的属性和父节点上的转换(g)。

如果您从某个地方(例如,Illustrator)导出SVG,则根据原始文件中的要求对节点进行分组会更容易。或者去掉变换并相应地设置子注释。

我想在Illustrator中打开SVG更容易,做必要的更改然后再次导出它,就像我做的那样获得附加的片段。

其他选项是组合矩阵变换和线的变换。基本上你的#root_node只是被翻译了(x:250px,y:100px)。在您的情况下,您只需要翻译行x + = 250&y + = 100。

<svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
        <g id="root_node" transform="matrix(1 0 0 1 250 100)">
          <rect width="200" height="60" rx="5" ry="5">
          </rect>
          <text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
            Root Node
          </text>   
        </g>
        <g id="child_node">
          <rect width="180" height="60" x="0" y="0" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="500" y="0" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="0" y="200" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="500" y="200" rx="5" ry="5">
          </rect>
          <text x="100" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
          <text x="600" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
          <text x="100" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
          <text x="600" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
          <!-- option 1 -->
          <!-- use transform in lines (x1, y1) and (x2, y2) -->
          <line class="st3" x1="250" y1="100" x2="179.3" y2="29.3" stroke="black" stroke-width="3"/>
          <line class="st3" x1="450" y1="100" x2="520.7" y2="29.3" stroke="black" stroke-width="3"/>
          <line class="st3" x1="250" y1="160" x2="179.3" y2="230.7" stroke="black" stroke-width="3"/>
          <line class="st3" x1="450" y1="160" x2="520.7" y2="230.7" stroke="black" stroke-width="3"/>
          <!-- option 2 -->
          <!-- combine group transform (translate) and line's transform -->
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(250, 100) rotate(-135)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(450, 100) rotate(-45)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(250, 160) rotate(135)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(450, 160) rotate(45)" stroke="black" stroke-width="3"/>
        </g>
        <g id="edges">
          
        </g>
      </svg>
      
      <svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
        <g id="root_node" transform="matrix(1 0 0 1 250 100)">
          <rect width="200" height="60" rx="5" ry="5">
          </rect>
          <text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
            Root Node
          </text>
        </g>
        <g id="child_node">
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(250, 100) rotate(-135)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(450, 100) rotate(-45)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(250, 160) rotate(135)" stroke="black" stroke-width="3"/>
          <line x1="0" y1="0" x2="100" y2="0" transform="translate(450, 160) rotate(45)" stroke="black" stroke-width="3"/>
          
          <rect width="180" height="60" x="0" y="0" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="500" y="0" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="0" y="200" rx="5" ry="5">
          </rect>
          <rect width="180" height="60" x="500" y="200" rx="5" ry="5">
          </rect>
          <text x="100" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
          <text x="600" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
          <text x="100" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
          <text x="600" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
        </g>
        
      </svg>
© www.soinside.com 2019 - 2024. All rights reserved.