在画布上绘制时禁用移动滚动?

时间:2018-07-19 05:19:49

标签: javascript canvas mobile scroll

我正在建立一个网站,您可以在该网站上用鼠标或触摸屏在HTML5画布上绘图。

但是在移动设备上,您可以在绘制时滚动网站。那使绘图非常不舒服。我已经尝试过了,但是不起作用:

    canvas.addEventListener("touchstart", function (e) {
  var mousePos = getTouchPos(canvas, e);
  var touch = e.touches[0];
  var mouseEvent = new MouseEvent("mousedown", {
    clientX: touch.clientX,
    clientY: touch.clientY
  });
  canvas.dispatchEvent(mouseEvent);
  if (e.target.tag == canvas) {
    e.preventDefault();
  }
}, false);
canvas.addEventListener("touchend", function (e) {
  var mouseEvent = new MouseEvent("mouseup", {});
  canvas.dispatchEvent(mouseEvent);
  if (e.target.tag == canvas) {
    e.preventDefault();
  }
}, false);
canvas.addEventListener("touchmove", function (e) {
  var touch = e.touches[0];
  var mouseEvent = new MouseEvent("mousemove", {
    clientX: touch.clientX,
    clientY: touch.clientY
  });
  canvas.dispatchEvent(mouseEvent);
  if (e.target.tag == canvas) {
    e.preventDefault();
  }
}, false);

您有什么想法,当您在画布上绘画时,如何避免在手机上滚动?

谢谢!

0 个答案:

没有答案