Thursday, June 28, 2012

Fire mouse move event less to prevent CPU drainage and get rid of menus closing because the mouse left it for 2 pixels!

Many times when we code and listen to the mousemove the result becomes too sensetive.


If I had a menu and wanted to turn it of when the user moved away from it fine. But I wanted this menu to open 2 pixels bellow the click item then inadvertently the mouse would float for a brief moment not on the calling button nor on the menu and thus the menu would close.


Another scenario would be when the user wants to select the last item but they ooppps went to far with their mouse and then the menu closed.


I don't want the menu to remain open until they click out of it either. So here comes the code that reducedes mouse sensitivity in:


var timer = null;
var mouseX = 0;
var mouseY = 0;
var enableHandler = false;
function DrawTo(e){
    mouseX = parseInt(e.pageX);
    mouseY = parseInt(e.pageY);

    console.log(mouseX+" "+mouseY);
}
$(document).mousemove(function (e) {
        if (enableHandler) {
            DrawTo(e);
            enableHandler = false;
        }
});

timer = window.setInterval(function(){
    enableHandler = true;
}, 50);


$(document).click(function () {
       enableHandler = true;
});
this code will let the mouse get engaged every 50ms. Also when clicked the enableHandler is set to true to not miss the first click. 


This is just an example if you were making a drawing application and didn't want to miss the first drawing point. 


Obviously if you were making a drawing application it would be smarter to tie a canDraw boolean to the events mousedown and mouseup but still enableHandler once onMouseDown would help with capturing the first drawing point.

10 comments:

  1. I was tired of frequent disturbance in work because of mouse pad size.Besides, I had to change my computer mouse almost in every year because of its tearing apart. So I switched to best ergonomic mouse pad .It's really awesome.

    ReplyDelete
  2. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks custom mouse mat

    ReplyDelete
  3. We can come over and for a "small fee" identify the problem and let you know how much it will cost to fix it".plumber Devonport

    ReplyDelete
  4. , when you stroll into the specialist's office do you ask "What amount"http://www.drainblasters.co.nz/

    ReplyDelete
  5. Thanks for the valuable information and insights you have so provided here... latest news about btc

    ReplyDelete
  6. it can turn into a serious problem. One can’t be prepared for every scenario that can cause havoc to one’s home or business. However it’s always good to have a plan for what to do if the inevitable happens. Especially if you live in a flood zone. Flood Damage Services

    ReplyDelete
  7. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me. Laptop HP

    ReplyDelete
  8. I really enjoyed reading this post, big fan. Keep up the good work andplease tell me when can you publish more articles or where can I read more on the subject? HipStore

    ReplyDelete
  9. Once in a while a plumber may get the expertise and begin doing tasks without considering authorizing. You as buyer should know about these plumbers. Plumbing

    ReplyDelete