将 svg 文档的所有直接子文档包装到中是否安全?用于缩放?

发布于 2024-09-15 16:17:28 字数 338 浏览 3 评论 0原文

在我的应用程序中,我加载不同的 SVG 文件并需要缩放它们。正如我所见,我需要将变换矩阵应用于元素以使其缩放,但这不适用于 SVG 元素,因此我需要将所有内容包装在 中并进行变换相反。

所以我的问题是这样做安全吗?是否允许像 这样的元素不是 的直接子元素?在这种情况下,最外层 上的转换会影响 内部的元素吗?如果我将所有内容都包装在 中,可能会出现哪些其他问题?

In my app I load different SVG files and need to zoom them. As I see it I need to apply a transformation matrix to an element to have it zoomed, but this doesn't work on SVG element, so I need to wrap everything in a <g> and transform it instead.

So my question is is it safe to do this? Is it allowed for elements like <defs> be not immediate children of <svg>? Will the transformation on outermost <g> affect the elements inside <defs> in this case? What other issues may arise if I wrap everything in a <g>?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-09-22 16:17:28

是的,您可以在任何需要的地方使用 ,但建议将其保留为根元素的直接子元素。问问自己是否真的需要将样式继承到 内容中。通常 中的内容会被其他元素引用,例如 ,因此大多数时候 位于何处并不重要放置了

元素上的转换不会直接影响子 ,因为该转换不是继承的。然而,它可能会影响某些 SVG DOM 调用的返回值,但一般来说,如果您要询问 内元素的详细信息,您无论如何都需要小心,因为通常需要更多上下文正确计算结果,例如百分比和 objectBoundingBox 单位分别取决于视口和引用元素。

如果包含的 SVG 片段具有选择根元素或 svg 元素子元素的 css 样式规则,您可能会遇到问题。

Yes you can use <defs> wherever you need it, although it's advisable to keep it as a direct child of the root element. Ask yourself if you really need to inherit style into the <defs> content. Usually the content inside <defs> is referenced by other elements, e.g <use>, so most of the time it doesn't really matter where the <defs> is placed.

The transformation on the <g> element won't affect a child <defs> directly, since the transform isn't inherited. However it may affect the return values from some SVG DOM calls, but in general if you're asking for details on elements inside a <defs> you need to be careful anyway since more context is usually needed to properly compute the result, e.g percentages and objectBoundingBox units depend on the viewport and referencing element respectively.

You may run into issues if the included SVG fragment has css stylerules that select e.g the root element, or the children of an svg element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文