【问题标题】:Find the center of SVG groups or shapes查找 SVG 组或形状的中心
【发布时间】:2021-03-25 12:03:34
【问题描述】:

我需要在 SVG 平面图上的房间中心放置标记(圆圈)。每个房间可以是单个形状(如矩形)或一组形状。圆圈应覆盖房间的大约 1/4: 半径 = (宽 + 高) / 8

svg 需要调整到其 div 容器的大小。

经过大量的试验和错误后,我想出了一些似乎可以工作的代码,但在我使用 getBBox()、getBoundingClientRect() 和 matrixTransform() 时也显得过于复杂:

  • getBBox 为我提供了正确的宽度和高度,但对于组元素,x = y = 0
  • getBoundingClientRect + matrixTransform 给了我正确的 cx 和 cy 坐标,但不是正确的房间宽度和高度。

有没有更简单的方法?

const draw = document.getElementsByTagName("svg")[0];

const NS = draw.getAttribute("xmlns");

// Get titles
const titleElements = draw.getElementsByTagNameNS(NS, "title");

Array.from(titleElements).forEach((titleElement) => {
  let parentElement = titleElement.parentElement;
  let clientRect = parentElement.getBoundingClientRect();
  let bbox = parentElement.getBBox();

  let point = draw.createSVGPoint();
  point.x = clientRect.left + clientRect.width / 2;
  point.y = clientRect.top + clientRect.height / 2;

  // Convert screen coordinates into SVG document coordinates
  const svgP = point.matrixTransform(draw.getScreenCTM().inverse());

  // clientRect doesn't resize with the svg
  // let radius = (clientRect.width + clientRect.height) / 8;
  // bbox resizes with the svg
  let radius = (bbox.width + bbox.height) / 8;

  let circle = document.createElementNS(NS, "circle");

  circle.setAttribute("cx", svgP.x.toString());
  circle.setAttribute("cy", svgP.y.toString());
  circle.setAttribute("r", radius.toString());
  circle.setAttribute("fill", "cyan");

  draw.appendChild(circle);
});
<!DOCTYPE html>
<html>
  <head>
    <title>Floor Plan</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <div id="app" style="width:300px;height:300px;">
    <svg
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:ev="http://www.w3.org/2001/xml-events"
      viewBox="0 0 792 612"
      xml:space="preserve"
      color-interpolation-filters="sRGB"
      class="st7"
    >
      <g>
        <g id="group1-1" transform="translate(659.691,-186.346) rotate(60)">
          <title>Space.1001</title>
          <g id="shape1-2">
            <rect x="0" y="508.5" width="90" height="90" class="st1" />
          </g>
        </g>
        <g id="group1000-10" transform="translate(482.508,-114.75) rotate(60)">
          <title>Space.1000</title>
          <g id="shape1000-11">
            <rect x="0" y="208.5" width="90" height="90" class="st4" />
          </g>
        </g>
        <g id="group1002-18" transform="translate(659.691,-67.0962) rotate(60)">
          <title>Space.1002</title>
          <g id="shape1002-19">
            <rect x="0" y="508.5" width="90" height="90" class="st5" />
          </g>
        </g>
        <rect x="0" y="208.5" width="90" height="90" class="st6">
          <title>Space.1004</title>
        </rect>
      </g>
    </svg>
    </div>
  </body>
</html>

【问题讨论】:

  • 你的代码基本正确。使用getBoundingClientRect() 并使用getScreenCTM().inverse() 进行转换,或者使用getBBox() 并使用getCTM().inverse() 进行转换。
  • 问题是我使用的是getBoundingClientRect“和”getBBox,而不是“或”。这就是为什么我怀疑必须有更好的方法。但如果我只尝试其中一个,它就会失败。
  • 您的代码分配了bbox,但从不使用它。
  • 我已经修复了代码并重新表述了我的问题。谢谢@ccprog!
  • 啊。我看到了我的错误。 .getCTM() 将变换赋予初始视口,而不是初始坐标系。那么最好的方法就是按照保罗的回答,在转换后的组内移动圆圈。

标签: javascript svg


【解决方案1】:

这里有一些更简单的代码。

const draw = document.getElementsByTagName("svg")[0];

const NS = draw.namespaceURI;

// Do the rooms that are groups
let rooms = document.querySelectorAll("title + g");
rooms.forEach(room => {
  room.appendChild( makeDot(room.getBBox()) );
});

// Do the rooms where the title is in a rect
let titles = document.querySelectorAll("rect > title");
titles.forEach(title => {
  let rect = title.parentElement;
  rect.insertAdjacentElement('afterend', makeDot(rect.getBBox()) );
});


// Make the circle element
function makeDot(roomBounds)
{
  let radius = (roomBounds.width + roomBounds.height) / 8;
  let circle = document.createElementNS(NS, "circle");
  circle.setAttribute("cx", roomBounds.x + roomBounds.width / 2);
  circle.setAttribute("cy", roomBounds.y + roomBounds.height / 2);
  circle.setAttribute("r", radius);
  circle.setAttribute("fill", "cyan");
  return circle;
}
    <div id="app" style="width:300px;height:300px;">
    <svg
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:ev="http://www.w3.org/2001/xml-events"
      viewBox="0 0 792 612"
      xml:space="preserve"
      color-interpolation-filters="sRGB"
      class="st7"
    >
      <g>
        <g id="group1-1" transform="translate(659.691,-186.346) rotate(60)">
          <title>Space.1001</title>
          <g id="shape1-2">
            <rect x="0" y="508.5" width="90" height="90" class="st1" />
          </g>
        </g>
        <g id="group1000-10" transform="translate(482.508,-114.75) rotate(60)">
          <title>Space.1000</title>
          <g id="shape1000-11">
            <rect x="0" y="208.5" width="90" height="90" class="st4" />
          </g>
        </g>
        <g id="group1002-18" transform="translate(659.691,-67.0962) rotate(60)">
          <title>Space.1002</title>
          <g id="shape1002-19">
            <rect x="0" y="508.5" width="90" height="90" class="st5" />
          </g>
        </g>
        <rect x="0" y="208.5" width="90" height="90" class="st6">
          <title>Space.1004</title>
        </rect>
      </g>
    </svg>
    </div>

【讨论】:

  • 谢谢!为组提供专用代码是有意义的。 rect 的代码是否适用于任何形状?正如我的问题中提到的,矩形只是一个例子。我可以有多边形、路径等。
  • 是的。但请注意,它会将点放在形状的 边界框 的中心,而不是形状的中心。如果路径或多边形具有奇怪形状的轮廓,则该点可能会在形状之外消失。找到任意形状轮廓的光学中心是一项复杂得多的任务。
  • 另外,insertAdjacentElement 对我来说可能是个问题,因为该计划可能有多个层次,我需要在所有内容之上添加点。但我明白了。
  • 那可能是个问题。我将它们插入它们所在的位置的原因是它们会受到任何transforms 的影响,这些transforms 可能存在于祖先元素上。如果您需要将它们插入到一个完全独立的图层上,即 SVG 的其他位置,那么您需要以某种方式对圆应用类似的变换。
  • 对,这就是我在原始代码中所做的。对我来说,您的代码的主要收获是将组与形状分开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
相关资源
最近更新 更多