如何转换 svg 中的路径,使其与其原始位置平行?

发布于 2024-08-10 20:22:42 字数 189 浏览 2 评论 0原文

我有一个在 SVG 中定义的路径。我想制作路径的两个副本并翻译它们,以便一个与原始路径平行,另一个与另一侧平行。这个想法是最终得到 3 条路径,全部相互平行且不重叠。

我已经尝试过简单的翻译,例如两条路径的转换=“翻译(10,10)”和转换=“翻译(-10,-10)”,但在某些路径中,它们最终会相互交叉,这不是我想要的想。

谢谢。

I have a path defined in SVG. I would like to make two copies of the path and translate them so that one sits parallel to the original on one side, and the other sits parallel on the other side. The idea is to end up with 3 paths, all parallel to each other and not overlapping.

I have tried simple translations such as transform="translate(10,10)" and transform="translate(-10,-10)" for the two paths, but in some paths they end up crossing each other which is not what I want.

Thanks.

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

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

发布评论

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

评论(3

情愿 2024-08-17 20:22:42

您的答案应该会像您提供的那样起作用。您可以提供更具体的问题示例,以得出更好的解决方案。

您提供的命令将在两个维度上移动该项目,而不仅仅是一个维度。

另外,请记住,SVG 使用左上角的点作为 0,0,向右/向下为正值。还要检查以确保您没有被单位绊倒。

Your answer should somewhat work as you've provided it. You might provide more concrete examples of your problem to evoke better solutions.

Your provided commands are going to move the item in two dimensions, not just one.

Also, keep in mind that SVG uses the upper left point as 0,0, and to the right/down are positive. Also check to make sure you're not getting tripped up by units.

始终不够 2024-08-17 20:22:42

如果您的原始路径具有 X,Y 的边界框,那么确保复制的路径不重叠的最简单方法是通过 +X 和 -X 进行平移,因此:

translate(-X, 0)

以及

translate(X, 0)

计算 X 值并设置它的位置在翻译参数中。

If your original path has a bounding box of X,Y then the simplest way to make sure that the copied don't overlap is to translate by +X and -X, so:

translate(-X, 0)

and

translate(X, 0)

where you have computed the value of X and set it in the translate argument.

优雅的叶子 2024-08-17 20:22:42

我将为您提供一些完全未经测试的代码,这些代码是在不查看 SVG DOM 规范的情况下编写的。然后您可以测试它并调整它以使其正常工作。

首先,获取元素的边界框:

var box = mypath.getBBox();

然后克隆路径两次(或创建元素):

var rightCopy = mypath.cloneNode(true);
var BottomCopy = mypath.cloneNode(true);

然后转换每个副本:

rightCopy.setAttribute("transform", "translate(" + box.width + ",0) " + rightCopy.getAttribute("变换"));

bottomCopy.setAttribute("transform", "translate(0," + box.height + ") " + BottomCopy.getAttribute("transform"));

它看起来很混乱的原因是因为原始路径可能已经有一个转换。

然后将这些节点添加回 DOM 中:

mypath.parentNode.insertBefore(rightCopy, mypath);

mypath.parentNode.insertBefore(bottomCopy, mypath);

I'll give you some completely untested code written without looking at the SVG DOM spec. You can then test it and tweak it to get it to work.

First, get the bounding box of an element:

var box = mypath.getBBox();

Then clone the path twice (or make elements):

var rightCopy = mypath.cloneNode(true);
var bottomCopy = mypath.cloneNode(true);

Then transform each copy:

rightCopy.setAttribute("transform", "translate(" + box.width + ",0) " + rightCopy.getAttribute("transform"));

bottomCopy.setAttribute("transform", "translate(0," + box.height + ") " + bottomCopy.getAttribute("transform"));

The reason it looks messy is because the original path might have a transform already on it.

Then add those nodes back into the DOM:

mypath.parentNode.insertBefore(rightCopy, mypath);

mypath.parentNode.insertBefore(bottomCopy, mypath);

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