Skip to content

Instantly share code, notes, and snippets.

@ahvonenj
Created December 16, 2015 11:05
Show Gist options
  • Save ahvonenj/3f4c2d3ae412402bb34e to your computer and use it in GitHub Desktop.
Save ahvonenj/3f4c2d3ae412402bb34e to your computer and use it in GitHub Desktop.
Canvas pointer coords no matter where and how big canvas is
relMouseCoords: function(canvas, e)
{
var rect = canvas.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
return { x: x, y: y };
}
relTapCoords: function(canvas, e)
{
var rect = canvas.getBoundingClientRect();
var x = e.touches[0].clientX - rect.left;
var y = e.touches[0].clientY - rect.top;
return { x: x, y: y };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment