Javascript 拉斐尔:如何开始;拖拽;通过代码而不是“通过”在对象上发生事件;“鼠标按下”吗;?

Javascript 拉斐尔:如何开始;拖拽;通过代码而不是“通过”在对象上发生事件;“鼠标按下”吗;?,javascript,raphael,Javascript,Raphael,我需要能够在单击鼠标左键时创建的圆形对象上启动raphael“拖动”事件 详细信息: 我正在编写一段代码,用来操纵一条闭合的路径。我需要这些功能: 1.通过鼠标拖动移动现有点 2.通过右键单击删除点 3.通过在路径上单击鼠标左键添加点,从而在该位置拆分路径 4.如果点是通过左键单击创建的,则允许用户在释放鼠标并将其放置之前将其拖动到其他位置 下面是我在给定raphael“path”对象上创建新圆的函数: 然后我可以这样做: var pt1 = ["M", 0, 0], pt2 = ["L

我需要能够在单击鼠标左键时创建的圆形对象上启动raphael“拖动”事件

详细信息:
我正在编写一段代码,用来操纵一条闭合的路径。我需要这些功能:
1.通过鼠标拖动移动现有点
2.通过右键单击删除点
3.通过在路径上单击鼠标左键添加点,从而在该位置拆分路径
4.如果点是通过左键单击创建的,则允许用户在释放鼠标并将其放置之前将其拖动到其他位置

下面是我在给定raphael“path”对象上创建新圆的函数:

然后我可以这样做:

var pt1 = ["M", 0, 0],
    pt2 = ["L", 10, 0],
    pt3 = ["L", 10, 10],
    pt4 = ["L", 0, 10],
    point_set = [pt1, pt2, pt3, pt4, ["Z"]],
    path = paper.path(point_set),
    c1 = make_circle(pt1, path),
    c2 = make_circle(pt2, path),
    c3 = make_circle(pt3, path),
    c4 = make_circle(pt4, path);  
make_circle(new_point, this).data("just_created", true);  
通过单击路径创建新点时,我执行以下操作:

var pt1 = ["M", 0, 0],
    pt2 = ["L", 10, 0],
    pt3 = ["L", 10, 10],
    pt4 = ["L", 0, 10],
    point_set = [pt1, pt2, pt3, pt4, ["Z"]],
    path = paper.path(point_set),
    c1 = make_circle(pt1, path),
    c2 = make_circle(pt2, path),
    c3 = make_circle(pt3, path),
    c4 = make_circle(pt4, path);  
make_circle(new_point, this).data("just_created", true);  
。。。此圆圈的mousemove处理程序将检查:

if (this.data("just_created")) { ... // follow mouse  
这里是我的完整代码

问题在于,由于圆半径很小,因此在释放新点之前快速将鼠标移动到将破坏mousemove处理程序,因为它已连接到圆

通过“拖动”事件移动现有圆时,一切正常。无论鼠标移动得有多快,圆圈都会保持不变

那么,是否有可能在按下鼠标左键但尚未释放的对象上启动raphael“拖动”事件


解决方案():

var宽度=300,
高度=300,
纸张偏移量=50,
maxX=宽度,
maxY=高度,
纸张=拉斐尔(纸张偏移,纸张偏移,300300),
拖拽;
制作路径([100100],[200100],[200200],[100200],“绿色”);
//重置“拖动”以避免初始拖动
拖动=空;
//一些数学公式可以确定一个点是否位于其他两个点之间,在某个阈值内
//基于:http://stackoverflow.com/questions/328107/how-can-you-determine-a-point-is-between-two-other-points-on-a-line-segment
函数介于(a、b、c)之间{
var x1=a[1],
x2=b[1],
x3=c[1],
y1=a[2],
y2=b[2],
y3=c[2],
阈值=1000;
var点积=(x3-x1)*(x2-x1)+(y3-y1)*(y2-y1);
if(dotproduct<0)返回false;//如果可能,提前返回
var squaredlengthba=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
if(dotproduct>squaredlengthba)返回false;//如果可能,提前返回
var叉积=(y3-y1)*(x2-x1)-(x3-x1)*(y2-y1);
if(数学abs(交叉积)maxY){
新_y=最大值;
}
//更新循环坐标
这个是.attr({
cx:new_x,
西:纽约
});
//更新参照点
点[1]=新的_x;
点[2]=新的;
//重新绘制路径
path.attr({
路径:路径数据(“点集”)
});
}
//基于鼠标事件的移动圆
函数句柄\鼠标移动\圆(e){
var new_x=e.clientX-纸张偏移量,
new_y=e.clientY-纸张偏移量;
更新_circle_xy.call(this,new_x,new_y);
}
//将鼠标放在圆圈上
//e.哪个1=左键单击
//e.哪3个=右键单击
功能手柄\鼠标向下\圆圈(e){
//单击鼠标右键删除目标点
如果(e.which==3){
var path=this.data(“路径”),
点集=路径数据(“点集”),
点=此数据(“点”),
索引=点集。索引(点);
//如果我们只剩下2分,什么都不要做
//(检查是否<4,因为最后一个元素不是点(“Z”))
if(point_set.length<4)返回false;
//移除目标点
点集拼接(索引1);
//如果移除的点是集合的头部,则将以下点作为新头部
如果(索引===0)点集[0][0]=“M”;
//重新绘制路径
path.attr({
路径:点集合
});
//最后,去掉圆圈
这个。删除();
}else如果(e.which==1){
拖动=这个;
}
}
//鼠标点击路径
函数句柄\鼠标向下\路径(e){
//单击鼠标左键进行拆分
如果(e.which==1){
var X=e.clientX-纸张偏移量,
Y=e.clientY-纸张偏移量,
新的_点=[“L”,X,Y],
点集=此.data(“点集”),
指数
//通过移除末端(“Z”)来“打开”路径
point_set.pop();
对于(变量i=0;ic = make_circle(new_point, this).data("just_created", true);
c.events[0].f.call(c, e); // This is very specific to this scenario
function global_mouseup(e) {      
    dragging = null;
}
function global_mousemove(e) {     
    if (dragging) { 
        handle_mousemove_circle.call(dragging, e);    
    }
}
if (document.addEventListener) {
    document.addEventListener("mousemove", global_mousemove, false);
    document.addEventListener("mouseup", global_mouseup, false);
} else {
    document.attachEvent('onmousemove', global_mousemove);
    document.attachEvent('onmouseup', global_mouseup);
}  
if (document.addEventListener) {
    document.addEventListener("mousemove", global_mousemove, false);
    document.addEventListener("mouseup", global_mouseup, false);
    document.addEventListener("mousedown", global_mousedown, false);
} else {
    document.attachEvent('onmousemove', global_mousemove);
    document.attachEvent('onmouseup', global_mouseup);
    document.attachEvent('onmousedown', global_mousedown);
}

function global_mousedown(e) {  
    if(dragging){
        e.preventDefault();     
    }
}