以下代碼的目的是拖動兩個矩形中的任意一個,並保持它們與線條連接。但是,正如您在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