Skip to content

Instantly share code, notes, and snippets.

@cdinu
Created September 25, 2015 13:42
Show Gist options
  • Save cdinu/0485245e8e7c70eaac90 to your computer and use it in GitHub Desktop.
Save cdinu/0485245e8e7c70eaac90 to your computer and use it in GitHub Desktop.
Get XY coordinates as percentages
// 1. Load an image in a new tab
// 2. Open JS console. Leave it open
// 3. Paste this
var s = document.createElement('script');
s.setAttribute('src','https://code.jquery.com/jquery-2.1.4.min.js');
document.head.appendChild(s);
// 4. Then paste this
var oldx = 0, oldy = 0;
var newImg = $('img').attr('src');
$('img').detach();
$('body').append('<img src="' + newImg + '" style="margin:0; height:100vh; width:auto">');
$('img').off( "click" ).click(function(e) {
var offset = $(this).offset();
var x = Math.floor((e.pageX - offset.left) / $(this).width() * 10000)/100;
var y = Math.floor((e.pageY - offset.top) / $(this).height() * 10000)/100;
console.log(x, y, x - oldx, y - oldy);
oldx = x; oldy = y;
});
// 5. Click on the points you're interested. You'll get x,y coordinates in percentage of the image
// and dx and dy from the previous clicked point.
@tyasird
Copy link

tyasird commented Jun 24, 2021

It works perfect. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment