getBBox() SVGRect 是如何计算的? [英] How is the getBBox() SVGRect calculated?

查看:23
本文介绍了getBBox() SVGRect 是如何计算的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 g 元素,其中包含一个或多个 path 元素.正如我在另一个问题中提到的,我缩放并翻译了g 元素通过计算 transform 属性使其适合画布另一部分的网格.

I have a g element that contains one or more path elements. As I mentioned in another question, I scale and translate the g element by computing a transform attribute so that it fits on a grid in another part of the canvas.

计算是使用两个矩形之间的差异完成的,g 元素中的 getBBox() 和网格周围的矩形.

The calculation is done using the difference between two rectangles, the getBBox() from the g element and the rectangle around the grid.

问题是——在我完成转换之后,我更新了 g 元素的内容并再次调用 getBBox()没有 删除 transform.结果矩形似乎是在不考虑 transform 的情况下计算的.我原以为它会反映这种变化.这种行为是否符合 SVG 规范?如何获得转换后的矩形的边界框?

Here is the question -- after I do the transform, I update the contents of the g element and call getBBox() again, without removing the transform. The resulting rectangle appears to be calculated without considering the transform. I would have expected it to reflect the change. Is this behavior consistent with the SVG specification? How do I get the bounding box of the transformed rectangle?

顺便说一句,这是在 Firefox 4 中运行的 HTML 5 文档中,如果这有什么不同的话.

This, BTW, is in an HTML 5 document running in Firefox 4, if that makes any difference.

更新:显然这种行为显然违反了规范.来自 此处在 w3c 的文本:

Update: Apparently this behavior seems pretty clearly in violation of the specification. From the text here at w3c:

SVGRect getBBox()

返回当前用户空间中所有包含的图形元素的几何图形的紧密边界框(即,在应用转换"属性后,如果有的话),不包括描边、剪裁、遮罩和过滤效果).请注意,即使在元素尚未呈现的情况下,getBBox 也必须返回调用该方法时的实际边界框.

Returns the tight bounding box in current user space (i.e., after application of the ‘transform’ attribute, if any) on the geometry of all contained graphics elements, exclusive of stroking, clipping, masking and filter effects). Note that getBBox must return the actual bounding box at the time the method was called, even in case the element has not yet been rendered.

我读对了吗?如果是这样,这似乎是 Firefox 使用的 SVG 实现中的勘误表;我还没有机会尝试其他任何东西.如果有人可以指出我在哪里,我会提交错误报告.

Am I reading this correctly? If so this seems to be an errata in the SVG implementation Firefox uses; I haven't had a chance to try any other. I would file a bug report if someone could point me to where.

推荐答案

您看到的行为是正确的,并且与规范一致.应用转换,然后 bbox 以当前用户单位"计算,即当前用户空间.因此,如果您想查看元素转换的结果,则需要查看父节点或类似节点的 bbox.这有点令人困惑,但在 SVG Tiny 1.2 规范 SVGLocatable 中解释得更好其中包含许多示例,阐明了它应该做什么.

The behaviour you see is correct, and consistent with the spec. The transform gets applied, then the bbox is calculated in "current user units", i.e. the current user space. So if you want to see the result of a transform on the element you'd need to look at the bbox of a parent node or similar. It's a bit confusing, but explained a lot better in the SVG Tiny 1.2 spec for SVGLocatable That contains a number of examples that clarify what it's supposed to do.

这篇关于getBBox() SVGRect 是如何计算的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆