在raphael.js中使用paper.add()时设置元素ID

时间:2014-02-04 19:23:25

标签: javascript raphael

我正在使用以下paper.add,有没有办法在声明这些元素时设置id?

paper.add([{
    "type": "circle",
    "fill": "#858AB5",
    "stroke": "none",
    "cx": 182.511,
    "cy": 178.146,
    "r": 162
}, {
    "type": "rect",
    "fill": "none",
    "stroke": "none",
    "x": 49.095,
    "y": 145.852,
    "width": 261.809,
    "height": 79.397
}, {
    "type": "text",
    "fill": "#FFFFFF",
    "stroke": "none",
    "transform": "t79.9719,174.3274",
    "font-family": "Impact",
    "font-size": 36,
    "text-anchor": "start",
    "text": "Darma & Greg"
} {
    "type": "text",
    "fill": "#FFFFFF",
    "stroke": "none",
    "transform": "t93.4012,211.6449",
    "font-family": "Impact",
    "font-size": 33,
    "text-anchor": "start",
    "text": "July 12th 2014"
}, {
    "type": "path",
    "fill": "#FFFFFF",
    "stroke": "none",
    "path": "M190.441,32.469c0,4.932-3.998,8.93-8.93,8.93c-4.932,0-8.93-3.998-8.93-8.93s3.998-8.93,8.93-8.93\r\n\t\t\tC186.443,23.538,190.441,27.537,190.441,32.469z"
}]);

2 个答案:

答案 0 :(得分:0)

使用paper.add()时,我无法找到设置id的方法;所以我使用了这段代码。

paper.forEach(function (el) {

    if (el.type =='text'){
        elements.push(el)

   elements[counted].node.onclick = selected;
 elements[counted].node.id = counted;

 ft = paper.freeTransform(elements[counted], {  drag: ['center', 'self'],
        rotate: ['axisY'],
        scale: ['bboxCorners', 'bboxSides'],
        keepRatio: ['bboxCorners'],
        snap: {
            rotate: 5
        },
        draw: 'bbox'
     }, function(ft, events) {
        console.log(ft.attrs);
    });


counted ++; 
    }
});

答案 1 :(得分:0)

回答可能有点迟,但您可以使用 Element.data(键,[值])来设置您想要的任何数据。

在这里查看http://raphaeljs.com/reference.html#Element.data