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(&#39;on&#39; + 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); </script> <script> window.onload = function(){ var oCanvas = document.querySelector( "#canvas" ), oGc = oCanvas.getContext( '2d' ), 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 = '#09f'; oGc.stroke(); oInfo.innerHTML = '鼠标的当前坐标是:(' + oPos.x + ',' + oPos.y + ')'; }, 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!