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

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

一个新手
Release: 2018-05-11 14:18:43
Original
3996 people have browsed it

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);
        }
    }
Copy after login

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);
Copy after login

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>



Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template