]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Convert screen coordinates into canvas coordinates when dealing with mouse events 1471/head
authorEvert Timberg <evert.timberg@gmail.com>
Sun, 20 Sep 2015 21:57:27 +0000 (17:57 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Sun, 20 Sep 2015 21:57:27 +0000 (17:57 -0400)
src/core/core.helpers.js

index ab58b0789dc7b1bb7567447402318da639292370..bd18fd08d9da0f37e8132ee81fbf6ecaae2b4558 100644 (file)
                                boundingRect = canvas.getBoundingClientRect();
 
                        if (e.touches) {
-                               mouseX = e.touches[0].clientX - boundingRect.left;
-                               mouseY = e.touches[0].clientY - boundingRect.top;
+                               mouseX = e.touches[0].clientX;
+                               mouseY = e.touches[0].clientY;
 
                        } else {
-                               mouseX = e.clientX - boundingRect.left;
-                               mouseY = e.clientY - boundingRect.top;
+                               mouseX = e.clientX;
+                               mouseY = e.clientY;
                        }
 
+                       // Scale mouse coordinates into canvas coordinates
+                       // by following the pattern laid out by 'jerryj' in the comments of 
+                       // http://www.html5canvastutorials.com/advanced/html5-canvas-mouse-coordinates/
+                       mouseX = Math.round((mouseX - boundingRect.left) / (boundingRect.right - boundingRect.left) * canvas.width);
+                       mouseY = Math.round((mouseY - boundingRect.top) / (boundingRect.bottom - boundingRect.top) * canvas.height);
+
                        return {
                                x: mouseX,
                                y: mouseY