A simple example showing how to detect a double tap with d3.js, and an accompanying method for reusability.
-
-
Save venkatesh22/de1a4af8a1ee9c7912bfa84878c671c6 to your computer and use it in GitHub Desktop.
Detecting double taps with d3.js
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<style> | |
div { | |
position: absolute; | |
height:300px; | |
width:300px; | |
border:2px solid #000; | |
text-align: center; | |
line-height: 300px; | |
} | |
</style> | |
</head> | |
<body> | |
<div>Double tap inside the box</div> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script type="text/javascript"> | |
d3.selection.prototype.dblTap = function(callback) { | |
var last = 0; | |
return this.each(function() { | |
d3.select(this).on("touchstart", function(e) { | |
if ((d3.event.timeStamp - last) < 500) { | |
return callback(e); | |
} | |
last = d3.event.timeStamp; | |
}); | |
}); | |
} | |
d3.select("div").dblTap(function() { | |
alert("Double tap!"); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment