Created
August 19, 2025 21:37
-
-
Save revuel/fdbcef904e650337e594680fcfdc5efc to your computer and use it in GitHub Desktop.
Snippet to get coordinates inside a canvas. To be used in the browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
const canvas = document.querySelector("canvas"); | |
if (!canvas) { | |
console.warn("⚠️ No <canvas> found in page."); | |
return; | |
} | |
canvas.addEventListener("click", (e) => { | |
const rect = canvas.getBoundingClientRect(); | |
const x = e.clientX - rect.left; | |
const y = e.clientY - rect.top; | |
console.log(`Click en: x=${x}, y=${y}`); | |
}); | |
console.log("✅ Listener added to canvas. Click and see coordinates on the console."); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment