2016-06-01 120 views
0

我試圖找到一種方法,我可以單擊各個像素,然後像素的XY座標將被記錄/打印。我發現在線下面的代碼,但它只顯示最後點擊的像素的座標。有沒有辦法保存/打印我點擊過的所有像素的座標。我正在爲Java applet做這件事,這會讓我更容易製作多邊形。 我寧願輸出(當我點擊像素)是這樣的,這樣我就可以複製&粘貼到Java:記錄像素onclick的XY座標

int [] x={x1,x2,x3,x4,x...}; 
int [] y={y1,y2,y3,y4,y...}; 

這裏是我發現的代碼:

<html> 
 
<head> 
 
<script language="JavaScript"> 
 
function point_it(event){ 
 
\t pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("pointer_div").offsetLeft; 
 
\t pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("pointer_div").offsetTop; 
 
\t document.getElementById("cross").style.left = (pos_x-1) ; 
 
\t document.getElementById("cross").style.top = (pos_y-15) ; 
 
\t document.getElementById("cross").style.visibility = "visible" ; 
 
\t document.pointform.form_x.value = pos_x; 
 
\t document.pointform.form_y.value = pos_y; 
 
} 
 
</script> 
 
</head> 
 
<body> 
 
<form name="pointform" method="post"> 
 
<div id="pointer_div" onclick="point_it(event)" style = "background-image:url('image.jpg');width:2400px;height:1848px;"> 
 
<img src="point.gif" id="cross" style="position:relative;visibility:hidden;z-index:2;"></div> 
 
You pointed on x = <input type="text" name="form_x" size="4" /> - y = <input type="text" name="form_y" size="4" /> 
 
</form> 
 
</body> 
 
</html>

+0

'document.pointform.form_x.value = pos_x; document.pointform.form_y.value = pos_y;'顯示位置。這裏是對javascript數組的描述:http://www.w3schools.com/js/js_arrays.asp – Sang

回答

0

<html> 
 

 
<head> 
 
    <script language="JavaScript"> 
 
    var x = []; 
 
    var y = []; 
 

 
    function point_it(event) { 
 
     pos_x = event.offsetX ? (event.offsetX) : event.pageX - document.getElementById("pointer_div").offsetLeft; 
 
     pos_y = event.offsetY ? (event.offsetY) : event.pageY - document.getElementById("pointer_div").offsetTop; 
 
     document.getElementById("cross").style.left = (pos_x - 1); 
 
     document.getElementById("cross").style.top = (pos_y - 15); 
 
     document.getElementById("cross").style.visibility = "visible"; 
 
     x.push(pos_x); 
 
     y.push(pos_y); 
 
     document.getElementById("form_x").innerHTML = x; 
 
     document.getElementById("form_y").innerHTML = y; 
 
    }; 
 

 
    function remove_it() { 
 
     x.pop(); 
 
     y.pop(); 
 
     document.getElementById("form_x").innerHTML = x; 
 
     document.getElementById("form_y").innerHTML = y; 
 
    } 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <form name="pointform" method="post"> 
 
    <div id="pointer_div" onclick="point_it(event)" style="background-image:url('image.jpg');width:2400px;height:1848px;"> 
 
     <img src="point.gif" id="cross" style="position:relative;visibility:hidden;z-index:2;"> 
 
    </div> 
 
    You pointed on 
 
    <br>x = <span id="form_x"></span> 
 
    <br>y = <span id="form_y"></span> 
 
    <br> 
 
    <button onclick="remove_it()">remove last</button> 
 
    </form> 
 
</body> 
 

 
</html>

+0

請包括描述以及代碼示例。 –

+0

我使用你的代碼,它工作得很好,但我有一個問題。有時我會搞砸我點擊的位置,我想刪除這一點。有沒有辦法通過按Ctrl + Z或通過刪除最後一個點的按鈕來刪除點? – AA24

+0

您可以添加一個按鈕來刪除最後一個點。點擊它將從兩個數組中刪除最後一個元素。查看編輯的代碼。 – sudhirk496