Skip to content

Instantly share code, notes, and snippets.

@rurabe
Last active August 29, 2015 14:10
Show Gist options
  • Save rurabe/a16d0a096ef59b3c0956 to your computer and use it in GitHub Desktop.
Save rurabe/a16d0a096ef59b3c0956 to your computer and use it in GitHub Desktop.
A jQuery extension that reports the locations of clicks in the SVG coordinate system
(function(){
var transform = function(el,pt){
return pt.matrixTransform( el.getScreenCTM().inverse() );
};
$.fn.extend({
reportLocations : function(callback){
var $this = this;
var svg = $this.closest('svg')[0];
var pt = svg.createSVGPoint();
var el = $this[0];
this.on("click",function(e){
pt.x = e.clientX; pt.y = e.clientY;
var transformedPt = pt.matrixTransform( el.getScreenCTM().inverse() );
callback.call($this,e,transformedPt)
});
}
});
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment