Code taken from Live drawing of a line in D3.js
var line;
var vis = d3.select("body").append("svg")
.attr("width", 600)
.attr("height", 400)
.on("mousedown", mousedown)
.on("mouseup", mouseup);
function mousedown() {
var m = d3.mouse(this);
line = vis.append("line")
.attr("x1", m[0])
.attr("y1", m[1])
.attr("x2", m[0])
.attr("y2", m[1]);
vis.on("mousemove", mousemove);
}
function mousemove() {
var m = d3.mouse(this);
line.attr("x2", m[0])
.attr("y2", m[1]);
}
function mouseup() {
vis.on("mousemove", null);
}
How do I know if the user clicks on a previously appended line? In the mousedown() function I only want a new line appended if the user is clicking on the svg, and not a line appended to the svg.
Essentially, in the mousedown() function, how do you say that if m != line, then continue appending a line?