gpt4 book ai didi

javascript - 避免在拉斐尔中无意中拖动

转载 作者:行者123 更新时间:2023-11-30 13:46:35 26 4
gpt4 key购买 nike

在此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) );
}

http://jsfiddle.net/htLn819y/4/

关于javascript - 避免在拉斐尔中无意中拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59202326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com