避免在拉斐尔中无意中拖动
Avoid dragging unintentionally in Raphael
在这个 jsFiddle 中,我有一个 Raphael canvas 和一个可以单击或拖动的矩形。
如果将鼠标移动到矩形上然后单击它,您可能会稍微移动它(文本会显示)。我需要在我的应用程序中处理点击并在用户打算点击时忽略拖动运动。例如,只有当拖动大于 5 px 时,我才需要启用拖动。如果只是为了点击,有没有办法让 Raphael 不移动矩形?
var paper = Raphael("canvas", 600, 600);
var title = this.paper.text(50, 10, 'click on the square');
var rect = this.paper.rect(20, 20, 30, 30);
rect.attr('fill', 'black')
var start = function () {
this.odx = 0;
this.ody = 0;
},
move = function (dx, dy) {
title.attr('text', 'mouse moved')
this.translate(dx - this.odx, dy - this.ody);
this.odx = dx;
this.ody = dy;
},
up = function () {};
rect.drag(move, start, up);
rect.mousedown(function(e){
title.attr('text', 'mouse down')
})
您可以在移动开始时存储初始坐标,而忽略当前坐标和初始坐标之间的距离坐标 小于 x
像素。
var paper = Raphael("canvas", 600, 600);
var title = this.paper.text(50, 10, 'click on the square');
var rect = this.paper.rect(20, 20, 30, 30);
var PX_TO_IGNORE = 5; // pixels to ignore
rect.attr('fill', 'black')
var start = function () {
this.odx = 0;
this.ody = 0;
this.initialCor = null;
},
move = function (dx, dy) {
// check if not initialized(if it`s first click)
if(this.initialCor == null) {
this.initialCor = {x: dx, y: dy};
return;
}
// ignore PX_TO_IGNORE pixels(basically circle with radius PX_TO_IGNORE)
if(distance(this.initialCor.x, dx, this.initialCor.y, dy) < PX_TO_IGNORE ) {
return;
}
title.attr('text', 'mouse moved')
this.translate(dx - this.odx, dy - this.ody);
this.odx = dx;
this.ody = dy;
},
up = function () {
title.attr('text', 'mouse up')
};
rect.drag(move, start, up);
rect.mousedown(function(e){
title.attr('text', 'mouse down')
})
// helper function to calcuate distance between two coordinates
function distance(x1,x2,y1,y2) {
return Math.sqrt( Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2) );
}
在这个 jsFiddle 中,我有一个 Raphael canvas 和一个可以单击或拖动的矩形。
如果将鼠标移动到矩形上然后单击它,您可能会稍微移动它(文本会显示)。我需要在我的应用程序中处理点击并在用户打算点击时忽略拖动运动。例如,只有当拖动大于 5 px 时,我才需要启用拖动。如果只是为了点击,有没有办法让 Raphael 不移动矩形?
var paper = Raphael("canvas", 600, 600);
var title = this.paper.text(50, 10, 'click on the square');
var rect = this.paper.rect(20, 20, 30, 30);
rect.attr('fill', 'black')
var start = function () {
this.odx = 0;
this.ody = 0;
},
move = function (dx, dy) {
title.attr('text', 'mouse moved')
this.translate(dx - this.odx, dy - this.ody);
this.odx = dx;
this.ody = dy;
},
up = function () {};
rect.drag(move, start, up);
rect.mousedown(function(e){
title.attr('text', 'mouse down')
})
您可以在移动开始时存储初始坐标,而忽略当前坐标和初始坐标之间的距离坐标 小于 x
像素。
var paper = Raphael("canvas", 600, 600);
var title = this.paper.text(50, 10, 'click on the square');
var rect = this.paper.rect(20, 20, 30, 30);
var PX_TO_IGNORE = 5; // pixels to ignore
rect.attr('fill', 'black')
var start = function () {
this.odx = 0;
this.ody = 0;
this.initialCor = null;
},
move = function (dx, dy) {
// check if not initialized(if it`s first click)
if(this.initialCor == null) {
this.initialCor = {x: dx, y: dy};
return;
}
// ignore PX_TO_IGNORE pixels(basically circle with radius PX_TO_IGNORE)
if(distance(this.initialCor.x, dx, this.initialCor.y, dy) < PX_TO_IGNORE ) {
return;
}
title.attr('text', 'mouse moved')
this.translate(dx - this.odx, dy - this.ody);
this.odx = dx;
this.ody = dy;
},
up = function () {
title.attr('text', 'mouse up')
};
rect.drag(move, start, up);
rect.mousedown(function(e){
title.attr('text', 'mouse down')
})
// helper function to calcuate distance between two coordinates
function distance(x1,x2,y1,y2) {
return Math.sqrt( Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2) );
}