简体   繁体   English

触摸事件仅在 canvas 上绘制时发生一次

[英]Touch event only happens once when drawing on canvas

Basically, i have to draw on a canvas in my web application based on Angular 7. Mouse drawing works just fine, while the touch part will work just for the first stroke and then never draws again.基本上,我必须在基于 Angular 的 web 应用程序中绘制 canvas 7. 鼠标绘图工作正常,而触摸部分仅适用于第一笔划,然后再也不会绘制。

What i have tried is binding the event on a mouse event following some guides, literally copy-pasting code and modifying just the variables names.我尝试过的是按照一些指南将事件绑定到鼠标事件上,从字面上复制粘贴代码并仅修改变量名称。 I can currently only test the touch events on the Chromium IPadPro simulator.我目前只能在 Chromium IPadPro 模拟器上测试触摸事件。

the preventdefault and stopimmediatepropagation calls make no difference if there are there or not preventdefault 和 stopimmediatepropagation 调用是否存在没有区别

fromEvent(canvasE1, 'touchstart').pipe(switchMap(() => {
        return fromEvent(canvasE1, 'touchmove').pipe(
          takeUntil(fromEvent(canvasE1, 'touchend')),
          takeUntil(fromEvent(canvasE1, 'touchcancel')),
          pairwise()
        );
      })).subscribe((res: [TouchEvent, TouchEvent]) => {
        const rect = canvasE1.getBoundingClientRect();

        const prevPos = {
          x: res[0].touches[0].clientX - rect.left,
          y: res[0].touches[0].clientY - rect.top
        };
        res[0].preventDefault();
        res[0].stopImmediatePropagation();

        const currentPos = {
          x: res[1].touches[0].clientX - rect.left,
          y: res[1].touches[0].clientY - rect.top
        };
        res[1].preventDefault();
        res[1].stopImmediatePropagation();

        this.drawOnCanvas(prevPos, currentPos);
      });

private drawOnCanvas(prevPos: { x: number, y: number }, currentPos: { x: number, y: number }) {

    if (!this.twoDimensionalContext) {
      return;
    }
    this.twoDimensionalContext.beginPath();

    if (prevPos) {
      this.twoDimensionalContext.moveTo(prevPos.x, prevPos.y);
      this.twoDimensionalContext.lineTo(currentPos.x, currentPos.y);
      this.twoDimensionalContext.stroke();
    }
  }

the drawOnCanvas function works perfectly fine with the mouse events, so i think that's not the problem. drawOnCanvas function 可以很好地处理鼠标事件,所以我认为这不是问题所在。 the Mouse part just works perfectly fine and doesn't fire up when the touch device is detected.鼠标部分工作得很好,在检测到触摸设备时不会启动。

To event preventDefault, you need to add map into fromEvent, after takeUtil, for example, changing this:事件preventDefault,你需要添加map到fromEvent,在takeUtil之后,例如,改变这个:

    return fromEvent(canvasE1, 'touchmove').pipe(
        takeUntil(fromEvent(canvasE1, 'touchend')),
        takeUntil(fromEvent(canvasE1, 'touchcancel')),
        pairwise()
     );

to

        return fromEvent(canvasE1, 'touchmove').pipe(
          takeUntil(fromEvent(canvasE1, 'touchend')),
          takeUntil(fromEvent(canvasE1, 'touchcancel')),
          map( (event) => {
              event.preventDefault();
              event.stopPropagation();
              return event; 
          }),
          pairwise()
        );

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

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