[英]How to perpetually execute a function while the mouse is down, until the mouse is up?
I have the following function which rotates an image 我具有以下旋转图像的功能
function rotateImage(offsetSelector, xCoordinate, yCoordinate, imageSelector){
var x = xCoordinate - offsetSelector.offset().left - offsetSelector.width()/2;
var y = -1*(yCoordinate - offsetSelector.offset().top - offsetSelector.height()/2);
var theta = Math.atan2(y,x)*(180/Math.PI);
var rotate = 'rotate(' + theta + 'deg)';
imageSelector.css('-moz-transform', rotate);
}
However when I call it the following way it only executes once upon a mousedown. 但是,当我以以下方式调用它时,仅在鼠标按下时执行一次。
$('#someImage').on('mousedown', function(event){
rotateImage($(this).parent(), event.pageX,event.pageY, $(this));
});
My intent is for the image to rotate while it's being grabbed and until the user lets go of the mouse click. 我的意图是使图像在被抓住的同时旋转,直到用户放开鼠标为止。 Is there a simple way to do this without the use of external libraries? 有没有使用外部库的简单方法?
Example: 例:
var timer;
function rotateImageTimer(offsetSelector, xCoordinate, yCoordinate, imageSelector)
{
timer = setInterval("rotateImage('"+offsetSelector+"', '"+xCoordinate+"', '"+yCoordinate+"', '"+imageSelector+"')", 100);
}
function rotateImage(offsetSelector, xCoordinate, yCoordinate, imageSelector){
var x = xCoordinate - offsetSelector.offset().left - offsetSelector.width()/2;
var y = -1*(yCoordinate - offsetSelector.offset().top - offsetSelector.height()/2);
var theta = Math.atan2(y,x)*(180/Math.PI);
var rotate = 'rotate(' + theta + 'deg)';
imageSelector.css('-moz-transform', rotate);
}
$('#someImage').on('mousedown', function(event){
rotateImageTimer($(this).parent(), event.pageX,event.pageY, $(this));
});
$('#someImage').on('mouseup', function(event){
clearIneterval(timer);
});
You will need to repeatedly call some code using setInterval when you mousedown, and cancel it when you mouseup. 按下鼠标时,您将需要使用setInterval反复调用某些代码,而按下鼠标时,则将其取消。
An example of that can be found here: http://www.codingforums.com/showthread.php?t=166115 可以在此处找到一个示例: http : //www.codingforums.com/showthread.php?t=166115
Some information on setInterval & setTimeout: http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/ 有关setInterval和setTimeout的一些信息: http : //www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
var isMouseDown = false;
$('#someImage').on('mousedown', function(event){
isMouseDown = true;
rotateImage($(this).parent(), event.pageX,event.pageY, $(this));
});
$('#someImage').on('mouseup', function(event){
isMouseDown = false;
});
function rotateImage(offsetSelector, xCoordinate, yCoordinate, imageSelector){
while(isMouseDown){
var x = xCoordinate - offsetSelector.offset().left - offsetSelector.width()/2;
var y = -1*(yCoordinate - offsetSelector.offset().top - offsetSelector.height()/2);
var theta = Math.atan2(y,x)*(180/Math.PI);
var rotate = 'rotate(' + theta + 'deg)';
imageSelector.css('-moz-transform', rotate);
}// end of while
}
In above code I have a variabl isMouseDown
. 在上面的代码中,我有一个变量isMouseDown
。 When mouse is down its set to true
. 当鼠标按下时,其设置为true
。 While its true your image should rotate. 虽然它是真的,但您的图像应该旋转。 I am also binding event on mouseup
. 我也在mouseup
上绑定事件。 When its called , isMouseDown
gets set to false
. 调用时, isMouseDown
设置为false
。 Hence stopping the rotation. 因此停止旋转。
I use same technique for my drawing app when I need to draw on canvas when mouse is down and stop when its up again. 当我需要在鼠标按下时在画布上绘画,而在鼠标再次按下时停止在画布上时,我对绘画应用程序使用相同的技术。 Hope it helps :) 希望能帮助到你 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.