I am trying to get the coordinates of the mouse-click on a image. So I'm using getBoundingClientRect like this
function showCoords(canvas, event) {
var rect = canvas.getBoundingClientRect();
var x = event.clientX - rect.left;
var y = event.clientY - rect.top;
console.log("x: " + x + " y: " + y);
}
But I get this error "canvas.getBoundingClientRect is not a function"
The getBoundingClientRect() method returns the size of an element and its position relative to the viewport. The getBoundingClientRect() method returns a DOMRect object with eight properties: left, top, right, bottom, x, y, width, height.
getBoundingClientRect() method returns a DOMRect object providing information about the size of an element and its position relative to the viewport.
Border, padding and margin are not included.
That means that canvas
variable is not actually a Canvas element.
It can be undefined
, still not initialized or incorrectly selected.
You need to double check it and maybe try to use event.target
if the click event is added to the canvas element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With