Skip to content

Instantly share code, notes, and snippets.

@revuel
Created August 19, 2025 21:37
Show Gist options
  • Save revuel/fdbcef904e650337e594680fcfdc5efc to your computer and use it in GitHub Desktop.
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
(() => {
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