简体   繁体   English

Android WebView中的Javascript触摸事件

[英]Javascript touch events in an android webview

I have this javascript code working correctly. 我有此javascript代码正常工作。 All it does is save a mousedown's coordinates, and modify the schematic on mouseup, depending on mousedown and mouseup coordinates. 它所做的只是保存mousedown的坐标,并根据mousedown和mouseup坐标修改mouseup上的原理图。

Rather than reinvent the wheel, I am trying to use it in an android app, by loading it inside a webview. 与其重新发明轮子,不如尝试通过将其加载到webview中在android应用程序中使用它。

The relevant changes are to change the mousedown and up listeners for : 相关更改是为了更改鼠标的向下和向上监听器:

            canvas.addEventListener("touchstart", saveDownCoords, false);
            canvas.addEventListener("touchend", modifySchematic, false);

and the function calculating the coordinates has been changed so : 并且计算坐标的功能已更改,因此:

function mouseCoords(e) {
  var cellSize = e.target.getAttribute("data-lib-shelves-cell-size");
  var cellRect = e.target.getBoundingClientRect();
  return {
      //x : Math.floor((e.clientX - cellRect.left) / cellSize),
      //y : Math.floor((e.clientY - cellRect.top) / cellSize)
      x : Math.floor((e.touches[0].clientX - cellRect.left) / cellSize),
      y : Math.floor((e.touches[0].clientY - cellRect.top) / cellSize)
  };
}

Nothing works at all unless I touch several fingers. 除非我触摸多根手指,否则什么都不会起作用。 Then it does what the program did with the mouse, sometimes using the down from the first finger, and the up of the second finger; 然后,它使用鼠标来执行程序的操作,有时使用第一个手指的下指和第二个手指的上指; sometimes the down and up of the first finger, but only if a second finger was used, never when only one finger is. 有时是第一根手指的上下,但只有在使用第二根手指的情况下,才不会出现,只有在使用一根手指时才不会。

I have not written a touch handler for the webview, nor for the activity inside which it resides. 我没有为webview或它所在的活动编写触摸处理程序。

Did anyone ever encounter such a behaviour? 有没有人遇到过这样的行为?

My problem resided in the javascript logic. 我的问题出在javascript逻辑中。 One can't get a touchend's coordinates, but only the coordinates of the last touchmove before it. 无法获得触摸端的坐标,而只能获得它之前最后一个触摸移动的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM