adding ID's to raphael objects

天涯浪子 提交于 2019-11-28 11:36:14

Well, the way I'm doing this is the following. First I write all the paths in an object, for example:

var paths = {
    path1: 'the paths coordinates',
    path2: 'the paths coordinates',
    path3: 'the paths coordinates',
}

Then you just loop trough all the paths, seting the coordinates for each path and giving them an ID (this is an internal Raphael's ID):

for(path in paths){
   var newpath = paper.path(paths[path]);
   newpath.attr({options})
   newpath.id = path;
}

Now, if you want to get one of this elements you can use the next Raphael feature:

var thisPath = paper.getById('path1');

This way you can use the path on any of the Raphael methods. So, if you need to get the node in the dome you can do the following:

var node = thisPath.node

But if you need to animate the path, you better use the Raphael's animate method, or if you need to change attibutes the attr method.

thisPath.animate(.....)

If you need to apply some change to all the paths, you can use:

paper.forEach(function(thisArg))

you need to pass the function to run on each element and thisArg reference the element on each iteration

And maybe you want to take a look to the Raphael's sets wich can be useful for use methods on groups of elements. If you need any help using this features just let me know and I'll do my best to help you out. Bye!

You could just push them to an array :

var pathArray = new Array();
var path_gs = rsr.path("path coords");
pathArray.push(path_gs); 

Then loop through pathArray.

Another option is grouping them in sets.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!