I made a raycaster to cross some object inside the canvas. It works well if the canvas is one in the browser window, but it does not get intersection if I put the canvas inside some other GUI, and therefore the position of the canvas is different. I think this is a problem with the coordinates of the mouse. How can I debug it? If I know the coordinates of the mouse, how can I understand what is the coordinate of the canvas?
function getPosition(event) {
var x = new Number();
var y = new Number();
if (event.x != undefined && event.y != undefined) {
x = event.x;
y = event.y;
} else {
x = event.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = event.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
return {x: x, y: y};
}
source
share