我正试图在转换后的画布上获取鼠标位置。这里是我的大小调整方法:如何在转换后的HTML5画布上获取鼠标位置
window.addEventListener('resize', resize);
function resize() {
screenWidth = window.innerWidth;
screenHeight = window.innerHeight;
scaleFillNative = MathMAX(screenWidth/maxScreenWidth, screenHeight/maxScreenHeight);
mainCanvas.width = screenWidth;
mainCanvas.height = screenHeight;
mainContext.setTransform(scaleFillNative, 0, 0, scaleFillNative, Math.floor((screenWidth - (maxScreenWidth * scaleFillNative))/2),
Math.floor((screenHeight - (maxScreenHeight * scaleFillNative))/2));
}
的maxScreenWidth和maxScreenHeight表示画布应该转变到本机屏幕尺寸。
实际调整大小工作正常。然而,问题是我试图在画布上的鼠标位置渲染一个圆。鼠标位置被设定为如下:
window.addEventListener('mousemove', gameInput, false);
var mouseX, mouseY;
function gameInput(e) {
e.preventDefault();
e.stopPropagation();
mouseX = e.clientX;
mouseY = e.clientY;
}
然后它呈现像这样:
renderCircle(mouseX/scaleFillNative, mouseY/scaleFillNative, 10);
x位置被正确地呈现。但是,当我调整窗口大小以使宽度小于高度时,它不再呈现在正确的x位置。 y位置始终偏移。
看看这个http://stackoverflow.com/questions/17130395/real-mouse-position-in-canvas – Derek
我试过了,但它有同样的结果。 – user3024235
@ user3024235你能分享一个工作小提琴吗? – sam