Created
June 11, 2013 14:32
-
-
Save ripper234/5757309 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function drawMouseSpeedDemo() { | |
var mrefreshinterval = 500; // update display every 500ms | |
var lastmousex=-1; | |
var lastmousey=-1; | |
var lastmousetime; | |
var mousetravel = 0; | |
var mpoints = []; | |
var mpoints_max = 30; | |
$('html').mousemove(function(e) { | |
var mousex = e.pageX; | |
var mousey = e.pageY; | |
if (lastmousex > -1) { | |
mousetravel += Math.max( Math.abs(mousex-lastmousex), Math.abs(mousey-lastmousey) ); | |
} | |
lastmousex = mousex; | |
lastmousey = mousey; | |
}); | |
var mdraw = function() { | |
var md = new Date(); | |
var timenow = md.getTime(); | |
if (lastmousetime && lastmousetime!=timenow) { | |
var pps = Math.round(mousetravel / (timenow - lastmousetime) * 1000); | |
mpoints.push(pps); | |
if (mpoints.length > mpoints_max) | |
mpoints.splice(0,1); | |
mousetravel = 0; | |
$('#mousespeed').sparkline(mpoints, { width: mpoints.length*2, tooltipSuffix: ' pixels per second' }); | |
} | |
lastmousetime = timenow; | |
setTimeout(mdraw, mrefreshinterval); | |
} | |
// We could use setInterval instead, but I prefer to do it this way | |
setTimeout(mdraw, mrefreshinterval); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can I use this on React.js. And how to use it on React.js ???