How can I animate a vector path being drawn with Raphael JS?
I have a set of coordinates that I would like to connect using Raphael JS.
On a grid, the coordinates are (x,y sets). I would like to start from one end and “connect the dots” as the user watches. The end product would look something like this:
Picture 9.png http://img213.imageshack.us/img213/8013/picture9my.png
Ideally, I would like to be able to make the paths curved so they would look more like this (coordinates added for reference):
Picture 10.png http://img534.imageshack.us/img534/1789/picture10fc.png
The coordinates are:
26,-6
14,-12
5,-20
11,-28
14,-37
5,-40
I’ve been searching Google and I have read through this question How to draw a vector path progressively? (Raphael.js) but I am trying to use Raphael.js specifically and the example on that page for Raphael.js does not seem to work nor does it talk about using multiple coordinate points for inputs.
Progressive Paths
Drawing a path progressively is easy to do. I dislike the second-most-accepted answer on this question because it recreates a path at every step, clearing the paper in between. Here’s the utility function I’ve used time and time again:
You can see it in action on my site.
This alone would make animating the progressive path construction of your points in a linear fashion absolutely simple. You’d simply compile your path…
And then pass it to whatever path animation function you’ve set up. In my case,
Curve Interpolation
Raphael 2.0’s Catmull Rom feature makes curving gracefully between your points extremely straightforward (thanks to Erik Dahlström for pointing this out). All you need to do is construct a path using the ‘R’ command to move between points, and Raphael will do the rest.
You can see all the pieces working together here.