Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get image pixel coordinates relative left top corner

Tags:

javascript

I need to get image coordinates on click relative left top corner. So left top corner of the image is conditional 0,0 and then first pixel 1,0... 2..0 etc. Is there something in javascript construction I can use to get this logic?

like image 226
John Travolta Avatar asked Nov 27 '12 15:11

John Travolta


People also ask

How do you find XY coordinates of a div?

If the element is in the main document you can get the DIV's coordinates with... var X=window. getComputedStyle(abc,null). getPropertyValue('left'); var Y=window.

How do I find the coordinates of a div?

Basically you have to loop up through all the element's parents and add their offsets together. However, if you just wanted the x,y position of the element relative to its container, then all you need is: var x = el. offsetLeft, y = el.

How do you find the coordinates in HTML?

In order to find the coordinates of the top left corner of the HTML page, you can use the web browser's instance properties DisplayRectangleX and DisplayRectangleY. For example, after storing a browser's instance into the variable %Browser%, then %Browser. DisplayRectangleX% will return the X dimension and %Browser.


1 Answers

You can try something like this:-

  <img id="board" style="z-index: 0; left: 300;position: absolute; top: 600px" align=baseline     border=0 hspace=0 src="design/board.gif">

  function findPos(obj){
  var curleft = 0;
  var curtop = 0;

  if (obj.offsetParent) {
do {
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;
   } while (obj = obj.offsetParent);

return {X:curleft,Y:curtop};
 }
}

 findPos(document.getElementById('board'));
 alert(curleft);
 alert(curtop);
like image 130
Rahul Tripathi Avatar answered Oct 17 '22 15:10

Rahul Tripathi