我有一个事件监听器来抓取相对于它定位在mousemove上的fires上的画布的游标坐标。如何更改此设置,以便在鼠标移动之前提供页面加载的坐标?我试过了,但坐标显示undefined
。看看:
<body onload="start()">
<script>
function start(){
Coords.start();
}
var Coords = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 600;
this.canvas.height = 350;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.canvas.addEventListener("mousemove", getRelativeCoords, false);
getRelativeCoords(Coords.canvas); //my attempt at grabbing coords on page load. this displays 'underfined'
},
}
function getRelativeCoords(event) {
document.getElementById("coordinates").innerHTML = "x: " + event.offsetX + " y: " + event.offsetY;
}
</script>
<p id="coordinates"></p>
</body>
我有一个显示coordiantes的范围。
我对javascript(几天前开始学习)并不熟悉,所以我真的很感激解释。谢谢