In google chrome, getBoundingClientRect().x is undefined

前端 未结 1 786
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 06:48

I\'m performing drawing operations on canvas. The best way to calculate cursor position relative to canvase top left corner is, in my opinion, usage of .getBoundingCli

相关标签:
1条回答
  • 2020-12-29 07:21

    The object returned by getBoundingClientRect() may have x and y properties in some browsers, but not all. It always has left, top, right, and bottom properties.

    I recommend using the MDN docs instead of any W3C specs when you want to know what browsers actually implement. See the MDN docs for getBoundingClientRect() for more accurate information on this function.

    So all you need to do is change your rect.x and rect.y to rect.left and rect.top.

    0 讨论(0)
提交回复
热议问题