js 获取页面鼠标行为轨迹并描绘

2 篇文章 0 订阅
2 篇文章 0 订阅

转载自 https://zhidao.baidu.com/question/1176280230955939779.html

<!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>

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过HTML5的Canvas元素和JavaScript来实现在网页上用鼠标画出自己的名字并获取鼠标移动轨迹。以下是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Canvas Demo</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="myCanvas" width="500" height="500"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var isDrawing = false; var mouseMoves = []; canvas.addEventListener("mousedown", function(e) { isDrawing = true; ctx.beginPath(); ctx.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop); mouseMoves.push({x: e.clientX - canvas.offsetLeft, y: e.clientY - canvas.offsetTop}); }); canvas.addEventListener("mousemove", function(e) { if (isDrawing) { ctx.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop); ctx.stroke(); mouseMoves.push({x: e.clientX - canvas.offsetLeft, y: e.clientY - canvas.offsetTop}); } }); canvas.addEventListener("mouseup", function(e) { isDrawing = false; }); // 将 mouseMoves 发送到后台进行处理 </script> </body> </html> ``` 在这个示例代码中,我们创建了一个Canvas元素,并绑定了鼠标事件。当用户按下鼠标左键时,我们开始绘制路径,并将路径的起点设置为鼠标当前位置,并将当前位置添加到 `mouseMoves` 数组中。当用户移动鼠标时,如果正在绘制路径,则将路径的终点设置为鼠标当前位置,并绘制出路径,同时将当前位置添加到 `mouseMoves` 数组中。当用户松开鼠标左键时,停止绘制路径。 最后,我们可以将 `mouseMoves` 数组发送到后台进行处理。 你可以根据自己的需求修改这个示例代码,实现在Canvas上绘制出自己的名字并获取鼠标移动轨迹。需要注意的是,对用户的隐私应该保护,不应该收集任何敏感信息,同时也需要遵守相关的法律法规。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值