mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-07 04:39:06 +02:00
Convert screen coordinates into canvas coordinates when dealing with mouse events
This commit is contained in:
parent
7d0a7844ea
commit
162aaa993a
@ -642,14 +642,20 @@
|
|||||||
boundingRect = canvas.getBoundingClientRect();
|
boundingRect = canvas.getBoundingClientRect();
|
||||||
|
|
||||||
if (e.touches) {
|
if (e.touches) {
|
||||||
mouseX = e.touches[0].clientX - boundingRect.left;
|
mouseX = e.touches[0].clientX;
|
||||||
mouseY = e.touches[0].clientY - boundingRect.top;
|
mouseY = e.touches[0].clientY;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
mouseX = e.clientX - boundingRect.left;
|
mouseX = e.clientX;
|
||||||
mouseY = e.clientY - boundingRect.top;
|
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 {
|
return {
|
||||||
x: mouseX,
|
x: mouseX,
|
||||||
y: mouseY
|
y: mouseY
|
||||||
|
Loading…
Reference in New Issue
Block a user