2016-09-21 66 views

回答

0

您需要編輯路徑。不,沒有數組,只有路徑字符串。當然,您可以編寫更高級別的代碼來保存您從中生成路徑字符串的陣列中的節點。

簡單的執行是這樣的:

function Polygon (nodes) { 
    if (nodes instanceof Array) this.nodes = nodes; 
    else this.nodes = []; 
} 

Polygon.prototype.draw = function() { 
    var path = "M" + this.nodes[0][0] + "," + this.nodes[0][1]; 
    for (var i=1; i<this.nodes.length; i++) { 
     path += "L" + this.nodes[i][0] + "," + this.nodes[i][1]; 
    } 
    path += "Z"; 
    return path; 
} 

然後,你會怎麼做:

var p = new Polygon([[100,100],[100,200],[200,200],[200,100]]); 

var pp = paper.path(p.draw()); 

// Modify node: 
p.nodes[2] = [300,300]; 
pp.attr('path',p.draw()); 

// Add node: 
p.nodes.push([250,150]); 
pp.attr('path',p.draw()); 

當然,你可以與你實現API的更多創意。例如,polygon.draw()可以自動更新鏈接的Raphael元素。上面的代碼只是基本思想的一個簡單例子。