How can I get e.offsetX on mobile/iPad

北城以北 提交于 2021-02-06 15:17:19

问题


There was no offsetX in e.touches[0] when I tried on iPad. Does anyone know where I can get this value of offsetX?


回答1:


You can use clientX or pageX, see here




回答2:


The correct answer based on the comments in the suggested answer:

e.offsetX = e.touches[0].pageX - e.touches[0].target.offsetLeft;     
e.offsetY = e.touches[0].pageY - e.touches[0].target.offsetTop;

This ignores any transformations such as rotations or scaling. Also be sure to check if there are any touches.




回答3:


Thanks, @Kontiki - this is the solution that finally fixed things for me:

if("touchmove" == e.type)
{

    let r = canvas.getBoundingClientRect();
    currX = e.touches[0].clientX - r.left;
    currY = e.touches[0].clientY - r.top;
}
else
{
    currX = e.offsetX;
    currY = e.offsetY;
}



回答4:


The page - offset / client approach did not work for me. There was still an offset. I found this other solution that works perfectly:

let r = canvas.getBoundingClientRect();

let x = e.touches[0].pageX - r.left;
let y = e.touches[0].pageY - r.top;




回答5:


Was having similar issue on binding event-handler using jQuery's .on function on canvas element (Don't know the reason).

I resolved it by binding event-handler using addEventListener. The event object in the handler has offsetX and offsetY defined with proper values.

Hope it helps...



来源:https://stackoverflow.com/questions/11287877/how-can-i-get-e-offsetx-on-mobile-ipad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!