Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect the distance that the user's mouse has moved?

I'm trying to detect the distance the mouse has moved, in pixels. I am currently using:

$(document).mousemove(function(event) {
    var startingTop = 10,
        startingLeft = 22,
        math = Math.abs(((startingTop - event.clientY) + (startingLeft - event.clientX)) + 14) + 'px';
    $('span').text('From your starting point(22x10) you moved:   ' + math);
});

However, I don't feel like this is the right way to do this, or is it? It doesn't feel to consistent to me.

Here is a demo of how it is working right now: http://jsfiddle.net/Em4Xu/1/

Extra Details:

I'm actually developing a drag & drop plugin and I want to create a feature called distance, like draggable has it, where you have to pull your mouse a certain amount of pixels before it drags. I'm not 100% sure how to do this, so first I need to get the pixels that the mouse has moved from the startingTop and startingLeft position.

Does anyone have any suggestions?

like image 644
Shawn31313 Avatar asked Dec 31 '11 05:12

Shawn31313


1 Answers

You got your math wrong. Here's improved version: http://jsfiddle.net/stulentsev/Em4Xu/26/

$(document).mousemove(function(event) {
    var startingTop = 10,
        startingLeft = 22,
        math = Math.round(Math.sqrt(Math.pow(startingTop - event.clientY, 2) + 
                                    Math.pow(startingLeft - event.clientX, 2))) + 'px';
    $('span').text('From your starting point(22x10) you moved:   ' + math);
});
like image 178
Sergio Tulentsev Avatar answered Oct 19 '22 18:10

Sergio Tulentsev