SVG 重新排序 z 索引(Raphael 可选)

发布于 2024-11-18 02:06:43 字数 158 浏览 4 评论 0原文

创建后如何重新排序 Raphael 或其底层 SVG 元素。 更好的是,SVG 中是否存在像图层这样的东西?

理想情况下,我希望有两个或多个层可以随时放置元素;背景层和前景层。如果这不是一个选择,那么将元素弹出到前面就可以了,在这种特殊情况下将其推到后面会更好。

谢谢,

How can I reorder Raphael or their underlying SVG elements after creation.
Better yet, do something like layers exist in SVG?

Ideally I would like two or more layers in which to place elements at any time; A background and a foreground layer. If that is not an option, popping elements to the front would be ok, and pushing it to the back would be better in this particular case.

Thanks,

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

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

发布评论

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

评论(6

初见终念 2024-11-25 02:06:43

给我代码!

// move element "on top of" all others within the same grouping
el.parentNode.appendChild(el); 

// move element "underneath" all others within the same grouping
el.parentNode.insertBefore(el,el.parentNode.firstChild);

// move element "on top of" all others in the entire document
el.ownerSVGElement.appendChild(el); 

// move element "underneath" all others in the entire document
el.ownerSVGElement.appendChild(el,el.ownerSVGElement.firstChild); 

特别是在 Raphael 中,使用 toBack()toFront()

raphElement.toBack()  // Move this element below/behind all others
raphElement.toFront() // Move this element above/in front of all others

详细信息

SVG 使用 "画家模型" 绘制对象时:文档中稍后出现的项目将在之后绘制(在) 文档中较早出现的元素。要更改项目的分层,您必须使用 appendChildinsertBefore 等。

您可以在此处查看示例: http://phrogz.net/SVG/drag_under_transformation.xhtml

  1. 将红色和蓝色物体,使它们重叠。
  2. 单击每个对象并观察它弹出到顶部。 (但是,黄色圆圈旨在始终可见。)

此示例中的元素重新排序是由源代码的第 93/94 行完成的:

el.addEventListener('mousedown',function(e){
  el.parentNode.appendChild(el); // move to top
  ...
},false);

当鼠标在元素上按下时,它会移动到它所有兄弟元素的最后一个元素,导致它最后绘制,“在所有其他元素之上”。

Gimme the Code!

// move element "on top of" all others within the same grouping
el.parentNode.appendChild(el); 

// move element "underneath" all others within the same grouping
el.parentNode.insertBefore(el,el.parentNode.firstChild);

// move element "on top of" all others in the entire document
el.ownerSVGElement.appendChild(el); 

// move element "underneath" all others in the entire document
el.ownerSVGElement.appendChild(el,el.ownerSVGElement.firstChild); 

Within Raphael specifically, it's even easier by using toBack() and toFront():

raphElement.toBack()  // Move this element below/behind all others
raphElement.toFront() // Move this element above/in front of all others

Details

SVG uses a "painters model" when drawing objects: items that appear later in the document are drawn after (on top of) elements that appear earlier in the document. To change the layering of items, you must re-order the elements in the DOM, using appendChild or insertBefore or the like.

You can see an example of this here: http://phrogz.net/SVG/drag_under_transformation.xhtml

  1. Drag the red and blue objects so that they overlap.
  2. Click on each object and watch it pop to the top. (The yellow circles are intended to always be visible, however.)

The re-ordering of elements on this example is done by lines 93/94 of the source code:

el.addEventListener('mousedown',function(e){
  el.parentNode.appendChild(el); // move to top
  ...
},false);

When the mouse is pushed down on an element, it is moved to be the last element of all its siblings, causing it to draw last, "on top" of all others.

芸娘子的小脾气 2024-11-25 02:06:43

如果您使用 Raphael,向后和向前弹出元素非常简单:

element.toBack()
element.toFront()

这是相关文档

If you're using Raphael, popping elements backwards and forwards is very straightforward:

element.toBack()
element.toFront()

Here's the relevant documentation.

苍风燃霜 2024-11-25 02:06:43

SVG 中没有 z 索引,代码中后面的对象出现在第一个对象的上方。因此,根据您的需要,您可以将节点移动到树的开头或结尾。

(group) 元素是 svg 中的通用容器,因此它们可以是您的图层。只需在组之间移动节点即可实现您的需要。

There's no z-index in SVG, objects that are later in the code appear above the first ones. So for your needs, you can move the node to the start of the tree or the end of it.

<g> (group) element is a generic container in svg, so they can be layers for you. Just move nodes between the groups to achieve what you need.

明天过后 2024-11-25 02:06:43

如果您预定义图形对象,则可以随着时间的推移以不同的顺序对它们进行分层:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1000 1000" width="400" height="400">
  <rect x="0" y="0" width="1000" height="1000" fill="black"/>
  <defs>
    <rect id="a1" x="0"   y="0"   width="500" height="500" fill="red"/>
    <rect id="a2" x="100" y="100" width="500" height="500" fill="blue"/>
  </defs>
  <g> 
    <use xlink:href="#a1"/>
    <use xlink:href="#a2"/>
    <set attributeName="visibility"  to="hidden" begin="2s"/> 
  </g>
  <g visibility="hidden"> 
    <use xlink:href="#a2"/>
    <use xlink:href="#a1"/>
    <set attributeName="visibility"  to="visible" begin="2s"/> 
  </g>
</svg>

If you predefine your graphic objects, you can then layer them in different orders as time evolves:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1000 1000" width="400" height="400">
  <rect x="0" y="0" width="1000" height="1000" fill="black"/>
  <defs>
    <rect id="a1" x="0"   y="0"   width="500" height="500" fill="red"/>
    <rect id="a2" x="100" y="100" width="500" height="500" fill="blue"/>
  </defs>
  <g> 
    <use xlink:href="#a1"/>
    <use xlink:href="#a2"/>
    <set attributeName="visibility"  to="hidden" begin="2s"/> 
  </g>
  <g visibility="hidden"> 
    <use xlink:href="#a2"/>
    <use xlink:href="#a1"/>
    <set attributeName="visibility"  to="visible" begin="2s"/> 
  </g>
</svg>
感情洁癖 2024-11-25 02:06:43

我还没有尝试过,但是 SVG 渲染顺序 看起来可以成为一个解决方案。而且“z-index”即将到来,它已经在 提案中

I haven't tried it yet but SVG render order looks like it could be a solution. And also 'z-index' is coming, it is already in proposal

眼泪都笑了 2024-11-25 02:06:43

实际上,我认为单独的appendChild()会复制元素,而不仅仅是移动它。这可能不是问题,但如果您想避免重复,我建议如下:

el.parentNode.appendChild(el.parentNode.removeChild(el));

Actually, I think appendChild() alone will copy the element and not just move it. This may not be a problem but if you want to avoid duplicates I suggest something like this:

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