gpt4 book ai didi

javascript - 浏览器中的 Wacom 取消移动

转载 作者:行者123 更新时间:2023-12-03 01:42:17 25 4
gpt4 key购买 nike

我目前正在尝试使用我的 wacom intous 在浏览器中的 Canvas 上绘制一些内容。

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

当我使用鼠标时,这会按预期工作。当我使用 wacom 平板电脑时,移动将在大约 20 像素后被取消,并且 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 在几个像素后停止绘制吗?

最佳答案

我遇到了这个确切的问题,在遍历几个像素后出现“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

关于javascript - 浏览器中的 Wacom 取消移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com