在kanvas中实现touch事件

2017-06-14  本文已影响0人  璃_b11f
<!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>

上一篇下一篇

猜你喜欢

热点阅读