Last active
August 27, 2025 14:23
-
-
Save revuel/3539b92a7ce9d79c39e9ba2c893f4eee to your computer and use it in GitHub Desktop.
canvas clicker & normalising coords
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(); | |
// Raw click coordinates relative to canvas | |
const rawX = e.clientX - rect.left; | |
const rawY = e.clientY - rect.top; | |
// Normalize based on the internal resolution of the canvas | |
const normalizedX = rawX / rect.width; | |
const normalizedY = rawY / rect.height; | |
console.log( | |
`Click at (raw): x=${rawX.toFixed(2)}, y=${rawY.toFixed(2)} | (normalized): {"x": ${normalizedX.toFixed(4)}, "y": ${normalizedY.toFixed(4)}}` | |
); | |
}); | |
console.log("✅ Listener with normalization added. Click on the canvas to log coordinates."); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment