Skip to content

Instantly share code, notes, and snippets.

@revuel
Last active August 27, 2025 14:23
Show Gist options
  • Save revuel/3539b92a7ce9d79c39e9ba2c893f4eee to your computer and use it in GitHub Desktop.
Save revuel/3539b92a7ce9d79c39e9ba2c893f4eee to your computer and use it in GitHub Desktop.
canvas clicker & normalising coords
(() => {
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