📜  鼠标坐标不是数学与 js 中的画布坐标 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:10.755000             🧑  作者: Mango

代码示例1
function getCursorPosition(canvas, event) {
    const rect = canvas.getBoundingClientRect()
    const x = event.clientX - rect.left
    const y = event.clientY - rect.top
    console.log("x: " + x + " y: " + y)
}

const canvas = document.querySelector('canvas')
canvas.addEventListener('mousedown', function(e) {
    getCursorPosition(canvas, e)
})