zoukankan      html  css  js  c++  java
  • 计算鼠标移动的精确速度

    效果
    要达到无论在什么机器上,算出来的速度是一样的。

    思路:
    计算两次mousemove之间的位移和时间,就可以算出精确的速度

    不要将onMousemove的调用时间间隔看成是均等的,事实上也不是均等的,而是根据机器的运行状况实时调整的,所以两次调用之间的时间间隔不能看成是单位时间。关于mousemove的讨论

    示例

        var preX = 0;
        var preTime  = undefined;
        document.addEventListener("mousedown",onMouseDown);
    
    
        function onMouseDown(event){
            preTime = undefined;
            document.addEventListener("mousemove",getVolecity);
            document.addEventListener("mouseup",onMouseUp);
        }
    
        function onMouseUp(event){
            document.removeEventListener("mousemove",getVolecity);
        }
    
    
        /**
         * 计算速度
         * @param event
         */
        function getVolecity(event){
            var currTime = Date.now();
            if(typeof preTime != "undefined"){    //跳过第一次
                var dist = event.clientX - preX;
                var time = currTime - preTime;
                var volecity = dist/time;   
                console.log("volecity:"+volecity);
            }
            preX = event.clientX;
            preTime = currTime;
        }

    原文:https://blog.csdn.net/ruangong1203/article/details/52474746 

  • 相关阅读:
    AdvStringGrid使用小结
    svn提示out of date的解决方法
    delphi之socket通讯
    Delphi的Socket编程步骤
    C++ Socket编程步骤
    centos7安装docker
    centos7安装指南
    UltraISO制作U盘启动盘
    浅谈linux 文件的三个时间
    自动配置zabbix-agent
  • 原文地址:https://www.cnblogs.com/7qin/p/10197045.html
Copyright © 2011-2022 走看看