forked from aaizemberg's block: d3-brush mini example
xxxxxxxxxx
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
<script>
// set drawing region dimensions
var width = 400, height = 400;
// create drawing region svg
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height",height);
// add outer black box
svg.append("rect")
.attr("width",width)
.attr("height",height)
.style("fill","none")
.style("stroke","black");
// create brush
var brush = d3.brush();
// set brush extend (0,0) is top left corner
brush.extent([[0, 0], [ width, height]]);
// attach events
brush
.on("start brush", brushing)
.on("end", brushend);
// call brush on selection
svg.append("g")
.call(brush);
// during brushing send coordinates to console
function brushing() {
// console.log( d3.event.selection );
var s = d3.event.selection,
x0 = s[0][0],
y0 = s[0][1],
x1 = s[1][0],
y1 = s[1][1],
dx = s[1][0] - x0,
dy = s[1][1] - y0;
console.log("("+x0+","+y0+")-("+x1+","+y1+")");
}
// on brush end, console log if no selection
function brushend() {
console.log('end');
if (!d3.event.selection) {
console.log('There is no selection');
}
}
</script>
</body>
</html>
https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js