Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从另一个函数中调用时,鼠标坐标不可用_Javascript_D3.js - Fatal编程技术网

Javascript 从另一个函数中调用时,鼠标坐标不可用

Javascript 从另一个函数中调用时,鼠标坐标不可用,javascript,d3.js,Javascript,D3.js,我正在重新组织一些代码,使变量对各种函数可见。我有一个名为getMouseXY的函数,当该函数在全局环境中(不是从另一个函数调用)时,该函数运行良好。然而,当我把这个函数放在另一个函数中时,它给出了一个错误:uncaughttypeerror:undefined不是一个我确定来自d3.mouse(this)的函数。我问控制台这是什么,这是窗口,这似乎是合理的。另一种选择是,我尝试使用.pageX方法,但坐标不正确。如果有必要,我可以解决这个问题,但我不明白为什么我原来的方法在这种情况下会失败。很

我正在重新组织一些代码,使变量对各种函数可见。我有一个名为
getMouseXY
的函数,当该函数在全局环境中(不是从另一个函数调用)时,该函数运行良好。然而,当我把这个函数放在另一个函数中时,它给出了一个错误:
uncaughttypeerror:undefined不是一个我确定来自
d3.mouse(this)
的函数。我问控制台这是什么,这是窗口,这似乎是合理的。另一种选择是,我尝试使用
.pageX
方法,但坐标不正确。如果有必要,我可以解决这个问题,但我不明白为什么我原来的方法在这种情况下会失败。很抱歉,这不是一个有效的示例,但是有人能解释为什么会抛出错误吗<从主线程调用code>activateGuides
。代码如下:

var activateGuides = function() { // controls the guides (cursor) in the contour area

    var getMouseXY = function() { // get the mouse coordinates & report in terms of [0...1] in the contour area
    // var mouse = d3.mouse(this); // error: undefined, but this = window
    // mX = mouse[0];
    // mY = mouse[1];
    mX = d3.event.pageX; // this approach works with slightly wrong coords
    mY = d3.event.pageY;
    mX = mX - lPad // offset reference point (ul corner of contour is 0,0)
    mY = mY - tPad
    if (mX < 0) {mX = 0}; // truncate low
    if (mY < 0) {mY = 0};
    if (mX > conWidth) {mX = conWidth}; // truncate high
    if (mY > conHeight) {mY = conHeight};
    mX = mX/conWidth // as fraction
    mY = mY/conHeight
    followMouse(mX, mY); // draws guidelines at the mouse position
    document.Show.mouseX.value = mX; 
    document.Show.mouseY.value = mY;
    } // end of getMouseXY

} // end of activateGuides
var activateGuides=function(){//控制轮廓区域中的导向(光标)
var getMouseXY=function(){//获取鼠标坐标并报告等高线区域中的[0…1]
//var mouse=d3.mouse(this);//错误:未定义,但this=window
//mX=鼠标[0];
//mY=鼠标[1];
mX=d3.event.pageX;//这种方法适用于稍有错误的坐标
mY=d3.event.pageY;
mX=mX-lPad//偏移参考点(轮廓的ul角为0,0)
mY=mY-tPad
if(mX<0){mX=0};//截断低位
如果(mY<0){mY=0};
if(mX>conWidth){mX=conWidth};//截断高
如果(mY>conHeight){mY=conHeight};
mX=mX/conWidth//作为分数
我的身高
followMouse(mX,mY);//在鼠标位置绘制准则
document.Show.mouseX.value=mX;
document.Show.mouseY.value=mY;
}//getMouseXY的结束
}//activateGuides结束

试试event.x和event。y@dandavis谢谢你的建议,它工作得更好,我可以清理确切的坐标。我主要想知道为什么
d3.mouse(这个)
在新的上下文中停止工作。有什么想法吗?我用
d3.mouse(document.getElementById(“CON”))替换了
d3.mouse(this)
,其中“CON”是我使用鼠标的svg元素的id。这也消除了修复偏移的需要。但是仍然很奇怪,为什么
this
不起作用。您传递的this在您的外部包装函数中意味着与直接对象方法不同的东西。您也可以使用function.bind(this)将某个this值冻结到位,但您现在拥有的应该可以按预期工作。
event.x
event.y
提供相对于页面左上角的位置,而不是相对于svg项的位置。它不等同于d3。鼠标(this)
返回相对于
左上角的位置。