在 RaphaelJS 中,如何在路径中移动各个节点?

In RaphaelJS, how do I move around individual nodes in a path?

是否有包含这些的数组?

另外,是否也可以在现有路径中添加节点?

您需要编辑路径。不,没有数组,只有路径字符串。您当然可以编写更高级别的代码来跟踪您生成路径字符串的数组中的节点。

一个简单的实现类似于:

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