2016-09-21 23 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元素。上面的代码只是基本思想的一个简单例子。

相关问题