Home  >  Article  >  Web Front-end  >  How to get the current coordinates of the mouse in real time

How to get the current coordinates of the mouse in real time

一个新手
一个新手Original
2018-05-11 14:18:433924browse

This article is about a simple function of obtaining mouse coordinates in real time. In canvas animation development, obtaining mouse coordinates, keyboard keys, etc. are common operations. We will slowly encapsulate them into a public Library.

1. Event compatibility:

function bindEvent(obj, event, fn) {
        if (obj.attachEvent) { //ie
            obj.attachEvent('on' + event, function () {
                fn.call(obj);
            });
        } else {
            //chrome&ff
            obj.addEventListener(event, fn, false);
        }
    }

The above is compatible with ie8 and corrects the pointing of this keyword in lower versions of ie. The below is compatible with chrome and ff. For other more commonly used packages, please refer to my javascript open source framework gdom

2. Build a basic library using immediate expressions

Add a method to obtain mouse coordinates

;(function (window) {
    window.G = {};
    function bindEvent(obj, event, fn) {
        if (obj.attachEvent) { //ie
            obj.attachEvent('on' + event, function () {
                fn.call(obj);
            });
        } else {
            //chrome&ff
            obj.addEventListener(event, fn, false);
        }
    }

    G.getPos = function( dom ){
        var oPos = { x : 0, y : 0 };
        bindEvent( dom, 'mousemove', function( ev ){
            var oEvent = ev || event, x, y;
            if ( oEvent.pageX || oEvent.pageY ){
                x = oEvent.pageX;
                y = oEvent.pageY;
            }else {
                x = oEvent.clientX + document.body.scrollLeft || document.documentElement.scrollLeft;
                y = oEvent.clientX + document.body.scrollTop || document.documentElement.scrollTop;
            }
            x -= dom.offsetLeft;
            y -= dom.offsetTop;
            oPos.x = x;
            oPos.y = y;
        } );
        return oPos;
    };

})(window);

3. Introduce the encapsulated js library, bind canvas as the listening object, and print the coordinates of the current mouse.

The coordinates of the mouse. I have drawn 2 lines here for convenience. Observe.



<script>
;(function (window) {
    window.G = {};
    function bindEvent(obj, event, fn) {
        if (obj.attachEvent) { //ie
            obj.attachEvent(&amp;#39;on&amp;#39; + event, function () {
                fn.call(obj);
            });
        } else {
            //chrome&amp;ff
            obj.addEventListener(event, fn, false);
        }
    }

    G.getPos = function( dom ){
        var oPos = { x : 0, y : 0 };
        bindEvent( dom, &#39;mousemove&#39;, function( ev ){
            var oEvent = ev || event, x, y;
            if ( oEvent.pageX || oEvent.pageY ){
                x = oEvent.pageX;
                y = oEvent.pageY;
            }else {
                x = oEvent.clientX + document.body.scrollLeft || document.documentElement.scrollLeft;
                y = oEvent.clientX + document.body.scrollTop || document.documentElement.scrollTop;
            }
            x -= dom.offsetLeft;
            y -= dom.offsetTop;
            oPos.x = x;
            oPos.y = y;
        } );
        return oPos;
    };

})(window);
</script>

<script>
window.onload = function(){
    var oCanvas = document.querySelector( "#canvas" ),
        oGc = oCanvas.getContext( &#39;2d&#39; ),
        width = oCanvas.width, height = oCanvas.height,
        oInfo = document.querySelector( "#info" ),
        oPos = G.getPos( oCanvas );
        oCanvas.addEventListener( "mousemove", function(){
            
            oGc.clearRect( 0, 0, width, height );
            oGc.beginPath();
            oGc.moveTo( oPos.x, 0 );
            oGc.lineTo( oPos.x, height );
            oGc.moveTo( 0, oPos.y );
            oGc.lineTo( width, oPos.y );
            oGc.closePath();
            oGc.strokeStyle = &#39;#09f&#39;;
            oGc.stroke();

            oInfo.innerHTML = &#39;鼠标的当前坐标是:(&#39; + oPos.x + &#39;,&#39; + oPos.y + &#39;)&#39;;
        }, false );
}
</script>



The above is the detailed content of How to get the current coordinates of the mouse in real time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Usage of MUI top tabsNext article:Usage of MUI top tabs