我試圖找到一種方法,我可以單擊各個像素,然後像素的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>
'document.pointform.form_x.value = pos_x; document.pointform.form_y.value = pos_y;'顯示位置。這裏是對javascript數組的描述:http://www.w3schools.com/js/js_arrays.asp – Sang