以下代码的目的是拖动两个矩形中的任意一个,并保持它们与线条连接。但是,正如您在jsfiddle中看到的,该线路在其他地方移动。这段代码有什么问题?Raphael:在保持连接状态下拖动矩形
这是HTML
<div id="canvas"></div>
这是JavaScript
window.onload = function() {
var paper = Raphael("canvas", 600, 600);
var rect1 = paper.rect(100, 100, 100, 100).attr({"fill":"blue"});
var rect2 = paper.rect(400, 100, 100, 100).attr({"fill":"green"});
rect1.node.id = "rect1";
rect2.node.id = "rect2";
var x1=200, y1=150, x2=400, y2=150;
var line = paper.path(['M', x1, y1, 'L', x2, y2]);
var start = function() {
// storing original coordinates
this.ox = this.attr("x");
this.oy = this.attr("y");
},
move = function (dx, dy) {
// move will be called with dx and dy
this.attr({x: this.ox + dx, y: this.oy + dy});
if (this.node.id == "rect1") {
x1 += dx;
y1 += dy;
}
else if (this.node.id == "rect2") {
x2 += dx;
y2 += dy;
}
line.attr("path", ['M', x1, y1, 'L', x2, y2]);
},
up = function() {
// restoring state
};
rect1.drag(move, start, up);
rect2.drag(move, start, up);
};
抱歉,您的jsfiddle不工作,是编辑后的版本? – ps0604
我没有更新网址 - 应该这样做。 –
谢谢,你的答案也适用 – ps0604