我有这段代码,它在点击和拖动时在画布周围拖动一个球。
canvas.addEventListener('mousedown', function () {
var initialClickX = mouse.x;
var initialClickY = mouse.y;
var initialBallX = ball.x;
var initialBallY = ball.y;
canvas.addEventListener('mousemove', function() {
onMouseMove(initialClickX, initialClickY, mouse.x, mouse.y, initialBallX, initialBallY)
}, false);
}, false);
function onMouseMove(initialClickX, initialClickY, mouseX, mouseY, initialBallX, initialBallY){
ball.x = mouseX + initialBallX - initialClickX;
ball.y = mouseY + initialBallY - initialClickY;
draw();
}
问题是,当我鼠标向上时,我无法让球停止拖动。我知道我需要添加:
canvas.removeEventListener('mousemove', function() {
onMouseMove(initialClickX, initialClickY, mouse.x, mouse.y, initialBallX, initialBallY)
}, false);
但是因为该函数在包装器中,所以上面没有任何效果,球仍然粘在我的光标上。任何人都可以提出解决这个问题的方法吗?