我是编程新手,并且将 Javascript 作为学校类(class)。我遇到了这个问题,并且由于无法更新文本而陷入困境;例如,当我将鼠标移到 Canvas 上时,它只会与文本重叠。有没有更新文本的解决方案,或者我只是缺少一些基本的东西?
/* This program displays the x and y
* coordinates in a label on the screen
* and updates when the mouse moves */
function start(){
mouseMoveMethod(drawCoordinates);
}
function drawCoordinates(e){
var txt = new Text(e.getX(), "15pt Arial");
var text = new Text(e.getY(), "15pt Arial");
txt.setPosition(100, 100);
txt.setColor(Color.black);
add(txt);
text.setPosition(100, 200);
text.setColor(Color.black);
add(text);
}
最佳答案
问题是每次执行drawCooperatives 方法时都会创建一个新的文本。 如果这是基于浏览器的,你需要这样的东西......
function drawCoordinates(e){
//Check if we can find the lable for X first.
//If we can't, create a new one....
var txt = document.getElementById('txtX');
if(txt==null)
{
txt = new Text(e.getX(), "15pt Arial");
txt.setPosition(100, 100);
txt.setColor(Color.black);
add(txt);
}
else
txt.text = e.getX();
//and the same for Y
}
希望有帮助
关于javascript - 使用 mouseMoveMethod 时如何更新文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496961/