Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve the same offsetX on touch like mouse event

Hello I am trying to get the offsetX,Y of touch event which should be identical to offsetX of mouse event. To do so I have used this code:

 ev.offsetX = ev.targetTouches[0].pageX- canvasName.offsetLeft 

I have even tried to simulate the touch event into mouse event but for that purpose i need the offsetX/Y, which is unavailable in touch event. Is there any way offsetX/Y can be calculated for touch? Please help

like image 914
Lamia Mehreen Avatar asked Jun 16 '13 06:06

Lamia Mehreen


2 Answers

I use this

var rect = e.target.getBoundingClientRect(); var x = e.targetTouches[0].pageX - rect.left; var y = e.targetTouches[0].pageY - rect.top; 
like image 179
Alex Nikulin Avatar answered Sep 17 '22 14:09

Alex Nikulin


The best answer here don't work for me, on webpage with scrolling! This will work better:

var bcr = e.target.getBoundingClientRect(); var x = e.targetTouches[0].clientX - bcr.x; var y = e.targetTouches[0].clientY - bcr.y; 

I used it to solve my problem.

like image 33
rejnok Avatar answered Sep 20 '22 14:09

rejnok