I've got an intricate SVG path composed of many segments defined with M#,#L#,# M#,#L#,#. I'd like to animate the drawing of this path, but in the order that they're listed. I've tried the solutions listed here: Can't make paths draw growing slowly with D3 , but they draw each segment of the path in parallel. How do I modify this so that they're animated sequentially?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I faced this problem in my whiteboard animation app. If your SVG file has a path with multiple move (M#) segments within a
<path>
element, parallel animation is unavoidable.What you have to do is to break up the Move commands (M) within the
<path>
elements and store them into individual elements.Either instruct your graphic artist to do so or write a simple parser to reconstruct your SVG file before loading it in DOM.
e.g. This svg file with multiple moves commands within the
<path>
element will have all the segments drawn at the same time.The next svg file will have each segment drawn in sequence. Of course you have to code your animation to animate one path at a time. If they are all within on path elements, this would not be possible.
D3 transitions have a
delay()
function that allows you to specify when to start the animation. You can use this to start drawing successive path segments after the previous ones have been drawn. You could also use the transition'send
event to start the next transition after the previous one finishes. This way, you don't even have to specify the delay.Alternatively, you could animate the path in SVG itself without Javascript. See here for an example of this. All you need to do is basically append an
animate
element to thepath
that tells it how to draw it.