Html5 Canvas : How To Handle Mousedown Mouseup Mouseclick
I been playing around with html5 canvas and ran into a problem. canvas.onmousedown = function(e){ dragOffset.x = e.x - mainLayer.trans.x; dragOffset.y = e.y - mainL
Solution 1:
A click event happens after a successful mousedown and mouseup on an element. Is there any particular reason you are using click on top of the mouse events? You should be fine with just mousedown/mouseup, click is a convenience event that saves you a little bit of coding.
I would generally do it this way:
var mouseIsDown = false;
canvas.onmousedown = function(e){
dragOffset.x = e.x - mainLayer.trans.x;
dragOffset.y = e.y - mainLayer.trans.y;
mouseIsDown = true;
}
canvas.onmouseup = function(e){
if(mouseIsDown) mouseClick(e);
mouseIsDown = false;
}
canvas.onmousemove = function(e){
if(!mouseIsDown) return;
mainLayer.trans.x = e.x - dragOffset.x;
mainLayer.trans.y = e.y - dragOffset.y;
returnfalse;
}
functionmouseClick(e){
// click action
}
Solution 2:
in the function mouse move set a boolean to say that a move occured, encompase all the code in mouseup and click with an if statement to check that a drag did not occur. After these if statements and before the end of the functions set the dragging boolean to false.
Solution 3:
Try declaring a variable such as clickStatus = 0;
and at the start of each function check to ensure the correct value.
if (clickstatus == 0) {
clickstatus =1;
...//function code
};
Post a Comment for "Html5 Canvas : How To Handle Mousedown Mouseup Mouseclick"