美文网首页
在kanvas中实现touch事件

在kanvas中实现touch事件

作者: 璃_b11f | 来源:发表于2017-06-14 19:17 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <title>画画板系统</title>
        <style>
            body{
                margin:0;
            }
            canvas{
                background:#eee;
            }
        </style>
    </head>
    <body>
        <canvas id="mycanvas"></canvas>
        <script>
            var canvas = document.querySelector("#mycanvas");
            canvas.width = window.innerWidth;
            canvas.height = 500;
    
            //获取绘图环境
            var cxt = canvas.getContext("2d");
    
            //绑定触摸事件
            canvas.addEventListener("touchstart", function(e){
                cxt.beginPath();
                cxt.moveTo(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
                
                canvas.addEventListener("touchmove", function(e){
                    cxt.lineTo(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
                    cxt.stroke();
                })
            })
        </script>
    </body>
    </html>
    
    

    相关文章

      网友评论

          本文标题:在kanvas中实现touch事件

          本文链接:https://www.haomeiwen.com/subject/umjeqxtx.html