javascript - 使用JS获取SVG内部元素的绝对坐标

标签 javascript svg ecmascript-6

好时光论坛用户。我提前为我的英语道歉。我找不到答案(我决定问说英语的观众)。

嵌套在组中的元素相对于父容器 () 的绝对定位(坐标)。

<svg width="100%" height="100%" viewBox="0 0 1000 1000" preserveAspectRatio="xMidYMin slice" x="0" y="0" tabindex="1">
  <g transform="translate(100 100)">
       <g transform="translate(100 100)"> 
            <circle r="50" cx="25" cy="25" fill="yellow" />
       </g>
  </g>
<svg>


我想使用 ES6 + 相对于 SVG 的圆坐标。即 x = 100 + 100 + 25, y = 100 + 100 + 25 。

我怎样才能得到这些坐标? (最多可以无限嵌套组)。感谢您的帮助。

enter image description here

最佳答案

  • 查找 cxcy圆的值
  • 应用圆具有的任何变换
  • 升级到每个祖先元素并应用找到的任何转换
  • 到达根 SVG 元素时停止


  • function getCirclePosition(circleElemId)
    {
      var elem = document.getElementById(circleElemId);
      var svg = elem.ownerSVGElement;
    
      // Get the cx and cy coordinates
      var pt = svg.createSVGPoint();
      pt.x = elem.cx.baseVal.value;
      pt.y = elem.cy.baseVal.value;
    
      while (true)
      {
        // Get this elements transform
        var transform = elem.transform.baseVal.consolidate();
        // If it has a transform, then apply it to our point
        if (transform) {
          var matrix = elem.transform.baseVal.consolidate().matrix;
          pt = pt.matrixTransform(matrix);
        }
        // If this element's parent is the root SVG element, then stop
        if (elem.parentNode == svg)
          break;
        // Otherwise step up to the parent element and repeat the process
        elem = elem.parentNode;
      }
      return pt;
    }
    
    
    var pos = getCirclePosition("thecircle");
    console.log("Coordinates are: " + pos.x + "," + pos.y);
    <svg width="100%" height="100%" viewBox="0 0 1000 1000" preserveAspectRatio="xMidYMin slice" x="0" y="0" tabindex="1">
      <g transform="translate(100 100)">
           <g transform="translate(100 100)"> 
                <circle id="thecircle" r="50" cx="25" cy="25" fill="yellow" />
           </g>
      </g>
    <svg>


    更新

    正如@Vad0k 所指出的,有一种更简单但不太准确的方法,可以用来代替:

    function getCirclePosition(circleElemId)
    {
      var elem = document.getElementById(circleElemId);
      var svg = elem.ownerSVGElement;
    
      // Get the cx and cy coordinates
      var pt = svg.createSVGPoint();
      pt.x = elem.cx.baseVal.value;
      pt.y = elem.cy.baseVal.value;
    
      return pt.matrixTransform(getTransformToElement(elem, svg));
    }
    
    
    function getTransformToElement(fromElement, toElement) {
      return toElement.getCTM().inverse().multiply(fromElement.getCTM());
    };
    
    
    var pos = getCirclePosition("thecircle");
    console.log("Coordinates are: " + pos.x + "," + pos.y);
    <svg width="100%" height="100%" viewBox="0 0 1000 1000" preserveAspectRatio="xMidYMin slice" x="0" y="0" tabindex="1">
      <g transform="translate(100 100)">
           <g transform="translate(100 100)"> 
                <circle id="thecircle" r="50" cx="25" cy="25" fill="yellow" />
           </g>
      </g>
    <svg>

    关于javascript - 使用JS获取SVG内部元素的绝对坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53635449/

    相关文章:

    python - 根据一组坐标的数据对 map 进行着色

    code-coverage - 让 Karma、6to5ify 和 Istanbul 尔打球

    javascript - 使用 css 选择器和 javascript 断言随按钮单击而更改的文本

    javascript - 将表单数据提交到php文件,但在不同页面上查看相同的结果

    javascript - AngularJS + d3js : Issues with resizing objects

    javascript - 更改 svg 元素中的图像

    javascript - 使用 index.ts 导出 'default'

    node.js - ES6 React服务端渲染,如何导入React组件?

    javascript - 4 盒子里的三 Angular 形,里面有图像

    javascript - D3表中的数据排序