Skip to content

Instantly share code, notes, and snippets.

@Lukas238
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save Lukas238/ce24c84e48c585d0d2e5 to your computer and use it in GitHub Desktop.

Select an option

Save Lukas238/ce24c84e48c585d0d2e5 to your computer and use it in GitHub Desktop.
Convert mouse coordinate to cartecian (centered on parent div)

##jQuery: Convert mouse coordinate to cartecian (centered on parent div)

Demo

$('.canvas').on('click', function(e){
  var offset = $(this).offset();
    var x = (e.pageX - offset.left);
    var y = (e.pageY - offset.top);

  var A= $(this).width();
  var B= $(this).height();
  var Xmax = A / 2; 
  var Xmin = Xmax *-1;
  var Ymax = B / 2;
  var Ymin = Ymax *-1;
  
 var xp = (x - Xmax) * A / (Xmax - Xmin);
 var yp = (y - Ymax) * -B / (Ymax - Ymin);
  
  alert("X: " + xp + "  Y: " + yp);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment