Created
September 25, 2015 13:42
-
-
Save cdinu/0485245e8e7c70eaac90 to your computer and use it in GitHub Desktop.
Get XY coordinates as percentages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works perfect. Thank you.