我想創建一個具有自定義屬性和功能的自定義Raphael元素。該對象還必須包含預定義的Raphael對象。例如,我會有一個節點類,它將包含一個包含文本和其他元素的圓。問題是將這個新對象添加到一個集合中。這些需求是必需的,因爲非Raphael對象不能添加到集合中。因此,不能使用可以包含Raphael對象的自定義對象。該代碼是這樣的:Raphael.js:添加一個新的自定義元素
var Node = function (paper) {
// Coordinates & Dimensions
this.x = 0,
this.y = 0,
this.radius = 0,
this.draw = function() {
this.entireSet = paper.set();
var circle = paper.circle(this.x, this.y, this.radius);
this.circleObj = circle;
this.entireSet.push(circle);
var text = paper.text(this.x, this.y, this.text);
this.entireSet.push(text);
}
// other functions
}
var NodeList = function(paper){
this.nodes = paper.set(),
this.populateList = function(){
// in order to add a node to the set
// the object must be of type Raphael object
// otherwise the set will have no elements
this.nodes.push(// new node)
}
this.nextNode = function(){
// ...
}
this.previousNode = function(){
// ...
}
}
您正在尋找這樣的事情? http://raphaeljs.com/reference.html#Raphael.fn。它解釋瞭如何添加可以創建複雜對象的自定義函數,如函數的結果,如.arrow()或任何你想要的。 – cabreracanal
據我所知,Raphael.fn不會創建一個Raphael對象。因此,創建的對象不能添加到Raphael集合中。我的類看起來像這樣: VAR節點=函數(){// 座標和尺寸 this.x = 0, this.y = 0, this.radius = 0, this.stroke = 1, // ... 返回此; } – Claudia
這是您附加到.fn創建它的功能。如果你想在裏面創建一個帶有文本的圓圈(如果我理解你的問題),我會這樣做,就像這個例子https://gist.github.com/1043360。如果你的對象是一個路徑,那麼你可以將它添加到一個集合。更重要的是,您可以分別創建文本和圓圈,將它們放入設置中。你也可以把你的自定義屬性。 – cabreracanal