This D3.js example shows how to create a selection rectangle that is usable on both desktop and touch devices.
Last active
April 12, 2016 11:28
-
-
Save vwochnik/712c8131afc9b0be7f57a3dde95be47a to your computer and use it in GitHub Desktop.
D3.js rectangular selection
This file contains hidden or 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
license: mit |
This file contains hidden or 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 rect(x, y, w, h) { | |
return "M"+[x,y]+" l"+[w,0]+" l"+[0,h]+" l"+[-w,0]+"z"; | |
} | |
var width = 720, | |
height = 540; | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var selection = svg.append("path") | |
.attr("class", "selection") | |
.attr("visibility", "hidden"); | |
var startSelection = function(start) { | |
selection.attr("d", rect(start[0], start[0], 0, 0)) | |
.attr("visibility", "visible"); | |
}; | |
var moveSelection = function(start, moved) { | |
selection.attr("d", rect(start[0], start[1], moved[0]-start[0], moved[1]-start[1])); | |
}; | |
var endSelection = function(start, end) { | |
selection.attr("visibility", "hidden"); | |
}; | |
svg.on("mousedown", function() { | |
var subject = d3.select(window), parent = this.parentNode, | |
start = d3.mouse(parent); | |
startSelection(start); | |
subject | |
.on("mousemove.selection", function() { | |
moveSelection(start, d3.mouse(parent)); | |
}).on("mouseup.selection", function() { | |
endSelection(start, d3.mouse(parent)); | |
subject.on("mousemove.selection", null).on("mouseup.selection", null); | |
}); | |
}); | |
svg.on("touchstart", function() { | |
var subject = d3.select(this), parent = this.parentNode, | |
id = d3.event.changedTouches[0].identifier, | |
start = d3.touch(parent, id), pos; | |
startSelection(start); | |
subject | |
.on("touchmove."+id, function() { | |
if (pos = d3.touch(parent, id)) { | |
moveSelection(start, pos); | |
} | |
}).on("touchend."+id, function() { | |
if (pos = d3.touch(parent, id)) { | |
endSelection(start, pos); | |
subject.on("touchmove."+id, null).on("touchend."+id, null); | |
} | |
}); | |
}); |
This file contains hidden or 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 lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>D3 rectangular selection</title> | |
<style> | |
.selection { | |
fill: #ADD8E6; | |
stroke: #ADD8E6; | |
fill-opacity: 0.3; | |
stroke-opacity: 0.7; | |
stroke-width: 2; | |
stroke-dasharray: 5, 5; | |
} | |
</style> | |
</head> | |
<body> | |
<script src="//d3js.org/d3.v3.js"></script> | |
<script src="draw.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment