Last active
March 21, 2017 17:28
-
-
Save jonathanlurie/f7261e717fc94063b4992fcecb71ba69 to your computer and use it in GitHub Desktop.
Hides the cursor after 2 seconds, then show again when moving. https://bl.ocks.org/jonathanlurie/f7261e717fc94063b4992fcecb71ba69
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
<html> | |
<head> | |
</head> | |
<body> | |
Hides the cursor after 2 seconds, then show again when moving. | |
<script> | |
(function() { | |
// after how many millisec do we want to hide the cursor | |
var intervalMs = 2000; | |
function hideCursor(){ | |
document.body.style.cursor = "none"; | |
} | |
function showCursor(){ | |
document.body.style.cursor = "default"; | |
} | |
var lastMousePosition = {x:0, y:0}; | |
var mousePosition = {x:0, y:0}; | |
var lastTime = 0; | |
window.onmousemove = function(e) { | |
lastMousePosition.x = e.clientX; | |
lastMousePosition.y = e.clientY; | |
lastTime = performance.now(); | |
showCursor(); | |
} | |
window.setInterval(function(){ | |
if(lastMousePosition.x == mousePosition.x && | |
lastMousePosition.y == mousePosition.y && | |
(performance.now() - lastTime) >= intervalMs ) | |
{ | |
hideCursor(); | |
}else{ | |
lastMousePosition.x = mousePosition.x; | |
lastMousePosition.y = mousePosition.y; | |
} | |
}, 100); | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment