最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getBoundingClientRect is not a function - Stack Overflow

programmeradmin4浏览0评论

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);
}

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"

Share Improve this question asked Jan 16, 2018 at 21:58 SamSam 3,2443 gold badges20 silver badges22 bronze badges 1
  • 1 really depends on how you call showCoords - what do you pass as the first argument – Jaromanda X Commented Jan 16, 2018 at 22:01
Add a comment  | 

1 Answer 1

Reset to default 21

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.

发布评论

评论列表(0)

  1. 暂无评论