Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

mouse coordinates not math with canvas coordinates in js

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)
})
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #mouse #coordinates #math #canvas #coordinates #js
ADD COMMENT
Topic
Name
7+2 =