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

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

问题描述

我有一个g元素,其中包含一个或多个path元素.正如我在另一个问题中提到的那样,我缩放并翻译了元素,方法是计算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?

BTW是在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()

在所有包含的图形元素的几何图形上返回当前用户空间中的紧密边界框(即,在应用"transform"属性(如果有)之后),但不包括描边,裁剪,遮罩和滤镜效果.请注意,即使尚未渲染该元素,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的SVG Tiny 1.2规范中有更好的解释. 其中包含许多示例,阐明了其应做的事情.

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天全站免登陆