Can't get coordinates of touchevents in Javascript on Android devices

前端 未结 2 537
旧巷少年郎
旧巷少年郎 2020-12-24 13:14

I\'m testing the HTML5 canvas and using Javascript to draw for touch enabled devices. While I have it working in iOS devices I cannot get it to work on Android. I have narro

2条回答
  •  时光取名叫无心
    2020-12-24 13:41

    FINAL EDIT: Ok I got it working, if anyone finds this and has a similar problem you need to access the touch array within the event, and if just using a single touch (rather than multi touch) take the first item out of the array, as below, or you may need to offset it if that isn't accurate:

    var touch = event.touches[0];
    var x = touch.pageX;
    var y = touch.pageY;
    // or taking offset into consideration
    var x_2 = touch.pageX - canvas.offsetLeft;
    var y_2 = touch.pageY - canvas.offsetTop;
    

提交回复
热议问题