开发者

Raphael.js, how to select an element?

In Raphael,js, how can I select an element? For example, If I have an rectangular, 开发者_开发知识库how to select it? In Raphael, is there any way to select element like the selection of DOM element using jQuery?


To select an svg DOM element, supposing a node of the raphael element has an ID, you can do it in the 'jQuery' mode by $('#ID') or in the 'native' way document.getElementById('ID').

Moreover, using raphael handling events is very simple, for example when you click on a rectangle you can 'select' it, in this way (demo here => http://jsfiddle.net/steweb/zMYU8/):

markup:

<div id="canvas"></div>

js:

var selected = null; //var to store selected element

//initialize the raphael canvas and store it in a var
var canvas = Raphael(document.getElementById("canvas"), 320, 200);

//first rectangle
var r = canvas.rect(10, 10, 50, 50).attr("fill", "#FFFF22");

//second rectangle
var r1 = canvas.rect(70, 70, 50, 50).attr("fill", "#FFFF22");

//first rectangle click
r.click(function(){
  //change attributes
  r1.attr("stroke","black");  
  r.attr("stroke","green");
  selected = r; //update selected var
});

//second rectangle click
r1.click(function(){
  //change attributes
  r.attr("stroke","black");  
  r1.attr("stroke","green");
  selected = r1; //update selected var
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜