使用 canvas 防止在移动页面上滚动

Prevent scrolling on mobile on page with canvas

我在网上尝试了很多解决方案,但由于某种原因,当我在 canvas 上移动触摸时,此页面仍然垂直移动:

<html>  
<head>
<style>

html, body {
  overflow-x: hidden;
}

body {
  background-color: lightgray;
  position: relative;
}

body.noScroll { 
  overflow: hidden;
}

</style>
</head>
<body>
        <canvas id="myCanvas" style="touch-action: none;" width="800" height="800"></canvas><br>        

        <script>

            document.body.addEventListener("touchmove", getTouchPos)

            var canvas = document.getElementById('myCanvas')            
            var context = canvas.getContext('2d')   

            context.fillStyle = "#000";
            context.fillRect(0, 0, canvas.width, canvas.height);

            function getTouchPos(evt) {
                evt.preventDefault();
            }

        </script>   
    </body> 
</html>

在 iOS Safari 上测试。

你找到这个 post 了吗? Disable scroll/swipe action for html canvas drawing on ios

它导致了以下link。看起来您必须考虑多个事件。您还必须将 canvas 引用为 dom 元素,以评估目标

http://bencentra.com/code/2014/12/05/html5-canvas-touch-events.html