美文网首页
Javascript脚本获取页面鼠标行为轨迹并描绘DEMO

Javascript脚本获取页面鼠标行为轨迹并描绘DEMO

作者: 云爬虫技术研究笔记 | 来源:发表于2019-01-21 23:09 被阅读3886次
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Demo</title>
        <style>
            *{ 
                margin:0;
                padding:0;
                border:0;
            }
            .stage{
             
                 
            }
            .track-monitor{
             
                background-color:orange;
            }
            .track-pad{
             
                height:200px;
                background-color:green;
            }
            .track-coordinate{
                 
                background-color:purple;
                color:white;
                height:25px;
                line-height:25px;
                font-size:12px;
            }
            .track-coordinate-list{
             
                font-size:12px;
                width:100%;
                word-break:break-word;
                 
            }
        </style>
        <script>
            window.addEventListener('load',function(){
             
                var pad = document.getElementsByClassName('track-pad')[0];
                var monitor = document.getElementsByClassName('track-monitor')[0];
                var coordinate = document.getElementsByClassName('track-coordinate')[0];
                var clist = document.getElementsByClassName('track-coordinate-list')[0];
                var reset = document.getElementsByTagName('button')[0];
                var fixSize = function(){
                 
                    monitor.width = window.innerWidth;
                };
                 
                var context = monitor.getContext('2d');
                var cset = [];
                  
                 
                window.addEventListener('resize',function(){ fixSize(); });
                 
                pad.addEventListener('mousemove',function(e){
                             
                    context.strokeStyle = 'white';
                    context.lineTo(e.x,e.y);
                    context.stroke();
                    coordinate.innerHTML = e.x+':'+e.y;
                    cset.push(coordinate.innerHTML);
                    clist.innerHTML = cset.join(',');
                 
                });
                 
                reset.addEventListener('click',function(){
                     
                    fixSize();
                    cset = [];
                    clist.innerHTML = '';
                    coordinate.innerHTML='在绿色的方块中滑动鼠标';
                });
                 
                fixSize();
                 
            });
        </script>
    </head>
    <body>
        <div class="stage">
            <div class="track-pad"></div>
            <canvas width="100" height="200" class="track-monitor"></canvas>
            <div class="track-coordinate">在绿色的方块中滑动鼠标</div>
            <button>重置</button>
            <div class="track-coordinate-list"></div>
        </div>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:Javascript脚本获取页面鼠标行为轨迹并描绘DEMO

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