首页 > 解决方案 > 浏览器中的 Wacom 取消移动

问题描述

我目前正在尝试使用我的 wacom intous 在浏览器的画布上绘制一些东西。

该代码非常基本,除了在单击鼠标时找到鼠标的位置并绘制路径之外什么都不做。

当我使用鼠标时,这可以按预期工作。当我使用我的 wacom 数位板时,移动将在 ~20px 后被取消,并且lostpointercapture事件以及pointercancel事件都将被触发。

这是代码:

(function() {


var canvas = document.querySelector('.canvas');
  var ctx = canvas.getContext('2d');

  var currentPosition = { 
    x: 0, 
    y: 0 
  };

  function init() {
    adjustCanvasSize();
  }

  function adjustCanvasSize() {
    ctx.canvas.width = window.innerWidth;
    ctx.canvas.height = window.innerHeight;
  }

  function setPosition(ev) {
    currentPosition.x = ev.clientX;
    currentPosition.y = ev.clientY;
  }

  function draw(ev) {
    ev.preventDefault();
    if (ev.buttons !== 1) {
      return;
    }
    ctx.beginPath(); 
    ctx.lineWidth = 1; 
    ctx.lineCap = 'round'; 
    ctx.strokeStyle = '#1a1b1c'; 
    ctx.moveTo(currentPosition.x, currentPosition.y); 
    setPosition(ev);
    ctx.lineTo(currentPosition.x, currentPosition.y);
    ctx.stroke(); 
  }

  document.addEventListener('pointermove', draw);
  document.addEventListener('pointerdown', setPosition);
  document.addEventListener('pointerenter', setPosition);

  init();
})();

有谁知道为什么 wacom 在几个像素后停止绘图?

标签: javascriptcanvaswacom

解决方案


我遇到了这个确切的问题,经过几个像素后出现“lostpointercapture”PointerEvent

https://jsfiddle.net/mr1z7qg3/

解决方案是添加

touch-action: none; 

绘制位置的样式,否则浏览器会将其解释为平移/缩放触摸手势https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action

...在 Chrome 上

Firefox 在 about:config 中需要 dom.w3c_pointer_events.dispatch_by_pointer_messages


推荐阅读