SVG:当y属性为200时,为什么getBoundingClientRect返回190?

时间:2018-12-20 03:41:08

标签: javascript html svg

下面的代码将textBox1的y位置放置在200处,但是getBoundingClientRect返回的值是190。

为什么?

https://codepen.io/anon/pen/REKayR?editors=1011

<svg id="rootBox" width="500" height="800" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

    <rect x="0%" y="0%" width="100%" height="100%" fill="beige" />

    <svg id="textBox1" x="0%" y="200" width="100%" height="25%">
      <rect class="background" x="0%" y="0%" width="100%" height="100%" fill="gray" fill-opacity="0.5" />
      <text class="textGroup" x="0" y="0"><tspan x="50%" dy="-0.25em" text-anchor="middle">tspan line 0</tspan><tspan x="50%" dy="1.5em" text-anchor="middle">tspan line 1</tspan><tspan x="50%" dy="1.5em" text-anchor="middle">tspan line 2</tspan></text>
    </svg>

</svg>


var textBox = $("#textBox1");
var textBBox = textBox[0].getBoundingClientRect();
console.log("The y-pos is: " + textBBox.y);

2 个答案:

答案 0 :(得分:1)

getBoundingClientRect考虑了滚动位置之类的问题。 HTML主体上的任何边距或填充也会考虑在内,但要增加结果而不是减去。

如果确保没有向下滚动,并且没有考虑边距,您将获得200:

function log() {
  var textBox = $("#textBox1");
  var textBBox = textBox[0].getBoundingClientRect();
  console.log("The y-pos is: " + textBBox.y);
}
log();
setInterval(log, 1000);
html,
body {
  margin: 0;
  padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<svg id="rootBox" width="500" height="800" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

    <rect x="0%" y="0%" width="100%" height="100%" fill="beige" />

    <svg id="textBox1" x="0%" y="200" width="100%" height="25%">
      <rect class="background" x="0%" y="0%" width="100%" height="100%" fill="gray" fill-opacity="0.5" />
      <text class="textGroup" x="0" y="0"><tspan x="50%" dy="-0.25em" text-anchor="middle">tspan line 0</tspan><tspan x="50%" dy="1.5em" text-anchor="middle">tspan line 1</tspan><tspan x="50%" dy="1.5em" text-anchor="middle">tspan line 2</tspan></text>
    </svg>

</svg>

答案 1 :(得分:1)

.getBoundingClientRect()是通用Element界面的一部分,它根据屏幕视口计算矩形。 SVG提供了一些more specific methods

  • SVGGraphicsElement.getBBox()在绘制元素所在的局部坐标系中计算边界框。
  • SVGGraphicsElement.getCTM()计算局部坐标系和最近 SVG视口(例如<svg>元素)之间的变换矩阵。
  • SVGGraphicsElement.getScreenCTM()计算局部坐标系和屏幕视口之间的变换矩阵。

此外,DOMMatrix接口具有.inverse()方法,因此您可以轻松地计算相反方向的位置。 (例如,如果使用element.getScreenCTM().inverse()的结果转换鼠标事件的screenx / screenY位置,则将获得相对于该元素的鼠标位置。)

有点尴尬的是,您必须构造一个SVGPoint对象(只有通过SVGSVGElement.createSVGPoint()元素上的<svg>方法才能实现),才能应用某些内容您的矩阵。

对于您的问题,请考虑内部<svg>内部的rect的三个坐标系统的不同返回值:

var textBox = document.querySelector('#textBox1 rect');
var svg = document.querySelector('#rootBox');
var point = svg.createSVGPoint();

var local = textBox.getBBox();
point.x = local.x, point.y = local.y;
console.log("local: ", local.x, local.y);

var nearest = textBox.getCTM();
var point2 = point.matrixTransform(nearest);
console.log("nearest viewport: ", point2.x, point2.y);

var screen = textBox.getScreenCTM();
var point3 = point.matrixTransform(screen);
console.log("screen viewport: ", point3.x, point3.y);
<svg id="rootBox" width="500" height="800" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

    <rect x="0%" y="0%" width="100%" height="100%" fill="beige" />

    <svg id="textBox1" x="0%" y="200" width="100%" height="25%">
      <rect class="background" x="0%" y="0%" width="100%" height="100%" fill="gray" fill-opacity="0.5" />
      <text class="textGroup" x="0" y="0"><tspan x="50%" dy="-0.25em" text-anchor="middle">tspan line 0</tspan><tspan x="50%" dy="1.5em" text-anchor="middle">tspan line 1</tspan><tspan x="50%" dy="1.5em" text-anchor="middle">tspan line 2</tspan></text>
    </svg>
</svg>