cduck/drawsvg

Z-Indexing Elements

npiccolotto opened this issue · 2 comments

Hi, thanks a ton for this library!

The one thing that I always intensely dislike about SVG is that the drawing order equals the insertion order. Sometimes you just want to make sure that some elements are in the background without reorganizing your code.

It seems to me like a small but very useful feature to add an optional z-index property to elements, then sort elements by that before drawing. Elements without z-index are sorted by insertion as before. Would that be something you'd accept as PR or consider implementing yourself?

cduck commented

Glad you like the library! This is already a feature but it appears to not be well documented yet. Just use the z= keyword argument of any append or extend methods:

d = draw.Drawing(...)
d.append(element, z=2)  # Above
d.extend([el0, el1, el2], z=1)  # Below
g = draw.Group(...)
g.append(element, z=1)

Smaller z-values are drawn below larger z-values. Groups and other elements can have their own independent z-ordering for children but this doesn't influence the global order.

Perfect, loving it! Thanks again.