javascript - 在线等,急!!为什么我这个触屏 canvas画线的程序代码 触屏的位置 跟画画的位置对不上?我在google浏览器上测试的。
高洛峰
高洛峰 2017-04-11 11:16:59
0
0
770
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>canvas</title>
    <style>
        *{
            margin: 0px;
            padding:0px;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <canvas width="" height="" style="background: black;vertical-align: middle;" id="can">您的浏览器不支持CANVAS,请更新最新版浏览器。</canvas>
</body>
    <!--<script src="js/zepto.V1.1.6.min.js" type="text/javascript"></script>-->
    <!--<script src="js/zepTouch.js" type="text/javascript"></script>-->
    <script type="text/javascript">
        var can = document.getElementById('can');
        var cxt = can.getContext('2d');

        var windowWidth;
        var windowHeight;
        function resizeWindow(){
            windowWidth = window.innerWidth;
            windowHeight = window.innerHeight;
//            console.log(windowWidth);
//            console.log(windowHeight);
            can.style.width = windowWidth+'px';
            can.style.height = windowHeight+'px';
        }

        resizeWindow();
        window.onresize=function(){
            resizeWindow();
        }

        var moveX;
        var moveY;
        var drawDot = [];

       function draw(moveX,moveY,arry){
           cxt.clearRect(0,0,windowWidth,windowHeight);
           cxt.strokeStyle='#fff';
           cxt.lineWidth=2;
           cxt.beginPath();
           cxt.moveTo(moveX,moveY);
           for(var i =0;i<arry.length;i++){
               cxt.lineTo(arry[i].x,arry[i].y);
           }
           cxt.stroke();
       }

        can.addEventListener('touchstart',function(e){
            var event = e||window.event;
            var touch = event.touches[0];

            moveX = touch.pageX;
            moveY = touch.pageY;
            console.log(moveX+' '+moveY);
        },false);

        can.addEventListener('touchmove',function(e){
            var event = e||window.event;
            var touch = event.touches[0];
            var touchDot ={};
            touchDot.x = touch.pageX;
            touchDot.y = touch.pageY;
            console.log(touchDot);
            drawDot.push(touchDot);
            draw(moveX,moveY,drawDot);

        });
    </script>
</html>
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板