0

我正在尝试在 HTML 画布中拖动一些形状,但在确定鼠标坐标 [dx,dy] 的变化方面遇到了问题

首先,坐标本身没有问题,存储在mousePos翻转效果完美无缺的工作中。我正在做的是,在第一次输入形状时,保存鼠标坐标。

pos = {x : mousePos[0] , y : mousePos[1]};

然后,onMotion 会在每次鼠标移动时更新坐标并记录当前位置

dx=mousePos[0]-pos.x;
dy=mousePos[1]-pos.y;
pos = {x : mousePos[0] , y : mousePos[1]};

然后我将 dx 和 dy 值添加到形状坐标中(让我们以一个简单的矩形为例)

ctx.fillRect(0+this.dx,0+this.dy,100+this.dx,100+this.dy);

只要鼠标不移动太快,它就可以相对较好地工作(虽然并不完美)。如果我非常快速地移动鼠标,而不离开窗口,矩形不会赶上鼠标。我可以理解是否有延迟赶上鼠标,但是增量值如何关闭?显然我们知道我们从哪里开始,即使在这个过程中跳过了几十个/几百个像素,最终鼠标应该停止并且应该计算正确的增量值。

任何帮助将不胜感激,因为我在这里遇到了概念墙。

4

1 回答 1

0

You might try to get e.layerX-Y when the onMotion is fired to get the real position instead of the delta. This way it can't be "off".

To use this, place your shape into a div with style="padding:0px;margin=0px;" , because the position is relative to the parent block.

于 2011-07-26T10:04:24.453 回答