I have a problem with variables scope. In the code below, I have two variables that i want to assign them some values and then later in the code use these values. The variables are: eSVG, originMouse. I assign their values when I am using mousedown event function and then trying to use these values in mousemove event function but i always get undefined values. Could you please tell me what is happening here?
Thanks guys
var eSVG, originMouse;
svg.on("mousedown", function() {
eSVG= this, // svg element
originMouse = d3.mouse(eSVG), // mouse origing
rect = svg.append("rect").attr("class", "zoom");
originMouse[0] = Math.max(0, Math.min(width, originMouse[0]));
originMouse[1] = Math.max(0, Math.min(height, originMouse[1]));
})
.on("mousemove", function() {
console.log(eSVG); **// undefined**
var m = d3.mouse(eSVG);
console.log(originMouse); **// undefined**
m[0] = Math.max(0, Math.min(width, m[0]));
m[1] = Math.max(0, Math.min(height, m[1]));
rect.attr("x", Math.min(originMouse[0], m[0])-130)
.attr("y", Math.min(originMouse[1], m[1])-80)
.attr("width", Math.abs(m[0] - originMouse[0]))
.attr("height", Math.abs(m[1] - originMouse[1]));
})
mousedown
is firing beforemousemove
?