在本书Core HTML5 Canvas: Graphics, Animation, and Game Development中,我发现作者将 windowToCanvas 实现为
function windowToCanvas(canvas, x, y) {
var bbox = canvas.getBoundingClientRect();
return { x: x - bbox.left * (canvas.width / bbox.width),
y: y - bbox.top * (canvas.height / bbox.height)
};
}
但我确实认为它应该返回:
{ x: (x - bbox.left) * (canvas.width / bbox.width),
y: (y - bbox.top) * (canvas.height / bbox.height)
};
我对吗?
您可以自由查看here章。在示例1.6