{"id":296,"date":"2018-09-29T23:31:51","date_gmt":"2018-09-29T15:31:51","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=296"},"modified":"2018-09-29T23:33:02","modified_gmt":"2018-09-29T15:33:02","slug":"how-to-capture-the-coordinates-of-your-mouse-as-it-moves-along-a-path-with-jquery","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-capture-the-coordinates-of-your-mouse-as-it-moves-along-a-path-with-jquery\/","title":{"rendered":"How to capture the coordinates of your mouse as it moves along a path with jQuery"},"content":{"rendered":"<p>When you want to move an object on your web page, you can do so by setting the top and left property of that object to several screen coordinates along a path of travel. Although you can create those coordinates by hand, it is more efficient to capture those coordinates using your mouse. <\/p>\n<p>Previously, I had discussed:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-detect-mouse-clicks\/\" rel=\"noopener\" target=\"_blank\">How to use jQuery to detect mouse clicks<\/a> and<\/li>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-get-mouse-cursor-coordinates-when-mouse-moves\/\" rel=\"noopener\" target=\"_blank\">How to use jQuery to get mouse cursor coordinates when mouse moves<\/a><\/li>\n<\/ul>\n<p>In this post, we extend those ideas to build a simple mechanism that captures the coordinates of your mouse as it moves alone a path.<\/p>\n<h2>Using jQuery detect to mouse clicks on the HTML document<\/h2>\n<p>Let's use mouse clicks to toggle the capturing of coordinates of the mouse as it moves. In order to do so, we provide a JavaScript callback function to the <a href=\"https:\/\/api.jquery.com\/mousedown\/\" rel=\"noopener\" target=\"_blank\">$.mousedown<\/a> function. <\/p>\n<p>Inside that callback function, we toggle a global variable between the values of true and false to start\/stop capture. Given these points, the following is the code snippet that prepares that global variable and registers a callback function that does the toggling when mouse clicks are detected:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ncaptureMouseCoordinates = false;\r\n$(document).mousedown(function(e) {\r\n    captureMouseCoordinates = !captureMouseCoordinates;\r\n});\r\n<\/pre>\n<p>Note that as long as you press any of the buttons on your mouse, the toggling of <code>captureMouseCoordinates<\/code> will happen.<\/p>\n<h2>Using jQuery to listen to mouse movements on the HTML document<\/h2>\n<p>Next, we add the code snippet that will record the mouse coordinates when the mouse moves when <code>captureMouseCoordinates<\/code> is true:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nmouseCoordinates = &#x5B;];\r\n$(document).mousemove(function(event) {\r\n  if (captureMouseCoordinates) {\r\n    mouseCoordinates.push(&#x5B;event.pageX, event.pageY]);\r\n  }\r\n});\r\n<\/pre>\n<p>First, we initialize a list, <code>mouseCoordinates<\/code>, to hold the mouse coordinates. After that, we supply a callback function to <a href=\"https:\/\/api.jquery.com\/mousemove\/\" rel=\"noopener\" target=\"_blank\">$.mousemove<\/a> that will save the x and y coordinates of the mouse cursor to the <code>mouseCoordinates<\/code> when <code>captureMouseCoordinates<\/code> is true.<\/p>\n<h2>Using jQuery to listen to space bar presses<\/h2>\n<p>In order to reconstruct <code>mouseCoordinates<\/code> for moving the object, we need to use a third event trigger. Since I have a <a href=\"https:\/\/www.amazon.com\/VELOCIFIRE-TKL01-Mechanical-Copywriters-Programmers\/dp\/B076D5WVYG\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=163821102d91771c2babbdacb5d6ddd9&language=en_US\" rel=\"noopener\" target=\"_blank\">keyboard<\/a> connected to my computer, I will use the space bar as the trigger to get the codes for reconstructing <code>mouseCoordinates<\/code>:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$(document).keydown(function(event){\r\n  if (event.keyCode == 32) {\r\n    console.log(JSON.stringify(mouseCoordinates));\r\n    mouseCoordinates = &#x5B;];\r\n  }\r\n});\r\n<\/pre>\n<p>In the above code, we supply a callback function to <a href=\"https:\/\/api.jquery.com\/keydown\/\" rel=\"noopener\" target=\"_blank\">$.keydown<\/a> that will write the contents of <code>mouseCoordinates<\/code> as JSON to console when the space bar is pressed. After doing so, it will reset <code>mouseCoordinates<\/code> to an empty list.<\/p>\n<h2>Putting all together<\/h2>\n<p>In case you need it, the following is a complete HTML document that you can use for capturing the coordinates of your mouse as it moves along a path with jQuery:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.3.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script type=&quot;text\/javascript&quot;&gt;\r\n      captureMouseCoordinates = false;\r\n      mouseCoordinates = &#x5B;];\r\n      $(document).mousedown(function(e) {\r\n        captureMouseCoordinates = !captureMouseCoordinates;\r\n      }).mousemove(function(event) {\r\n        if (captureMouseCoordinates) {\r\n          mouseCoordinates.push(&#x5B;event.pageX, event.pageY]);\r\n        }\r\n      }).keydown(function(event){\r\n        if (event.keyCode == 32) {\r\n          console.log(JSON.stringify(mouseCoordinates));\r\n          mouseCoordinates = &#x5B;];\r\n        }\r\n      });\r\n    &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>To capture the coordinates of your mouse as it moves along a path, you can first load the HTML document with your browser. After you had done so, you can click your mouse and move your cursor along a path of travel. <\/p>\n<p>When you wish to get the codes to reconstruct the path of travel, you can then press the space bar. After that, you can get the codes from the console window of your browser.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-4M\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4M&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4M&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F296&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you want to move an object on your web page, you can do so by setting the top and left property of that object to several screen coordinates along a path of travel. Although you can create those coordinates by hand, it is more efficient to capture those coordinates using your mouse. <\/p>\n<p>Previously, I had discussed:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-detect-mouse-clicks\/\" rel=\"noopener\" target=\"_blank\">How to use jQuery to detect mouse clicks<\/a> and<\/li>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-get-mouse-cursor-coordinates-when-mouse-moves\/\" rel=\"noopener\" target=\"_blank\">How to use jQuery to get mouse cursor coordinates when mouse moves<\/a><\/li>\n<\/ul>\n<p>In this post, we extend those ideas to build a simple mechanism that captures the coordinates of your mouse as it moves alone a path.<\/p>\n","protected":false},"author":1,"featured_media":1208,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[575,576,574,16,128,89,171],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jquery-logo-mark-dark.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-4M","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/296"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1208"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}