【问题标题】:detect finger touch javascript canvas检测手指触摸javascript画布
【发布时间】:2021-12-31 03:56:21
【问题描述】:

我有这个:

<canvas id="canvas" width="400" height="400" style="border:2px solid; background-color: #2c2c2c;"></canvas>
              <input type="button" value="Clear" class="canvas-clear" />
              <button onclick="toDataUrlForCanvas()">Submit</button>
          <script>
            $(document).ready(function() {
            var flag, dot_flag = false,
              prevX, prevY, currX, currY = 0,
              color = 'black', thickness = 2;
            var $canvas = $('#canvas');
            var ctx = $canvas[0].getContext('2d');
          
            $canvas.on('mousemove mousedown mouseup mouseout', function(e) {
              prevX = currX;
              prevY = currY;
              currX = e.clientX - $canvas.offset().left;
              currY = e.clientY - $canvas.offset().top;
              if (e.type == 'mousedown') {
                flag = true;
              }
              if (e.type == 'mouseup' || e.type == 'mouseout') {
                flag = false;
              }
              if (e.type == 'mousemove') {
                if (flag) {
                  ctx.beginPath();
                  ctx.moveTo(prevX, prevY);
                  ctx.lineTo(currX, currY);
                  ctx.strokeStyle = color;
                  ctx.lineWidth = thickness;
                  ctx.stroke();
                  ctx.closePath();
                }
              }
            });
          
            $('.canvas-clear').on('click', function(e) {
              c_width = $canvas.width();
              c_height = $canvas.height();
              ctx.clearRect(0, 0, c_width, c_height);
              $('#canvasimg').hide();
            });
          });
          
          function toDataUrlForCanvas() {
            var imgData = document.getElementById('canvas')
            imgData = imgData.toDataURL(); 
            console.log(imgData)
          }
          </script>

现在它检测到鼠标被按下,但是当我在移动设备上使用它时,它不起作用,因为它没有检测到我在画布上的手指。我试图在网上找到一些解决方案,但没有针对这种情况。我该如何解决这个问题?

【问题讨论】:

    标签: javascript html jquery canvas


    【解决方案1】:

    您必须使用触摸事件 API 来触发鼠标事件。欲了解更多信息,请使用此link,它解释了所有可用选项以及所有基于 canvas API 的示例。

    这是我的投资组合网站abjt.dev 上的代码 sn-p。 您也可以在 Github 上查看代码。

    let x = 0;
    let y = 0;
    document.querySelector('html').addEventListener('mousemove', (e) => {
        // do something
    });
    
    document.addEventListener('touchstart', (e) => {
        let touch = e.touches[0];
        x = touch.clientX;
        y = touch.clientY;
        // do something
    })
    document.addEventListener('touchmove', (e) => {
        let touch = e.touches[0];
        const mouseEvent = new MouseEvent("mousemove", {
            clientX: touch.clientX,
            clientY: touch.clientY
        });
        document.querySelector('html').dispatchEvent(mouseEvent);
        // do something
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 2021-10-30
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多